summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-10 18:20:01 +0200
committerGravatar Jonathan Beck2009-10-10 18:20:01 +0200
commit77b02c9404dbfef325b7a19228045a817cafe064 (patch)
tree1d571bb5faf3ba49a7d1f0e7cdf8195b271d728d
parentc18c9d087860e812431799fae406144928dd46c8 (diff)
downloadlibplist-77b02c9404dbfef325b7a19228045a817cafe064.tar.gz
libplist-77b02c9404dbfef325b7a19228045a817cafe064.tar.bz2
Rework public API to make it more consistent.
-rw-r--r--include/plist/plist.h345
-rw-r--r--src/plist.c419
2 files changed, 565 insertions, 199 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h
index 6388603..f8c5c53 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -65,13 +65,11 @@ extern "C" {
65 * The enumeration of plist node types. 65 * The enumeration of plist node types.
66 */ 66 */
67 typedef enum { 67 typedef enum {
68 PLIST_BOOLEAN, 68 PLIST_BOOLEAN, /**< Boolean, scalar type */
69 /**< Boolean, scalar type */
70 PLIST_UINT, /**< Unsigned integer, scalar type */ 69 PLIST_UINT, /**< Unsigned integer, scalar type */
71 PLIST_REAL, /**< Real, scalar type */ 70 PLIST_REAL, /**< Real, scalar type */
72 PLIST_STRING, 71 PLIST_STRING, /**< ASCII string, scalar type */
73 /**< ASCII string, scalar type */ 72 PLIST_ARRAY, /**< Ordered array, structured type */
74 PLIST_ARRAY,/**< Ordered array, structured type */
75 PLIST_DICT, /**< Unordered dictionary (key/value pair), structured type */ 73 PLIST_DICT, /**< Unordered dictionary (key/value pair), structured type */
76 PLIST_DATE, /**< Date, scalar type */ 74 PLIST_DATE, /**< Date, scalar type */
77 PLIST_DATA, /**< Binary data, scalar type */ 75 PLIST_DATA, /**< Binary data, scalar type */
@@ -103,6 +101,62 @@ extern "C" {
103 PLIST_API plist_t plist_new_array(void); 101 PLIST_API plist_t plist_new_array(void);
104 102
105/** 103/**
104 * Create a new plist_t type #PLIST_STRING
105 *
106 * @param val the sting value, encoded in UTF8.
107 * @return the created item
108 * @sa #plist_type
109 */
110 PLIST_API plist_t plist_new_string(const char *val);
111
112/**
113 * Create a new plist_t type #PLIST_BOOLEAN
114 *
115 * @param val the boolean value, 0 is false, other values are true.
116 * @return the created item
117 * @sa #plist_type
118 */
119 PLIST_API plist_t plist_new_bool(uint8_t val);
120
121/**
122 * Create a new plist_t type #PLIST_UINT
123 *
124 * @param val the unsigned integer value
125 * @return the created item
126 * @sa #plist_type
127 */
128 PLIST_API plist_t plist_new_uint(uint64_t val);
129
130/**
131 * Create a new plist_t type #PLIST_REAL
132 *
133 * @param val the real value
134 * @return the created item
135 * @sa #plist_type
136 */
137 PLIST_API plist_t plist_new_real(double val);
138
139/**
140 * Create a new plist_t type #PLIST_DATA
141 *
142 * @param val the binary buffer
143 * @param length the length of the buffer
144 * @return the created item
145 * @sa #plist_type
146 */
147 PLIST_API plist_t plist_new_data(const char *val, uint64_t length);
148
149/**
150 * Create a new plist_t type #PLIST_DATE
151 *
152 * @param sec the number of seconds since 01/01/2001
153 * @param usec the number of microseconds
154 * @return the created item
155 * @sa #plist_type
156 */
157 PLIST_API plist_t plist_new_date(int32_t sec, int32_t usec);
158
159/**
106 * Destruct a plist_t node and all its children recursively 160 * Destruct a plist_t node and all its children recursively
107 * 161 *
108 * @param plist the plist to free 162 * @param plist the plist to free
@@ -120,139 +174,113 @@ extern "C" {
120 174
121/******************************************** 175/********************************************
122 * * 176 * *
123 * Tree navigation * 177 * Array functions *
124 * * 178 * *
125 ********************************************/ 179 ********************************************/
126 180
127/** 181/**
128 * Get the first child of a node 182 * Get size of a #PLIST_ARRAY node.
129 * 183 *
130 * @param node the first child 184 * @param node the node of type #PLIST_ARRAY
185 * @return size of the #PLIST_ARRAY node
131 */ 186 */
132 PLIST_API plist_t plist_get_first_child(plist_t node); 187 PLIST_API uint32_t plist_array_get_size(plist_t node);
133
134 188
135/** 189/**
136 * Get the next sibling of a node 190 * Get the nth item in a #PLIST_ARRAY node.
137 * 191 *
138 * @param node the next sibling 192 * @param node the node of type #PLIST_ARRAY
193 * @param n the index of the item to get. Range is [0, array_size[
194 * @return the nth item or NULL if node is not of type #PLIST_ARRAY
139 */ 195 */
140 PLIST_API plist_t plist_get_next_sibling(plist_t node); 196 PLIST_API plist_t plist_array_get_item(plist_t node, uint32_t n);
141
142 197
143/** 198/**
144 * Get the previous sibling of a node 199 * Set the nth item in a #PLIST_ARRAY node.
200 * The previous item at index n will be freed using #plist_free
145 * 201 *
146 * @param node the previous sibling 202 * @param node the node of type #PLIST_ARRAY
203 * @param item the new item at index n
204 * @param n the index of the item to get. Range is [0, array_size[. Assert if n is not in range.
147 */ 205 */
148 PLIST_API plist_t plist_get_prev_sibling(plist_t node); 206 PLIST_API void plist_array_set_item(plist_t node, plist_t item, uint32_t n);
149 207
150/** 208/**
151 * Get the parent of a node 209 * Append a new item at the end of a #PLIST_ARRAY node.
152 * 210 *
153 * @param node the parent (NULL if node is root) 211 * @param node the node of type #PLIST_ARRAY
212 * @param item the new item
154 */ 213 */
155 PLIST_API plist_t plist_get_parent(plist_t node); 214 PLIST_API void plist_array_append_item(plist_t node, plist_t item);
156 215
157/** 216/**
158 * Get the nth child of a #PLIST_ARRAY node. 217 * Insert a new item at position n in a #PLIST_ARRAY node.
159 * 218 *
160 * @param node the node of type #PLIST_ARRAY 219 * @param node the node of type #PLIST_ARRAY
161 * @param n the index of the child to get. Range is [0, array_size[ 220 * @param item the new item to insert
162 * @return the nth children or NULL if node is not of type #PLIST_ARRAY 221 * @param n The position at which the node will be stored. Range is [0, array_size[. Assert if n is not in range.
163 */ 222 */
164 PLIST_API plist_t plist_get_array_nth_el(plist_t node, uint32_t n); 223 PLIST_API void plist_array_insert_item(plist_t node, plist_t item, uint32_t n);
165 224
166/** 225/**
167 * Get the child of a #PLIST_DICT node from the associated key value. 226 * Remove an existing position in a #PLIST_ARRAY node.
227 * Removed position will be freed using #plist_free
168 * 228 *
169 * @param node the node of type #PLIST_DICT 229 * @param node the node of type #PLIST_ARRAY
170 * @param key the key associated to the requested value 230 * @param n The position to remove. Range is [0, array_size[. Assert if n is not in range.
171 * @return the key associated value or NULL if node is not of type #PLIST_DICT
172 */ 231 */
173 PLIST_API plist_t plist_get_dict_el_from_key(plist_t node, const char *key); 232 PLIST_API void plist_array_remove_item(plist_t node, uint32_t n);
174
175 233
176/******************************************** 234/********************************************
177 * * 235 * *
178 * Setters * 236 * Dictionary functions *
179 * * 237 * *
180 ********************************************/ 238 ********************************************/
181 239
182/** 240/**
183 * Add a subnode to a node. The node must be of a structured type 241 * Get size of a #PLIST_DICT node.
184 * (ie #PLIST_DICT or #PLIST_ARRAY). This function fails silently
185 * if subnode already has a father.
186 *
187 * @param node the node to add a children to
188 * @param subnode the children node
189 */
190 PLIST_API void plist_add_sub_node(plist_t node, plist_t subnode);
191
192/**
193 * Add a subnode of type #PLIST_KEY to a node. The node must be of a structured type
194 * (ie #PLIST_DICT or #PLIST_ARRAY).
195 *
196 * @param node the node to add a children to
197 * @param val the key value encoded as an ASCII string (must be null terminated)
198 */
199 PLIST_API void plist_add_sub_key_el(plist_t node, const char *val);
200
201/**
202 * Add a subnode of type #PLIST_STRING to a node. The node must be of a structured type
203 * (ie #PLIST_DICT or #PLIST_ARRAY).
204 * 242 *
205 * @param node the node to add a children to 243 * @param node the node of type #PLIST_DICT
206 * @param val the string value encoded as an ASCII or UTF-8 string (must be null terminated) 244 * @return size of the #PLIST_DICT node
207 */ 245 */
208 PLIST_API void plist_add_sub_string_el(plist_t node, const char *val); 246 PLIST_API uint32_t plist_dict_get_size(plist_t node);
209 247
210/** 248/**
211 * Add a subnode of type #PLIST_BOOLEAN to a node. The node must be of a structured type 249 * Get the nth item in a #PLIST_DICT node.
212 * (ie #PLIST_DICT or #PLIST_ARRAY).
213 * 250 *
214 * @param node the node to add a children to 251 * @param node the node of type #PLIST_DICT
215 * @param val the boolean value (TRUE or FALSE) 252 * @param key the identifier of the item to get.
216 */ 253 * @return the item or NULL if node is not of type #PLIST_DICT
217 PLIST_API void plist_add_sub_bool_el(plist_t node, uint8_t val);
218
219/**
220 * Add a subnode of type #PLIST_UINT to a node. The node must be of a structured type
221 * (ie #PLIST_DICT or #PLIST_ARRAY).
222 *
223 * @param node the node to add a children to
224 * @param val the unsigned integer value
225 */ 254 */
226 PLIST_API void plist_add_sub_uint_el(plist_t node, uint64_t val); 255 PLIST_API plist_t plist_dict_get_item(plist_t node, const char* key);
227 256
228/** 257/**
229 * Add a subnode of type #PLIST_REAL to a node. The node must be of a structured type 258 * Set item identified by key in a #PLIST_DICT node.
230 * (ie #PLIST_DICT or #PLIST_ARRAY). 259 * The previous item at index n will be freed using #plist_free
231 * 260 *
232 * @param node the node to add a children to 261 * @param node the node of type #PLIST_DICT
233 * @param val the real value 262 * @param item the new item associated to key
263 * @param key the identifier of the item to get. Assert if identifier is not present.
234 */ 264 */
235 PLIST_API void plist_add_sub_real_el(plist_t node, double val); 265 PLIST_API void plist_dict_set_item(plist_t node, plist_t item, const char* key);
236 266
237/** 267/**
238 * Add a subnode of type #PLIST_DATA to a node. The node must be of a structured type 268 * Insert a new item at position n in a #PLIST_DICT node.
239 * (ie #PLIST_DICT or #PLIST_ARRAY).
240 * 269 *
241 * @param node the node to add a children to 270 * @param node the node of type #PLIST_DICT
242 * @param val the binary buffer 271 * @param item the new item to insert
243 * @param length the length of the buffer 272 * @param key The identifier of the item to insert. Assert if identifier already present.
244 */ 273 */
245 PLIST_API void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length); 274 PLIST_API void plist_dict_insert_item(plist_t node, plist_t item, const char* key);
246 275
247/** 276/**
248 * Add a subnode of type #PLIST_DATE to a node. The node must be of a structured type 277 * Remove an existing position in a #PLIST_DICT node.
249 * (ie #PLIST_DICT or #PLIST_ARRAY). 278 * Removed position will be freed using #plist_free
250 * 279 *
251 * @param node the node to add a children to 280 * @param node the node of type #PLIST_DICT
252 * @param sec the number of seconds since 01/01/2001 281 * @param key The identifier of the item to remove. Assert if identifier is not present.
253 * @param usec the number of microseconds
254 */ 282 */
255 PLIST_API void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec); 283 PLIST_API void plist_dict_remove_item(plist_t node, const char* key);
256 284
257 285
258/******************************************** 286/********************************************
@@ -262,6 +290,13 @@ extern "C" {
262 ********************************************/ 290 ********************************************/
263 291
264/** 292/**
293 * Get the parent of a node
294 *
295 * @param node the parent (NULL if node is root)
296 */
297 PLIST_API plist_t plist_get_parent(plist_t node);
298
299/**
265 * Get the #plist_type of a node. 300 * Get the #plist_type of a node.
266 * 301 *
267 * @param node the node 302 * @param node the node
@@ -454,7 +489,6 @@ extern "C" {
454 PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist); 489 PLIST_API void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist);
455 490
456 491
457
458/******************************************** 492/********************************************
459 * * 493 * *
460 * Utils * 494 * Utils *
@@ -488,6 +522,143 @@ extern "C" {
488 */ 522 */
489 PLIST_API char plist_compare_node_value(plist_t node_l, plist_t node_r); 523 PLIST_API char plist_compare_node_value(plist_t node_l, plist_t node_r);
490 524
525
526//DEPRECATED API BELOW
527
528/*@}*/
529/**
530 * \defgroup DeprecatedAPI Deprecated libplist API
531 */
532/*@{*/
533
534/********************************************
535 * *
536 * Tree navigation *
537 * *
538 ********************************************/
539
540/**
541 * Get the first child of a node
542 *
543 * @param node the first child
544 */
545 PLIST_API plist_t plist_get_first_child(plist_t node);
546
547/**
548 * Get the next sibling of a node
549 *
550 * @param node the next sibling
551 */
552 PLIST_API plist_t plist_get_next_sibling(plist_t node);
553
554/**
555 * Get the previous sibling of a node
556 *
557 * @param node the previous sibling
558 */
559 PLIST_API plist_t plist_get_prev_sibling(plist_t node);
560
561/**
562 * Get the nth child of a #PLIST_ARRAY node.
563 *
564 * @param node the node of type #PLIST_ARRAY
565 * @param n the index of the child to get. Range is [0, array_size[
566 * @return the nth children or NULL if node is not of type #PLIST_ARRAY
567 */
568 PLIST_API plist_t plist_get_array_nth_el(plist_t node, uint32_t n);
569
570/**
571 * Get the child of a #PLIST_DICT node from the associated key value.
572 *
573 * @param node the node of type #PLIST_DICT
574 * @param key the key associated to the requested value
575 * @return the key associated value or NULL if node is not of type #PLIST_DICT
576 */
577 PLIST_API plist_t plist_get_dict_el_from_key(plist_t node, const char *key);
578
579
580/********************************************
581 * *
582 * Setters *
583 * *
584 ********************************************/
585
586/**
587 * Add a subnode to a node. The node must be of a structured type
588 * (ie #PLIST_DICT or #PLIST_ARRAY). This function fails silently
589 * if subnode already has a father.
590 *
591 * @param node the node to add a children to
592 * @param subnode the children node
593 */
594 PLIST_API void plist_add_sub_node(plist_t node, plist_t subnode);
595
596/**
597 * Add a subnode of type #PLIST_KEY to a node. The node must be of a structured type
598 * (ie #PLIST_DICT or #PLIST_ARRAY).
599 *
600 * @param node the node to add a children to
601 * @param val the key value encoded as an ASCII string (must be null terminated)
602 */
603 PLIST_API void plist_add_sub_key_el(plist_t node, const char *val);
604
605/**
606 * Add a subnode of type #PLIST_STRING to a node. The node must be of a structured type
607 * (ie #PLIST_DICT or #PLIST_ARRAY).
608 *
609 * @param node the node to add a children to
610 * @param val the string value encoded as an ASCII or UTF-8 string (must be null terminated)
611 */
612 PLIST_API void plist_add_sub_string_el(plist_t node, const char *val);
613
614/**
615 * Add a subnode of type #PLIST_BOOLEAN to a node. The node must be of a structured type
616 * (ie #PLIST_DICT or #PLIST_ARRAY).
617 *
618 * @param node the node to add a children to
619 * @param val the boolean value (TRUE or FALSE)
620 */
621 PLIST_API void plist_add_sub_bool_el(plist_t node, uint8_t val);
622
623/**
624 * Add a subnode of type #PLIST_UINT to a node. The node must be of a structured type
625 * (ie #PLIST_DICT or #PLIST_ARRAY).
626 *
627 * @param node the node to add a children to
628 * @param val the unsigned integer value
629 */
630 PLIST_API void plist_add_sub_uint_el(plist_t node, uint64_t val);
631
632/**
633 * Add a subnode of type #PLIST_REAL to a node. The node must be of a structured type
634 * (ie #PLIST_DICT or #PLIST_ARRAY).
635 *
636 * @param node the node to add a children to
637 * @param val the real value
638 */
639 PLIST_API void plist_add_sub_real_el(plist_t node, double val);
640
641/**
642 * Add a subnode of type #PLIST_DATA to a node. The node must be of a structured type
643 * (ie #PLIST_DICT or #PLIST_ARRAY).
644 *
645 * @param node the node to add a children to
646 * @param val the binary buffer
647 * @param length the length of the buffer
648 */
649 PLIST_API void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length);
650
651/**
652 * Add a subnode of type #PLIST_DATE to a node. The node must be of a structured type
653 * (ie #PLIST_DICT or #PLIST_ARRAY).
654 *
655 * @param node the node to add a children to
656 * @param sec the number of seconds since 01/01/2001
657 * @param usec the number of microseconds
658 */
659 PLIST_API void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec);
660
661
491/*@}*/ 662/*@}*/
492 663
493 664
diff --git a/src/plist.c b/src/plist.c
index 4d98fd2..8368d6e 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -80,57 +80,70 @@ plist_t plist_new_array(void)
80 return plist_new_node(data); 80 return plist_new_node(data);
81} 81}
82 82
83static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length) 83//These nodes should not be handled by users
84static plist_t plist_new_key(const char *val)
84{ 85{
85 //only structured types can have children 86 plist_data_t data = plist_new_plist_data();
86 plist_type node_type = plist_get_node_type(node); 87 data->type = PLIST_KEY;
87 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) { 88 data->strval = strdup(val);
88 //only structured types are allowed to have nulll value 89 data->length = strlen(val);
89 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 90 return plist_new_node(data);
91}
90 92
91 plist_t subnode = NULL; 93plist_t plist_new_string(const char *val)
94{
95 plist_data_t data = plist_new_plist_data();
96 data->type = PLIST_STRING;
97 data->strval = strdup(val);
98 data->length = strlen(val);
99 return plist_new_node(data);
100}
92 101
93 //now handle value 102plist_t plist_new_bool(uint8_t val)
94 plist_data_t data = plist_new_plist_data(); 103{
95 data->type = type; 104 plist_data_t data = plist_new_plist_data();
96 data->length = length; 105 data->type = PLIST_BOOLEAN;
106 data->boolval = val;
107 data->length = sizeof(uint8_t);
108 return plist_new_node(data);
109}
97 110
98 switch (type) { 111plist_t plist_new_uint(uint64_t val)
99 case PLIST_BOOLEAN: 112{
100 data->boolval = *((char *) value); 113 plist_data_t data = plist_new_plist_data();
101 break; 114 data->type = PLIST_UINT;
102 case PLIST_UINT: 115 data->intval = val;
103 data->intval = *((uint64_t *) value); 116 data->length = sizeof(uint64_t);
104 break; 117 return plist_new_node(data);
105 case PLIST_REAL: 118}
106 data->realval = *((double *) value);
107 break;
108 case PLIST_KEY:
109 case PLIST_STRING:
110 data->strval = strdup((char *) value);
111 break;
112 case PLIST_DATA:
113 data->buff = (uint8_t *) malloc(length);
114 memcpy(data->buff, value, length);
115 break;
116 case PLIST_DATE:
117 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
118 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
119 break;
120 case PLIST_ARRAY:
121 case PLIST_DICT:
122 default:
123 break;
124 }
125 119
126 subnode = plist_new_node(data); 120plist_t plist_new_real(double val)
127 if (node) 121{
128 g_node_append(node, subnode); 122 plist_data_t data = plist_new_plist_data();
129 return subnode; 123 data->type = PLIST_REAL;
130 } else 124 data->realval = val;
131 return NULL; 125 data->length = sizeof(double);
132 } 126 return plist_new_node(data);
133 return NULL; 127}
128
129plist_t plist_new_data(const char *val, uint64_t length)
130{
131 plist_data_t data = plist_new_plist_data();
132 data->type = PLIST_DATA;
133 data->buff = (uint8_t *) malloc(length);
134 memcpy(data->buff, val, length);
135 data->length = length;
136 return plist_new_node(data);
137}
138
139plist_t plist_new_date(int32_t sec, int32_t usec)
140{
141 plist_data_t data = plist_new_plist_data();
142 data->type = PLIST_DATE;
143 data->timeval.tv_sec = sec;
144 data->timeval.tv_usec = usec;
145 data->length = sizeof(GTimeVal);
146 return plist_new_node(data);
134} 147}
135 148
136void plist_free(plist_t plist) 149void plist_free(plist_t plist)
@@ -181,54 +194,131 @@ plist_t plist_copy(plist_t node)
181 return copied; 194 return copied;
182} 195}
183 196
184plist_t plist_get_first_child(plist_t node) 197uint32_t plist_array_get_size(plist_t node)
185{ 198{
186 return (plist_t) g_node_first_child((GNode *) node); 199 uint32_t ret = 0;
200 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
201 ret = g_node_n_children(node);
202 }
203 return ret;
187} 204}
188 205
189plist_t plist_get_next_sibling(plist_t node) 206plist_t plist_array_get_item(plist_t node, uint32_t n)
190{ 207{
191 return (plist_t) g_node_next_sibling((GNode *) node); 208 plist_t ret = NULL;
209 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
210 ret = (plist_t)g_node_nth_child(node, n);
211 }
212 return ret;
192} 213}
193 214
194plist_t plist_get_prev_sibling(plist_t node) 215void plist_array_set_item(plist_t node, plist_t item, uint32_t n)
195{ 216{
196 return (plist_t) g_node_prev_sibling((GNode *) node); 217 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
218 plist_t old_item = plist_array_get_item(node, n);
219 if (old_item) {
220 plist_free_node(old_item, NULL);
221 old_item = NULL;
222 plist_copy_node(item, &old_item);
223 }
224 }
225 return;
197} 226}
198 227
199plist_t plist_get_parent(plist_t node) 228void plist_array_append_item(plist_t node, plist_t item)
200{ 229{
201 return node ? (plist_t) ((GNode *) node)->parent : NULL; 230 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
231 g_node_append(node, item);
232 }
233 return;
202} 234}
203 235
204plist_t plist_get_array_nth_el(plist_t node, uint32_t n) 236void plist_array_insert_item(plist_t node, plist_t item, uint32_t n)
205{ 237{
206 plist_t ret = NULL;
207 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 238 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
208 uint32_t i = 0; 239 g_node_insert(node, n, item);
209 plist_t temp = plist_get_first_child(node); 240 }
241 return;
242}
210 243
211 while (i <= n && temp) { 244void plist_array_remove_item(plist_t node, uint32_t n)
212 if (i == n) 245{
213 ret = temp; 246 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
214 temp = plist_get_next_sibling(temp); 247 plist_t old_item = plist_array_get_item(node, n);
215 i++; 248 if (old_item) {
249 plist_free(old_item);
216 } 250 }
217 } 251 }
252 return;
253}
254
255uint32_t plist_dict_get_size(plist_t node)
256{
257 uint32_t ret = 0;
258 if (node && PLIST_DICT == plist_get_node_type(node)) {
259 ret = g_node_n_children(node) / 2;
260 }
218 return ret; 261 return ret;
219} 262}
220 263
221plist_t plist_get_dict_el_from_key(plist_t node, const char *key) 264plist_t plist_dict_get_item(plist_t node, const char* key)
222{ 265{
223 plist_t ret = NULL; 266 plist_t ret = NULL;
267
224 if (node && PLIST_DICT == plist_get_node_type(node)) { 268 if (node && PLIST_DICT == plist_get_node_type(node)) {
225 269
226 plist_t key_node = plist_find_node_by_key(node, key); 270 plist_t current = NULL;
227 ret = plist_get_next_sibling(key_node); 271 for (current = plist_get_first_child(node);
272 current;
273 current = plist_get_next_sibling(plist_get_next_sibling(current))) {
274
275 assert( PLIST_KEY == plist_get_node_type(current) );
276 plist_data_t data = plist_get_data(current);
277
278 if (data && !strcmp(key, data->strval)) {
279 ret = plist_get_next_sibling(current);
280 break;
281 }
282 }
228 } 283 }
229 return ret; 284 return ret;
230} 285}
231 286
287void plist_dict_set_item(plist_t node, plist_t item, const char* key)
288{
289 if (node && PLIST_DICT == plist_get_node_type(node)) {
290 plist_t old_item = plist_dict_get_item(node, key);
291 if (old_item) {
292 plist_free_node(old_item, NULL);
293 old_item = NULL;
294 plist_copy_node(item, &old_item);
295 }
296 }
297 return;
298}
299
300void plist_dict_insert_item(plist_t node, plist_t item, const char* key)
301{
302 if (node && PLIST_DICT == plist_get_node_type(node)) {
303 g_node_append(node, plist_new_key(key));
304 g_node_append(node, item);
305 }
306 return;
307}
308
309void plist_dict_remove_item(plist_t node, const char* key)
310{
311 if (node && PLIST_DICT == plist_get_node_type(node)) {
312 plist_t old_item = plist_dict_get_item(node, key);
313 if (old_item) {
314 plist_t key_node = g_node_prev_sibling(old_item);
315 plist_free(key_node);
316 plist_free(old_item);
317 }
318 }
319 return;
320}
321
232static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length) 322static char compare_node_value(plist_type type, plist_data_t data, const void *value, uint64_t length)
233{ 323{
234 char res = FALSE; 324 char res = FALSE;
@@ -335,6 +425,11 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
335 } 425 }
336} 426}
337 427
428plist_t plist_get_parent(plist_t node)
429{
430 return node ? (plist_t) ((GNode *) node)->parent : NULL;
431}
432
338plist_type plist_get_node_type(plist_t node) 433plist_type plist_get_node_type(plist_t node)
339{ 434{
340 if (node) { 435 if (node) {
@@ -345,51 +440,6 @@ plist_type plist_get_node_type(plist_t node)
345 return PLIST_NONE; 440 return PLIST_NONE;
346} 441}
347 442
348void plist_add_sub_node(plist_t node, plist_t subnode)
349{
350 if (node && subnode) {
351 plist_type type = plist_get_node_type(node);
352 if (type == PLIST_DICT || type == PLIST_ARRAY)
353 g_node_append(node, subnode);
354 }
355}
356
357void plist_add_sub_key_el(plist_t node, const char *val)
358{
359 plist_add_sub_element(node, PLIST_KEY, val, strlen(val));
360}
361
362void plist_add_sub_string_el(plist_t node, const char *val)
363{
364 plist_add_sub_element(node, PLIST_STRING, val, strlen(val));
365}
366
367void plist_add_sub_bool_el(plist_t node, uint8_t val)
368{
369 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
370}
371
372void plist_add_sub_uint_el(plist_t node, uint64_t val)
373{
374 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t));
375}
376
377void plist_add_sub_real_el(plist_t node, double val)
378{
379 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double));
380}
381
382void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length)
383{
384 plist_add_sub_element(node, PLIST_DATA, val, length);
385}
386
387void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec)
388{
389 GTimeVal val = { sec, usec };
390 plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal));
391}
392
393void plist_get_key_val(plist_t node, char **val) 443void plist_get_key_val(plist_t node, char **val)
394{ 444{
395 plist_type type = plist_get_node_type(node); 445 plist_type type = plist_get_node_type(node);
@@ -570,7 +620,6 @@ static plist_t plist_set_element_val(plist_t node, plist_type type, const void *
570 } 620 }
571} 621}
572 622
573
574void plist_set_key_val(plist_t node, const char *val) 623void plist_set_key_val(plist_t node, const char *val)
575{ 624{
576 plist_set_element_val(node, PLIST_KEY, val, strlen(val)); 625 plist_set_element_val(node, PLIST_KEY, val, strlen(val));
@@ -606,3 +655,149 @@ void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
606 GTimeVal val = { sec, usec }; 655 GTimeVal val = { sec, usec };
607 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal)); 656 plist_set_element_val(node, PLIST_DATE, &val, sizeof(GTimeVal));
608} 657}
658
659//DEPRECATED API BELOW
660
661static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *value, uint64_t length)
662{
663 //only structured types can have children
664 plist_type node_type = plist_get_node_type(node);
665 if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) {
666 //only structured types are allowed to have nulll value
667 if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) {
668
669 plist_t subnode = NULL;
670
671 //now handle value
672 plist_data_t data = plist_new_plist_data();
673 data->type = type;
674 data->length = length;
675
676 switch (type) {
677 case PLIST_BOOLEAN:
678 data->boolval = *((char *) value);
679 break;
680 case PLIST_UINT:
681 data->intval = *((uint64_t *) value);
682 break;
683 case PLIST_REAL:
684 data->realval = *((double *) value);
685 break;
686 case PLIST_KEY:
687 case PLIST_STRING:
688 data->strval = strdup((char *) value);
689 break;
690 case PLIST_DATA:
691 data->buff = (uint8_t *) malloc(length);
692 memcpy(data->buff, value, length);
693 break;
694 case PLIST_DATE:
695 data->timeval.tv_sec = ((GTimeVal *) value)->tv_sec;
696 data->timeval.tv_usec = ((GTimeVal *) value)->tv_usec;
697 break;
698 case PLIST_ARRAY:
699 case PLIST_DICT:
700 default:
701 break;
702 }
703
704 subnode = plist_new_node(data);
705 if (node)
706 g_node_append(node, subnode);
707 return subnode;
708 } else
709 return NULL;
710 }
711 return NULL;
712}
713
714
715plist_t plist_get_first_child(plist_t node)
716{
717 return (plist_t) g_node_first_child((GNode *) node);
718}
719
720plist_t plist_get_next_sibling(plist_t node)
721{
722 return (plist_t) g_node_next_sibling((GNode *) node);
723}
724
725plist_t plist_get_prev_sibling(plist_t node)
726{
727 return (plist_t) g_node_prev_sibling((GNode *) node);
728}
729
730plist_t plist_get_array_nth_el(plist_t node, uint32_t n)
731{
732 plist_t ret = NULL;
733 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
734 uint32_t i = 0;
735 plist_t temp = plist_get_first_child(node);
736
737 while (i <= n && temp) {
738 if (i == n)
739 ret = temp;
740 temp = plist_get_next_sibling(temp);
741 i++;
742 }
743 }
744 return ret;
745}
746
747plist_t plist_get_dict_el_from_key(plist_t node, const char *key)
748{
749 plist_t ret = NULL;
750 if (node && PLIST_DICT == plist_get_node_type(node)) {
751
752 plist_t key_node = plist_find_node_by_key(node, key);
753 ret = plist_get_next_sibling(key_node);
754 }
755 return ret;
756}
757
758void plist_add_sub_node(plist_t node, plist_t subnode)
759{
760 if (node && subnode) {
761 plist_type type = plist_get_node_type(node);
762 if (type == PLIST_DICT || type == PLIST_ARRAY)
763 g_node_append(node, subnode);
764 }
765}
766
767void plist_add_sub_key_el(plist_t node, const char *val)
768{
769 plist_add_sub_element(node, PLIST_KEY, val, strlen(val));
770}
771
772void plist_add_sub_string_el(plist_t node, const char *val)
773{
774 plist_add_sub_element(node, PLIST_STRING, val, strlen(val));
775}
776
777void plist_add_sub_bool_el(plist_t node, uint8_t val)
778{
779 plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t));
780}
781
782void plist_add_sub_uint_el(plist_t node, uint64_t val)
783{
784 plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t));
785}
786
787void plist_add_sub_real_el(plist_t node, double val)
788{
789 plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double));
790}
791
792void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length)
793{
794 plist_add_sub_element(node, PLIST_DATA, val, length);
795}
796
797void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec)
798{
799 GTimeVal val = { sec, usec };
800 plist_add_sub_element(node, PLIST_DATE, &val, sizeof(GTimeVal));
801}
802
803