Created
September 16, 2021 12:31
-
-
Save marvinborner/8733f2d665be99dba44e4d954a64c4ea to your computer and use it in GitHub Desktop.
Quick and dirty plist xml dump/formatter for debugging/representation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <plist/plist.h> | |
#define INDENT 4 | |
void plist_dump(plist_t plist, int depth) | |
{ | |
for (int i = 0; i < depth * INDENT; i++) | |
printf(" "); | |
if (PLIST_IS_BOOLEAN(plist)) { | |
unsigned char val; | |
plist_get_bool_val(plist, &val); | |
printf("<bool>%s</bool>\n", val ? "true" : "false"); | |
} else if (PLIST_IS_UINT(plist)) { | |
unsigned long val; | |
plist_get_uint_val(plist, &val); | |
printf("<uint>%lu</uint>\n", val); | |
} else if (PLIST_IS_REAL(plist)) { | |
double val; | |
plist_get_real_val(plist, &val); | |
printf("<real>%f</real>\n", val); | |
} else if (PLIST_IS_STRING(plist)) { | |
char *val; | |
plist_get_string_val(plist, &val); | |
printf("<string>%s</string>\n", val); | |
free(val); | |
} else if (PLIST_IS_ARRAY(plist)) { | |
plist_array_iter iter; | |
plist_array_new_iter(plist, &iter); | |
plist_t val; | |
printf("<array>\n"); | |
int id = 0; | |
while (1) { | |
plist_array_next_item(plist, iter, &val); | |
if (!val) | |
break; | |
for (int i = 0; i < (depth + 1) * INDENT; i++) | |
printf(" "); | |
printf("<array_item id=\"%d\">\n", id++); | |
plist_dump(val, depth + 2); | |
for (int i = 0; i < (depth + 1) * INDENT; i++) | |
printf(" "); | |
printf("</array_item>\n"); | |
} | |
for (int i = 0; i < depth * INDENT; i++) | |
printf(" "); | |
printf("</array>\n"); | |
} else if (PLIST_IS_DICT(plist)) { | |
plist_dict_iter iter; | |
plist_dict_new_iter(plist, &iter); | |
char *key = 0; | |
plist_t val; | |
printf("<dict>\n"); | |
int id = 0; | |
while (1) { | |
plist_dict_next_item(plist, iter, &key, &val); | |
if (!key || !val) | |
break; | |
for (int i = 0; i < (depth + 1) * INDENT; i++) | |
printf(" "); | |
printf("<dict_item key=\"%s\" id=\"%d\">\n", key, id++); | |
plist_dump(val, depth + 2); | |
free(key); | |
for (int i = 0; i < (depth + 1) * INDENT; i++) | |
printf(" "); | |
printf("</dict_item>\n"); | |
} | |
for (int i = 0; i < depth * INDENT; i++) | |
printf(" "); | |
printf("</dict>\n"); | |
} else if (PLIST_IS_DATE(plist)) { | |
int sec, usec; | |
plist_get_date_val(plist, &sec, &usec); | |
printf("<date>%d</date>\n", sec); // Since 01/01/2001 | |
} else if (PLIST_IS_DATA(plist)) { | |
char *val; | |
unsigned long length; | |
plist_get_data_val(plist, &val, &length); | |
printf("<data>%.*s</data>\n", (int)length, val); | |
free(val); | |
} else if (PLIST_IS_KEY(plist)) { | |
char *val; | |
plist_get_key_val(plist, &val); | |
printf("<key>%s</key>\n", val); | |
free(val); | |
} else if (PLIST_IS_UID(plist)) { | |
unsigned long val; | |
plist_get_uid_val(plist, &val); | |
printf("<uid>%lu</uid>\n", val); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment