diff options
| author | 2009-10-10 18:20:01 +0200 | |
|---|---|---|
| committer | 2009-10-10 18:20:01 +0200 | |
| commit | 77b02c9404dbfef325b7a19228045a817cafe064 (patch) | |
| tree | 1d571bb5faf3ba49a7d1f0e7cdf8195b271d728d /src/plist.c | |
| parent | c18c9d087860e812431799fae406144928dd46c8 (diff) | |
| download | libplist-77b02c9404dbfef325b7a19228045a817cafe064.tar.gz libplist-77b02c9404dbfef325b7a19228045a817cafe064.tar.bz2 | |
Rework public API to make it more consistent.
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 419 |
1 files changed, 307 insertions, 112 deletions
diff --git a/src/plist.c b/src/plist.c index 4d98fd2..8368d6e 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -80,57 +80,70 @@ plist_t plist_new_array(void) | |||
| 80 | return plist_new_node(data); | 80 | return plist_new_node(data); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length) | 83 | //These nodes should not be handled by users |
| 84 | static plist_t plist_new_key(const char *val) | ||
| 84 | { | 85 | { |
| 85 | //only structured types can have children | 86 | plist_data_t data = plist_new_plist_data(); |
| 86 | plist_type node_type = plist_get_node_type(node); | 87 | data->type = PLIST_KEY; |
| 87 | if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) { | 88 | data->strval = strdup(val); |
| 88 | //only structured types are allowed to have nulll value | 89 | data->length = strlen(val); |
| 89 | if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { | 90 | return plist_new_node(data); |
| 91 | } | ||
| 90 | 92 | ||
| 91 | plist_t subnode = NULL; | 93 | plist_t plist_new_string(const char *val) |
| 94 | { | ||
| 95 | plist_data_t data = plist_new_plist_data(); | ||
| 96 | data->type = PLIST_STRING; | ||
| 97 | data->strval = strdup(val); | ||
| 98 | data->length = strlen(val); | ||
| 99 | return plist_new_node(data); | ||
| 100 | } | ||
| 92 | 101 | ||
| 93 | //now handle value | 102 | plist_t plist_new_bool(uint8_t val) |
| 94 | plist_data_t data = plist_new_plist_data(); | 103 | { |
| 95 | data->type = type; | 104 | plist_data_t data = plist_new_plist_data(); |
| 96 | data->length = length; | 105 | data->type = PLIST_BOOLEAN; |
| 106 | data->boolval = val; | ||
| 107 | data->length = sizeof(uint8_t); | ||
| 108 | return plist_new_node(data); | ||
| 109 | } | ||
| 97 | 110 | ||
| 98 | switch (type) { | 111 | plist_t plist_new_uint(uint64_t val) |
| 99 | case PLIST_BOOLEAN: | 112 | { |
| 100 | data->boolval = *((char *) value); | 113 | plist_data_t data = plist_new_plist_data(); |
| 101 | break; | 114 | data->type = PLIST_UINT; |
| 102 | case PLIST_UINT: | 115 | data->intval = val; |
| 103 | data->intval = *((uint64_t *) value); | 116 | data->length = sizeof(uint64_t); |
| 104 | break; | 117 | return plist_new_node(data); |
| 105 | case PLIST_REAL: | 118 | } |
| 106 | data->realval = *((double *) value); | ||
| 107 | break; | ||
| 108 | case PLIST_KEY: | ||
| 109 | case PLIST_STRING: | ||
| 110 | data->strval = strdup((char *) value); | ||
| 111 | break; | ||
| 112 | case PLIST_DATA: | ||
| 113 | data->buff = (uint8_t *) malloc(length); | ||
| 114 | memcpy(data->buff, value, length); | ||
| 115 | break; | ||
| 116 | case PLIST_DATE: | ||
| 117 | data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec; | ||
| 118 | data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec; | ||
| 119 | break; | ||
| 120 | case PLIST_ARRAY: | ||
| 121 | case PLIST_DICT: | ||
| 122 | default: | ||
| 123 | break; | ||
| 124 | } | ||
| 125 | 119 | ||
| 126 | subnode = plist_new_node(data); | 120 | plist_t plist_new_real(double val) |
| 127 | if (node) | 121 | { |
| 128 | g_node_append(node, subnode); | 122 | plist_data_t data = plist_new_plist_data(); |
| 129 | return subnode; | 123 | data->type = PLIST_REAL; |
| 130 | } else | 124 | data->realval = val; |
| 131 | return NULL; | 125 | data->length = sizeof(double); |
| 132 | } | 126 | return plist_new_node(data); |
| 133 | return NULL; | 127 | } |
| 128 | |||
| 129 | plist_t plist_new_data(const char *val, uint64_t length) | ||
| 130 | { | ||
| 131 | plist_data_t data = plist_new_plist_data(); | ||
| 132 | data->type = PLIST_DATA; | ||
| 133 | data->buff = (uint8_t *) malloc(length); | ||
| 134 | memcpy(data->buff, val, length); | ||
| 135 | data->length = length; | ||
| 136 | return plist_new_node(data); | ||
| 137 | } | ||
| 138 | |||
| 139 | plist_t plist_new_date(int32_t sec, int32_t usec) | ||
| 140 | { | ||
| 141 | plist_data_t data = plist_new_plist_data(); | ||
| 142 | data->type = PLIST_DATE; | ||
| 143 | data->timeval.tv_sec = sec; | ||
| 144 | data->timeval.tv_usec = usec; | ||
| 145 | data->length = sizeof(GTimeVal); | ||
| 146 | return plist_new_node(data); | ||
| 134 | } | 147 | } |
| 135 | 148 | ||
| 136 | void plist_free(plist_t plist) | 149 | void plist_free(plist_t plist) |
| @@ -181,54 +194,131 @@ plist_t plist_copy(plist_t node) | |||
| 181 | return copied; | 194 | return copied; |
| 182 | } | 195 | } |
| 183 | 196 | ||
| 184 | plist_t plist_get_first_child(plist_t node) | 197 | uint32_t plist_array_get_size(plist_t node) |
| 185 | { | 198 | { |
| 186 | return (plist_t) g_node_first_child((GNode *) node); | 199 | uint32_t ret = 0; |
| 200 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { | ||
| 201 | ret = g_node_n_children(node); | ||
| 202 | } | ||
| 203 | return ret; | ||
| 187 | } | 204 | } |
| 188 | 205 | ||
| 189 | plist_t plist_get_next_sibling(plist_t node) | 206 | plist_t plist_array_get_item(plist_t node, uint32_t n) |
| 190 | { | 207 | { |
| 191 | return (plist_t) g_node_next_sibling((GNode *) node); | 208 | plist_t ret = NULL; |
| 209 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { | ||
| 210 | ret = (plist_t)g_node_nth_child(node, n); | ||
| 211 | } | ||
| 212 | return ret; | ||
| 192 | } | 213 | } |
| 193 | 214 | ||
| 194 | plist_t plist_get_prev_sibling(plist_t node) | 215 | void plist_array_set_item(plist_t node, plist_t item, uint32_t n) |
| 195 | { | 216 | { |
| 196 | return (plist_t) g_node_prev_sibling((GNode *) node); | 217 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { |
| 218 | plist_t old_item = plist_array_get_item(node, n); | ||
| 219 | if (old_item) { | ||
| 220 | plist_free_node(old_item, NULL); | ||
| 221 | old_item = NULL; | ||
| 222 | plist_copy_node(item, &old_item); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | return; | ||
| 197 | } | 226 | } |
| 198 | 227 | ||
| 199 | plist_t plist_get_parent(plist_t node) | 228 | void plist_array_append_item(plist_t node, plist_t item) |
| 200 | { | 229 | { |
| 201 | return node ? (plist_t) ((GNode *) node)->parent : NULL; | 230 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { |
| 231 | g_node_append(node, item); | ||
| 232 | } | ||
| 233 | return; | ||
| 202 | } | 234 | } |
| 203 | 235 | ||
| 204 | plist_t plist_get_array_nth_el(plist_t node, uint32_t n) | 236 | void plist_array_insert_item(plist_t node, plist_t item, uint32_t n) |
| 205 | { | 237 | { |
| 206 | plist_t ret = NULL; | ||
| 207 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { | 238 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { |
| 208 | uint32_t i = 0; | 239 | g_node_insert(node, n, item); |
| 209 | plist_t temp = plist_get_first_child(node); | 240 | } |
| 241 | return; | ||
| 242 | } | ||
| 210 | 243 | ||
| 211 | while (i <= n && temp) { | 244 | void plist_array_remove_item(plist_t node, uint32_t n) |
| 212 | if (i == n) | 245 | { |
| 213 | ret = temp; | 246 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { |
| 214 | temp = plist_get_next_sibling(temp); | 247 | plist_t old_item = plist_array_get_item(node, n); |
| 215 | i++; | 248 | if (old_item) { |
| 249 | plist_free(old_item); | ||
| 216 | } | 250 | } |
| 217 | } | 251 | } |
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | uint32_t plist_dict_get_size(plist_t node) | ||
| 256 | { | ||
| 257 | uint32_t ret = 0; | ||
| 258 | if (node && PLIST_DICT == plist_get_node_type(node)) { | ||
| 259 | ret = g_node_n_children(node) / 2; | ||
| 260 | } | ||
| 218 | return ret; | 261 | return ret; |
| 219 | } | 262 | } |
| 220 | 263 | ||
| 221 | plist_t plist_get_dict_el_from_key(plist_t node, const char *key) | 264 | plist_t plist_dict_get_item(plist_t node, const char* key) |
| 222 | { | 265 | { |
| 223 | plist_t ret = NULL; | 266 | plist_t ret = NULL; |
| 267 | |||
| 224 | if (node && PLIST_DICT == plist_get_node_type(node)) { | 268 | if (node && PLIST_DICT == plist_get_node_type(node)) { |
| 225 | 269 | ||
| 226 | plist_t key_node = plist_find_node_by_key(node, key); | 270 | plist_t current = NULL; |
| 227 | ret = plist_get_next_sibling(key_node); | 271 | for (current = plist_get_first_child(node); |
| 272 | current; | ||
| 273 | current = plist_get_next_sibling(plist_get_next_sibling(current))) { | ||
| 274 | |||
| 275 | assert( PLIST_KEY == plist_get_node_type(current) ); | ||
| 276 | plist_data_t data = plist_get_data(current); | ||
| 277 | |||
| 278 | if (data && !strcmp(key, data->strval)) { | ||
| 279 | ret = plist_get_next_sibling(current); | ||
| 280 | break; | ||
| 281 | } | ||
| 282 | } | ||
| 228 | } | 283 | } |
| 229 | return ret; | 284 | return ret; |
| 230 | } | 285 | } |
| 231 | 286 | ||
| 287 | void plist_dict_set_item(plist_t node, plist_t item, const char* key) | ||
| 288 | { | ||
| 289 | if (node && PLIST_DICT == plist_get_node_type(node)) { | ||
| 290 | plist_t old_item = plist_dict_get_item(node, key); | ||
| 291 | if (old_item) { | ||
| 292 | plist_free_node(old_item, NULL); | ||
| 293 | old_item = NULL; | ||
| 294 | plist_copy_node(item, &old_item); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | |||
| 300 | void plist_dict_insert_item(plist_t node, plist_t item, const char* key) | ||
| 301 | { | ||
| 302 | if (node && PLIST_DICT == plist_get_node_type(node)) { | ||
| 303 | g_node_append(node, plist_new_key(key)); | ||
| 304 | g_node_append(node, item); | ||
| 305 | } | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | |||
| 309 | void plist_dict_remove_item(plist_t node, const char* key) | ||
| 310 | { | ||
| 311 | if (node && PLIST_DICT == plist_get_node_type(node)) { | ||
| 312 | plist_t old_item = plist_dict_get_item(node, key); | ||
| 313 | if (old_item) { | ||
| 314 | plist_t key_node = g_node_prev_sibling(old_item); | ||
| 315 | plist_free(key_node); | ||
| 316 | plist_free(old_item); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | return; | ||
| 320 | } | ||
| 321 | |||
| 232 | static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length) | 322 | static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length) |
| 233 | { | 323 | { |
| 234 | char res = FALSE; | 324 | char res = FALSE; |
| @@ -335,6 +425,11 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu | |||
| 335 | } | 425 | } |
| 336 | } | 426 | } |
| 337 | 427 | ||
| 428 | plist_t plist_get_parent(plist_t node) | ||
| 429 | { | ||
| 430 | return node ? (plist_t) ((GNode *) node)->parent : NULL; | ||
| 431 | } | ||
| 432 | |||
| 338 | plist_type plist_get_node_type(plist_t node) | 433 | plist_type plist_get_node_type(plist_t node) |
| 339 | { | 434 | { |
| 340 | if (node) { | 435 | if (node) { |
| @@ -345,51 +440,6 @@ plist_type plist_get_node_type(plist_t node) | |||
| 345 | return PLIST_NONE; | 440 | return PLIST_NONE; |
| 346 | } | 441 | } |
| 347 | 442 | ||
| 348 | void plist_add_sub_node(plist_t node, plist_t subnode) | ||
| 349 | { | ||
| 350 | if (node && subnode) { | ||
| 351 | plist_type type = plist_get_node_type(node); | ||
| 352 | if (type == PLIST_DICT || type == PLIST_ARRAY) | ||
| 353 | g_node_append(node, subnode); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | void plist_add_sub_key_el(plist_t node, const char *val) | ||
| 358 | { | ||
| 359 | plist_add_sub_element(node, PLIST_KEY, val, strlen(val)); | ||
| 360 | } | ||
| 361 | |||
| 362 | void plist_add_sub_string_el(plist_t node, const char *val) | ||
| 363 | { | ||
| 364 | plist_add_sub_element(node, PLIST_STRING, val, strlen(val)); | ||
| 365 | } | ||
| 366 | |||
| 367 | void plist_add_sub_bool_el(plist_t node, uint8_t val) | ||
| 368 | { | ||
| 369 | plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t)); | ||
| 370 | } | ||
| 371 | |||
| 372 | void plist_add_sub_uint_el(plist_t node, uint64_t val) | ||
| 373 | { | ||
| 374 | plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t)); | ||
| 375 | } | ||
| 376 | |||
| 377 | void plist_add_sub_real_el(plist_t node, double val) | ||
| 378 | { | ||
| 379 | plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double)); | ||
| 380 | } | ||
| 381 | |||
| 382 | void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length) | ||
| 383 | { | ||
| 384 | plist_add_sub_element(node, PLIST_DATA, val, length); | ||
| 385 | } | ||
| 386 | |||
| 387 | void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) | ||
| 388 | { | ||
| 389 | GTimeVal val = { sec, usec }; | ||
| 390 | plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal)); | ||
| 391 | } | ||
| 392 | |||
| 393 | void plist_get_key_val(plist_t node, char **val) | 443 | void plist_get_key_val(plist_t node, char **val) |
| 394 | { | 444 | { |
| 395 | plist_type type = plist_get_node_type(node); | 445 | plist_type type = plist_get_node_type(node); |
| @@ -570,7 +620,6 @@ static plist_t plist_set_element_val(plist_t node, plist_type type, const void * | |||
| 570 | } | 620 | } |
| 571 | } | 621 | } |
| 572 | 622 | ||
| 573 | |||
| 574 | void plist_set_key_val(plist_t node, const char *val) | 623 | void plist_set_key_val(plist_t node, const char *val) |
| 575 | { | 624 | { |
| 576 | plist_set_element_val(node, PLIST_KEY, val, strlen(val)); | 625 | plist_set_element_val(node, PLIST_KEY, val, strlen(val)); |
| @@ -606,3 +655,149 @@ void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) | |||
| 606 | GTimeVal val = { sec, usec }; | 655 | GTimeVal val = { sec, usec }; |
| 607 | plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal)); | 656 | plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal)); |
| 608 | } | 657 | } |
| 658 | |||
| 659 | //DEPRECATED API BELOW | ||
| 660 | |||
| 661 | static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length) | ||
| 662 | { | ||
| 663 | //only structured types can have children | ||
| 664 | plist_type node_type = plist_get_node_type(node); | ||
| 665 | if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) { | ||
| 666 | //only structured types are allowed to have nulll value | ||
| 667 | if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { | ||
| 668 | |||
| 669 | plist_t subnode = NULL; | ||
| 670 | |||
| 671 | //now handle value | ||
| 672 | plist_data_t data = plist_new_plist_data(); | ||
| 673 | data->type = type; | ||
| 674 | data->length = length; | ||
| 675 | |||
| 676 | switch (type) { | ||
| 677 | case PLIST_BOOLEAN: | ||
| 678 | data->boolval = *((char *) value); | ||
| 679 | break; | ||
| 680 | case PLIST_UINT: | ||
| 681 | data->intval = *((uint64_t *) value); | ||
| 682 | break; | ||
| 683 | case PLIST_REAL: | ||
| 684 | data->realval = *((double *) value); | ||
| 685 | break; | ||
| 686 | case PLIST_KEY: | ||
| 687 | case PLIST_STRING: | ||
| 688 | data->strval = strdup((char *) value); | ||
| 689 | break; | ||
| 690 | case PLIST_DATA: | ||
| 691 | data->buff = (uint8_t *) malloc(length); | ||
| 692 | memcpy(data->buff, value, length); | ||
| 693 | break; | ||
| 694 | case PLIST_DATE: | ||
| 695 | data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec; | ||
| 696 | data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec; | ||
| 697 | break; | ||
| 698 | case PLIST_ARRAY: | ||
| 699 | case PLIST_DICT: | ||
| 700 | default: | ||
| 701 | break; | ||
| 702 | } | ||
| 703 | |||
| 704 | subnode = plist_new_node(data); | ||
| 705 | if (node) | ||
| 706 | g_node_append(node, subnode); | ||
| 707 | return subnode; | ||
| 708 | } else | ||
| 709 | return NULL; | ||
| 710 | } | ||
| 711 | return NULL; | ||
| 712 | } | ||
| 713 | |||
| 714 | |||
| 715 | plist_t plist_get_first_child(plist_t node) | ||
| 716 | { | ||
| 717 | return (plist_t) g_node_first_child((GNode *) node); | ||
| 718 | } | ||
| 719 | |||
| 720 | plist_t plist_get_next_sibling(plist_t node) | ||
| 721 | { | ||
| 722 | return (plist_t) g_node_next_sibling((GNode *) node); | ||
| 723 | } | ||
| 724 | |||
| 725 | plist_t plist_get_prev_sibling(plist_t node) | ||
| 726 | { | ||
| 727 | return (plist_t) g_node_prev_sibling((GNode *) node); | ||
| 728 | } | ||
| 729 | |||
| 730 | plist_t plist_get_array_nth_el(plist_t node, uint32_t n) | ||
| 731 | { | ||
| 732 | plist_t ret = NULL; | ||
| 733 | if (node && PLIST_ARRAY == plist_get_node_type(node)) { | ||
| 734 | uint32_t i = 0; | ||
| 735 | plist_t temp = plist_get_first_child(node); | ||
| 736 | |||
| 737 | while (i <= n && temp) { | ||
| 738 | if (i == n) | ||
| 739 | ret = temp; | ||
| 740 | temp = plist_get_next_sibling(temp); | ||
| 741 | i++; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | return ret; | ||
| 745 | } | ||
| 746 | |||
| 747 | plist_t plist_get_dict_el_from_key(plist_t node, const char *key) | ||
| 748 | { | ||
| 749 | plist_t ret = NULL; | ||
| 750 | if (node && PLIST_DICT == plist_get_node_type(node)) { | ||
| 751 | |||
| 752 | plist_t key_node = plist_find_node_by_key(node, key); | ||
| 753 | ret = plist_get_next_sibling(key_node); | ||
| 754 | } | ||
| 755 | return ret; | ||
| 756 | } | ||
| 757 | |||
| 758 | void plist_add_sub_node(plist_t node, plist_t subnode) | ||
| 759 | { | ||
| 760 | if (node && subnode) { | ||
| 761 | plist_type type = plist_get_node_type(node); | ||
| 762 | if (type == PLIST_DICT || type == PLIST_ARRAY) | ||
| 763 | g_node_append(node, subnode); | ||
| 764 | } | ||
| 765 | } | ||
| 766 | |||
| 767 | void plist_add_sub_key_el(plist_t node, const char *val) | ||
| 768 | { | ||
| 769 | plist_add_sub_element(node, PLIST_KEY, val, strlen(val)); | ||
| 770 | } | ||
| 771 | |||
| 772 | void plist_add_sub_string_el(plist_t node, const char *val) | ||
| 773 | { | ||
| 774 | plist_add_sub_element(node, PLIST_STRING, val, strlen(val)); | ||
| 775 | } | ||
| 776 | |||
| 777 | void plist_add_sub_bool_el(plist_t node, uint8_t val) | ||
| 778 | { | ||
| 779 | plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t)); | ||
| 780 | } | ||
| 781 | |||
| 782 | void plist_add_sub_uint_el(plist_t node, uint64_t val) | ||
| 783 | { | ||
| 784 | plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t)); | ||
| 785 | } | ||
| 786 | |||
| 787 | void plist_add_sub_real_el(plist_t node, double val) | ||
| 788 | { | ||
| 789 | plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double)); | ||
| 790 | } | ||
| 791 | |||
| 792 | void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length) | ||
| 793 | { | ||
| 794 | plist_add_sub_element(node, PLIST_DATA, val, length); | ||
| 795 | } | ||
| 796 | |||
| 797 | void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) | ||
| 798 | { | ||
| 799 | GTimeVal val = { sec, usec }; | ||
| 800 | plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal)); | ||
| 801 | } | ||
| 802 | |||
| 803 | |||
