summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-07-08 19:30:53 +0200
committerGravatar Jonathan Beck2009-07-08 19:30:53 +0200
commitc8a140dce1d160c1766d3e0ac1346900efc193f3 (patch)
tree120f29a6fb0fb86b10df3f95a028b2ff12bc8126 /src/plist.c
parente925e6f61752fbdf9304c4f62bad3d21dc881a32 (diff)
downloadlibplist-c8a140dce1d160c1766d3e0ac1346900efc193f3.tar.gz
libplist-c8a140dce1d160c1766d3e0ac1346900efc193f3.tar.bz2
Add a deep copy function and value setters for nodes.
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c119
1 files changed, 116 insertions, 3 deletions
diff --git a/src/plist.c b/src/plist.c
index 7949bce..e43d50d 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -109,7 +109,7 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *
109 data->strval = strdup((char *) value); 109 data->strval = strdup((char *) value);
110 break; 110 break;
111 case PLIST_DATA: 111 case PLIST_DATA:
112 data->buff = (uint8_t*)malloc(length); 112 data->buff = (uint8_t *) malloc(length);
113 memcpy(data->buff, value, length); 113 memcpy(data->buff, value, length);
114 break; 114 break;
115 case PLIST_DATE: 115 case PLIST_DATE:
@@ -138,6 +138,28 @@ void plist_free(plist_t plist)
138 g_node_destroy(plist); 138 g_node_destroy(plist);
139} 139}
140 140
141static void plist_copy_node(GNode * node, gpointer parent_node)
142{
143 plist_t newnode = NULL;
144 plist_data_t data = plist_get_data(node);
145 plist_data_t newdata = plist_new_plist_data();
146
147 assert(data); // plist should always have data
148
149 memcpy(newdata, data, sizeof(struct plist_data_s));
150 newnode = plist_new_node(newdata);
151
152 if (parent_node) {
153 g_node_append(parent_node, newnode);
154 }
155 g_node_children_foreach(node, G_TRAVERSE_ALL, plist_copy_node, newnode);
156}
157
158plist_t plist_copy(plist_t node)
159{
160 plist_copy_node(node, NULL);
161}
162
141plist_t plist_get_first_child(plist_t node) 163plist_t plist_get_first_child(plist_t node)
142{ 164{
143 return (plist_t) g_node_first_child((GNode *) node); 165 return (plist_t) g_node_first_child((GNode *) node);
@@ -160,7 +182,7 @@ plist_t plist_get_array_nth_el(plist_t node, uint32_t n)
160 uint32_t i = 0; 182 uint32_t i = 0;
161 plist_t temp = plist_get_first_child(node); 183 plist_t temp = plist_get_first_child(node);
162 184
163 while ( i <= n && temp) { 185 while (i <= n && temp) {
164 if (i == n) 186 if (i == n)
165 ret = temp; 187 ret = temp;
166 temp = plist_get_next_sibling(temp); 188 temp = plist_get_next_sibling(temp);
@@ -465,5 +487,96 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
465 487
466char plist_compare_node_value(plist_t node_l, plist_t node_r) 488char plist_compare_node_value(plist_t node_l, plist_t node_r)
467{ 489{
468 return plist_data_compare( node_l , node_r ); 490 return plist_data_compare(node_l, node_r);
491}
492
493static plist_t plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length)
494{
495 //free previous allocated buffer
496 plist_data_t data = plist_get_data(node);
497 assert(data); // a node should always have data attached
498
499 switch (data->type) {
500 case PLIST_KEY:
501 case PLIST_STRING:
502 free(data->strval);
503 data->strval = NULL;
504 break;
505 case PLIST_DATA:
506 free(data->buff);
507 data->buff = NULL;
508 break;
509 default:
510 break;
511 }
512
513 //now handle value
514
515 data->type = type;
516 data->length = length;
517
518 switch (type) {
519 case PLIST_BOOLEAN:
520 data->boolval = *((char *) value);
521 break;
522 case PLIST_UINT:
523 data->intval = *((uint64_t *) value);
524 break;
525 case PLIST_REAL:
526 data->realval = *((double *) value);
527 break;
528 case PLIST_KEY:
529 case PLIST_STRING:
530 data->strval = strdup((char *) value);
531 break;
532 case PLIST_DATA:
533 data->buff = (uint8_t *) malloc(length);
534 memcpy(data->buff, value, length);
535 break;
536 case PLIST_DATE:
537 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
538 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
539 break;
540 case PLIST_ARRAY:
541 case PLIST_DICT:
542 default:
543 break;
544 }
545}
546
547
548void plist_set_key_val(plist_t node, const char *val)
549{
550 plist_set_element_val(node, PLIST_KEY, val, strlen(val));
551}
552
553void plist_set_string_val(plist_t node, const char *val)
554{
555 plist_set_element_val(node, PLIST_STRING, val, strlen(val));
556}
557
558void plist_set_bool_val(plist_t node, uint8_t val)
559{
560 plist_set_element_val(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
561}
562
563void plist_set_uint_val(plist_t node, uint64_t val)
564{
565 plist_set_element_val(node, PLIST_UINT, &val, sizeof(uint64_t));
566}
567
568void plist_set_real_val(plist_t node, double val)
569{
570 plist_set_element_val(node, PLIST_REAL, &val, sizeof(double));
571}
572
573void plist_set_data_val(plist_t node, const char *val, uint64_t length)
574{
575 plist_set_element_val(node, PLIST_DATA, val, length);
576}
577
578void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
579{
580 GTimeVal val = { sec, usec };
581 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal));
469} 582}