diff options
| author | 2009-10-28 18:31:34 +0100 | |
|---|---|---|
| committer | 2009-10-28 18:31:34 +0100 | |
| commit | 6710f4bfb980dd0fe6e75e4d6cba75532cde30e8 (patch) | |
| tree | 6429eb306d3d464ebbcc0de558559b636d8058c2 /plutil/plutil.c | |
| parent | fed2573566c2da1c5489260069a99ae9d2abf255 (diff) | |
| download | libplist-6710f4bfb980dd0fe6e75e4d6cba75532cde30e8.tar.gz libplist-6710f4bfb980dd0fe6e75e4d6cba75532cde30e8.tar.bz2 | |
Format sources to ANSI style using AStyle (astyle --style=ansi).
Diffstat (limited to 'plutil/plutil.c')
| -rw-r--r-- | plutil/plutil.c | 225 |
1 files changed, 120 insertions, 105 deletions
diff --git a/plutil/plutil.c b/plutil/plutil.c index 75cd3f1..e9eaef1 100644 --- a/plutil/plutil.c +++ b/plutil/plutil.c | |||
| @@ -8,15 +8,15 @@ | |||
| 8 | * modify it under the terms of the GNU Lesser General Public | 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either | 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. | 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * | 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, | 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. | 15 | * Lesser General Public License for more details. |
| 16 | * | 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public | 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software | 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | 22 | ||
| @@ -35,114 +35,129 @@ | |||
| 35 | 35 | ||
| 36 | int main(int argc, char *argv[]) | 36 | int main(int argc, char *argv[]) |
| 37 | { | 37 | { |
| 38 | FILE *iplist = NULL; | 38 | FILE *iplist = NULL; |
| 39 | plist_t root_node = NULL; | 39 | plist_t root_node = NULL; |
| 40 | char *plist_out = NULL; | 40 | char *plist_out = NULL; |
| 41 | int size = 0; | 41 | int size = 0; |
| 42 | char *plist_entire = NULL; | 42 | char *plist_entire = NULL; |
| 43 | struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); | 43 | struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); |
| 44 | Options *options = parse_arguments(argc, argv); | 44 | Options *options = parse_arguments(argc, argv); |
| 45 | 45 | ||
| 46 | if (!options) { | 46 | if (!options) |
| 47 | print_usage(); | 47 | { |
| 48 | free(filestats); | 48 | print_usage(); |
| 49 | return 0; | 49 | free(filestats); |
| 50 | } | 50 | return 0; |
| 51 | //read input file | 51 | } |
| 52 | iplist = fopen(options->in_file, "rb"); | 52 | //read input file |
| 53 | if (!iplist) | 53 | iplist = fopen(options->in_file, "rb"); |
| 54 | return 1; | 54 | if (!iplist) |
| 55 | stat(options->in_file, filestats); | 55 | return 1; |
| 56 | plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1)); | 56 | stat(options->in_file, filestats); |
| 57 | fread(plist_entire, sizeof(char), filestats->st_size, iplist); | 57 | plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1)); |
| 58 | fclose(iplist); | 58 | fread(plist_entire, sizeof(char), filestats->st_size, iplist); |
| 59 | 59 | fclose(iplist); | |
| 60 | 60 | ||
| 61 | //convert one format to another | 61 | |
| 62 | 62 | //convert one format to another | |
| 63 | 63 | ||
| 64 | if (memcmp(plist_entire, "bplist00", 8) == 0) { | 64 | |
| 65 | plist_from_bin(plist_entire, filestats->st_size, &root_node); | 65 | if (memcmp(plist_entire, "bplist00", 8) == 0) |
| 66 | plist_to_xml(root_node, &plist_out, &size); | 66 | { |
| 67 | } else { | 67 | plist_from_bin(plist_entire, filestats->st_size, &root_node); |
| 68 | plist_from_xml(plist_entire, filestats->st_size, &root_node); | 68 | plist_to_xml(root_node, &plist_out, &size); |
| 69 | plist_to_bin(root_node, &plist_out, &size); | 69 | } |
| 70 | } | 70 | else |
| 71 | plist_free(root_node); | 71 | { |
| 72 | free(plist_entire); | 72 | plist_from_xml(plist_entire, filestats->st_size, &root_node); |
| 73 | free(filestats); | 73 | plist_to_bin(root_node, &plist_out, &size); |
| 74 | 74 | } | |
| 75 | if (plist_out) { | 75 | plist_free(root_node); |
| 76 | if (options->out_file != NULL) { | 76 | free(plist_entire); |
| 77 | FILE *oplist = fopen(options->out_file, "wb"); | 77 | free(filestats); |
| 78 | if (!oplist) | 78 | |
| 79 | return 1; | 79 | if (plist_out) |
| 80 | fwrite(plist_out, size, sizeof(char), oplist); | 80 | { |
| 81 | fclose(oplist); | 81 | if (options->out_file != NULL) |
| 82 | } | 82 | { |
| 83 | //if no output file specified, write to stdout | 83 | FILE *oplist = fopen(options->out_file, "wb"); |
| 84 | else | 84 | if (!oplist) |
| 85 | fwrite(plist_out, size, sizeof(char), stdout); | 85 | return 1; |
| 86 | 86 | fwrite(plist_out, size, sizeof(char), oplist); | |
| 87 | free(plist_out); | 87 | fclose(oplist); |
| 88 | } else | 88 | } |
| 89 | printf("ERROR\n"); | 89 | //if no output file specified, write to stdout |
| 90 | 90 | else | |
| 91 | free(options); | 91 | fwrite(plist_out, size, sizeof(char), stdout); |
| 92 | return 0; | 92 | |
| 93 | free(plist_out); | ||
| 94 | } | ||
| 95 | else | ||
| 96 | printf("ERROR\n"); | ||
| 97 | |||
| 98 | free(options); | ||
| 99 | return 0; | ||
| 93 | } | 100 | } |
| 94 | 101 | ||
| 95 | Options *parse_arguments(int argc, char *argv[]) | 102 | Options *parse_arguments(int argc, char *argv[]) |
| 96 | { | 103 | { |
| 97 | int i = 0; | 104 | int i = 0; |
| 98 | 105 | ||
| 99 | Options *options = (Options *) malloc(sizeof(Options)); | 106 | Options *options = (Options *) malloc(sizeof(Options)); |
| 100 | memset(options, 0, sizeof(Options)); | 107 | memset(options, 0, sizeof(Options)); |
| 101 | 108 | ||
| 102 | for (i = 1; i < argc; i++) { | 109 | for (i = 1; i < argc; i++) |
| 103 | if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) { | 110 | { |
| 104 | if ((i + 1) == argc) { | 111 | if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) |
| 105 | free(options); | 112 | { |
| 106 | return NULL; | 113 | if ((i + 1) == argc) |
| 107 | } | 114 | { |
| 108 | options->in_file = argv[i + 1]; | 115 | free(options); |
| 109 | i++; | 116 | return NULL; |
| 110 | continue; | 117 | } |
| 111 | } | 118 | options->in_file = argv[i + 1]; |
| 112 | 119 | i++; | |
| 113 | if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) { | 120 | continue; |
| 114 | if ((i + 1) == argc) { | 121 | } |
| 115 | free(options); | 122 | |
| 116 | return NULL; | 123 | if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) |
| 117 | } | 124 | { |
| 118 | options->out_file = argv[i + 1]; | 125 | if ((i + 1) == argc) |
| 119 | i++; | 126 | { |
| 120 | continue; | 127 | free(options); |
| 121 | } | 128 | return NULL; |
| 122 | 129 | } | |
| 123 | if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) { | 130 | options->out_file = argv[i + 1]; |
| 124 | options->debug = 1; | 131 | i++; |
| 125 | } | 132 | continue; |
| 126 | 133 | } | |
| 127 | if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) { | 134 | |
| 128 | free(options); | 135 | if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) |
| 129 | return NULL; | 136 | { |
| 130 | } | 137 | options->debug = 1; |
| 131 | } | 138 | } |
| 132 | 139 | ||
| 133 | if (!options->in_file /*|| !options->out_file */ ) { | 140 | if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) |
| 134 | free(options); | 141 | { |
| 135 | return NULL; | 142 | free(options); |
| 136 | } | 143 | return NULL; |
| 137 | 144 | } | |
| 138 | return options; | 145 | } |
| 146 | |||
| 147 | if (!options->in_file /*|| !options->out_file */ ) | ||
| 148 | { | ||
| 149 | free(options); | ||
| 150 | return NULL; | ||
| 151 | } | ||
| 152 | |||
| 153 | return options; | ||
| 139 | } | 154 | } |
| 140 | 155 | ||
| 141 | void print_usage() | 156 | void print_usage() |
| 142 | { | 157 | { |
| 143 | printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n"); | 158 | printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n"); |
| 144 | printf("\n"); | 159 | printf("\n"); |
| 145 | printf("\t-i or --infile: The file to read in.\n"); | 160 | printf("\t-i or --infile: The file to read in.\n"); |
| 146 | printf("\t-o or --outfile: The file to convert to.\n"); | 161 | printf("\t-o or --outfile: The file to convert to.\n"); |
| 147 | printf("\t-d, -v or --debug: Provide extended debug information.\n\n"); | 162 | printf("\t-d, -v or --debug: Provide extended debug information.\n\n"); |
| 148 | } | 163 | } |
