diff options
| -rw-r--r-- | dev/plutil.c | 1 | ||||
| -rw-r--r-- | src/plist.c | 153 | ||||
| -rw-r--r-- | src/plist.h | 12 |
3 files changed, 9 insertions, 157 deletions
diff --git a/dev/plutil.c b/dev/plutil.c index 0e25291..4a34077 100644 --- a/dev/plutil.c +++ b/dev/plutil.c | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <stdio.h> | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 12 | 12 | ||
| 13 | |||
| 13 | int main(int argc, char *argv[]) | 14 | int main(int argc, char *argv[]) |
| 14 | { | 15 | { |
| 15 | struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); | 16 | struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); |
diff --git a/src/plist.c b/src/plist.c index 1c00cc6..5d8fc0e 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -101,151 +101,6 @@ void free_plist(xmlDocPtr plist) | |||
| 101 | xmlFreeDoc(plist); | 101 | xmlFreeDoc(plist); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | /** Adds a new node as a child to a given node. | ||
| 105 | * | ||
| 106 | * This is a lower level function so you probably want to use | ||
| 107 | * add_key_str_dict_element, add_key_dict_node or add_key_data_dict_element | ||
| 108 | * instead. | ||
| 109 | * | ||
| 110 | * @param plist The plist XML document to which the to_node belongs. | ||
| 111 | * @param name The name of the new node. | ||
| 112 | * @param content The string containing the text node of the new node. | ||
| 113 | * @param to_node The node to attach the child node to. If none is given, the | ||
| 114 | * root node of the given document is used. | ||
| 115 | * @param depth The number of tabs to indent the new node. | ||
| 116 | * | ||
| 117 | * @return The newly created node. | ||
| 118 | */ | ||
| 119 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth) | ||
| 120 | { | ||
| 121 | int i = 0; | ||
| 122 | xmlNode *child; | ||
| 123 | |||
| 124 | if (!plist) | ||
| 125 | return NULL; | ||
| 126 | assert(depth >= 0); | ||
| 127 | if (!to_node) | ||
| 128 | to_node = xmlDocGetRootElement(plist); | ||
| 129 | |||
| 130 | for (i = 0; i < depth; i++) { | ||
| 131 | xmlNodeAddContent(to_node, "\t"); | ||
| 132 | } | ||
| 133 | child = xmlNewChild(to_node, NULL, name, content); | ||
| 134 | xmlNodeAddContent(to_node, "\n"); | ||
| 135 | |||
| 136 | return child; | ||
| 137 | } | ||
| 138 | |||
| 139 | /** Adds a string key-pair to a plist XML document. | ||
| 140 | * | ||
| 141 | * @param plist The plist XML document to add the new node to. | ||
| 142 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 143 | * @param key The string containing the key value. | ||
| 144 | * @param value The string containing the value. | ||
| 145 | * @param depth The number of tabs to indent the new node. | ||
| 146 | * | ||
| 147 | * @return The newly created key node. | ||
| 148 | */ | ||
| 149 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 150 | { | ||
| 151 | xmlNode *keyPtr; | ||
| 152 | |||
| 153 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | ||
| 154 | add_child_to_plist(plist, "string", value, dict, depth); | ||
| 155 | |||
| 156 | return keyPtr; | ||
| 157 | } | ||
| 158 | |||
| 159 | /** Adds a new dictionary key-pair to a plist XML document. | ||
| 160 | * | ||
| 161 | * @param plist The plist XML document to add the new node to. | ||
| 162 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 163 | * @param key The string containing the key value. | ||
| 164 | * @param value The string containing the value. | ||
| 165 | * @param depth The number of tabs to indent the new node. | ||
| 166 | * | ||
| 167 | * @return The newly created dict node. | ||
| 168 | */ | ||
| 169 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 170 | { | ||
| 171 | xmlNode *child; | ||
| 172 | |||
| 173 | add_child_to_plist(plist, "key", key, dict, depth); | ||
| 174 | child = add_child_to_plist(plist, "dict", value, dict, depth); | ||
| 175 | |||
| 176 | return child; | ||
| 177 | } | ||
| 178 | |||
| 179 | /** Adds a new data dictionary key-pair to a plist XML document. | ||
| 180 | * | ||
| 181 | * @param plist The plist XML document to add the new node to. | ||
| 182 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 183 | * @param key The string containing the key value. | ||
| 184 | * @param value The string containing the value. | ||
| 185 | * @param depth The number of tabs to indent the new node. | ||
| 186 | * | ||
| 187 | * @return The newly created key node. | ||
| 188 | */ | ||
| 189 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 190 | { | ||
| 191 | xmlNode *keyPtr; | ||
| 192 | |||
| 193 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | ||
| 194 | add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); | ||
| 195 | |||
| 196 | return keyPtr; | ||
| 197 | } | ||
| 198 | |||
| 199 | /** Reads a set of keys and strings into an array from a plist XML document. | ||
| 200 | * | ||
| 201 | * @param dict The root XMLNode of a plist XML document to be read. | ||
| 202 | * | ||
| 203 | * @return An array where each even number is a key and the odd numbers are | ||
| 204 | * values. If the odd number is \0, that's the end of the list. | ||
| 205 | */ | ||
| 206 | char **read_dict_element_strings(xmlNode * dict) | ||
| 207 | { | ||
| 208 | char **return_me = NULL, **old = NULL; | ||
| 209 | int current_length = 0; | ||
| 210 | int current_pos = 0; | ||
| 211 | xmlNode *dict_walker; | ||
| 212 | |||
| 213 | for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { | ||
| 214 | if (!xmlStrcmp(dict_walker->name, "key")) { | ||
| 215 | current_length += 2; | ||
| 216 | old = return_me; | ||
| 217 | return_me = realloc(return_me, sizeof(char *) * current_length); | ||
| 218 | if (!return_me) { | ||
| 219 | free(old); | ||
| 220 | return NULL; | ||
| 221 | } | ||
| 222 | return_me[current_pos++] = xmlNodeGetContent(dict_walker); | ||
| 223 | return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | old = return_me; | ||
| 228 | return_me = realloc(return_me, sizeof(char *) * (current_length + 1)); | ||
| 229 | return_me[current_pos] = NULL; | ||
| 230 | |||
| 231 | return return_me; | ||
| 232 | } | ||
| 233 | |||
| 234 | /** Destroys a dictionary as returned by read_dict_element_strings | ||
| 235 | */ | ||
| 236 | void free_dictionary(char **dictionary) | ||
| 237 | { | ||
| 238 | int i = 0; | ||
| 239 | |||
| 240 | if (!dictionary) | ||
| 241 | return; | ||
| 242 | |||
| 243 | for (i = 0; dictionary[i]; i++) { | ||
| 244 | free(dictionary[i]); | ||
| 245 | } | ||
| 246 | |||
| 247 | free(dictionary); | ||
| 248 | } | ||
| 249 | 104 | ||
| 250 | /* | 105 | /* |
| 251 | * Binary propertylist code follows | 106 | * Binary propertylist code follows |
| @@ -357,6 +212,14 @@ void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void * | |||
| 357 | { | 212 | { |
| 358 | } | 213 | } |
| 359 | 214 | ||
| 215 | /** Adds a new key pair to a dict. | ||
| 216 | * | ||
| 217 | * @param dict The dict node in the plist. | ||
| 218 | * @param key the key name of the key pair. | ||
| 219 | * @param type The the type of the value in the key pair. | ||
| 220 | * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value | ||
| 221 | * | ||
| 222 | */ | ||
| 360 | void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value) | 223 | void plist_add_dict_element(dict_t dict, char *key, plist_type type, void *value) |
| 361 | { | 224 | { |
| 362 | if (!dict || !key || !value) | 225 | if (!dict || !key || !value) |
diff --git a/src/plist.h b/src/plist.h index 34e3934..ed3d2b2 100644 --- a/src/plist.h +++ b/src/plist.h | |||
| @@ -22,8 +22,6 @@ | |||
| 22 | #ifndef PLIST_H | 22 | #ifndef PLIST_H |
| 23 | #define PLIST_H | 23 | #define PLIST_H |
| 24 | 24 | ||
| 25 | #include <libxml/parser.h> | ||
| 26 | #include <libxml/tree.h> | ||
| 27 | #include <stdint.h> | 25 | #include <stdint.h> |
| 28 | #include <wchar.h> | 26 | #include <wchar.h> |
| 29 | 27 | ||
| @@ -33,16 +31,7 @@ | |||
| 33 | #include <glib.h> | 31 | #include <glib.h> |
| 34 | 32 | ||
| 35 | char *format_string(const char *buf, int cols, int depth); | 33 | char *format_string(const char *buf, int cols, int depth); |
| 36 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); | ||
| 37 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); | ||
| 38 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); | ||
| 39 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth); | ||
| 40 | 34 | ||
| 41 | void free_plist(xmlDocPtr plist); | ||
| 42 | xmlDocPtr new_plist(); | ||
| 43 | |||
| 44 | char **read_dict_element_strings(xmlNode * dict); | ||
| 45 | void free_dictionary(char **dictionary); | ||
| 46 | 35 | ||
| 47 | /* Binary plist stuff */ | 36 | /* Binary plist stuff */ |
| 48 | 37 | ||
| @@ -70,7 +59,6 @@ typedef GNode *plist_t; | |||
| 70 | typedef GNode *dict_t; | 59 | typedef GNode *dict_t; |
| 71 | typedef GNode *array_t; | 60 | typedef GNode *array_t; |
| 72 | 61 | ||
| 73 | |||
| 74 | void plist_new_plist(plist_t * plist); | 62 | void plist_new_plist(plist_t * plist); |
| 75 | void plist_new_dict_in_plist(plist_t plist, dict_t * dict); | 63 | void plist_new_dict_in_plist(plist_t plist, dict_t * dict); |
| 76 | void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void **values, array_t * array); | 64 | void plist_new_array_in_plist(plist_t plist, int length, plist_type type, void **values, array_t * array); |
