summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-28 18:31:34 +0100
committerGravatar Jonathan Beck2009-10-28 18:31:34 +0100
commit6710f4bfb980dd0fe6e75e4d6cba75532cde30e8 (patch)
tree6429eb306d3d464ebbcc0de558559b636d8058c2 /src/plist.c
parentfed2573566c2da1c5489260069a99ae9d2abf255 (diff)
downloadlibplist-6710f4bfb980dd0fe6e75e4d6cba75532cde30e8.tar.gz
libplist-6710f4bfb980dd0fe6e75e4d6cba75532cde30e8.tar.bz2
Format sources to ANSI style using AStyle (astyle --style=ansi).
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c1212
1 files changed, 633 insertions, 579 deletions
diff --git a/src/plist.c b/src/plist.c
index 30be007..9628e38 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -28,719 +28,762 @@
28 28
29plist_t plist_new_node(plist_data_t data) 29plist_t plist_new_node(plist_data_t data)
30{ 30{
31 return (plist_t) g_node_new(data); 31 return (plist_t) g_node_new(data);
32} 32}
33 33
34plist_data_t plist_get_data(const plist_t node) 34plist_data_t plist_get_data(const plist_t node)
35{ 35{
36 if (!node) 36 if (!node)
37 return NULL; 37 return NULL;
38 return ((GNode *) node)->data; 38 return ((GNode *) node)->data;
39} 39}
40 40
41plist_data_t plist_new_plist_data(void) 41plist_data_t plist_new_plist_data(void)
42{ 42{
43 plist_data_t data = (plist_data_t) calloc(sizeof(struct plist_data_s), 1); 43 plist_data_t data = (plist_data_t) calloc(sizeof(struct plist_data_s), 1);
44 return data; 44 return data;
45} 45}
46 46
47static void plist_free_data(plist_data_t data) 47static void plist_free_data(plist_data_t data)
48{ 48{
49 if (data) { 49 if (data)
50 switch (data->type) { 50 {
51 case PLIST_KEY: 51 switch (data->type)
52 case PLIST_STRING: 52 {
53 free(data->strval); 53 case PLIST_KEY:
54 break; 54 case PLIST_STRING:
55 case PLIST_DATA: 55 free(data->strval);
56 free(data->buff); 56 break;
57 break; 57 case PLIST_DATA:
58 default: 58 free(data->buff);
59 break; 59 break;
60 } 60 default:
61 free(data); 61 break;
62 } 62 }
63 free(data);
64 }
63} 65}
64 66
65static void plist_free_node(GNode * node, gpointer none) 67static void plist_free_node(GNode * node, gpointer none)
66{ 68{
67 plist_data_t data = NULL; 69 plist_data_t data = NULL;
68 g_node_unlink(node); 70 g_node_unlink(node);
69 data = plist_get_data(node); 71 data = plist_get_data(node);
70 plist_free_data(data); 72 plist_free_data(data);
71 node->data = NULL; 73 node->data = NULL;
72 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_free_node, NULL); 74 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_free_node, NULL);
73} 75}
74 76
75plist_t plist_new_dict(void) 77plist_t plist_new_dict(void)
76{ 78{
77 plist_data_t data = plist_new_plist_data(); 79 plist_data_t data = plist_new_plist_data();
78 data->type = PLIST_DICT; 80 data->type = PLIST_DICT;
79 return plist_new_node(data); 81 return plist_new_node(data);
80} 82}
81 83
82plist_t plist_new_array(void) 84plist_t plist_new_array(void)
83{ 85{
84 plist_data_t data = plist_new_plist_data(); 86 plist_data_t data = plist_new_plist_data();
85 data->type = PLIST_ARRAY; 87 data->type = PLIST_ARRAY;
86 return plist_new_node(data); 88 return plist_new_node(data);
87} 89}
88 90
89//These nodes should not be handled by users 91//These nodes should not be handled by users
90static plist_t plist_new_key(const char *val) 92static plist_t plist_new_key(const char *val)
91{ 93{
92 plist_data_t data = plist_new_plist_data(); 94 plist_data_t data = plist_new_plist_data();
93 data->type = PLIST_KEY; 95 data->type = PLIST_KEY;
94 data->strval = strdup(val); 96 data->strval = strdup(val);
95 data->length = strlen(val); 97 data->length = strlen(val);
96 return plist_new_node(data); 98 return plist_new_node(data);
97} 99}
98 100
99plist_t plist_new_string(const char *val) 101plist_t plist_new_string(const char *val)
100{ 102{
101 plist_data_t data = plist_new_plist_data(); 103 plist_data_t data = plist_new_plist_data();
102 data->type = PLIST_STRING; 104 data->type = PLIST_STRING;
103 data->strval = strdup(val); 105 data->strval = strdup(val);
104 data->length = strlen(val); 106 data->length = strlen(val);
105 return plist_new_node(data); 107 return plist_new_node(data);
106} 108}
107 109
108plist_t plist_new_bool(uint8_t val) 110plist_t plist_new_bool(uint8_t val)
109{ 111{
110 plist_data_t data = plist_new_plist_data(); 112 plist_data_t data = plist_new_plist_data();
111 data->type = PLIST_BOOLEAN; 113 data->type = PLIST_BOOLEAN;
112 data->boolval = val; 114 data->boolval = val;
113 data->length = sizeof(uint8_t); 115 data->length = sizeof(uint8_t);
114 return plist_new_node(data); 116 return plist_new_node(data);
115} 117}
116 118
117plist_t plist_new_uint(uint64_t val) 119plist_t plist_new_uint(uint64_t val)
118{ 120{
119 plist_data_t data = plist_new_plist_data(); 121 plist_data_t data = plist_new_plist_data();
120 data->type = PLIST_UINT; 122 data->type = PLIST_UINT;
121 data->intval = val; 123 data->intval = val;
122 data->length = sizeof(uint64_t); 124 data->length = sizeof(uint64_t);
123 return plist_new_node(data); 125 return plist_new_node(data);
124} 126}
125 127
126plist_t plist_new_real(double val) 128plist_t plist_new_real(double val)
127{ 129{
128 plist_data_t data = plist_new_plist_data(); 130 plist_data_t data = plist_new_plist_data();
129 data->type = PLIST_REAL; 131 data->type = PLIST_REAL;
130 data->realval = val; 132 data->realval = val;
131 data->length = sizeof(double); 133 data->length = sizeof(double);
132 return plist_new_node(data); 134 return plist_new_node(data);
133} 135}
134 136
135plist_t plist_new_data(const char *val, uint64_t length) 137plist_t plist_new_data(const char *val, uint64_t length)
136{ 138{
137 plist_data_t data = plist_new_plist_data(); 139 plist_data_t data = plist_new_plist_data();
138 data->type = PLIST_DATA; 140 data->type = PLIST_DATA;
139 data->buff = (uint8_t *) malloc(length); 141 data->buff = (uint8_t *) malloc(length);
140 memcpy(data->buff, val, length); 142 memcpy(data->buff, val, length);
141 data->length = length; 143 data->length = length;
142 return plist_new_node(data); 144 return plist_new_node(data);
143} 145}
144 146
145plist_t plist_new_date(int32_t sec, int32_t usec) 147plist_t plist_new_date(int32_t sec, int32_t usec)
146{ 148{
147 plist_data_t data = plist_new_plist_data(); 149 plist_data_t data = plist_new_plist_data();
148 data->type = PLIST_DATE; 150 data->type = PLIST_DATE;
149 data->timeval.tv_sec = sec; 151 data->timeval.tv_sec = sec;
150 data->timeval.tv_usec = usec; 152 data->timeval.tv_usec = usec;
151 data->length = sizeof(GTimeVal); 153 data->length = sizeof(GTimeVal);
152 return plist_new_node(data); 154 return plist_new_node(data);
153} 155}
154 156
155void plist_free(plist_t plist) 157void plist_free(plist_t plist)
156{ 158{
157 if (plist) { 159 if (plist)
158 plist_free_node(plist, NULL); 160 {
159 g_node_destroy(plist); 161 plist_free_node(plist, NULL);
160 } 162 g_node_destroy(plist);
163 }
161} 164}
162 165
163static void plist_copy_node(GNode * node, gpointer parent_node_ptr) 166static void plist_copy_node(GNode * node, gpointer parent_node_ptr)
164{ 167{
165 plist_type node_type = PLIST_NONE; 168 plist_type node_type = PLIST_NONE;
166 plist_t newnode = NULL; 169 plist_t newnode = NULL;
167 plist_data_t data = plist_get_data(node); 170 plist_data_t data = plist_get_data(node);
168 plist_data_t newdata = plist_new_plist_data(); 171 plist_data_t newdata = plist_new_plist_data();
169 172
170 assert(data); // plist should always have data 173 assert(data); // plist should always have data
171 174
172 memcpy(newdata, data, sizeof(struct plist_data_s)); 175 memcpy(newdata, data, sizeof(struct plist_data_s));
173 176
174 node_type = plist_get_node_type(node); 177 node_type = plist_get_node_type(node);
175 if (node_type == PLIST_DATA || node_type == PLIST_STRING || node_type == PLIST_KEY) { 178 if (node_type == PLIST_DATA || node_type == PLIST_STRING || node_type == PLIST_KEY)
176 switch (node_type) { 179 {
177 case PLIST_DATA: 180 switch (node_type)
178 newdata->buff = (uint8_t *) malloc(data->length); 181 {
179 memcpy(newdata->buff, data->buff, data->length); 182 case PLIST_DATA:
180 case PLIST_KEY: 183 newdata->buff = (uint8_t *) malloc(data->length);
181 case PLIST_STRING: 184 memcpy(newdata->buff, data->buff, data->length);
182 newdata->strval = strdup((char *) data->strval); 185 case PLIST_KEY:
183 default: 186 case PLIST_STRING:
184 break; 187 newdata->strval = strdup((char *) data->strval);
185 } 188 default:
186 } 189 break;
187 newnode = plist_new_node(newdata); 190 }
188 191 }
189 if (*(plist_t*)parent_node_ptr) { 192 newnode = plist_new_node(newdata);
190 g_node_append(*(plist_t*)parent_node_ptr, newnode); 193
191 } 194 if (*(plist_t*)parent_node_ptr)
192 else { 195 {
193 *(plist_t*)parent_node_ptr = newnode; 196 g_node_append(*(plist_t*)parent_node_ptr, newnode);
194 } 197 }
195 198 else
196 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_copy_node, &newnode); 199 {
200 *(plist_t*)parent_node_ptr = newnode;
201 }
202
203 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_copy_node, &newnode);
197} 204}
198 205
199plist_t plist_copy(plist_t node) 206plist_t plist_copy(plist_t node)
200{ 207{
201 plist_t copied = NULL; 208 plist_t copied = NULL;
202 plist_copy_node(node, &copied); 209 plist_copy_node(node, &copied);
203 return copied; 210 return copied;
204} 211}
205 212
206uint32_t plist_array_get_size(plist_t node) 213uint32_t plist_array_get_size(plist_t node)
207{ 214{
208 uint32_t ret = 0; 215 uint32_t ret = 0;
209 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 216 if (node && PLIST_ARRAY == plist_get_node_type(node))
210 ret = g_node_n_children(node); 217 {
211 } 218 ret = g_node_n_children(node);
212 return ret; 219 }
220 return ret;
213} 221}
214 222
215plist_t plist_array_get_item(plist_t node, uint32_t n) 223plist_t plist_array_get_item(plist_t node, uint32_t n)
216{ 224{
217 plist_t ret = NULL; 225 plist_t ret = NULL;
218 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 226 if (node && PLIST_ARRAY == plist_get_node_type(node))
219 ret = (plist_t)g_node_nth_child(node, n); 227 {
220 } 228 ret = (plist_t)g_node_nth_child(node, n);
221 return ret; 229 }
230 return ret;
222} 231}
223 232
224uint32_t plist_array_get_item_index(plist_t node) 233uint32_t plist_array_get_item_index(plist_t node)
225{ 234{
226 plist_t father = plist_get_parent(node); 235 plist_t father = plist_get_parent(node);
227 if (PLIST_ARRAY == plist_get_node_type(father)) { 236 if (PLIST_ARRAY == plist_get_node_type(father))
228 return g_node_child_position(father, node); 237 {
229 } 238 return g_node_child_position(father, node);
239 }
230 return 0; 240 return 0;
231} 241}
232 242
233void plist_array_set_item(plist_t node, plist_t item, uint32_t n) 243void plist_array_set_item(plist_t node, plist_t item, uint32_t n)
234{ 244{
235 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 245 if (node && PLIST_ARRAY == plist_get_node_type(node))
236 plist_t old_item = plist_array_get_item(node, n); 246 {
237 if (old_item) { 247 plist_t old_item = plist_array_get_item(node, n);
238 plist_free_node(old_item, NULL); 248 if (old_item)
239 old_item = NULL; 249 {
240 plist_copy_node(item, &old_item); 250 plist_free_node(old_item, NULL);
241 } 251 old_item = NULL;
242 } 252 plist_copy_node(item, &old_item);
243 return; 253 }
254 }
255 return;
244} 256}
245 257
246void plist_array_append_item(plist_t node, plist_t item) 258void plist_array_append_item(plist_t node, plist_t item)
247{ 259{
248 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 260 if (node && PLIST_ARRAY == plist_get_node_type(node))
249 g_node_append(node, item); 261 {
250 } 262 g_node_append(node, item);
251 return; 263 }
264 return;
252} 265}
253 266
254void plist_array_insert_item(plist_t node, plist_t item, uint32_t n) 267void plist_array_insert_item(plist_t node, plist_t item, uint32_t n)
255{ 268{
256 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 269 if (node && PLIST_ARRAY == plist_get_node_type(node))
257 g_node_insert(node, n, item); 270 {
258 } 271 g_node_insert(node, n, item);
259 return; 272 }
273 return;
260} 274}
261 275
262void plist_array_remove_item(plist_t node, uint32_t n) 276void plist_array_remove_item(plist_t node, uint32_t n)
263{ 277{
264 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 278 if (node && PLIST_ARRAY == plist_get_node_type(node))
265 plist_t old_item = plist_array_get_item(node, n); 279 {
266 if (old_item) { 280 plist_t old_item = plist_array_get_item(node, n);
267 plist_free(old_item); 281 if (old_item)
268 } 282 {
269 } 283 plist_free(old_item);
270 return; 284 }
285 }
286 return;
271} 287}
272 288
273uint32_t plist_dict_get_size(plist_t node) 289uint32_t plist_dict_get_size(plist_t node)
274{ 290{
275 uint32_t ret = 0; 291 uint32_t ret = 0;
276 if (node && PLIST_DICT == plist_get_node_type(node)) { 292 if (node && PLIST_DICT == plist_get_node_type(node))
277 ret = g_node_n_children(node) / 2; 293 {
278 } 294 ret = g_node_n_children(node) / 2;
279 return ret; 295 }
296 return ret;
280} 297}
281 298
282void plist_dict_new_iter(plist_t node, plist_dict_iter *iter) 299void plist_dict_new_iter(plist_t node, plist_dict_iter *iter)
283{ 300{
284 if (iter && *iter == NULL) { 301 if (iter && *iter == NULL)
285 *iter = malloc(sizeof(uint32_t)); 302 {
286 *((uint32_t*)(*iter)) = 0; 303 *iter = malloc(sizeof(uint32_t));
287 } 304 *((uint32_t*)(*iter)) = 0;
288 return; 305 }
306 return;
289} 307}
290 308
291void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val) 309void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val)
292{ 310{
293 uint32_t* iter_int = (uint32_t*) iter; 311 uint32_t* iter_int = (uint32_t*) iter;
294 312
295 if (key) { 313 if (key)
296 *key = NULL; 314 {
297 } 315 *key = NULL;
298 if (val) { 316 }
299 *val = NULL; 317 if (val)
300 } 318 {
319 *val = NULL;
320 }
301 321
302 if (node && PLIST_DICT == plist_get_node_type(node) && *iter_int < g_node_n_children(node)) { 322 if (node && PLIST_DICT == plist_get_node_type(node) && *iter_int < g_node_n_children(node))
323 {
303 324
304 if (key) { 325 if (key)
305 plist_get_key_val((plist_t)g_node_nth_child(node, *iter_int), key); 326 {
306 } 327 plist_get_key_val((plist_t)g_node_nth_child(node, *iter_int), key);
328 }
307 329
308 if (val) { 330 if (val)
309 *val = (plist_t) g_node_nth_child(node, *iter_int + 1); 331 {
310 } 332 *val = (plist_t) g_node_nth_child(node, *iter_int + 1);
333 }
311 334
312 *iter_int += 2; 335 *iter_int += 2;
313 } 336 }
314 return; 337 return;
315} 338}
316 339
317void plist_dict_get_item_key(plist_t node, char **key) 340void plist_dict_get_item_key(plist_t node, char **key)
318{ 341{
319 plist_t father = plist_get_parent(node); 342 plist_t father = plist_get_parent(node);
320 if (PLIST_DICT == plist_get_node_type(father)) { 343 if (PLIST_DICT == plist_get_node_type(father))
321 plist_get_key_val( (plist_t) g_node_prev_sibling(node), key); 344 {
322 } 345 plist_get_key_val( (plist_t) g_node_prev_sibling(node), key);
346 }
323} 347}
324 348
325plist_t plist_dict_get_item(plist_t node, const char* key) 349plist_t plist_dict_get_item(plist_t node, const char* key)
326{ 350{
327 plist_t ret = NULL; 351 plist_t ret = NULL;
328 352
329 if (node && PLIST_DICT == plist_get_node_type(node)) { 353 if (node && PLIST_DICT == plist_get_node_type(node))
354 {
330 355
331 plist_t current = NULL; 356 plist_t current = NULL;
332 for (current = (plist_t)g_node_first_child(node); 357 for (current = (plist_t)g_node_first_child(node);
333 current; 358 current;
334 current = (plist_t)g_node_next_sibling(g_node_next_sibling(current))) { 359 current = (plist_t)g_node_next_sibling(g_node_next_sibling(current)))
360 {
335 361
336 plist_data_t data = plist_get_data(current); 362 plist_data_t data = plist_get_data(current);
337 assert( PLIST_KEY == plist_get_node_type(current) ); 363 assert( PLIST_KEY == plist_get_node_type(current) );
338 364
339 if (data && !strcmp(key, data->strval)) { 365 if (data && !strcmp(key, data->strval))
340 ret = (plist_t)g_node_next_sibling(current); 366 {
341 break; 367 ret = (plist_t)g_node_next_sibling(current);
342 } 368 break;
343 } 369 }
344 } 370 }
345 return ret; 371 }
372 return ret;
346} 373}
347 374
348void plist_dict_set_item(plist_t node, const char* key, plist_t item) 375void plist_dict_set_item(plist_t node, const char* key, plist_t item)
349{ 376{
350 if (node && PLIST_DICT == plist_get_node_type(node)) { 377 if (node && PLIST_DICT == plist_get_node_type(node))
351 plist_t old_item = plist_dict_get_item(node, key); 378 {
352 if (old_item) { 379 plist_t old_item = plist_dict_get_item(node, key);
353 plist_free_node(old_item, NULL); 380 if (old_item)
354 old_item = NULL; 381 {
355 plist_copy_node(item, &old_item); 382 plist_free_node(old_item, NULL);
356 } 383 old_item = NULL;
357 } 384 plist_copy_node(item, &old_item);
358 return; 385 }
386 }
387 return;
359} 388}
360 389
361void plist_dict_insert_item(plist_t node, const char* key, plist_t item) 390void plist_dict_insert_item(plist_t node, const char* key, plist_t item)
362{ 391{
363 if (node && PLIST_DICT == plist_get_node_type(node)) { 392 if (node && PLIST_DICT == plist_get_node_type(node))
364 g_node_append(node, plist_new_key(key)); 393 {
365 g_node_append(node, item); 394 g_node_append(node, plist_new_key(key));
366 } 395 g_node_append(node, item);
367 return; 396 }
397 return;
368} 398}
369 399
370void plist_dict_remove_item(plist_t node, const char* key) 400void plist_dict_remove_item(plist_t node, const char* key)
371{ 401{
372 if (node && PLIST_DICT == plist_get_node_type(node)) { 402 if (node && PLIST_DICT == plist_get_node_type(node))
373 plist_t old_item = plist_dict_get_item(node, key); 403 {
374 if (old_item) { 404 plist_t old_item = plist_dict_get_item(node, key);
375 plist_t key_node = g_node_prev_sibling(old_item); 405 if (old_item)
376 plist_free(key_node); 406 {
377 plist_free(old_item); 407 plist_t key_node = g_node_prev_sibling(old_item);
378 } 408 plist_free(key_node);
379 } 409 plist_free(old_item);
380 return; 410 }
411 }
412 return;
381} 413}
382 414
383static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length) 415static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length)
384{ 416{
385 char res = FALSE; 417 char res = FALSE;
386 switch (type) { 418 switch (type)
387 case PLIST_BOOLEAN: 419 {
388 res = data->boolval == *((char *) value) ? TRUE : FALSE; 420 case PLIST_BOOLEAN:
389 break; 421 res = data->boolval == *((char *) value) ? TRUE : FALSE;
390 case PLIST_UINT: 422 break;
391 res = data->intval == *((uint64_t *) value) ? TRUE : FALSE; 423 case PLIST_UINT:
392 break; 424 res = data->intval == *((uint64_t *) value) ? TRUE : FALSE;
393 case PLIST_REAL: 425 break;
394 res = data->realval == *((double *) value) ? TRUE : FALSE; 426 case PLIST_REAL:
395 break; 427 res = data->realval == *((double *) value) ? TRUE : FALSE;
396 case PLIST_KEY: 428 break;
397 case PLIST_STRING: 429 case PLIST_KEY:
398 res = !strcmp(data->strval, ((char *) value)); 430 case PLIST_STRING:
399 break; 431 res = !strcmp(data->strval, ((char *) value));
400 case PLIST_DATA: 432 break;
401 res = !memcmp(data->buff, (char *) value, length); 433 case PLIST_DATA:
402 break; 434 res = !memcmp(data->buff, (char *) value, length);
403 case PLIST_DATE: 435 break;
404 res = !memcmp(&(data->timeval), value, sizeof(GTimeVal)); 436 case PLIST_DATE:
405 break; 437 res = !memcmp(&(data->timeval), value, sizeof(GTimeVal));
406 case PLIST_ARRAY: 438 break;
407 case PLIST_DICT: 439 case PLIST_ARRAY:
408 default: 440 case PLIST_DICT:
409 break; 441 default:
410 } 442 break;
411 return res; 443 }
444 return res;
412} 445}
413 446
414plist_t plist_access_pathv(plist_t plist, uint32_t length, va_list v) 447plist_t plist_access_pathv(plist_t plist, uint32_t length, va_list v)
415{ 448{
416 plist_t current = plist; 449 plist_t current = plist;
417 plist_type type = PLIST_NONE; 450 plist_type type = PLIST_NONE;
418 uint32_t i = 0; 451 uint32_t i = 0;
419 452
420 for (i = 0; i < length && current; i++) { 453 for (i = 0; i < length && current; i++)
421 type = plist_get_node_type(current); 454 {
422 455 type = plist_get_node_type(current);
423 if (type == PLIST_ARRAY) { 456
424 uint32_t index = va_arg(v, uint32_t); 457 if (type == PLIST_ARRAY)
425 current = plist_array_get_item(current, index); 458 {
426 } 459 uint32_t index = va_arg(v, uint32_t);
427 else if (type == PLIST_DICT) { 460 current = plist_array_get_item(current, index);
428 const char* key = va_arg(v, const char*); 461 }
429 current = plist_dict_get_item(current, key); 462 else if (type == PLIST_DICT)
430 } 463 {
431 } 464 const char* key = va_arg(v, const char*);
432 return current; 465 current = plist_dict_get_item(current, key);
466 }
467 }
468 return current;
433} 469}
434 470
435plist_t plist_access_path(plist_t plist, uint32_t length, ...) 471plist_t plist_access_path(plist_t plist, uint32_t length, ...)
436{ 472{
437 plist_t ret = NULL; 473 plist_t ret = NULL;
438 va_list v; 474 va_list v;
439 475
440 va_start(v, length); 476 va_start(v, length);
441 ret = plist_access_pathv(plist, length, v); 477 ret = plist_access_pathv(plist, length, v);
442 va_end(v); 478 va_end(v);
443 return ret; 479 return ret;
444} 480}
445 481
446static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) 482static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length)
447{ 483{
448 plist_data_t data = NULL; 484 plist_data_t data = NULL;
449 485
450 if (!node) 486 if (!node)
451 return; 487 return;
452 488
453 data = plist_get_data(node); 489 data = plist_get_data(node);
454 490
455 *type = data->type; 491 *type = data->type;
456 *length = data->length; 492 *length = data->length;
457 493
458 switch (*type) { 494 switch (*type)
459 case PLIST_BOOLEAN: 495 {
460 *((char *) value) = data->boolval; 496 case PLIST_BOOLEAN:
461 break; 497 *((char *) value) = data->boolval;
462 case PLIST_UINT: 498 break;
463 *((uint64_t *) value) = data->intval; 499 case PLIST_UINT:
464 break; 500 *((uint64_t *) value) = data->intval;
465 case PLIST_REAL: 501 break;
466 *((double *) value) = data->realval; 502 case PLIST_REAL:
467 break; 503 *((double *) value) = data->realval;
468 case PLIST_KEY: 504 break;
469 case PLIST_STRING: 505 case PLIST_KEY:
470 *((char **) value) = strdup(data->strval); 506 case PLIST_STRING:
471 break; 507 *((char **) value) = strdup(data->strval);
472 case PLIST_DATA: 508 break;
473 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); 509 case PLIST_DATA:
474 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t)); 510 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t));
475 break; 511 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t));
476 case PLIST_DATE: 512 break;
477 //exception : here we use memory on the stack since it is just a temporary buffer 513 case PLIST_DATE:
478 ((GTimeVal *) value)->tv_sec = data->timeval.tv_sec; 514 //exception : here we use memory on the stack since it is just a temporary buffer
479 ((GTimeVal *) value)->tv_usec = data->timeval.tv_usec; 515 ((GTimeVal *) value)->tv_sec = data->timeval.tv_sec;
480 break; 516 ((GTimeVal *) value)->tv_usec = data->timeval.tv_usec;
481 case PLIST_ARRAY: 517 break;
482 case PLIST_DICT: 518 case PLIST_ARRAY:
483 default: 519 case PLIST_DICT:
484 break; 520 default:
485 } 521 break;
522 }
486} 523}
487 524
488plist_t plist_get_parent(plist_t node) 525plist_t plist_get_parent(plist_t node)
489{ 526{
490 return node ? (plist_t) ((GNode *) node)->parent : NULL; 527 return node ? (plist_t) ((GNode *) node)->parent : NULL;
491} 528}
492 529
493plist_type plist_get_node_type(plist_t node) 530plist_type plist_get_node_type(plist_t node)
494{ 531{
495 if (node) { 532 if (node)
496 plist_data_t data = plist_get_data(node); 533 {
497 if (data) 534 plist_data_t data = plist_get_data(node);
498 return data->type; 535 if (data)
499 } 536 return data->type;
500 return PLIST_NONE; 537 }
538 return PLIST_NONE;
501} 539}
502 540
503void plist_get_key_val(plist_t node, char **val) 541void plist_get_key_val(plist_t node, char **val)
504{ 542{
505 plist_type type = plist_get_node_type(node); 543 plist_type type = plist_get_node_type(node);
506 uint64_t length = 0; 544 uint64_t length = 0;
507 if (PLIST_KEY == type) 545 if (PLIST_KEY == type)
508 plist_get_type_and_value(node, &type, (void *) val, &length); 546 plist_get_type_and_value(node, &type, (void *) val, &length);
509 assert(length == strlen(*val)); 547 assert(length == strlen(*val));
510} 548}
511 549
512void plist_get_string_val(plist_t node, char **val) 550void plist_get_string_val(plist_t node, char **val)
513{ 551{
514 plist_type type = plist_get_node_type(node); 552 plist_type type = plist_get_node_type(node);
515 uint64_t length = 0; 553 uint64_t length = 0;
516 if (PLIST_STRING == type) 554 if (PLIST_STRING == type)
517 plist_get_type_and_value(node, &type, (void *) val, &length); 555 plist_get_type_and_value(node, &type, (void *) val, &length);
518 assert(length == strlen(*val)); 556 assert(length == strlen(*val));
519} 557}
520 558
521void plist_get_bool_val(plist_t node, uint8_t * val) 559void plist_get_bool_val(plist_t node, uint8_t * val)
522{ 560{
523 plist_type type = plist_get_node_type(node); 561 plist_type type = plist_get_node_type(node);
524 uint64_t length = 0; 562 uint64_t length = 0;
525 if (PLIST_BOOLEAN == type) 563 if (PLIST_BOOLEAN == type)
526 plist_get_type_and_value(node, &type, (void *) val, &length); 564 plist_get_type_and_value(node, &type, (void *) val, &length);
527 assert(length == sizeof(uint8_t)); 565 assert(length == sizeof(uint8_t));
528} 566}
529 567
530void plist_get_uint_val(plist_t node, uint64_t * val) 568void plist_get_uint_val(plist_t node, uint64_t * val)
531{ 569{
532 plist_type type = plist_get_node_type(node); 570 plist_type type = plist_get_node_type(node);
533 uint64_t length = 0; 571 uint64_t length = 0;
534 if (PLIST_UINT == type) 572 if (PLIST_UINT == type)
535 plist_get_type_and_value(node, &type, (void *) val, &length); 573 plist_get_type_and_value(node, &type, (void *) val, &length);
536 assert(length == sizeof(uint64_t)); 574 assert(length == sizeof(uint64_t));
537} 575}
538 576
539void plist_get_real_val(plist_t node, double *val) 577void plist_get_real_val(plist_t node, double *val)
540{ 578{
541 plist_type type = plist_get_node_type(node); 579 plist_type type = plist_get_node_type(node);
542 uint64_t length = 0; 580 uint64_t length = 0;
543 if (PLIST_REAL == type) 581 if (PLIST_REAL == type)
544 plist_get_type_and_value(node, &type, (void *) val, &length); 582 plist_get_type_and_value(node, &type, (void *) val, &length);
545 assert(length == sizeof(double)); 583 assert(length == sizeof(double));
546} 584}
547 585
548void plist_get_data_val(plist_t node, char **val, uint64_t * length) 586void plist_get_data_val(plist_t node, char **val, uint64_t * length)
549{ 587{
550 plist_type type = plist_get_node_type(node); 588 plist_type type = plist_get_node_type(node);
551 if (PLIST_DATA == type) 589 if (PLIST_DATA == type)
552 plist_get_type_and_value(node, &type, (void *) val, length); 590 plist_get_type_and_value(node, &type, (void *) val, length);
553} 591}
554 592
555void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) 593void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
556{ 594{
557 plist_type type = plist_get_node_type(node); 595 plist_type type = plist_get_node_type(node);
558 uint64_t length = 0; 596 uint64_t length = 0;
559 GTimeVal val = { 0, 0 }; 597 GTimeVal val = { 0, 0 };
560 if (PLIST_DATE == type) 598 if (PLIST_DATE == type)
561 plist_get_type_and_value(node, &type, (void *) &val, &length); 599 plist_get_type_and_value(node, &type, (void *) &val, &length);
562 assert(length == sizeof(GTimeVal)); 600 assert(length == sizeof(GTimeVal));
563 *sec = val.tv_sec; 601 *sec = val.tv_sec;
564 *usec = val.tv_usec; 602 *usec = val.tv_usec;
565} 603}
566 604
567gboolean plist_data_compare(gconstpointer a, gconstpointer b) 605gboolean plist_data_compare(gconstpointer a, gconstpointer b)
568{ 606{
569 plist_data_t val_a = NULL; 607 plist_data_t val_a = NULL;
570 plist_data_t val_b = NULL; 608 plist_data_t val_b = NULL;
571 609
572 if (!a || !b) 610 if (!a || !b)
573 return FALSE; 611 return FALSE;
574 612
575 if (!((GNode *) a)->data || !((GNode *) b)->data) 613 if (!((GNode *) a)->data || !((GNode *) b)->data)
576 return FALSE; 614 return FALSE;
577 615
578 val_a = plist_get_data((plist_t) a); 616 val_a = plist_get_data((plist_t) a);
579 val_b = plist_get_data((plist_t) b); 617 val_b = plist_get_data((plist_t) b);
580 618
581 if (val_a->type != val_b->type) 619 if (val_a->type != val_b->type)
582 return FALSE; 620 return FALSE;
583 621
584 switch (val_a->type) { 622 switch (val_a->type)
585 case PLIST_BOOLEAN: 623 {
586 case PLIST_UINT: 624 case PLIST_BOOLEAN:
587 case PLIST_REAL: 625 case PLIST_UINT:
588 if (val_a->intval == val_b->intval) //it is an union so this is sufficient 626 case PLIST_REAL:
589 return TRUE; 627 if (val_a->intval == val_b->intval) //it is an union so this is sufficient
590 else 628 return TRUE;
591 return FALSE; 629 else
592 630 return FALSE;
593 case PLIST_KEY: 631
594 case PLIST_STRING: 632 case PLIST_KEY:
595 if (!strcmp(val_a->strval, val_b->strval)) 633 case PLIST_STRING:
596 return TRUE; 634 if (!strcmp(val_a->strval, val_b->strval))
597 else 635 return TRUE;
598 return FALSE; 636 else
599 637 return FALSE;
600 case PLIST_DATA: 638
601 if (!memcmp(val_a->buff, val_b->buff, val_a->length)) 639 case PLIST_DATA:
602 return TRUE; 640 if (!memcmp(val_a->buff, val_b->buff, val_a->length))
603 else 641 return TRUE;
604 return FALSE; 642 else
605 case PLIST_ARRAY: 643 return FALSE;
606 case PLIST_DICT: 644 case PLIST_ARRAY:
607 //compare pointer 645 case PLIST_DICT:
608 if (a == b) 646 //compare pointer
609 return TRUE; 647 if (a == b)
610 else 648 return TRUE;
611 return FALSE; 649 else
612 break; 650 return FALSE;
613 case PLIST_DATE: 651 break;
614 if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal))) 652 case PLIST_DATE:
615 return TRUE; 653 if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal)))
616 else 654 return TRUE;
617 return FALSE; 655 else
618 default: 656 return FALSE;
619 break; 657 default:
620 } 658 break;
621 return FALSE; 659 }
660 return FALSE;
622} 661}
623 662
624char plist_compare_node_value(plist_t node_l, plist_t node_r) 663char plist_compare_node_value(plist_t node_l, plist_t node_r)
625{ 664{
626 return plist_data_compare(node_l, node_r); 665 return plist_data_compare(node_l, node_r);
627} 666}
628 667
629static void plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length) 668static void plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length)
630{ 669{
631 //free previous allocated buffer 670 //free previous allocated buffer
632 plist_data_t data = plist_get_data(node); 671 plist_data_t data = plist_get_data(node);
633 assert(data); // a node should always have data attached 672 assert(data); // a node should always have data attached
634 673
635 switch (data->type) { 674 switch (data->type)
636 case PLIST_KEY: 675 {
637 case PLIST_STRING: 676 case PLIST_KEY:
638 free(data->strval); 677 case PLIST_STRING:
639 data->strval = NULL; 678 free(data->strval);
640 break; 679 data->strval = NULL;
641 case PLIST_DATA: 680 break;
642 free(data->buff); 681 case PLIST_DATA:
643 data->buff = NULL; 682 free(data->buff);
644 break; 683 data->buff = NULL;
645 default: 684 break;
646 break; 685 default:
647 } 686 break;
648 687 }
649 //now handle value 688
650 689 //now handle value
651 data->type = type; 690
652 data->length = length; 691 data->type = type;
653 692 data->length = length;
654 switch (type) { 693
655 case PLIST_BOOLEAN: 694 switch (type)
656 data->boolval = *((char *) value); 695 {
657 break; 696 case PLIST_BOOLEAN:
658 case PLIST_UINT: 697 data->boolval = *((char *) value);
659 data->intval = *((uint64_t *) value); 698 break;
660 break; 699 case PLIST_UINT:
661 case PLIST_REAL: 700 data->intval = *((uint64_t *) value);
662 data->realval = *((double *) value); 701 break;
663 break; 702 case PLIST_REAL:
664 case PLIST_KEY: 703 data->realval = *((double *) value);
665 case PLIST_STRING: 704 break;
666 data->strval = strdup((char *) value); 705 case PLIST_KEY:
667 break; 706 case PLIST_STRING:
668 case PLIST_DATA: 707 data->strval = strdup((char *) value);
669 data->buff = (uint8_t *) malloc(length); 708 break;
670 memcpy(data->buff, value, length); 709 case PLIST_DATA:
671 break; 710 data->buff = (uint8_t *) malloc(length);
672 case PLIST_DATE: 711 memcpy(data->buff, value, length);
673 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec; 712 break;
674 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec; 713 case PLIST_DATE:
675 break; 714 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
676 case PLIST_ARRAY: 715 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
677 case PLIST_DICT: 716 break;
678 default: 717 case PLIST_ARRAY:
679 break; 718 case PLIST_DICT:
680 } 719 default:
720 break;
721 }
681} 722}
682 723
683void plist_set_type(plist_t node, plist_type type) 724void plist_set_type(plist_t node, plist_type type)
684{ 725{
685 if ( g_node_n_children(node) == 0 ) { 726 if ( g_node_n_children(node) == 0 )
686 plist_data_t data = plist_get_data(node); 727 {
687 plist_free_data( data ); 728 plist_data_t data = plist_get_data(node);
688 data = plist_new_plist_data(); 729 plist_free_data( data );
689 data->type = type; 730 data = plist_new_plist_data();
690 switch(type){ 731 data->type = type;
691 case PLIST_BOOLEAN: 732 switch (type)
692 data->length = sizeof(uint8_t); 733 {
693 break; 734 case PLIST_BOOLEAN:
694 case PLIST_UINT: 735 data->length = sizeof(uint8_t);
695 data->length = sizeof(uint64_t); 736 break;
696 break; 737 case PLIST_UINT:
697 case PLIST_REAL: 738 data->length = sizeof(uint64_t);
698 data->length = sizeof(double); 739 break;
699 break; 740 case PLIST_REAL:
700 case PLIST_DATE: 741 data->length = sizeof(double);
701 data->length = sizeof(GTimeVal); 742 break;
702 break; 743 case PLIST_DATE:
703 default: 744 data->length = sizeof(GTimeVal);
704 data->length = 0; 745 break;
705 break; 746 default:
706 } 747 data->length = 0;
707 } 748 break;
749 }
750 }
708} 751}
709 752
710void plist_set_key_val(plist_t node, const char *val) 753void plist_set_key_val(plist_t node, const char *val)
711{ 754{
712 plist_set_element_val(node, PLIST_KEY, val, strlen(val)); 755 plist_set_element_val(node, PLIST_KEY, val, strlen(val));
713} 756}
714 757
715void plist_set_string_val(plist_t node, const char *val) 758void plist_set_string_val(plist_t node, const char *val)
716{ 759{
717 plist_set_element_val(node, PLIST_STRING, val, strlen(val)); 760 plist_set_element_val(node, PLIST_STRING, val, strlen(val));
718} 761}
719 762
720void plist_set_bool_val(plist_t node, uint8_t val) 763void plist_set_bool_val(plist_t node, uint8_t val)
721{ 764{
722 plist_set_element_val(node, PLIST_BOOLEAN, &val, sizeof(uint8_t)); 765 plist_set_element_val(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
723} 766}
724 767
725void plist_set_uint_val(plist_t node, uint64_t val) 768void plist_set_uint_val(plist_t node, uint64_t val)
726{ 769{
727 plist_set_element_val(node, PLIST_UINT, &val, sizeof(uint64_t)); 770 plist_set_element_val(node, PLIST_UINT, &val, sizeof(uint64_t));
728} 771}
729 772
730void plist_set_real_val(plist_t node, double val) 773void plist_set_real_val(plist_t node, double val)
731{ 774{
732 plist_set_element_val(node, PLIST_REAL, &val, sizeof(double)); 775 plist_set_element_val(node, PLIST_REAL, &val, sizeof(double));
733} 776}
734 777
735void plist_set_data_val(plist_t node, const char *val, uint64_t length) 778void plist_set_data_val(plist_t node, const char *val, uint64_t length)
736{ 779{
737 plist_set_element_val(node, PLIST_DATA, val, length); 780 plist_set_element_val(node, PLIST_DATA, val, length);
738} 781}
739 782
740void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) 783void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
741{ 784{
742 GTimeVal val = { sec, usec }; 785 GTimeVal val = { sec, usec };
743 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal)); 786 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal));
744} 787}
745 788
746//DEPRECATED API BELOW 789//DEPRECATED API BELOW
@@ -748,176 +791,187 @@ void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
748 791
749static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length) 792static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length)
750{ 793{
751 //only structured types can have children 794 //only structured types can have children
752 plist_type node_type = plist_get_node_type(node); 795 plist_type node_type = plist_get_node_type(node);
753 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) { 796 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY)
754 //only structured types are allowed to have nulll value 797 {
755 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 798 //only structured types are allowed to have nulll value
756 799 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY)))
757 plist_t subnode = NULL; 800 {
758 801
759 //now handle value 802 plist_t subnode = NULL;
760 plist_data_t data = plist_new_plist_data(); 803
761 data->type = type; 804 //now handle value
762 data->length = length; 805 plist_data_t data = plist_new_plist_data();
763 806 data->type = type;
764 switch (type) { 807 data->length = length;
765 case PLIST_BOOLEAN: 808
766 data->boolval = *((char *) value); 809 switch (type)
767 break; 810 {
768 case PLIST_UINT: 811 case PLIST_BOOLEAN:
769 data->intval = *((uint64_t *) value); 812 data->boolval = *((char *) value);
770 break; 813 break;
771 case PLIST_REAL: 814 case PLIST_UINT:
772 data->realval = *((double *) value); 815 data->intval = *((uint64_t *) value);
773 break; 816 break;
774 case PLIST_KEY: 817 case PLIST_REAL:
775 case PLIST_STRING: 818 data->realval = *((double *) value);
776 data->strval = strdup((char *) value); 819 break;
777 break; 820 case PLIST_KEY:
778 case PLIST_DATA: 821 case PLIST_STRING:
779 data->buff = (uint8_t *) malloc(length); 822 data->strval = strdup((char *) value);
780 memcpy(data->buff, value, length); 823 break;
781 break; 824 case PLIST_DATA:
782 case PLIST_DATE: 825 data->buff = (uint8_t *) malloc(length);
783 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec; 826 memcpy(data->buff, value, length);
784 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec; 827 break;
785 break; 828 case PLIST_DATE:
786 case PLIST_ARRAY: 829 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
787 case PLIST_DICT: 830 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
788 default: 831 break;
789 break; 832 case PLIST_ARRAY:
790 } 833 case PLIST_DICT:
791 834 default:
792 subnode = plist_new_node(data); 835 break;
793 if (node) 836 }
794 g_node_append(node, subnode); 837
795 return subnode; 838 subnode = plist_new_node(data);
796 } else 839 if (node)
797 return NULL; 840 g_node_append(node, subnode);
798 } 841 return subnode;
799 return NULL; 842 }
843 else
844 return NULL;
845 }
846 return NULL;
800} 847}
801 848
802 849
803plist_t plist_get_first_child(plist_t node) 850plist_t plist_get_first_child(plist_t node)
804{ 851{
805 return (plist_t) g_node_first_child((GNode *) node); 852 return (plist_t) g_node_first_child((GNode *) node);
806} 853}
807 854
808plist_t plist_get_next_sibling(plist_t node) 855plist_t plist_get_next_sibling(plist_t node)
809{ 856{
810 return (plist_t) g_node_next_sibling((GNode *) node); 857 return (plist_t) g_node_next_sibling((GNode *) node);
811} 858}
812 859
813plist_t plist_get_prev_sibling(plist_t node) 860plist_t plist_get_prev_sibling(plist_t node)
814{ 861{
815 return (plist_t) g_node_prev_sibling((GNode *) node); 862 return (plist_t) g_node_prev_sibling((GNode *) node);
816} 863}
817 864
818plist_t plist_get_array_nth_el(plist_t node, uint32_t n) 865plist_t plist_get_array_nth_el(plist_t node, uint32_t n)
819{ 866{
820 plist_t ret = NULL; 867 plist_t ret = NULL;
821 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 868 if (node && PLIST_ARRAY == plist_get_node_type(node))
822 uint32_t i = 0; 869 {
823 plist_t temp = plist_get_first_child(node); 870 uint32_t i = 0;
871 plist_t temp = plist_get_first_child(node);
824 872
825 while (i <= n && temp) { 873 while (i <= n && temp)
826 if (i == n) 874 {
827 ret = temp; 875 if (i == n)
828 temp = plist_get_next_sibling(temp); 876 ret = temp;
829 i++; 877 temp = plist_get_next_sibling(temp);
830 } 878 i++;
831 } 879 }
832 return ret; 880 }
881 return ret;
833} 882}
834 883
835plist_t plist_get_dict_el_from_key(plist_t node, const char *key) 884plist_t plist_get_dict_el_from_key(plist_t node, const char *key)
836{ 885{
837 plist_t ret = NULL; 886 plist_t ret = NULL;
838 if (node && PLIST_DICT == plist_get_node_type(node)) { 887 if (node && PLIST_DICT == plist_get_node_type(node))
888 {
839 889
840 plist_t key_node = plist_find_node_by_key(node, key); 890 plist_t key_node = plist_find_node_by_key(node, key);
841 ret = plist_get_next_sibling(key_node); 891 ret = plist_get_next_sibling(key_node);
842 } 892 }
843 return ret; 893 return ret;
844} 894}
845 895
846void plist_add_sub_node(plist_t node, plist_t subnode) 896void plist_add_sub_node(plist_t node, plist_t subnode)
847{ 897{
848 if (node && subnode) { 898 if (node && subnode)
849 plist_type type = plist_get_node_type(node); 899 {
850 if (type == PLIST_DICT || type == PLIST_ARRAY) 900 plist_type type = plist_get_node_type(node);
851 g_node_append(node, subnode); 901 if (type == PLIST_DICT || type == PLIST_ARRAY)
852 } 902 g_node_append(node, subnode);
903 }
853} 904}
854 905
855void plist_add_sub_key_el(plist_t node, const char *val) 906void plist_add_sub_key_el(plist_t node, const char *val)
856{ 907{
857 plist_add_sub_element(node, PLIST_KEY, val, strlen(val)); 908 plist_add_sub_element(node, PLIST_KEY, val, strlen(val));
858} 909}
859 910
860void plist_add_sub_string_el(plist_t node, const char *val) 911void plist_add_sub_string_el(plist_t node, const char *val)
861{ 912{
862 plist_add_sub_element(node, PLIST_STRING, val, strlen(val)); 913 plist_add_sub_element(node, PLIST_STRING, val, strlen(val));
863} 914}
864 915
865void plist_add_sub_bool_el(plist_t node, uint8_t val) 916void plist_add_sub_bool_el(plist_t node, uint8_t val)
866{ 917{
867 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t)); 918 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
868} 919}
869 920
870void plist_add_sub_uint_el(plist_t node, uint64_t val) 921void plist_add_sub_uint_el(plist_t node, uint64_t val)
871{ 922{
872 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t)); 923 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t));
873} 924}
874 925
875void plist_add_sub_real_el(plist_t node, double val) 926void plist_add_sub_real_el(plist_t node, double val)
876{ 927{
877 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double)); 928 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double));
878} 929}
879 930
880void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length) 931void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length)
881{ 932{
882 plist_add_sub_element(node, PLIST_DATA, val, length); 933 plist_add_sub_element(node, PLIST_DATA, val, length);
883} 934}
884 935
885void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) 936void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec)
886{ 937{
887 GTimeVal val = { sec, usec }; 938 GTimeVal val = { sec, usec };
888 plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal)); 939 plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal));
889} 940}
890 941
891static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length) 942static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length)
892{ 943{
893 plist_t current = NULL; 944 plist_t current = NULL;
894 945
895 if (!plist) 946 if (!plist)
896 return NULL; 947 return NULL;
897 948
898 for (current = (plist_t)g_node_first_child(plist); current; current = (plist_t)g_node_next_sibling(current)) { 949 for (current = (plist_t)g_node_first_child(plist); current; current = (plist_t)g_node_next_sibling(current))
950 {
899 951
900 plist_data_t data = plist_get_data(current); 952 plist_data_t data = plist_get_data(current);
901 953
902 if (data->type == type && data->length == length && compare_node_value(type, data, value, length)) { 954 if (data->type == type && data->length == length && compare_node_value(type, data, value, length))
903 return current; 955 {
904 } 956 return current;
905 if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { 957 }
906 plist_t sub = plist_find_node(current, type, value, length); 958 if (data->type == PLIST_DICT || data->type == PLIST_ARRAY)
907 if (sub) 959 {
908 return sub; 960 plist_t sub = plist_find_node(current, type, value, length);
909 } 961 if (sub)
910 } 962 return sub;
911 return NULL; 963 }
964 }
965 return NULL;
912} 966}
913 967
914plist_t plist_find_node_by_key(plist_t plist, const char *value) 968plist_t plist_find_node_by_key(plist_t plist, const char *value)
915{ 969{
916 return plist_find_node(plist, PLIST_KEY, value, strlen(value)); 970 return plist_find_node(plist, PLIST_KEY, value, strlen(value));
917} 971}
918 972
919plist_t plist_find_node_by_string(plist_t plist, const char *value) 973plist_t plist_find_node_by_string(plist_t plist, const char *value)
920{ 974{
921 return plist_find_node(plist, PLIST_STRING, value, strlen(value)); 975 return plist_find_node(plist, PLIST_STRING, value, strlen(value));
922} 976}
923 977