diff options
| author | 2011-05-27 14:55:31 +0200 | |
|---|---|---|
| committer | 2011-05-27 14:55:31 +0200 | |
| commit | 024e755d9f3c33e742ce158542b1ded057a88f4f (patch) | |
| tree | 7f80705e0c3dd35fd86fcd943dbf0d0c6b9b78ab /src/plist.c | |
| parent | 94cb55d34dd9cb9bda539999dc017af76ec64a4f (diff) | |
| download | libplist-024e755d9f3c33e742ce158542b1ded057a88f4f.tar.gz libplist-024e755d9f3c33e742ce158542b1ded057a88f4f.tar.bz2 | |
Make libplist glib free
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 101 |
1 files changed, 58 insertions, 43 deletions
diff --git a/src/plist.c b/src/plist.c index 7028d81..6ee54cd 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -26,16 +26,19 @@ | |||
| 26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> | 27 | #include <stdio.h> |
| 28 | 28 | ||
| 29 | #include <node.h> | ||
| 30 | #include <node_iterator.h> | ||
| 31 | |||
| 29 | plist_t plist_new_node(plist_data_t data) | 32 | plist_t plist_new_node(plist_data_t data) |
| 30 | { | 33 | { |
| 31 | return (plist_t) g_node_new(data); | 34 | return (plist_t) node_create(NULL, data); |
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | plist_data_t plist_get_data(const plist_t node) | 37 | plist_data_t plist_get_data(const plist_t node) |
| 35 | { | 38 | { |
| 36 | if (!node) | 39 | if (!node) |
| 37 | return NULL; | 40 | return NULL; |
| 38 | return ((GNode *) node)->data; | 41 | return ((node_t*)node)->data; |
| 39 | } | 42 | } |
| 40 | 43 | ||
| 41 | plist_data_t plist_new_plist_data(void) | 44 | plist_data_t plist_new_plist_data(void) |
| @@ -64,15 +67,22 @@ static void plist_free_data(plist_data_t data) | |||
| 64 | } | 67 | } |
| 65 | } | 68 | } |
| 66 | 69 | ||
| 67 | static void plist_free_node(GNode * node, gpointer none) | 70 | static void plist_free_node(node_t* node) |
| 68 | { | 71 | { |
| 69 | plist_data_t data = NULL; | 72 | plist_data_t data = NULL; |
| 70 | g_node_unlink(node); | 73 | node_detach(node->parent, node); |
| 71 | data = plist_get_data(node); | 74 | data = plist_get_data(node); |
| 72 | plist_free_data(data); | 75 | plist_free_data(data); |
| 73 | node->data = NULL; | 76 | node->data = NULL; |
| 74 | g_node_children_foreach(node, G_TRAVERSE_ALL, plist_free_node, NULL); | 77 | |
| 75 | g_node_destroy(node); | 78 | node_iterator_t *ni = node_iterator_create(node->children); |
| 79 | node_t *ch; | ||
| 80 | while ((ch = node_iterator_next(ni))) { | ||
| 81 | plist_free_node(ch); | ||
| 82 | } | ||
| 83 | node_iterator_destroy(ni); | ||
| 84 | |||
| 85 | node_destroy(node); | ||
| 76 | } | 86 | } |
| 77 | 87 | ||
| 78 | plist_t plist_new_dict(void) | 88 | plist_t plist_new_dict(void) |
| @@ -151,7 +161,7 @@ plist_t plist_new_date(int32_t sec, int32_t usec) | |||
| 151 | data->type = PLIST_DATE; | 161 | data->type = PLIST_DATE; |
| 152 | data->timeval.tv_sec = sec; | 162 | data->timeval.tv_sec = sec; |
| 153 | data->timeval.tv_usec = usec; | 163 | data->timeval.tv_usec = usec; |
| 154 | data->length = sizeof(GTimeVal); | 164 | data->length = sizeof(struct timeval); |
| 155 | return plist_new_node(data); | 165 | return plist_new_node(data); |
| 156 | } | 166 | } |
| 157 | 167 | ||
| @@ -159,11 +169,11 @@ void plist_free(plist_t plist) | |||
| 159 | { | 169 | { |
| 160 | if (plist) | 170 | if (plist) |
| 161 | { | 171 | { |
| 162 | plist_free_node(plist, NULL); | 172 | plist_free_node(plist); |
| 163 | } | 173 | } |
| 164 | } | 174 | } |
| 165 | 175 | ||
| 166 | static void plist_copy_node(GNode * node, gpointer parent_node_ptr) | 176 | static void plist_copy_node(node_t *node, void *parent_node_ptr) |
| 167 | { | 177 | { |
| 168 | plist_type node_type = PLIST_NONE; | 178 | plist_type node_type = PLIST_NONE; |
| 169 | plist_t newnode = NULL; | 179 | plist_t newnode = NULL; |
| @@ -195,14 +205,19 @@ static void plist_copy_node(GNode * node, gpointer parent_node_ptr) | |||
| 195 | 205 | ||
| 196 | if (*(plist_t*)parent_node_ptr) | 206 | if (*(plist_t*)parent_node_ptr) |
| 197 | { | 207 | { |
| 198 | g_node_append(*(plist_t*)parent_node_ptr, newnode); | 208 | node_attach(*(plist_t*)parent_node_ptr, newnode); |
| 199 | } | 209 | } |
| 200 | else | 210 | else |
| 201 | { | 211 | { |
| 202 | *(plist_t*)parent_node_ptr = newnode; | 212 | *(plist_t*)parent_node_ptr = newnode; |
| 203 | } | 213 | } |
| 204 | 214 | ||
| 205 | g_node_children_foreach(node, G_TRAVERSE_ALL, plist_copy_node, &newnode); | 215 | node_iterator_t *ni = node_iterator_create(node->children); |
| 216 | node_t *ch; | ||
| 217 | while ((ch = node_iterator_next(ni))) { | ||
| 218 | plist_copy_node(ch, &newnode); | ||
| 219 | } | ||
| 220 | node_iterator_destroy(ni); | ||
| 206 | } | 221 | } |
| 207 | 222 | ||
| 208 | plist_t plist_copy(plist_t node) | 223 | plist_t plist_copy(plist_t node) |
| @@ -217,7 +232,7 @@ uint32_t plist_array_get_size(plist_t node) | |||
| 217 | uint32_t ret = 0; | 232 | uint32_t ret = 0; |
| 218 | if (node && PLIST_ARRAY == plist_get_node_type(node)) | 233 | if (node && PLIST_ARRAY == plist_get_node_type(node)) |
| 219 | { | 234 | { |
| 220 | ret = g_node_n_children(node); | 235 | ret = node_n_children(node); |
| 221 | } | 236 | } |
| 222 | return ret; | 237 | return ret; |
| 223 | } | 238 | } |
| @@ -227,7 +242,7 @@ plist_t plist_array_get_item(plist_t node, uint32_t n) | |||
| 227 | plist_t ret = NULL; | 242 | plist_t ret = NULL; |
| 228 | if (node && PLIST_ARRAY == plist_get_node_type(node)) | 243 | if (node && PLIST_ARRAY == plist_get_node_type(node)) |
| 229 | { | 244 | { |
| 230 | ret = (plist_t)g_node_nth_child(node, n); | 245 | ret = (plist_t)node_nth_child(node, n); |
| 231 | } | 246 | } |
| 232 | return ret; | 247 | return ret; |
| 233 | } | 248 | } |
| @@ -237,7 +252,7 @@ uint32_t plist_array_get_item_index(plist_t node) | |||
| 237 | plist_t father = plist_get_parent(node); | 252 | plist_t father = plist_get_parent(node); |
| 238 | if (PLIST_ARRAY == plist_get_node_type(father)) | 253 | if (PLIST_ARRAY == plist_get_node_type(father)) |
| 239 | { | 254 | { |
| 240 | return g_node_child_position(father, node); | 255 | return node_child_position(father, node); |
| 241 | } | 256 | } |
| 242 | return 0; | 257 | return 0; |
| 243 | } | 258 | } |
| @@ -249,7 +264,7 @@ void plist_array_set_item(plist_t node, plist_t item, uint32_t n) | |||
| 249 | plist_t old_item = plist_array_get_item(node, n); | 264 | plist_t old_item = plist_array_get_item(node, n); |
| 250 | if (old_item) | 265 | if (old_item) |
| 251 | { | 266 | { |
| 252 | plist_free_node(old_item, NULL); | 267 | plist_free_node(old_item); |
| 253 | old_item = NULL; | 268 | old_item = NULL; |
| 254 | plist_copy_node(item, &old_item); | 269 | plist_copy_node(item, &old_item); |
| 255 | } | 270 | } |
| @@ -261,7 +276,7 @@ void plist_array_append_item(plist_t node, plist_t item) | |||
| 261 | { | 276 | { |
| 262 | if (node && PLIST_ARRAY == plist_get_node_type(node)) | 277 | if (node && PLIST_ARRAY == plist_get_node_type(node)) |
| 263 | { | 278 | { |
| 264 | g_node_append(node, item); | 279 | node_attach(node, item); |
| 265 | } | 280 | } |
| 266 | return; | 281 | return; |
| 267 | } | 282 | } |
| @@ -270,7 +285,7 @@ void plist_array_insert_item(plist_t node, plist_t item, uint32_t n) | |||
| 270 | { | 285 | { |
| 271 | if (node && PLIST_ARRAY == plist_get_node_type(node)) | 286 | if (node && PLIST_ARRAY == plist_get_node_type(node)) |
| 272 | { | 287 | { |
| 273 | g_node_insert(node, n, item); | 288 | node_insert(node, n, item); |
| 274 | } | 289 | } |
| 275 | return; | 290 | return; |
| 276 | } | 291 | } |
| @@ -293,7 +308,7 @@ uint32_t plist_dict_get_size(plist_t node) | |||
| 293 | uint32_t ret = 0; | 308 | uint32_t ret = 0; |
| 294 | if (node && PLIST_DICT == plist_get_node_type(node)) | 309 | if (node && PLIST_DICT == plist_get_node_type(node)) |
| 295 | { | 310 | { |
| 296 | ret = g_node_n_children(node) / 2; | 311 | ret = node_n_children(node) / 2; |
| 297 | } | 312 | } |
| 298 | return ret; | 313 | return ret; |
| 299 | } | 314 | } |
| @@ -321,17 +336,17 @@ void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_ | |||
| 321 | *val = NULL; | 336 | *val = NULL; |
| 322 | } | 337 | } |
| 323 | 338 | ||
| 324 | if (node && PLIST_DICT == plist_get_node_type(node) && *iter_int < g_node_n_children(node)) | 339 | if (node && PLIST_DICT == plist_get_node_type(node) && *iter_int < node_n_children(node)) |
| 325 | { | 340 | { |
| 326 | 341 | ||
| 327 | if (key) | 342 | if (key) |
| 328 | { | 343 | { |
| 329 | plist_get_key_val((plist_t)g_node_nth_child(node, *iter_int), key); | 344 | plist_get_key_val((plist_t)node_nth_child(node, *iter_int), key); |
| 330 | } | 345 | } |
| 331 | 346 | ||
| 332 | if (val) | 347 | if (val) |
| 333 | { | 348 | { |
| 334 | *val = (plist_t) g_node_nth_child(node, *iter_int + 1); | 349 | *val = (plist_t) node_nth_child(node, *iter_int + 1); |
| 335 | } | 350 | } |
| 336 | 351 | ||
| 337 | *iter_int += 2; | 352 | *iter_int += 2; |
| @@ -344,7 +359,7 @@ void plist_dict_get_item_key(plist_t node, char **key) | |||
| 344 | plist_t father = plist_get_parent(node); | 359 | plist_t father = plist_get_parent(node); |
| 345 | if (PLIST_DICT == plist_get_node_type(father)) | 360 | if (PLIST_DICT == plist_get_node_type(father)) |
| 346 | { | 361 | { |
| 347 | plist_get_key_val( (plist_t) g_node_prev_sibling(node), key); | 362 | plist_get_key_val( (plist_t) node_prev_sibling(node), key); |
| 348 | } | 363 | } |
| 349 | } | 364 | } |
| 350 | 365 | ||
| @@ -356,9 +371,9 @@ plist_t plist_dict_get_item(plist_t node, const char* key) | |||
| 356 | { | 371 | { |
| 357 | 372 | ||
| 358 | plist_t current = NULL; | 373 | plist_t current = NULL; |
| 359 | for (current = (plist_t)g_node_first_child(node); | 374 | for (current = (plist_t)node_first_child(node); |
| 360 | current; | 375 | current; |
| 361 | current = (plist_t)g_node_next_sibling(g_node_next_sibling(current))) | 376 | current = (plist_t)node_next_sibling(node_next_sibling(current))) |
| 362 | { | 377 | { |
| 363 | 378 | ||
| 364 | plist_data_t data = plist_get_data(current); | 379 | plist_data_t data = plist_get_data(current); |
| @@ -366,7 +381,7 @@ plist_t plist_dict_get_item(plist_t node, const char* key) | |||
| 366 | 381 | ||
| 367 | if (data && !strcmp(key, data->strval)) | 382 | if (data && !strcmp(key, data->strval)) |
| 368 | { | 383 | { |
| 369 | ret = (plist_t)g_node_next_sibling(current); | 384 | ret = (plist_t)node_next_sibling(current); |
| 370 | break; | 385 | break; |
| 371 | } | 386 | } |
| 372 | } | 387 | } |
| @@ -381,7 +396,7 @@ void plist_dict_set_item(plist_t node, const char* key, plist_t item) | |||
| 381 | plist_t old_item = plist_dict_get_item(node, key); | 396 | plist_t old_item = plist_dict_get_item(node, key); |
| 382 | if (old_item) | 397 | if (old_item) |
| 383 | { | 398 | { |
| 384 | plist_free_node(old_item, NULL); | 399 | plist_free_node(old_item); |
| 385 | old_item = NULL; | 400 | old_item = NULL; |
| 386 | plist_copy_node(item, &old_item); | 401 | plist_copy_node(item, &old_item); |
| 387 | } | 402 | } |
| @@ -393,8 +408,8 @@ void plist_dict_insert_item(plist_t node, const char* key, plist_t item) | |||
| 393 | { | 408 | { |
| 394 | if (node && PLIST_DICT == plist_get_node_type(node)) | 409 | if (node && PLIST_DICT == plist_get_node_type(node)) |
| 395 | { | 410 | { |
| 396 | g_node_append(node, plist_new_key(key)); | 411 | node_attach(node, plist_new_key(key)); |
| 397 | g_node_append(node, item); | 412 | node_attach(node, item); |
| 398 | } | 413 | } |
| 399 | return; | 414 | return; |
| 400 | } | 415 | } |
| @@ -406,7 +421,7 @@ void plist_dict_remove_item(plist_t node, const char* key) | |||
| 406 | plist_t old_item = plist_dict_get_item(node, key); | 421 | plist_t old_item = plist_dict_get_item(node, key); |
| 407 | if (old_item) | 422 | if (old_item) |
| 408 | { | 423 | { |
| 409 | plist_t key_node = g_node_prev_sibling(old_item); | 424 | plist_t key_node = node_prev_sibling(old_item); |
| 410 | plist_free(key_node); | 425 | plist_free(key_node); |
| 411 | plist_free(old_item); | 426 | plist_free(old_item); |
| 412 | } | 427 | } |
| @@ -482,8 +497,8 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu | |||
| 482 | break; | 497 | break; |
| 483 | case PLIST_DATE: | 498 | case PLIST_DATE: |
| 484 | //exception : here we use memory on the stack since it is just a temporary buffer | 499 | //exception : here we use memory on the stack since it is just a temporary buffer |
| 485 | ((GTimeVal *) value)->tv_sec = data->timeval.tv_sec; | 500 | ((struct timeval*) value)->tv_sec = data->timeval.tv_sec; |
| 486 | ((GTimeVal *) value)->tv_usec = data->timeval.tv_usec; | 501 | ((struct timeval*) value)->tv_usec = data->timeval.tv_usec; |
| 487 | break; | 502 | break; |
| 488 | case PLIST_ARRAY: | 503 | case PLIST_ARRAY: |
| 489 | case PLIST_DICT: | 504 | case PLIST_DICT: |
| @@ -494,7 +509,7 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu | |||
| 494 | 509 | ||
| 495 | plist_t plist_get_parent(plist_t node) | 510 | plist_t plist_get_parent(plist_t node) |
| 496 | { | 511 | { |
| 497 | return node ? (plist_t) ((GNode *) node)->parent : NULL; | 512 | return node ? (plist_t) ((node_t*) node)->parent : NULL; |
| 498 | } | 513 | } |
| 499 | 514 | ||
| 500 | plist_type plist_get_node_type(plist_t node) | 515 | plist_type plist_get_node_type(plist_t node) |
| @@ -564,15 +579,15 @@ void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) | |||
| 564 | { | 579 | { |
| 565 | plist_type type = plist_get_node_type(node); | 580 | plist_type type = plist_get_node_type(node); |
| 566 | uint64_t length = 0; | 581 | uint64_t length = 0; |
| 567 | GTimeVal val = { 0, 0 }; | 582 | struct timeval val = { 0, 0 }; |
| 568 | if (PLIST_DATE == type) | 583 | if (PLIST_DATE == type) |
| 569 | plist_get_type_and_value(node, &type, (void *) &val, &length); | 584 | plist_get_type_and_value(node, &type, (void *) &val, &length); |
| 570 | assert(length == sizeof(GTimeVal)); | 585 | assert(length == sizeof(struct timeval)); |
| 571 | *sec = val.tv_sec; | 586 | *sec = val.tv_sec; |
| 572 | *usec = val.tv_usec; | 587 | *usec = val.tv_usec; |
| 573 | } | 588 | } |
| 574 | 589 | ||
| 575 | gboolean plist_data_compare(gconstpointer a, gconstpointer b) | 590 | int plist_data_compare(const void *a, const void *b) |
| 576 | { | 591 | { |
| 577 | plist_data_t val_a = NULL; | 592 | plist_data_t val_a = NULL; |
| 578 | plist_data_t val_b = NULL; | 593 | plist_data_t val_b = NULL; |
| @@ -580,7 +595,7 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) | |||
| 580 | if (!a || !b) | 595 | if (!a || !b) |
| 581 | return FALSE; | 596 | return FALSE; |
| 582 | 597 | ||
| 583 | if (!((GNode *) a)->data || !((GNode *) b)->data) | 598 | if (!((node_t*) a)->data || !((node_t*) b)->data) |
| 584 | return FALSE; | 599 | return FALSE; |
| 585 | 600 | ||
| 586 | val_a = plist_get_data((plist_t) a); | 601 | val_a = plist_get_data((plist_t) a); |
| @@ -620,7 +635,7 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) | |||
| 620 | return FALSE; | 635 | return FALSE; |
| 621 | break; | 636 | break; |
| 622 | case PLIST_DATE: | 637 | case PLIST_DATE: |
| 623 | if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal))) | 638 | if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(struct timeval))) |
| 624 | return TRUE; | 639 | return TRUE; |
| 625 | else | 640 | else |
| 626 | return FALSE; | 641 | return FALSE; |
| @@ -681,8 +696,8 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val | |||
| 681 | memcpy(data->buff, value, length); | 696 | memcpy(data->buff, value, length); |
| 682 | break; | 697 | break; |
| 683 | case PLIST_DATE: | 698 | case PLIST_DATE: |
| 684 | data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec; | 699 | data->timeval.tv_sec = ((struct timeval*) value)->tv_sec; |
| 685 | data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec; | 700 | data->timeval.tv_usec = ((struct timeval*) value)->tv_usec; |
| 686 | break; | 701 | break; |
| 687 | case PLIST_ARRAY: | 702 | case PLIST_ARRAY: |
| 688 | case PLIST_DICT: | 703 | case PLIST_DICT: |
| @@ -693,7 +708,7 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val | |||
| 693 | 708 | ||
| 694 | void plist_set_type(plist_t node, plist_type type) | 709 | void plist_set_type(plist_t node, plist_type type) |
| 695 | { | 710 | { |
| 696 | if ( g_node_n_children(node) == 0 ) | 711 | if ( node_n_children(node) == 0 ) |
| 697 | { | 712 | { |
| 698 | plist_data_t data = plist_get_data(node); | 713 | plist_data_t data = plist_get_data(node); |
| 699 | plist_free_data( data ); | 714 | plist_free_data( data ); |
| @@ -711,7 +726,7 @@ void plist_set_type(plist_t node, plist_type type) | |||
| 711 | data->length = sizeof(double); | 726 | data->length = sizeof(double); |
| 712 | break; | 727 | break; |
| 713 | case PLIST_DATE: | 728 | case PLIST_DATE: |
| 714 | data->length = sizeof(GTimeVal); | 729 | data->length = sizeof(struct timeval); |
| 715 | break; | 730 | break; |
| 716 | default: | 731 | default: |
| 717 | data->length = 0; | 732 | data->length = 0; |
| @@ -752,7 +767,7 @@ void plist_set_data_val(plist_t node, const char *val, uint64_t length) | |||
| 752 | 767 | ||
| 753 | void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) | 768 | void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) |
| 754 | { | 769 | { |
| 755 | GTimeVal val = { sec, usec }; | 770 | struct timeval val = { sec, usec }; |
| 756 | plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal)); | 771 | plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval)); |
| 757 | } | 772 | } |
| 758 | 773 | ||
