summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c76
1 files changed, 44 insertions, 32 deletions
diff --git a/src/plist.c b/src/plist.c
index 31490d0..c4d6bfa 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -40,12 +40,12 @@ const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
40 * 40 *
41 * @return The formatted string. 41 * @return The formatted string.
42 */ 42 */
43char* format_string(const char* buf, int cols, int depth) 43char *format_string(const char *buf, int cols, int depth)
44{ 44{
45 int colw = depth + cols + 1; 45 int colw = depth + cols + 1;
46 int len = strlen(buf); 46 int len = strlen(buf);
47 int nlines = len / cols + 1; 47 int nlines = len / cols + 1;
48 char* new_buf = (char*)malloc(nlines * colw + depth + 1); 48 char *new_buf = (char *) malloc(nlines * colw + depth + 1);
49 int i = 0; 49 int i = 0;
50 int j = 0; 50 int j = 0;
51 51
@@ -53,18 +53,18 @@ char* format_string(const char* buf, int cols, int depth)
53 assert(depth >= 0); 53 assert(depth >= 0);
54 54
55 // Inserts new lines and tabs at appropriate locations 55 // Inserts new lines and tabs at appropriate locations
56 for (i = 0; i < nlines; i++){ 56 for (i = 0; i < nlines; i++) {
57 new_buf[i * colw] = '\n'; 57 new_buf[i * colw] = '\n';
58 for (j = 0; j < depth; j++) 58 for (j = 0; j < depth; j++)
59 new_buf[i * colw + 1 + j] = '\t'; 59 new_buf[i * colw + 1 + j] = '\t';
60 memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); 60 memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols);
61 } 61 }
62 new_buf[len+(1+depth)*nlines] = '\n'; 62 new_buf[len + (1 + depth) * nlines] = '\n';
63 63
64 // Inserts final row of indentation and termination character 64 // Inserts final row of indentation and termination character
65 for (j = 0; j < depth; j++) 65 for (j = 0; j < depth; j++)
66 new_buf[len+(1+depth)*nlines + 1 + j] = '\t'; 66 new_buf[len + (1 + depth) * nlines + 1 + j] = '\t';
67 new_buf[len+(1+depth)*nlines+depth+1] = '\0'; 67 new_buf[len + (1 + depth) * nlines + depth + 1] = '\0';
68 68
69 return new_buf; 69 return new_buf;
70} 70}
@@ -73,12 +73,14 @@ char* format_string(const char* buf, int cols, int depth)
73 * 73 *
74 * @return The plist XML document. 74 * @return The plist XML document.
75 */ 75 */
76xmlDocPtr new_plist() { 76xmlDocPtr new_plist()
77{
77 char *plist = strdup(plist_base); 78 char *plist = strdup(plist_base);
78 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); 79 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
79 80
80 if (!plist_xml) return NULL; 81 if (!plist_xml)
81 82 return NULL;
83
82 free(plist); 84 free(plist);
83 85
84 return plist_xml; 86 return plist_xml;
@@ -88,8 +90,10 @@ xmlDocPtr new_plist() {
88 * 90 *
89 * @param plist The XML document to destroy. 91 * @param plist The XML document to destroy.
90 */ 92 */
91void free_plist(xmlDocPtr plist) { 93void free_plist(xmlDocPtr plist)
92 if (!plist) return; 94{
95 if (!plist)
96 return;
93 97
94 xmlFreeDoc(plist); 98 xmlFreeDoc(plist);
95} 99}
@@ -109,14 +113,17 @@ void free_plist(xmlDocPtr plist) {
109 * 113 *
110 * @return The newly created node. 114 * @return The newly created node.
111 */ 115 */
112xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth) { 116xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth)
117{
113 int i = 0; 118 int i = 0;
114 xmlNode *child; 119 xmlNode *child;
115 120
116 if (!plist) return NULL; 121 if (!plist)
122 return NULL;
117 assert(depth >= 0); 123 assert(depth >= 0);
118 if (!to_node) to_node = xmlDocGetRootElement(plist); 124 if (!to_node)
119 125 to_node = xmlDocGetRootElement(plist);
126
120 for (i = 0; i < depth; i++) { 127 for (i = 0; i < depth; i++) {
121 xmlNodeAddContent(to_node, "\t"); 128 xmlNodeAddContent(to_node, "\t");
122 } 129 }
@@ -136,12 +143,13 @@ xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *conte
136 * 143 *
137 * @return The newly created key node. 144 * @return The newly created key node.
138 */ 145 */
139xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { 146xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
147{
140 xmlNode *keyPtr; 148 xmlNode *keyPtr;
141 149
142 keyPtr = add_child_to_plist(plist, "key", key, dict, depth); 150 keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
143 add_child_to_plist(plist, "string", value, dict, depth); 151 add_child_to_plist(plist, "string", value, dict, depth);
144 152
145 return keyPtr; 153 return keyPtr;
146} 154}
147 155
@@ -155,9 +163,10 @@ xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *ke
155 * 163 *
156 * @return The newly created dict node. 164 * @return The newly created dict node.
157 */ 165 */
158xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { 166xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
167{
159 xmlNode *child; 168 xmlNode *child;
160 169
161 add_child_to_plist(plist, "key", key, dict, depth); 170 add_child_to_plist(plist, "key", key, dict, depth);
162 child = add_child_to_plist(plist, "dict", value, dict, depth); 171 child = add_child_to_plist(plist, "dict", value, dict, depth);
163 172
@@ -174,12 +183,13 @@ xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, cons
174 * 183 *
175 * @return The newly created key node. 184 * @return The newly created key node.
176 */ 185 */
177xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { 186xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
187{
178 xmlNode *keyPtr; 188 xmlNode *keyPtr;
179 189
180 keyPtr = add_child_to_plist(plist, "key", key, dict, depth); 190 keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
181 add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); 191 add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth);
182 192
183 return keyPtr; 193 return keyPtr;
184} 194}
185 195
@@ -190,17 +200,18 @@ xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *k
190 * @return An array where each even number is a key and the odd numbers are 200 * @return An array where each even number is a key and the odd numbers are
191 * values. If the odd number is \0, that's the end of the list. 201 * values. If the odd number is \0, that's the end of the list.
192 */ 202 */
193char **read_dict_element_strings(xmlNode *dict) { 203char **read_dict_element_strings(xmlNode * dict)
204{
194 char **return_me = NULL, **old = NULL; 205 char **return_me = NULL, **old = NULL;
195 int current_length = 0; 206 int current_length = 0;
196 int current_pos = 0; 207 int current_pos = 0;
197 xmlNode *dict_walker; 208 xmlNode *dict_walker;
198 209
199 for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { 210 for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) {
200 if (!xmlStrcmp(dict_walker->name, "key")) { 211 if (!xmlStrcmp(dict_walker->name, "key")) {
201 current_length += 2; 212 current_length += 2;
202 old = return_me; 213 old = return_me;
203 return_me = realloc(return_me, sizeof(char*) * current_length); 214 return_me = realloc(return_me, sizeof(char *) * current_length);
204 if (!return_me) { 215 if (!return_me) {
205 free(old); 216 free(old);
206 return NULL; 217 return NULL;
@@ -209,25 +220,26 @@ char **read_dict_element_strings(xmlNode *dict) {
209 return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); 220 return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next);
210 } 221 }
211 } 222 }
212 223
213 old = return_me; 224 old = return_me;
214 return_me = realloc(return_me, sizeof(char*) * (current_length+1)); 225 return_me = realloc(return_me, sizeof(char *) * (current_length + 1));
215 return_me[current_pos] = NULL; 226 return_me[current_pos] = NULL;
216 227
217 return return_me; 228 return return_me;
218} 229}
219 230
220/** Destroys a dictionary as returned by read_dict_element_strings 231/** Destroys a dictionary as returned by read_dict_element_strings
221 */ 232 */
222void free_dictionary(char **dictionary) { 233void free_dictionary(char **dictionary)
234{
223 int i = 0; 235 int i = 0;
224 236
225 if (!dictionary) return; 237 if (!dictionary)
226 238 return;
239
227 for (i = 0; dictionary[i]; i++) { 240 for (i = 0; dictionary[i]; i++) {
228 free(dictionary[i]); 241 free(dictionary[i]);
229 } 242 }
230 243
231 free(dictionary); 244 free(dictionary);
232} 245}
233