diff options
| author | 2008-12-13 13:56:55 +0100 | |
|---|---|---|
| committer | 2008-12-13 13:56:55 +0100 | |
| commit | e220e2cf08809a6a8853a8c9c7b06cef4e90cb57 (patch) | |
| tree | 51472b19b56c5816fc050fcac6273a5931f9f4f6 /plutil/plutil.c | |
| parent | 3fdd24aea06a9bf38d9d34fb8bccbb7023ed3100 (diff) | |
| download | libplist-e220e2cf08809a6a8853a8c9c7b06cef4e90cb57.tar.gz libplist-e220e2cf08809a6a8853a8c9c7b06cef4e90cb57.tar.bz2 | |
Add plutil and do some cleaning.
Diffstat (limited to 'plutil/plutil.c')
| -rw-r--r-- | plutil/plutil.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/plutil/plutil.c b/plutil/plutil.c new file mode 100644 index 0000000..ef22a77 --- /dev/null +++ b/plutil/plutil.c | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* | ||
| 2 | * plutil.C | ||
| 3 | * source for plist convertion tool | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Zach C. All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 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 | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | |||
| 23 | #include "plist/plist.h" | ||
| 24 | #include "plutil.h" | ||
| 25 | |||
| 26 | #include <stdio.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <sys/stat.h> | ||
| 30 | |||
| 31 | int main(int argc, char *argv[]) | ||
| 32 | { | ||
| 33 | struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); | ||
| 34 | Options *options = parse_arguments(argc, argv); | ||
| 35 | |||
| 36 | if (!options) { | ||
| 37 | print_usage(); | ||
| 38 | return 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | iphone_set_debug(options->debug); | ||
| 42 | |||
| 43 | //read input file | ||
| 44 | FILE *iplist = fopen(options->in_file, "r"); | ||
| 45 | if (!iplist) | ||
| 46 | return 1; | ||
| 47 | stat(options->in_file, filestats); | ||
| 48 | char *plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1)); | ||
| 49 | fread(plist_entire, sizeof(char), filestats->st_size, iplist); | ||
| 50 | fclose(iplist); | ||
| 51 | |||
| 52 | |||
| 53 | //convert one format to another | ||
| 54 | plist_t root_node = NULL; | ||
| 55 | char *plist_out = NULL; | ||
| 56 | int size = 0; | ||
| 57 | |||
| 58 | if (memcmp(plist_entire, "bplist00", 8) == 0) { | ||
| 59 | plist_from_bin(plist_entire, filestats->st_size, &root_node); | ||
| 60 | plist_to_xml(root_node, &plist_out, &size); | ||
| 61 | } else { | ||
| 62 | plist_from_xml(plist_entire, filestats->st_size, &root_node); | ||
| 63 | plist_to_bin(root_node, &plist_out, &size); | ||
| 64 | } | ||
| 65 | |||
| 66 | if (plist_out) { | ||
| 67 | if (options->out_file != NULL) { | ||
| 68 | FILE *oplist = fopen(options->out_file, "wb"); | ||
| 69 | if (!oplist) | ||
| 70 | return 1; | ||
| 71 | fwrite(plist_out, size, sizeof(char), oplist); | ||
| 72 | fclose(oplist); | ||
| 73 | } | ||
| 74 | //if no output file specified, write to stdout | ||
| 75 | else | ||
| 76 | fwrite(plist_out, size, sizeof(char), stdout); | ||
| 77 | } else | ||
| 78 | printf("ERROR\n"); | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | Options *parse_arguments(int argc, char *argv[]) | ||
| 83 | { | ||
| 84 | int i = 0; | ||
| 85 | |||
| 86 | Options *options = (Options *) malloc(sizeof(Options)); | ||
| 87 | memset(options, 0, sizeof(Options)); | ||
| 88 | |||
| 89 | for (i = 1; i < argc; i++) { | ||
| 90 | if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) { | ||
| 91 | if ((i + 1) == argc) { | ||
| 92 | free(options); | ||
| 93 | return NULL; | ||
| 94 | } | ||
| 95 | options->in_file = argv[i + 1]; | ||
| 96 | i++; | ||
| 97 | continue; | ||
| 98 | } | ||
| 99 | |||
| 100 | if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) { | ||
| 101 | if ((i + 1) == argc) { | ||
| 102 | free(options); | ||
| 103 | return NULL; | ||
| 104 | } | ||
| 105 | options->out_file = argv[i + 1]; | ||
| 106 | i++; | ||
| 107 | continue; | ||
| 108 | } | ||
| 109 | |||
| 110 | if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) { | ||
| 111 | options->debug = 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) { | ||
| 115 | free(options); | ||
| 116 | return NULL; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | if (!options->in_file /*|| !options->out_file */ ) { | ||
| 121 | free(options); | ||
| 122 | return NULL; | ||
| 123 | } | ||
| 124 | |||
| 125 | return options; | ||
| 126 | } | ||
| 127 | |||
| 128 | void print_usage() | ||
| 129 | { | ||
| 130 | printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n"); | ||
| 131 | printf("\n"); | ||
| 132 | printf("\t-i or --infile: The file to read in.\n"); | ||
| 133 | printf("\t-o or --outfile: The file to convert to.\n"); | ||
| 134 | printf("\t-d, -v or --debug: Provide extended debug information.\n\n"); | ||
| 135 | } | ||
