summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c173
1 files changed, 134 insertions, 39 deletions
diff --git a/src/plist.c b/src/plist.c
index d737ab8..640bbe5 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -74,46 +74,50 @@ plist_t plist_new_array()
74 74
75plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length) 75plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length)
76{ 76{
77 //only structured types are allowed to have nulll value 77 //only structured types can have children
78 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 78 plist_type node_type = plist_get_node_type(node);
79 //now handle value 79 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) {
80 plist_data_t data = plist_new_plist_data(); 80 //only structured types are allowed to have nulll value
81 data->type = type; 81 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) {
82 data->length = length; 82 //now handle value
83 83 plist_data_t data = plist_new_plist_data();
84 switch (type) { 84 data->type = type;
85 case PLIST_BOOLEAN: 85 data->length = length;
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_KEY:
95 case PLIST_STRING:
96 data->strval = strdup((char *) value);
97 break;
98 case PLIST_UNICODE:
99 data->unicodeval = wcsdup((wchar_t *) value);
100 break;
101 case PLIST_DATA:
102 memcpy(data->buff, value, length);
103 break;
104 case PLIST_ARRAY:
105 case PLIST_DICT:
106 case PLIST_DATE:
107 default:
108 break;
109 }
110 86
111 plist_t subnode = plist_new_node(data); 87 switch (type) {
112 if (node) 88 case PLIST_BOOLEAN:
113 g_node_append(node, subnode); 89 data->boolval = *((char *) value);
114 return subnode; 90 break;
115 } else 91 case PLIST_UINT:
116 return NULL; 92 data->intval = *((uint64_t *) value);
93 break;
94 case PLIST_REAL:
95 data->realval = *((double *) value);
96 break;
97 case PLIST_KEY:
98 case PLIST_STRING:
99 data->strval = strdup((char *) value);
100 break;
101 case PLIST_UNICODE:
102 data->unicodeval = wcsdup((wchar_t *) value);
103 break;
104 case PLIST_DATA:
105 memcpy(data->buff, value, length);
106 break;
107 case PLIST_ARRAY:
108 case PLIST_DICT:
109 case PLIST_DATE:
110 default:
111 break;
112 }
113
114 plist_t subnode = plist_new_node(data);
115 if (node)
116 g_node_append(node, subnode);
117 return subnode;
118 } else
119 return NULL;
120 }
117} 121}
118 122
119void plist_free(plist_t plist) 123void plist_free(plist_t plist)
@@ -243,3 +247,94 @@ uint64_t plist_get_node_uint_val(plist_t node)
243 else 247 else
244 return 0; 248 return 0;
245} 249}
250
251void plist_add_sub_node(plist_t node, plist_t subnode)
252{
253 if (node && subnode) {
254 plist_type type = plist_get_node_type(node);
255 if (type == PLIST_DICT || type == PLIST_ARRAY)
256 g_node_append(node, subnode);
257 }
258}
259
260void plist_add_sub_key_el(plist_t node, char *val)
261{
262 plist_add_sub_element(node, PLIST_KEY, val, strlen(val));
263}
264
265void plist_add_sub_string_el(plist_t node, char *val)
266{
267 plist_add_sub_element(node, PLIST_STRING, val, strlen(val));
268}
269
270void plist_add_sub_bool_el(plist_t node, uint8_t val)
271{
272 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
273}
274
275void plist_add_sub_uint_el(plist_t node, uint64_t val)
276{
277 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t));
278}
279
280void plist_add_sub_real_el(plist_t node, double val)
281{
282 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double));
283}
284
285void plist_add_sub_data_el(plist_t node, char *val, uint64_t length)
286{
287 plist_add_sub_element(node, PLIST_DATA, val, length);
288}
289
290void plist_get_key_val(plist_t node, char **val)
291{
292 plist_type type = plist_get_node_type(node);
293 uint64_t length = 0;
294 if (PLIST_KEY == type)
295 plist_get_type_and_value(node, &type, (void *) val, &length);
296 assert(length == strlen(*val));
297}
298
299void plist_get_string_val(plist_t node, char **val)
300{
301 plist_type type = plist_get_node_type(node);
302 uint64_t length = 0;
303 if (PLIST_STRING == type)
304 plist_get_type_and_value(node, &type, (void *) val, &length);
305 assert(length == strlen(*val));
306}
307
308void plist_get_bool_val(plist_t node, uint8_t * val)
309{
310 plist_type type = plist_get_node_type(node);
311 uint64_t length = 0;
312 if (PLIST_BOOLEAN == type)
313 plist_get_type_and_value(node, &type, (void *) val, &length);
314 assert(length == sizeof(uint8_t));
315}
316
317void plist_get_uint_val(plist_t node, uint64_t * val)
318{
319 plist_type type = plist_get_node_type(node);
320 uint64_t length = 0;
321 if (PLIST_UINT == type)
322 plist_get_type_and_value(node, &type, (void *) val, &length);
323 assert(length == sizeof(uint64_t));
324}
325
326void plist_get_real_val(plist_t node, double *val)
327{
328 plist_type type = plist_get_node_type(node);
329 uint64_t length = 0;
330 if (PLIST_REAL == type)
331 plist_get_type_and_value(node, &type, (void *) val, &length);
332 assert(length == sizeof(double));
333}
334
335void plist_get_data_val(plist_t node, char **val, uint64_t * length)
336{
337 plist_type type = plist_get_node_type(node);
338 if (PLIST_UINT == type)
339 plist_get_type_and_value(node, &type, (void *) val, length);
340}