diff options
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 103 |
1 files changed, 72 insertions, 31 deletions
diff --git a/src/plist.c b/src/plist.c index 932ea5e..172eceb 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -28,23 +28,45 @@ | |||
| 28 | #include <stdlib.h> | 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> | 29 | #include <stdio.h> |
| 30 | 30 | ||
| 31 | plist_t plist_new_node(plist_data_t data) | ||
| 32 | { | ||
| 33 | return (plist_t)g_node_new(data); | ||
| 34 | } | ||
| 35 | |||
| 36 | plist_data_t plist_get_data(plist_t node) | ||
| 37 | { | ||
| 38 | if (!node) | ||
| 39 | return NULL; | ||
| 40 | return ((GNode*)node)->data; | ||
| 41 | } | ||
| 42 | |||
| 43 | plist_data_t plist_new_plist_data() | ||
| 44 | { | ||
| 45 | plist_data_t data = (plist_data_t) calloc(sizeof(struct plist_data_s), 1); | ||
| 46 | return data; | ||
| 47 | } | ||
| 48 | |||
| 49 | void plist_free_plist_data(plist_data_t data) | ||
| 50 | { | ||
| 51 | free(data); | ||
| 52 | } | ||
| 31 | 53 | ||
| 32 | void plist_new_dict(plist_t * plist) | 54 | void plist_new_dict(plist_t * plist) |
| 33 | { | 55 | { |
| 34 | if (*plist != NULL) | 56 | if (*plist != NULL) |
| 35 | return; | 57 | return; |
| 36 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | 58 | plist_data_t data = plist_new_plist_data(); |
| 37 | data->type = PLIST_DICT; | 59 | data->type = PLIST_DICT; |
| 38 | *plist = g_node_new(data); | 60 | *plist = plist_new_node(data); |
| 39 | } | 61 | } |
| 40 | 62 | ||
| 41 | void plist_new_array(plist_t * plist) | 63 | void plist_new_array(plist_t * plist) |
| 42 | { | 64 | { |
| 43 | if (*plist != NULL) | 65 | if (*plist != NULL) |
| 44 | return; | 66 | return; |
| 45 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | 67 | plist_data_t data = plist_new_plist_data(); |
| 46 | data->type = PLIST_ARRAY; | 68 | data->type = PLIST_ARRAY; |
| 47 | *plist = g_node_new(data); | 69 | *plist = plist_new_node(data); |
| 48 | } | 70 | } |
| 49 | 71 | ||
| 50 | void plist_new_dict_in_plist(plist_t plist, plist_t * dict) | 72 | void plist_new_dict_in_plist(plist_t plist, plist_t * dict) |
| @@ -52,9 +74,9 @@ void plist_new_dict_in_plist(plist_t plist, plist_t * dict) | |||
| 52 | if (!plist || *dict) | 74 | if (!plist || *dict) |
| 53 | return; | 75 | return; |
| 54 | 76 | ||
| 55 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | 77 | plist_data_t data = plist_new_plist_data(); |
| 56 | data->type = PLIST_DICT; | 78 | data->type = PLIST_DICT; |
| 57 | *dict = g_node_new(data); | 79 | *dict = plist_new_node(data); |
| 58 | g_node_append(plist, *dict); | 80 | g_node_append(plist, *dict); |
| 59 | } | 81 | } |
| 60 | 82 | ||
| @@ -72,14 +94,14 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu | |||
| 72 | if (!dict || !key || !value) | 94 | if (!dict || !key || !value) |
| 73 | return; | 95 | return; |
| 74 | 96 | ||
| 75 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | 97 | plist_data_t data = plist_new_plist_data(); |
| 76 | data->type = PLIST_KEY; | 98 | data->type = PLIST_KEY; |
| 77 | data->strval = strdup(key); | 99 | data->strval = strdup(key); |
| 78 | GNode *keynode = g_node_new(data); | 100 | plist_t keynode = plist_new_node(data); |
| 79 | g_node_append(dict, keynode); | 101 | g_node_append(dict, keynode); |
| 80 | 102 | ||
| 81 | //now handle value | 103 | //now handle value |
| 82 | struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | 104 | plist_data_t val = plist_new_plist_data(); |
| 83 | val->type = type; | 105 | val->type = type; |
| 84 | val->length = length; | 106 | val->length = length; |
| 85 | 107 | ||
| @@ -108,7 +130,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu | |||
| 108 | default: | 130 | default: |
| 109 | break; | 131 | break; |
| 110 | } | 132 | } |
| 111 | GNode *valnode = g_node_new(val); | 133 | plist_t valnode = plist_new_node(val); |
| 112 | g_node_append(dict, valnode); | 134 | g_node_append(dict, valnode); |
| 113 | } | 135 | } |
| 114 | 136 | ||
| @@ -117,24 +139,41 @@ void plist_free(plist_t plist) | |||
| 117 | g_node_destroy(plist); | 139 | g_node_destroy(plist); |
| 118 | } | 140 | } |
| 119 | 141 | ||
| 120 | plist_t find_query_node(plist_t plist, char *key, char *request) | 142 | plist_t plist_get_first_child(plist_t node) |
| 143 | { | ||
| 144 | return (plist_t)g_node_first_child( (GNode*)node ); | ||
| 145 | } | ||
| 146 | |||
| 147 | plist_t plist_get_next_sibling(plist_t node) | ||
| 148 | { | ||
| 149 | return (plist_t)g_node_next_sibling( (GNode*)node ); | ||
| 150 | } | ||
| 151 | |||
| 152 | plist_t plist_get_prev_sibling(plist_t node) | ||
| 153 | { | ||
| 154 | 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) | ||
| 121 | { | 158 | { |
| 122 | if (!plist) | 159 | if (!plist) |
| 123 | return NULL; | 160 | return NULL; |
| 124 | 161 | ||
| 125 | GNode *current = NULL; | 162 | plist_t current = NULL; |
| 126 | for (current = plist->children; current; current = current->next) { | 163 | plist_t next = NULL; |
| 164 | for (current = plist_get_first_child(plist); current; current = next) { | ||
| 127 | 165 | ||
| 128 | struct plist_data *data = (struct plist_data *) current->data; | 166 | next = plist_get_next_sibling(current); |
| 167 | plist_data_t data = plist_get_data(current); | ||
| 129 | 168 | ||
| 130 | if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) { | 169 | if (data->type == PLIST_KEY && !strcmp(data->strval, key) && next) { |
| 131 | 170 | ||
| 132 | data = (struct plist_data *) current->next->data; | 171 | data = plist_get_data(next); |
| 133 | if (data->type == PLIST_STRING && !strcmp(data->strval, request)) | 172 | if (data->type == PLIST_STRING && !strcmp(data->strval, request)) |
| 134 | return current->next; | 173 | return next; |
| 135 | } | 174 | } |
| 136 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | 175 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { |
| 137 | GNode *sub = find_query_node(current, key, request); | 176 | plist_t sub = plist_find_query_node(current, key, request); |
| 138 | if (sub) | 177 | if (sub) |
| 139 | return sub; | 178 | return sub; |
| 140 | } | 179 | } |
| @@ -142,7 +181,7 @@ plist_t find_query_node(plist_t plist, char *key, char *request) | |||
| 142 | return NULL; | 181 | return NULL; |
| 143 | } | 182 | } |
| 144 | 183 | ||
| 145 | char compare_node_value(plist_type type, struct plist_data *data, void *value) | 184 | char compare_node_value(plist_type type, plist_data_t data, void *value) |
| 146 | { | 185 | { |
| 147 | char res = FALSE; | 186 | char res = FALSE; |
| 148 | switch (type) { | 187 | switch (type) { |
| @@ -174,21 +213,21 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) | |||
| 174 | return res; | 213 | return res; |
| 175 | } | 214 | } |
| 176 | 215 | ||
| 177 | plist_t find_node(plist_t plist, plist_type type, void *value) | 216 | plist_t plist_find_node(plist_t plist, plist_type type, void *value) |
| 178 | { | 217 | { |
| 179 | if (!plist) | 218 | if (!plist) |
| 180 | return NULL; | 219 | return NULL; |
| 181 | 220 | ||
| 182 | GNode *current = NULL; | 221 | plist_t current = NULL; |
| 183 | for (current = plist->children; current; current = current->next) { | 222 | for (current = plist_get_first_child(plist); current; current = plist_get_next_sibling(current)) { |
| 184 | 223 | ||
| 185 | struct plist_data *data = (struct plist_data *) current->data; | 224 | plist_data_t data = plist_get_data(current); |
| 186 | 225 | ||
| 187 | if (data->type == type && compare_node_value(type, data, value)) { | 226 | if (data->type == type && compare_node_value(type, data, value)) { |
| 188 | return current; | 227 | return current; |
| 189 | } | 228 | } |
| 190 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | 229 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { |
| 191 | GNode *sub = find_node(current, type, value); | 230 | plist_t sub = plist_find_node(current, type, value); |
| 192 | if (sub) | 231 | if (sub) |
| 193 | return sub; | 232 | return sub; |
| 194 | } | 233 | } |
| @@ -196,12 +235,12 @@ plist_t find_node(plist_t plist, plist_type type, void *value) | |||
| 196 | return NULL; | 235 | return NULL; |
| 197 | } | 236 | } |
| 198 | 237 | ||
| 199 | void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length) | 238 | void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) |
| 200 | { | 239 | { |
| 201 | if (!node) | 240 | if (!node) |
| 202 | return; | 241 | return; |
| 203 | 242 | ||
| 204 | struct plist_data *data = (struct plist_data *) node->data; | 243 | plist_data_t data = plist_get_data(node); |
| 205 | 244 | ||
| 206 | *type = data->type; | 245 | *type = data->type; |
| 207 | *length = data->length; | 246 | *length = data->length; |
| @@ -236,16 +275,18 @@ void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * | |||
| 236 | 275 | ||
| 237 | plist_type plist_get_node_type(plist_t node) | 276 | plist_type plist_get_node_type(plist_t node) |
| 238 | { | 277 | { |
| 239 | if (node && node->data) | 278 | if (node) { |
| 240 | return ((struct plist_data *) node->data)->type; | 279 | plist_data_t data = plist_get_data(node); |
| 241 | else | 280 | if (data) |
| 242 | return PLIST_NONE; | 281 | return data->type; |
| 282 | } | ||
| 283 | return PLIST_NONE; | ||
| 243 | } | 284 | } |
| 244 | 285 | ||
| 245 | uint64_t plist_get_node_uint_val(plist_t node) | 286 | uint64_t plist_get_node_uint_val(plist_t node) |
| 246 | { | 287 | { |
| 247 | if (PLIST_UINT == plist_get_node_type(node)) | 288 | if (PLIST_UINT == plist_get_node_type(node)) |
| 248 | return ((struct plist_data *) node->data)->intval; | 289 | return plist_get_data(node)->intval; |
| 249 | else | 290 | else |
| 250 | return 0; | 291 | return 0; |
| 251 | } | 292 | } |
