diff options
| author | 2023-02-06 18:57:40 +0100 | |
|---|---|---|
| committer | 2023-02-06 18:57:40 +0100 | |
| commit | 6390abcd1c94f4c29291c81322726d6946fd345f (patch) | |
| tree | 78230cf2e3469afcaed35051ee615d7a3369e617 | |
| parent | d3908006349f38bcfc0151daebd98b6873a2dbfc (diff) | |
| download | libplist-6390abcd1c94f4c29291c81322726d6946fd345f.tar.gz libplist-6390abcd1c94f4c29291c81322726d6946fd345f.tar.bz2 | |
plistutil: Add command line switch to allow sorting of the output plist
| -rw-r--r-- | docs/plistutil.1 | 3 | ||||
| -rw-r--r-- | tools/plistutil.c | 29 |
2 files changed, 26 insertions, 6 deletions
diff --git a/docs/plistutil.1 b/docs/plistutil.1 index 5342e91..b0bf61d 100644 --- a/docs/plistutil.1 +++ b/docs/plistutil.1 | |||
| @@ -30,6 +30,9 @@ convert to/from JSON or OpenStep the output format needs to specified. | |||
| 30 | JSON and OpenStep only: Print output in compact form. By default, the output | 30 | JSON and OpenStep only: Print output in compact form. By default, the output |
| 31 | will be pretty-printed. | 31 | will be pretty-printed. |
| 32 | .TP | 32 | .TP |
| 33 | .B \-s, \-\-sort | ||
| 34 | Sort all dictionary nodes lexicographically by key before converting to the output format. | ||
| 35 | .TP | ||
| 33 | .B \-h, \-\-help | 36 | .B \-h, \-\-help |
| 34 | Prints usage information. | 37 | Prints usage information. |
| 35 | .TP | 38 | .TP |
diff --git a/tools/plistutil.c b/tools/plistutil.c index 6254b7c..1c199fe 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -41,10 +41,12 @@ | |||
| 41 | typedef struct _options | 41 | typedef struct _options |
| 42 | { | 42 | { |
| 43 | char *in_file, *out_file; | 43 | char *in_file, *out_file; |
| 44 | uint8_t debug; | ||
| 45 | uint8_t compact; | ||
| 46 | uint8_t in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json, 4 = openstep | 44 | uint8_t in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json, 4 = openstep |
| 45 | uint8_t flags; | ||
| 47 | } options_t; | 46 | } options_t; |
| 47 | #define OPT_DEBUG (1 << 0) | ||
| 48 | #define OPT_COMPACT (1 << 1) | ||
| 49 | #define OPT_SORT (1 << 2) | ||
| 48 | 50 | ||
| 49 | static void print_usage(int argc, char *argv[]) | 51 | static void print_usage(int argc, char *argv[]) |
| 50 | { | 52 | { |
| @@ -65,6 +67,8 @@ static void print_usage(int argc, char *argv[]) | |||
| 65 | printf(" and binary to XML.\n"); | 67 | printf(" and binary to XML.\n"); |
| 66 | printf(" -c, --compact JSON and OpenStep only: Print output in compact form.\n"); | 68 | printf(" -c, --compact JSON and OpenStep only: Print output in compact form.\n"); |
| 67 | printf(" By default, the output will be pretty-printed.\n"); | 69 | printf(" By default, the output will be pretty-printed.\n"); |
| 70 | printf(" -s, --sort Sort all dictionary nodes lexicographically by key\n"); | ||
| 71 | printf(" before converting to the output format.\n"); | ||
| 68 | printf(" -d, --debug Enable extended debug output\n"); | 72 | printf(" -d, --debug Enable extended debug output\n"); |
| 69 | printf(" -v, --version Print version information\n"); | 73 | printf(" -v, --version Print version information\n"); |
| 70 | printf("\n"); | 74 | printf("\n"); |
| @@ -128,11 +132,15 @@ static options_t *parse_arguments(int argc, char *argv[]) | |||
| 128 | } | 132 | } |
| 129 | else if (!strcmp(argv[i], "--compact") || !strcmp(argv[i], "-c")) | 133 | else if (!strcmp(argv[i], "--compact") || !strcmp(argv[i], "-c")) |
| 130 | { | 134 | { |
| 131 | options->compact = 1; | 135 | options->flags |= OPT_COMPACT; |
| 136 | } | ||
| 137 | else if (!strcmp(argv[i], "--sort") || !strcmp(argv[i], "-s")) | ||
| 138 | { | ||
| 139 | options->flags |= OPT_SORT; | ||
| 132 | } | 140 | } |
| 133 | else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d")) | 141 | else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d")) |
| 134 | { | 142 | { |
| 135 | options->debug = 1; | 143 | options->flags |= OPT_DEBUG; |
| 136 | } | 144 | } |
| 137 | else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) | 145 | else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) |
| 138 | { | 146 | { |
| @@ -252,6 +260,9 @@ int main(int argc, char *argv[]) | |||
| 252 | { | 260 | { |
| 253 | input_res = plist_from_bin(plist_entire, read_size, &root_node); | 261 | input_res = plist_from_bin(plist_entire, read_size, &root_node); |
| 254 | if (input_res == PLIST_ERR_SUCCESS) { | 262 | if (input_res == PLIST_ERR_SUCCESS) { |
| 263 | if (options->flags & OPT_SORT) { | ||
| 264 | plist_sort(root_node); | ||
| 265 | } | ||
| 255 | output_res = plist_to_xml(root_node, &plist_out, &size); | 266 | output_res = plist_to_xml(root_node, &plist_out, &size); |
| 256 | } | 267 | } |
| 257 | } | 268 | } |
| @@ -259,6 +270,9 @@ int main(int argc, char *argv[]) | |||
| 259 | { | 270 | { |
| 260 | input_res = plist_from_xml(plist_entire, read_size, &root_node); | 271 | input_res = plist_from_xml(plist_entire, read_size, &root_node); |
| 261 | if (input_res == PLIST_ERR_SUCCESS) { | 272 | if (input_res == PLIST_ERR_SUCCESS) { |
| 273 | if (options->flags & OPT_SORT) { | ||
| 274 | plist_sort(root_node); | ||
| 275 | } | ||
| 262 | output_res = plist_to_bin(root_node, &plist_out, &size); | 276 | output_res = plist_to_bin(root_node, &plist_out, &size); |
| 263 | } | 277 | } |
| 264 | } | 278 | } |
| @@ -267,14 +281,17 @@ int main(int argc, char *argv[]) | |||
| 267 | { | 281 | { |
| 268 | input_res = plist_from_memory(plist_entire, read_size, &root_node); | 282 | input_res = plist_from_memory(plist_entire, read_size, &root_node); |
| 269 | if (input_res == PLIST_ERR_SUCCESS) { | 283 | if (input_res == PLIST_ERR_SUCCESS) { |
| 284 | if (options->flags & OPT_SORT) { | ||
| 285 | plist_sort(root_node); | ||
| 286 | } | ||
| 270 | if (options->out_fmt == 1) { | 287 | if (options->out_fmt == 1) { |
| 271 | output_res = plist_to_bin(root_node, &plist_out, &size); | 288 | output_res = plist_to_bin(root_node, &plist_out, &size); |
| 272 | } else if (options->out_fmt == 2) { | 289 | } else if (options->out_fmt == 2) { |
| 273 | output_res = plist_to_xml(root_node, &plist_out, &size); | 290 | output_res = plist_to_xml(root_node, &plist_out, &size); |
| 274 | } else if (options->out_fmt == 3) { | 291 | } else if (options->out_fmt == 3) { |
| 275 | output_res = plist_to_json(root_node, &plist_out, &size, !options->compact); | 292 | output_res = plist_to_json(root_node, &plist_out, &size, !(options->flags & OPT_COMPACT)); |
| 276 | } else if (options->out_fmt == 4) { | 293 | } else if (options->out_fmt == 4) { |
| 277 | output_res = plist_to_openstep(root_node, &plist_out, &size, !options->compact); | 294 | output_res = plist_to_openstep(root_node, &plist_out, &size, !(options->flags & OPT_COMPACT)); |
| 278 | } | 295 | } |
| 279 | } | 296 | } |
| 280 | } | 297 | } |
