diff options
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 245 |
1 files changed, 0 insertions, 245 deletions
diff --git a/src/plist.c b/src/plist.c deleted file mode 100644 index b9d9e6a..0000000 --- a/src/plist.c +++ /dev/null | |||
| @@ -1,245 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * plist.c | ||
| 3 | * Builds plist XML structures. | ||
| 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 | #include <libxml/parser.h> | ||
| 23 | #include <libxml/tree.h> | ||
| 24 | #include <string.h> | ||
| 25 | #include <assert.h> | ||
| 26 | #include "plist.h" | ||
| 27 | |||
| 28 | const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ | ||
| 29 | <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\ | ||
| 30 | <plist version=\"1.0\">\n\ | ||
| 31 | </plist>\0"; | ||
| 32 | |||
| 33 | /** Formats a block of text to be a given indentation and width. | ||
| 34 | * | ||
| 35 | * The total width of the return string will be depth + cols. | ||
| 36 | * | ||
| 37 | * @param buf The string to format. | ||
| 38 | * @param cols The number of text columns for returned block of text. | ||
| 39 | * @param depth The number of tabs to indent the returned block of text. | ||
| 40 | * | ||
| 41 | * @return The formatted string. | ||
| 42 | */ | ||
| 43 | static char *format_string(const char *buf, int cols, int depth) | ||
| 44 | { | ||
| 45 | int colw = depth + cols + 1; | ||
| 46 | int len = strlen(buf); | ||
| 47 | int nlines = len / cols + 1; | ||
| 48 | char *new_buf = (char *) malloc(nlines * colw + depth + 1); | ||
| 49 | int i = 0; | ||
| 50 | int j = 0; | ||
| 51 | |||
| 52 | assert(cols >= 0); | ||
| 53 | assert(depth >= 0); | ||
| 54 | |||
| 55 | // Inserts new lines and tabs at appropriate locations | ||
| 56 | for (i = 0; i < nlines; i++) { | ||
| 57 | new_buf[i * colw] = '\n'; | ||
| 58 | for (j = 0; j < depth; j++) | ||
| 59 | new_buf[i * colw + 1 + j] = '\t'; | ||
| 60 | memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); | ||
| 61 | } | ||
| 62 | new_buf[len + (1 + depth) * nlines] = '\n'; | ||
| 63 | |||
| 64 | // Inserts final row of indentation and termination character | ||
| 65 | for (j = 0; j < depth; j++) | ||
| 66 | new_buf[len + (1 + depth) * nlines + 1 + j] = '\t'; | ||
| 67 | new_buf[len + (1 + depth) * nlines + depth + 1] = '\0'; | ||
| 68 | |||
| 69 | return new_buf; | ||
| 70 | } | ||
| 71 | |||
| 72 | /** Creates a new plist XML document. | ||
| 73 | * | ||
| 74 | * @return The plist XML document. | ||
| 75 | */ | ||
| 76 | xmlDocPtr new_plist(void) | ||
| 77 | { | ||
| 78 | char *plist = strdup(plist_base); | ||
| 79 | xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); | ||
| 80 | |||
| 81 | if (!plist_xml) | ||
| 82 | return NULL; | ||
| 83 | |||
| 84 | free(plist); | ||
| 85 | |||
| 86 | return plist_xml; | ||
| 87 | } | ||
| 88 | |||
| 89 | /** Destroys a previously created XML document. | ||
| 90 | * | ||
| 91 | * @param plist The XML document to destroy. | ||
| 92 | */ | ||
| 93 | void free_plist(xmlDocPtr plist) | ||
| 94 | { | ||
| 95 | if (!plist) | ||
| 96 | return; | ||
| 97 | |||
| 98 | xmlFreeDoc(plist); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** Adds a new node as a child to a given node. | ||
| 102 | * | ||
| 103 | * This is a lower level function so you probably want to use | ||
| 104 | * add_key_str_dict_element, add_key_dict_node or add_key_data_dict_element | ||
| 105 | * instead. | ||
| 106 | * | ||
| 107 | * @param plist The plist XML document to which the to_node belongs. | ||
| 108 | * @param name The name of the new node. | ||
| 109 | * @param content The string containing the text node of the new node. | ||
| 110 | * @param to_node The node to attach the child node to. If none is given, the | ||
| 111 | * root node of the given document is used. | ||
| 112 | * @param depth The number of tabs to indent the new node. | ||
| 113 | * | ||
| 114 | * @return The newly created node. | ||
| 115 | */ | ||
| 116 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth) | ||
| 117 | { | ||
| 118 | int i = 0; | ||
| 119 | xmlNode *child; | ||
| 120 | |||
| 121 | if (!plist) | ||
| 122 | return NULL; | ||
| 123 | assert(depth >= 0); | ||
| 124 | if (!to_node) | ||
| 125 | to_node = xmlDocGetRootElement(plist); | ||
| 126 | |||
| 127 | for (i = 0; i < depth; i++) { | ||
| 128 | xmlNodeAddContent(to_node, "\t"); | ||
| 129 | } | ||
| 130 | child = xmlNewChild(to_node, NULL, name, content); | ||
| 131 | xmlNodeAddContent(to_node, "\n"); | ||
| 132 | |||
| 133 | return child; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** Adds a string key-pair to a plist XML document. | ||
| 137 | * | ||
| 138 | * @param plist The plist XML document to add the new node to. | ||
| 139 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 140 | * @param key The string containing the key value. | ||
| 141 | * @param value The string containing the value. | ||
| 142 | * @param depth The number of tabs to indent the new node. | ||
| 143 | * | ||
| 144 | * @return The newly created key node. | ||
| 145 | */ | ||
| 146 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 147 | { | ||
| 148 | xmlNode *keyPtr; | ||
| 149 | |||
| 150 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | ||
| 151 | add_child_to_plist(plist, "string", value, dict, depth); | ||
| 152 | |||
| 153 | return keyPtr; | ||
| 154 | } | ||
| 155 | |||
| 156 | /** Adds a new dictionary key-pair to a plist XML document. | ||
| 157 | * | ||
| 158 | * @param plist The plist XML document to add the new node to. | ||
| 159 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 160 | * @param key The string containing the key value. | ||
| 161 | * @param value The string containing the value. | ||
| 162 | * @param depth The number of tabs to indent the new node. | ||
| 163 | * | ||
| 164 | * @return The newly created dict node. | ||
| 165 | */ | ||
| 166 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 167 | { | ||
| 168 | xmlNode *child; | ||
| 169 | |||
| 170 | add_child_to_plist(plist, "key", key, dict, depth); | ||
| 171 | child = add_child_to_plist(plist, "dict", value, dict, depth); | ||
| 172 | |||
| 173 | return child; | ||
| 174 | } | ||
| 175 | |||
| 176 | /** Adds a new data dictionary key-pair to a plist XML document. | ||
| 177 | * | ||
| 178 | * @param plist The plist XML document to add the new node to. | ||
| 179 | * @param dict The dictionary node within the plist XML document to add the new node to. | ||
| 180 | * @param key The string containing the key value. | ||
| 181 | * @param value The string containing the value. | ||
| 182 | * @param depth The number of tabs to indent the new node. | ||
| 183 | * | ||
| 184 | * @return The newly created key node. | ||
| 185 | */ | ||
| 186 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) | ||
| 187 | { | ||
| 188 | xmlNode *keyPtr; | ||
| 189 | |||
| 190 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | ||
| 191 | add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); | ||
| 192 | |||
| 193 | return keyPtr; | ||
| 194 | } | ||
| 195 | |||
| 196 | /** Reads a set of keys and strings into an array from a plist XML document. | ||
| 197 | * | ||
| 198 | * @param dict The root XMLNode of a plist XML document to be read. | ||
| 199 | * | ||
| 200 | * @return An array where each even number is a key and the odd numbers are | ||
| 201 | * values. If the odd number is \0, that's the end of the list. | ||
| 202 | */ | ||
| 203 | char **read_dict_element_strings(xmlNode * dict) | ||
| 204 | { | ||
| 205 | char **return_me = NULL, **old = NULL; | ||
| 206 | int current_length = 0; | ||
| 207 | int current_pos = 0; | ||
| 208 | xmlNode *dict_walker; | ||
| 209 | |||
| 210 | for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { | ||
| 211 | if (!xmlStrcmp(dict_walker->name, "key")) { | ||
| 212 | current_length += 2; | ||
| 213 | old = return_me; | ||
| 214 | return_me = realloc(return_me, sizeof(char *) * current_length); | ||
| 215 | if (!return_me) { | ||
| 216 | free(old); | ||
| 217 | return NULL; | ||
| 218 | } | ||
| 219 | return_me[current_pos++] = xmlNodeGetContent(dict_walker); | ||
| 220 | return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | old = return_me; | ||
| 225 | return_me = realloc(return_me, sizeof(char *) * (current_length + 1)); | ||
| 226 | return_me[current_pos] = NULL; | ||
| 227 | |||
| 228 | return return_me; | ||
| 229 | } | ||
| 230 | |||
| 231 | /** Destroys a dictionary as returned by read_dict_element_strings | ||
| 232 | */ | ||
| 233 | void free_dictionary(char **dictionary) | ||
| 234 | { | ||
| 235 | int i = 0; | ||
| 236 | |||
| 237 | if (!dictionary) | ||
| 238 | return; | ||
| 239 | |||
| 240 | for (i = 0; dictionary[i]; i++) { | ||
| 241 | free(dictionary[i]); | ||
| 242 | } | ||
| 243 | |||
| 244 | free(dictionary); | ||
| 245 | } | ||
