summaryrefslogtreecommitdiffstats
path: root/tools/plistutil.c
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2015-01-23 15:00:25 +0100
committerGravatar Martin Szulecki2015-01-23 15:00:25 +0100
commit7f28d8f75710026337c253622b2ed51303a2ad3d (patch)
treef07aa1ebfe528f161432e93dd48d6faa7a42cef8 /tools/plistutil.c
parentf4caa6a751e1fdf7e7775f6c29de605fc91f1027 (diff)
downloadlibplist-7f28d8f75710026337c253622b2ed51303a2ad3d.tar.gz
libplist-7f28d8f75710026337c253622b2ed51303a2ad3d.tar.bz2
plistutil: Cleanup code style a bit and remove obsolete extra header
Diffstat (limited to 'tools/plistutil.c')
-rw-r--r--tools/plistutil.c150
1 files changed, 78 insertions, 72 deletions
diff --git a/tools/plistutil.c b/tools/plistutil.c
index 6b7b053..85e54fc 100644
--- a/tools/plistutil.c
+++ b/tools/plistutil.c
@@ -22,7 +22,6 @@
22 22
23 23
24#include "plist/plist.h" 24#include "plist/plist.h"
25#include "plistutil.h"
26 25
27#include <stdio.h> 26#include <stdio.h>
28#include <stdlib.h> 27#include <stdlib.h>
@@ -33,6 +32,77 @@
33#pragma warning(disable:4996) 32#pragma warning(disable:4996)
34#endif 33#endif
35 34
35typedef struct _options
36{
37 char *in_file, *out_file;
38 uint8_t debug, in_fmt, out_fmt;
39} options_t;
40
41static void print_usage(int argc, char *argv[])
42{
43 char *name = NULL;
44 name = strrchr(argv[0], '/');
45 printf("Usage: %s -i|--infile FILE [-o|--outfile FILE] [-d|--debug]\n", (name ? name + 1: argv[0]));
46 printf("Convert a plist FILE from binary to XML format or vice-versa.\n\n");
47 printf(" -i, --infile FILE\tThe FILE to convert from\n");
48 printf(" -o, --outfile FILE\tOptional FILE to convert to or stdout if not used\n");
49 printf(" -d, --debug\t\tEnable extended debug output\n");
50 printf("\n");
51}
52
53static options_t *parse_arguments(int argc, char *argv[])
54{
55 int i = 0;
56
57 options_t *options = (options_t *) malloc(sizeof(options_t));
58 memset(options, 0, sizeof(options_t));
59
60 for (i = 1; i < argc; i++)
61 {
62 if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i"))
63 {
64 if ((i + 1) == argc)
65 {
66 free(options);
67 return NULL;
68 }
69 options->in_file = argv[i + 1];
70 i++;
71 continue;
72 }
73
74 if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o"))
75 {
76 if ((i + 1) == argc)
77 {
78 free(options);
79 return NULL;
80 }
81 options->out_file = argv[i + 1];
82 i++;
83 continue;
84 }
85
86 if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d"))
87 {
88 options->debug = 1;
89 }
90
91 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
92 {
93 free(options);
94 return NULL;
95 }
96 }
97
98 if (!options->in_file)
99 {
100 free(options);
101 return NULL;
102 }
103
104 return options;
105}
36 106
37int main(int argc, char *argv[]) 107int main(int argc, char *argv[])
38{ 108{
@@ -43,27 +113,26 @@ int main(int argc, char *argv[])
43 int read_size = 0; 113 int read_size = 0;
44 char *plist_entire = NULL; 114 char *plist_entire = NULL;
45 struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); 115 struct stat *filestats = (struct stat *) malloc(sizeof(struct stat));
46 Options *options = parse_arguments(argc, argv); 116 options_t *options = parse_arguments(argc, argv);
47 117
48 if (!options) 118 if (!options)
49 { 119 {
50 print_usage(); 120 print_usage(argc, argv);
51 free(filestats); 121 free(filestats);
52 return 0; 122 return 0;
53 } 123 }
54 //read input file 124
125 // read input file
55 iplist = fopen(options->in_file, "rb"); 126 iplist = fopen(options->in_file, "rb");
56 if (!iplist) 127 if (!iplist)
57 return 1; 128 return 1;
129
58 stat(options->in_file, filestats); 130 stat(options->in_file, filestats);
59 plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1)); 131 plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1));
60 read_size = fread(plist_entire, sizeof(char), filestats->st_size, iplist); 132 read_size = fread(plist_entire, sizeof(char), filestats->st_size, iplist);
61 fclose(iplist); 133 fclose(iplist);
62 134
63 135 // convert from binary to xml or vice-versa
64 //convert one format to another
65
66
67 if (memcmp(plist_entire, "bplist00", 8) == 0) 136 if (memcmp(plist_entire, "bplist00", 8) == 0)
68 { 137 {
69 plist_from_bin(plist_entire, read_size, &root_node); 138 plist_from_bin(plist_entire, read_size, &root_node);
@@ -88,7 +157,7 @@ int main(int argc, char *argv[])
88 fwrite(plist_out, size, sizeof(char), oplist); 157 fwrite(plist_out, size, sizeof(char), oplist);
89 fclose(oplist); 158 fclose(oplist);
90 } 159 }
91 //if no output file specified, write to stdout 160 // if no output file specified, write to stdout
92 else 161 else
93 fwrite(plist_out, size, sizeof(char), stdout); 162 fwrite(plist_out, size, sizeof(char), stdout);
94 163
@@ -100,66 +169,3 @@ int main(int argc, char *argv[])
100 free(options); 169 free(options);
101 return 0; 170 return 0;
102} 171}
103
104Options *parse_arguments(int argc, char *argv[])
105{
106 int i = 0;
107
108 Options *options = (Options *) malloc(sizeof(Options));
109 memset(options, 0, sizeof(Options));
110
111 for (i = 1; i < argc; i++)
112 {
113 if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i"))
114 {
115 if ((i + 1) == argc)
116 {
117 free(options);
118 return NULL;
119 }
120 options->in_file = argv[i + 1];
121 i++;
122 continue;
123 }
124
125 if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o"))
126 {
127 if ((i + 1) == argc)
128 {
129 free(options);
130 return NULL;
131 }
132 options->out_file = argv[i + 1];
133 i++;
134 continue;
135 }
136
137 if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v"))
138 {
139 options->debug = 1;
140 }
141
142 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
143 {
144 free(options);
145 return NULL;
146 }
147 }
148
149 if (!options->in_file /*|| !options->out_file */ )
150 {
151 free(options);
152 return NULL;
153 }
154
155 return options;
156}
157
158void print_usage()
159{
160 printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n");
161 printf("\n");
162 printf("\t-i or --infile: The file to read in.\n");
163 printf("\t-o or --outfile: The file to convert to.\n");
164 printf("\t-d, -v or --debug: Provide extended debug information.\n\n");
165}