diff options
| author | 2008-12-13 16:16:58 +0100 | |
|---|---|---|
| committer | 2008-12-13 16:16:58 +0100 | |
| commit | c39685d3a87858e7ad8ada0da2798aaf670969b4 (patch) | |
| tree | 743a653c4e29209b851d9602912aa5a74e9ef687 | |
| parent | e220e2cf08809a6a8853a8c9c7b06cef4e90cb57 (diff) | |
| download | libplist-c39685d3a87858e7ad8ada0da2798aaf670969b4.tar.gz libplist-c39685d3a87858e7ad8ada0da2798aaf670969b4.tar.bz2 | |
Refine API and fix some warnings.
| -rw-r--r-- | include/plist/plist.h | 23 | ||||
| -rw-r--r-- | src/Makefile.am | 2 | ||||
| -rw-r--r-- | src/bplist.c | 14 | ||||
| -rw-r--r-- | src/plist.c | 166 | ||||
| -rw-r--r-- | src/plist.h | 4 | ||||
| -rw-r--r-- | src/xplist.c | 2 |
6 files changed, 82 insertions, 129 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h index 49d978c..4469f06 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h | |||
| @@ -45,11 +45,14 @@ typedef enum { | |||
| 45 | PLIST_NONE | 45 | PLIST_NONE |
| 46 | } plist_type; | 46 | } plist_type; |
| 47 | 47 | ||
| 48 | //Plist edition | 48 | //Plist creation and edition |
| 49 | void plist_new_dict(plist_t * plist); | 49 | //utilitary functions to create root nodes (supposed to be dict or array) |
| 50 | void plist_new_array(plist_t * plist); | 50 | plist_t plist_new_dict(); |
| 51 | void plist_new_dict_in_plist(plist_t plist, plist_t * dict); | 51 | plist_t plist_new_array(); |
| 52 | void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length); | 52 | //Plist edition, create a new root if node is NULL |
| 53 | plist_t plist_add_sub_element( plist_t node, plist_type type, void* value, uint64_t length); | ||
| 54 | |||
| 55 | //plist free | ||
| 53 | void plist_free(plist_t plist); | 56 | void plist_free(plist_t plist); |
| 54 | 57 | ||
| 55 | //plist navigation | 58 | //plist navigation |
| @@ -57,20 +60,16 @@ plist_t plist_get_first_child(plist_t node); | |||
| 57 | plist_t plist_get_next_sibling(plist_t node); | 60 | plist_t plist_get_next_sibling(plist_t node); |
| 58 | plist_t plist_get_prev_sibling(plist_t node); | 61 | plist_t plist_get_prev_sibling(plist_t node); |
| 59 | 62 | ||
| 63 | plist_t plist_find_node(plist_t plist, plist_type type, void *value); | ||
| 64 | void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length); | ||
| 60 | 65 | ||
| 66 | //import and export functions | ||
| 61 | void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); | 67 | void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); |
| 62 | void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length); | 68 | void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length); |
| 63 | 69 | ||
| 64 | void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist); | 70 | void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist); |
| 65 | void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist); | 71 | void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist); |
| 66 | 72 | ||
| 67 | plist_t plist_find_query_node(plist_t plist, char *key, char *request); | ||
| 68 | plist_t plist_find_node(plist_t plist, plist_type type, void *value); | ||
| 69 | void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length); | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | #ifdef __cplusplus | 73 | #ifdef __cplusplus |
| 75 | } | 74 | } |
| 76 | #endif | 75 | #endif |
diff --git a/src/Makefile.am b/src/Makefile.am index 1b81710..07143b6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | INCLUDES = -I$(top_srcdir)/include | 1 | INCLUDES = -I$(top_srcdir)/include |
| 2 | 2 | ||
| 3 | AM_CFLAGS = $(libxml2_CFLAGS) $(libglib2_CFLAGS) | 3 | AM_CFLAGS = $(libxml2_CFLAGS) $(libglib2_CFLAGS) -D_GNU_SOURCE |
| 4 | AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS) | 4 | AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS) |
| 5 | 5 | ||
| 6 | lib_LTLIBRARIES = libplist.la | 6 | lib_LTLIBRARIES = libplist.la |
diff --git a/src/bplist.c b/src/bplist.c index 48b996d..741a92c 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -80,7 +80,7 @@ void byte_convert(char *address, size_t size) | |||
| 80 | 80 | ||
| 81 | #define be64dec(x) bswap_64( *(uint64_t*)(x) ) | 81 | #define be64dec(x) bswap_64( *(uint64_t*)(x) ) |
| 82 | 82 | ||
| 83 | #define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8))) | 83 | #define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8))) |
| 84 | #define get_real_bytes(x) (x >> 32 ? 4 : 8) | 84 | #define get_real_bytes(x) (x >> 32 ? 4 : 8) |
| 85 | 85 | ||
| 86 | plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) | 86 | plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) |
| @@ -446,7 +446,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) | |||
| 446 | 446 | ||
| 447 | guint plist_data_hash(gconstpointer key) | 447 | guint plist_data_hash(gconstpointer key) |
| 448 | { | 448 | { |
| 449 | plist_data_t data = plist_get_data(key); | 449 | plist_data_t data = plist_get_data((plist_t) key); |
| 450 | 450 | ||
| 451 | guint hash = data->type; | 451 | guint hash = data->type; |
| 452 | guint i = 0; | 452 | guint i = 0; |
| @@ -467,14 +467,14 @@ guint plist_data_hash(gconstpointer key) | |||
| 467 | size = strlen(buff); | 467 | size = strlen(buff); |
| 468 | break; | 468 | break; |
| 469 | case PLIST_UNICODE: | 469 | case PLIST_UNICODE: |
| 470 | buff = data->unicodeval; | 470 | buff = (char *) data->unicodeval; |
| 471 | size = strlen(buff) * sizeof(wchar_t); | 471 | size = strlen(buff) * sizeof(wchar_t); |
| 472 | break; | 472 | break; |
| 473 | case PLIST_DATA: | 473 | case PLIST_DATA: |
| 474 | case PLIST_ARRAY: | 474 | case PLIST_ARRAY: |
| 475 | case PLIST_DICT: | 475 | case PLIST_DICT: |
| 476 | //for these types only hash pointer | 476 | //for these types only hash pointer |
| 477 | buff = &key; | 477 | buff = (char *) &key; |
| 478 | size = sizeof(gconstpointer); | 478 | size = sizeof(gconstpointer); |
| 479 | break; | 479 | break; |
| 480 | case PLIST_DATE: | 480 | case PLIST_DATE: |
| @@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) | |||
| 497 | if (!((GNode *) a)->data || !((GNode *) b)->data) | 497 | if (!((GNode *) a)->data || !((GNode *) b)->data) |
| 498 | return FALSE; | 498 | return FALSE; |
| 499 | 499 | ||
| 500 | plist_data_t val_a = plist_get_data(a); | 500 | plist_data_t val_a = plist_get_data((plist_t) a); |
| 501 | plist_data_t val_b = plist_get_data(b); | 501 | plist_data_t val_b = plist_get_data((plist_t) b); |
| 502 | 502 | ||
| 503 | if (val_a->type != val_b->type) | 503 | if (val_a->type != val_b->type) |
| 504 | return FALSE; | 504 | return FALSE; |
| @@ -519,7 +519,7 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) | |||
| 519 | else | 519 | else |
| 520 | return FALSE; | 520 | return FALSE; |
| 521 | case PLIST_UNICODE: | 521 | case PLIST_UNICODE: |
| 522 | if (!strcmp(val_a->unicodeval, val_b->unicodeval)) | 522 | if (!wcscmp(val_a->unicodeval, val_b->unicodeval)) |
| 523 | return TRUE; | 523 | return TRUE; |
| 524 | else | 524 | else |
| 525 | return FALSE; | 525 | return FALSE; |
diff --git a/src/plist.c b/src/plist.c index 172eceb..ca80c74 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -30,14 +30,14 @@ | |||
| 30 | 30 | ||
| 31 | plist_t plist_new_node(plist_data_t data) | 31 | plist_t plist_new_node(plist_data_t data) |
| 32 | { | 32 | { |
| 33 | return (plist_t)g_node_new(data); | 33 | return (plist_t) g_node_new(data); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | plist_data_t plist_get_data(plist_t node) | 36 | plist_data_t plist_get_data(const plist_t node) |
| 37 | { | 37 | { |
| 38 | if (!node) | 38 | if (!node) |
| 39 | return NULL; | 39 | return NULL; |
| 40 | return ((GNode*)node)->data; | 40 | return ((GNode *) node)->data; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | plist_data_t plist_new_plist_data() | 43 | plist_data_t plist_new_plist_data() |
| @@ -48,90 +48,71 @@ plist_data_t plist_new_plist_data() | |||
| 48 | 48 | ||
| 49 | void plist_free_plist_data(plist_data_t data) | 49 | void plist_free_plist_data(plist_data_t data) |
| 50 | { | 50 | { |
| 51 | free(data); | 51 | if (data) { |
| 52 | } | 52 | switch (data->type) { |
| 53 | |||
| 54 | void plist_new_dict(plist_t * plist) | ||
| 55 | { | ||
| 56 | if (*plist != NULL) | ||
| 57 | return; | ||
| 58 | plist_data_t data = plist_new_plist_data(); | ||
| 59 | data->type = PLIST_DICT; | ||
| 60 | *plist = plist_new_node(data); | ||
| 61 | } | ||
| 62 | 53 | ||
| 63 | void plist_new_array(plist_t * plist) | 54 | default: |
| 64 | { | 55 | break; |
| 65 | if (*plist != NULL) | 56 | } |
| 66 | return; | 57 | free(data); |
| 67 | plist_data_t data = plist_new_plist_data(); | 58 | } |
| 68 | data->type = PLIST_ARRAY; | ||
| 69 | *plist = plist_new_node(data); | ||
| 70 | } | 59 | } |
| 71 | 60 | ||
| 72 | void plist_new_dict_in_plist(plist_t plist, plist_t * dict) | 61 | plist_t plist_new_dict() |
| 73 | { | 62 | { |
| 74 | if (!plist || *dict) | ||
| 75 | return; | ||
| 76 | |||
| 77 | plist_data_t data = plist_new_plist_data(); | 63 | plist_data_t data = plist_new_plist_data(); |
| 78 | data->type = PLIST_DICT; | 64 | data->type = PLIST_DICT; |
| 79 | *dict = plist_new_node(data); | 65 | return plist_new_node(data); |
| 80 | g_node_append(plist, *dict); | ||
| 81 | } | 66 | } |
| 82 | 67 | ||
| 83 | 68 | plist_t plist_new_array() | |
| 84 | /** Adds a new key pair to a dict. | ||
| 85 | * | ||
| 86 | * @param dict The dict node in the plist. | ||
| 87 | * @param key the key name of the key pair. | ||
| 88 | * @param type The the type of the value in the key pair. | ||
| 89 | * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value | ||
| 90 | * | ||
| 91 | */ | ||
| 92 | void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length) | ||
| 93 | { | 69 | { |
| 94 | if (!dict || !key || !value) | ||
| 95 | return; | ||
| 96 | |||
| 97 | plist_data_t data = plist_new_plist_data(); | 70 | plist_data_t data = plist_new_plist_data(); |
| 98 | data->type = PLIST_KEY; | 71 | data->type = PLIST_ARRAY; |
| 99 | data->strval = strdup(key); | 72 | return plist_new_node(data); |
| 100 | plist_t keynode = plist_new_node(data); | 73 | } |
| 101 | g_node_append(dict, keynode); | 74 | |
| 102 | 75 | plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length) | |
| 103 | //now handle value | 76 | { |
| 104 | plist_data_t val = plist_new_plist_data(); | 77 | //only structured types are allowed to have nulll value |
| 105 | val->type = type; | 78 | if (!value && (type == PLIST_DICT || type == PLIST_ARRAY)) { |
| 106 | val->length = length; | 79 | //now handle value |
| 80 | plist_data_t data = plist_new_plist_data(); | ||
| 81 | data->type = type; | ||
| 82 | data->length = length; | ||
| 83 | |||
| 84 | switch (type) { | ||
| 85 | case PLIST_BOOLEAN: | ||
| 86 | data->boolval = *((char *) value); | ||
| 87 | break; | ||
| 88 | case PLIST_UINT: | ||
| 89 | data->intval = *((uint64_t *) value); | ||
| 90 | break; | ||
| 91 | case PLIST_REAL: | ||
| 92 | data->realval = *((double *) value); | ||
| 93 | break; | ||
| 94 | case PLIST_STRING: | ||
| 95 | data->strval = strdup((char *) value); | ||
| 96 | break; | ||
| 97 | case PLIST_UNICODE: | ||
| 98 | data->unicodeval = wcsdup((wchar_t *) value); | ||
| 99 | break; | ||
| 100 | case PLIST_DATA: | ||
| 101 | memcpy(data->buff, value, length); | ||
| 102 | break; | ||
| 103 | case PLIST_ARRAY: | ||
| 104 | case PLIST_DICT: | ||
| 105 | case PLIST_DATE: | ||
| 106 | default: | ||
| 107 | break; | ||
| 108 | } | ||
| 107 | 109 | ||
| 108 | switch (type) { | 110 | plist_t subnode = plist_new_node(data); |
| 109 | case PLIST_BOOLEAN: | 111 | if (node) |
| 110 | val->boolval = *((char *) value); | 112 | g_node_append(node, subnode); |
| 111 | break; | 113 | return subnode; |
| 112 | case PLIST_UINT: | 114 | } else |
| 113 | val->intval = *((uint64_t *) value); | 115 | return NULL; |
| 114 | break; | ||
| 115 | case PLIST_REAL: | ||
| 116 | val->realval = *((double *) value); | ||
| 117 | break; | ||
| 118 | case PLIST_STRING: | ||
| 119 | val->strval = strdup((char *) value); | ||
| 120 | break; | ||
| 121 | case PLIST_UNICODE: | ||
| 122 | val->unicodeval = wcsdup((wchar_t *) value); | ||
| 123 | break; | ||
| 124 | case PLIST_DATA: | ||
| 125 | memcpy(val->buff, value, length); | ||
| 126 | break; | ||
| 127 | case PLIST_ARRAY: | ||
| 128 | case PLIST_DICT: | ||
| 129 | case PLIST_DATE: | ||
| 130 | default: | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | plist_t valnode = plist_new_node(val); | ||
| 134 | g_node_append(dict, valnode); | ||
| 135 | } | 116 | } |
| 136 | 117 | ||
| 137 | void plist_free(plist_t plist) | 118 | void plist_free(plist_t plist) |
| @@ -141,44 +122,17 @@ void plist_free(plist_t plist) | |||
| 141 | 122 | ||
| 142 | plist_t plist_get_first_child(plist_t node) | 123 | plist_t plist_get_first_child(plist_t node) |
| 143 | { | 124 | { |
| 144 | return (plist_t)g_node_first_child( (GNode*)node ); | 125 | return (plist_t) g_node_first_child((GNode *) node); |
| 145 | } | 126 | } |
| 146 | 127 | ||
| 147 | plist_t plist_get_next_sibling(plist_t node) | 128 | plist_t plist_get_next_sibling(plist_t node) |
| 148 | { | 129 | { |
| 149 | return (plist_t)g_node_next_sibling( (GNode*)node ); | 130 | return (plist_t) g_node_next_sibling((GNode *) node); |
| 150 | } | 131 | } |
| 151 | 132 | ||
| 152 | plist_t plist_get_prev_sibling(plist_t node) | 133 | plist_t plist_get_prev_sibling(plist_t node) |
| 153 | { | 134 | { |
| 154 | return (plist_t)g_node_prev_sibling( (GNode*)node ); | 135 | return (plist_t) g_node_prev_sibling((GNode *) node); |
| 155 | } | ||
| 156 | |||
| 157 | plist_t plist_find_query_node(plist_t plist, char *key, char *request) | ||
| 158 | { | ||
| 159 | if (!plist) | ||
| 160 | return NULL; | ||
| 161 | |||
| 162 | plist_t current = NULL; | ||
| 163 | plist_t next = NULL; | ||
| 164 | for (current = plist_get_first_child(plist); current; current = next) { | ||
| 165 | |||
| 166 | next = plist_get_next_sibling(current); | ||
| 167 | plist_data_t data = plist_get_data(current); | ||
| 168 | |||
| 169 | if (data->type == PLIST_KEY && !strcmp(data->strval, key) && next) { | ||
| 170 | |||
| 171 | data = plist_get_data(next); | ||
| 172 | if (data->type == PLIST_STRING && !strcmp(data->strval, request)) | ||
| 173 | return next; | ||
| 174 | } | ||
| 175 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | ||
| 176 | plist_t sub = plist_find_query_node(current, key, request); | ||
| 177 | if (sub) | ||
| 178 | return sub; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | return NULL; | ||
| 182 | } | 136 | } |
| 183 | 137 | ||
| 184 | char compare_node_value(plist_type type, plist_data_t data, void *value) | 138 | char compare_node_value(plist_type type, plist_data_t data, void *value) |
diff --git a/src/plist.h b/src/plist.h index ca3201a..b7c74d9 100644 --- a/src/plist.h +++ b/src/plist.h | |||
| @@ -49,10 +49,10 @@ struct plist_data_s { | |||
| 49 | plist_type type; | 49 | plist_type type; |
| 50 | }; | 50 | }; |
| 51 | 51 | ||
| 52 | typedef struct plist_data_s* plist_data_t; | 52 | typedef struct plist_data_s *plist_data_t; |
| 53 | 53 | ||
| 54 | plist_t plist_new_node(plist_data_t data); | 54 | plist_t plist_new_node(plist_data_t data); |
| 55 | plist_data_t plist_get_data(plist_t node); | 55 | plist_data_t plist_get_data(const plist_t node); |
| 56 | plist_data_t plist_new_plist_data(); | 56 | plist_data_t plist_new_plist_data(); |
| 57 | void plist_free_plist_data(plist_data_t node); | 57 | void plist_free_plist_data(plist_data_t node); |
| 58 | 58 | ||
diff --git a/src/xplist.c b/src/xplist.c index 4f81e1b..6c27375 100644 --- a/src/xplist.c +++ b/src/xplist.c | |||
| @@ -221,7 +221,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 221 | break; | 221 | break; |
| 222 | 222 | ||
| 223 | plist_data_t data = plist_new_plist_data(); | 223 | plist_data_t data = plist_new_plist_data(); |
| 224 | GNode *subnode = g_node_new(data); | 224 | plist_t subnode = plist_new_node(data); |
| 225 | if (*plist_node) | 225 | if (*plist_node) |
| 226 | g_node_append(*plist_node, subnode); | 226 | g_node_append(*plist_node, subnode); |
| 227 | else | 227 | else |
