diff options
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 251 |
1 files changed, 0 insertions, 251 deletions
diff --git a/src/plist.c b/src/plist.c deleted file mode 100644 index 932ea5e..0000000 --- a/src/plist.c +++ /dev/null | |||
| @@ -1,251 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * plist.c | ||
| 3 | * Builds plist XML structures. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Zach C. All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | |||
| 23 | #include <string.h> | ||
| 24 | #include <assert.h> | ||
| 25 | #include "utils.h" | ||
| 26 | #include "plist.h" | ||
| 27 | #include <wchar.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | |||
| 31 | |||
| 32 | void plist_new_dict(plist_t * plist) | ||
| 33 | { | ||
| 34 | if (*plist != NULL) | ||
| 35 | return; | ||
| 36 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | ||
| 37 | data->type = PLIST_DICT; | ||
| 38 | *plist = g_node_new(data); | ||
| 39 | } | ||
| 40 | |||
| 41 | void plist_new_array(plist_t * plist) | ||
| 42 | { | ||
| 43 | if (*plist != NULL) | ||
| 44 | return; | ||
| 45 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | ||
| 46 | data->type = PLIST_ARRAY; | ||
| 47 | *plist = g_node_new(data); | ||
| 48 | } | ||
| 49 | |||
| 50 | void plist_new_dict_in_plist(plist_t plist, plist_t * dict) | ||
| 51 | { | ||
| 52 | if (!plist || *dict) | ||
| 53 | return; | ||
| 54 | |||
| 55 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | ||
| 56 | data->type = PLIST_DICT; | ||
| 57 | *dict = g_node_new(data); | ||
| 58 | g_node_append(plist, *dict); | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | /** Adds a new key pair to a dict. | ||
| 63 | * | ||
| 64 | * @param dict The dict node in the plist. | ||
| 65 | * @param key the key name of the key pair. | ||
| 66 | * @param type The the type of the value in the key pair. | ||
| 67 | * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value | ||
| 68 | * | ||
| 69 | */ | ||
| 70 | void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length) | ||
| 71 | { | ||
| 72 | if (!dict || !key || !value) | ||
| 73 | return; | ||
| 74 | |||
| 75 | struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | ||
| 76 | data->type = PLIST_KEY; | ||
| 77 | data->strval = strdup(key); | ||
| 78 | GNode *keynode = g_node_new(data); | ||
| 79 | g_node_append(dict, keynode); | ||
| 80 | |||
| 81 | //now handle value | ||
| 82 | struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); | ||
| 83 | val->type = type; | ||
| 84 | val->length = length; | ||
| 85 | |||
| 86 | switch (type) { | ||
| 87 | case PLIST_BOOLEAN: | ||
| 88 | val->boolval = *((char *) value); | ||
| 89 | break; | ||
| 90 | case PLIST_UINT: | ||
| 91 | val->intval = *((uint64_t *) value); | ||
| 92 | break; | ||
| 93 | case PLIST_REAL: | ||
| 94 | val->realval = *((double *) value); | ||
| 95 | break; | ||
| 96 | case PLIST_STRING: | ||
| 97 | val->strval = strdup((char *) value); | ||
| 98 | break; | ||
| 99 | case PLIST_UNICODE: | ||
| 100 | val->unicodeval = wcsdup((wchar_t *) value); | ||
| 101 | break; | ||
| 102 | case PLIST_DATA: | ||
| 103 | memcpy(val->buff, value, length); | ||
| 104 | break; | ||
| 105 | case PLIST_ARRAY: | ||
| 106 | case PLIST_DICT: | ||
| 107 | case PLIST_DATE: | ||
| 108 | default: | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | GNode *valnode = g_node_new(val); | ||
| 112 | g_node_append(dict, valnode); | ||
| 113 | } | ||
| 114 | |||
| 115 | void plist_free(plist_t plist) | ||
| 116 | { | ||
| 117 | g_node_destroy(plist); | ||
| 118 | } | ||
| 119 | |||
| 120 | plist_t find_query_node(plist_t plist, char *key, char *request) | ||
| 121 | { | ||
| 122 | if (!plist) | ||
| 123 | return NULL; | ||
| 124 | |||
| 125 | GNode *current = NULL; | ||
| 126 | for (current = plist->children; current; current = current->next) { | ||
| 127 | |||
| 128 | struct plist_data *data = (struct plist_data *) current->data; | ||
| 129 | |||
| 130 | if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) { | ||
| 131 | |||
| 132 | data = (struct plist_data *) current->next->data; | ||
| 133 | if (data->type == PLIST_STRING && !strcmp(data->strval, request)) | ||
| 134 | return current->next; | ||
| 135 | } | ||
| 136 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | ||
| 137 | GNode *sub = find_query_node(current, key, request); | ||
| 138 | if (sub) | ||
| 139 | return sub; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | return NULL; | ||
| 143 | } | ||
| 144 | |||
| 145 | char compare_node_value(plist_type type, struct plist_data *data, void *value) | ||
| 146 | { | ||
| 147 | char res = FALSE; | ||
| 148 | switch (type) { | ||
| 149 | case PLIST_BOOLEAN: | ||
| 150 | res = data->boolval == *((char *) value) ? TRUE : FALSE; | ||
| 151 | break; | ||
| 152 | case PLIST_UINT: | ||
| 153 | res = data->intval == *((uint64_t *) value) ? TRUE : FALSE; | ||
| 154 | break; | ||
| 155 | case PLIST_REAL: | ||
| 156 | res = data->realval == *((double *) value) ? TRUE : FALSE; | ||
| 157 | break; | ||
| 158 | case PLIST_KEY: | ||
| 159 | case PLIST_STRING: | ||
| 160 | res = !strcmp(data->strval, ((char *) value)); | ||
| 161 | break; | ||
| 162 | case PLIST_UNICODE: | ||
| 163 | res = !wcscmp(data->unicodeval, ((wchar_t *) value)); | ||
| 164 | break; | ||
| 165 | case PLIST_DATA: | ||
| 166 | res = !strcmp(data->buff, ((char *) value)); | ||
| 167 | break; | ||
| 168 | case PLIST_ARRAY: | ||
| 169 | case PLIST_DICT: | ||
| 170 | case PLIST_DATE: | ||
| 171 | default: | ||
| 172 | break; | ||
| 173 | } | ||
| 174 | return res; | ||
| 175 | } | ||
| 176 | |||
| 177 | plist_t find_node(plist_t plist, plist_type type, void *value) | ||
| 178 | { | ||
| 179 | if (!plist) | ||
| 180 | return NULL; | ||
| 181 | |||
| 182 | GNode *current = NULL; | ||
| 183 | for (current = plist->children; current; current = current->next) { | ||
| 184 | |||
| 185 | struct plist_data *data = (struct plist_data *) current->data; | ||
| 186 | |||
| 187 | if (data->type == type && compare_node_value(type, data, value)) { | ||
| 188 | return current; | ||
| 189 | } | ||
| 190 | if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { | ||
| 191 | GNode *sub = find_node(current, type, value); | ||
| 192 | if (sub) | ||
| 193 | return sub; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | return NULL; | ||
| 197 | } | ||
| 198 | |||
| 199 | void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length) | ||
| 200 | { | ||
| 201 | if (!node) | ||
| 202 | return; | ||
| 203 | |||
| 204 | struct plist_data *data = (struct plist_data *) node->data; | ||
| 205 | |||
| 206 | *type = data->type; | ||
| 207 | *length = data->length; | ||
| 208 | |||
| 209 | switch (*type) { | ||
| 210 | case PLIST_BOOLEAN: | ||
| 211 | *((char *) value) = data->boolval; | ||
| 212 | break; | ||
| 213 | case PLIST_UINT: | ||
| 214 | *((uint64_t *) value) = data->intval; | ||
| 215 | break; | ||
| 216 | case PLIST_REAL: | ||
| 217 | *((double *) value) = data->realval; | ||
| 218 | break; | ||
| 219 | case PLIST_STRING: | ||
| 220 | *((char **) value) = strdup(data->strval); | ||
| 221 | break; | ||
| 222 | case PLIST_UNICODE: | ||
| 223 | *((wchar_t **) value) = wcsdup(data->unicodeval); | ||
| 224 | break; | ||
| 225 | case PLIST_KEY: | ||
| 226 | *((char **) value) = strdup(data->strval); | ||
| 227 | break; | ||
| 228 | case PLIST_DATA: | ||
| 229 | case PLIST_ARRAY: | ||
| 230 | case PLIST_DICT: | ||
| 231 | case PLIST_DATE: | ||
| 232 | default: | ||
| 233 | break; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | plist_type plist_get_node_type(plist_t node) | ||
| 238 | { | ||
| 239 | if (node && node->data) | ||
| 240 | return ((struct plist_data *) node->data)->type; | ||
| 241 | else | ||
| 242 | return PLIST_NONE; | ||
| 243 | } | ||
| 244 | |||
| 245 | uint64_t plist_get_node_uint_val(plist_t node) | ||
| 246 | { | ||
| 247 | if (PLIST_UINT == plist_get_node_type(node)) | ||
| 248 | return ((struct plist_data *) node->data)->intval; | ||
| 249 | else | ||
| 250 | return 0; | ||
| 251 | } | ||
