diff options
| author | 2009-10-13 20:03:38 +0200 | |
|---|---|---|
| committer | 2009-10-13 20:03:38 +0200 | |
| commit | e492ef675c404cc6c0d1cfa26e47a1c16c850d5f (patch) | |
| tree | 57cdc453f9d29e465ff83622c8b43d89e46dca00 /src | |
| parent | 3cff5a16030168cdc45102a770b54b973170cd7f (diff) | |
| download | libplist-e492ef675c404cc6c0d1cfa26e47a1c16c850d5f.tar.gz libplist-e492ef675c404cc6c0d1cfa26e47a1c16c850d5f.tar.bz2 | |
Add path accessor util function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/plist.c | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/src/plist.c b/src/plist.c index 78ac07c..ed73d53 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -408,37 +408,36 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v | |||
| 408 | return res; | 408 | return res; |
| 409 | } | 409 | } |
| 410 | 410 | ||
| 411 | static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length) | 411 | plist_t plist_access_pathv(plist_t plist, uint32_t length, va_list v) |
| 412 | { | 412 | { |
| 413 | plist_t current = NULL; | 413 | plist_t current = plist; |
| 414 | 414 | plist_type type = PLIST_NONE; | |
| 415 | if (!plist) | 415 | uint32_t i = 0; |
| 416 | return NULL; | 416 | |
| 417 | 417 | for (i = 0; i < length && current; i++) { | |
| 418 | for (current = (plist_t)g_node_first_child(plist); current; current = (plist_t)g_node_next_sibling(current)) { | 418 | type = plist_get_node_type(current); |
| 419 | 419 | ||
| 420 | plist_data_t data = plist_get_data(current); | 420 | if (type == PLIST_ARRAY) { |
| 421 | 421 | uint32_t index = va_arg(v, uint32_t); | |
| 422 | if (data->type == type && data->length == length && compare_node_value(type, data, value, length)) { | 422 | current = plist_array_get_item(current, index); |
| 423 | return current; | ||
| 424 | } | 423 | } |
| 425 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | 424 | else if (type == PLIST_DICT) { |
| 426 | plist_t sub = plist_find_node(current, type, value, length); | 425 | const char* key = va_arg(v, const char*); |
| 427 | if (sub) | 426 | current = plist_dict_get_item(current, key); |
| 428 | return sub; | ||
| 429 | } | 427 | } |
| 430 | } | 428 | } |
| 431 | return NULL; | 429 | return current; |
| 432 | } | 430 | } |
| 433 | 431 | ||
| 434 | plist_t plist_find_node_by_key(plist_t plist, const char *value) | 432 | plist_t plist_access_path(plist_t plist, uint32_t length, ...) |
| 435 | { | 433 | { |
| 436 | return plist_find_node(plist, PLIST_KEY, value, strlen(value)); | 434 | plist_t ret = NULL; |
| 437 | } | 435 | va_list v; |
| 438 | 436 | ||
| 439 | plist_t plist_find_node_by_string(plist_t plist, const char *value) | 437 | va_start(v, length); |
| 440 | { | 438 | ret = plist_access_pathv(plist, length, v); |
| 441 | return plist_find_node(plist, PLIST_STRING, value, strlen(value)); | 439 | va_end(v); |
| 440 | return ret; | ||
| 442 | } | 441 | } |
| 443 | 442 | ||
| 444 | static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) | 443 | static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) |
| @@ -886,4 +885,36 @@ void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) | |||
| 886 | plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal)); | 885 | plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal)); |
| 887 | } | 886 | } |
| 888 | 887 | ||
| 888 | static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length) | ||
| 889 | { | ||
| 890 | plist_t current = NULL; | ||
| 891 | |||
| 892 | if (!plist) | ||
| 893 | return NULL; | ||
| 894 | |||
| 895 | for (current = (plist_t)g_node_first_child(plist); current; current = (plist_t)g_node_next_sibling(current)) { | ||
| 896 | |||
| 897 | plist_data_t data = plist_get_data(current); | ||
| 898 | |||
| 899 | if (data->type == type && data->length == length && compare_node_value(type, data, value, length)) { | ||
| 900 | return current; | ||
| 901 | } | ||
| 902 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | ||
| 903 | plist_t sub = plist_find_node(current, type, value, length); | ||
| 904 | if (sub) | ||
| 905 | return sub; | ||
| 906 | } | ||
| 907 | } | ||
| 908 | return NULL; | ||
| 909 | } | ||
| 910 | |||
| 911 | plist_t plist_find_node_by_key(plist_t plist, const char *value) | ||
| 912 | { | ||
| 913 | return plist_find_node(plist, PLIST_KEY, value, strlen(value)); | ||
| 914 | } | ||
| 915 | |||
| 916 | plist_t plist_find_node_by_string(plist_t plist, const char *value) | ||
| 917 | { | ||
| 918 | return plist_find_node(plist, PLIST_STRING, value, strlen(value)); | ||
| 919 | } | ||
| 889 | 920 | ||
