summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-10 22:10:52 +0200
committerGravatar Jonathan Beck2009-10-10 22:10:52 +0200
commitbc08d5f530b3b4a73b31464ee498d610ecb2ea13 (patch)
tree1142c3b38916aaea3c4b7db0ba0a72033252aa7a
parent009274f4dfb7829f2ee98d46afd5e54892806990 (diff)
downloadlibplist-bc08d5f530b3b4a73b31464ee498d610ecb2ea13.tar.gz
libplist-bc08d5f530b3b4a73b31464ee498d610ecb2ea13.tar.bz2
Add dict iteration. Add functions to retrieve index and key from node.
-rw-r--r--include/plist/plist.h38
-rw-r--r--src/plist.c43
2 files changed, 73 insertions, 8 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h
index beb467a..484203b 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -62,6 +62,11 @@ extern "C" {
62 typedef void *plist_t; 62 typedef void *plist_t;
63 63
64/** 64/**
65 * The plist dictionary iterator.
66 */
67 typedef uint32_t *plist_dict_iter;
68
69/**
65 * The enumeration of plist node types. 70 * The enumeration of plist node types.
66 */ 71 */
67 typedef enum { 72 typedef enum {
@@ -196,6 +201,14 @@ extern "C" {
196 PLIST_API plist_t plist_array_get_item(plist_t node, uint32_t n); 201 PLIST_API plist_t plist_array_get_item(plist_t node, uint32_t n);
197 202
198/** 203/**
204 * Get the index of an item. item must be a member of a #PLIST_ARRAY node.
205 *
206 * @param node the node
207 * @return the node index
208 */
209 PLIST_API uint32_t plist_array_get_item_index(plist_t node);
210
211/**
199 * Set the nth item in a #PLIST_ARRAY node. 212 * Set the nth item in a #PLIST_ARRAY node.
200 * The previous item at index n will be freed using #plist_free 213 * The previous item at index n will be freed using #plist_free
201 * 214 *
@@ -238,12 +251,31 @@ extern "C" {
238 ********************************************/ 251 ********************************************/
239 252
240/** 253/**
241 * Get size of a #PLIST_DICT node. 254 * Create iterator of a #PLIST_DICT node.
255 * The allocated iterator shoult be freed with tandard free function
256 *
257 * @param node the node of type #PLIST_DICT
258 * @param iter iterator of the #PLIST_DICT node
259 */
260 PLIST_API void plist_dict_new_iter(plist_t node, plist_dict_iter *iter);
261
262/**
263 * Increment iterator of a #PLIST_DICT node.
242 * 264 *
243 * @param node the node of type #PLIST_DICT 265 * @param node the node of type #PLIST_DICT
244 * @return size of the #PLIST_DICT node 266 * @param iter iterator of the dictionary
267 * @param key a location to store the key, or NULL.
268 * @param val a location to store the value, or NULL.
269 */
270 PLIST_API void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val);
271
272/**
273 * Get key associated to an item. Item must be member of a dictionary
274 *
275 * @param node the node
276 * @param key a location to store the key.
245 */ 277 */
246 PLIST_API uint32_t plist_dict_get_size(plist_t node); 278 PLIST_API void plist_dict_get_item_key(plist_t node, char **key);
247 279
248/** 280/**
249 * Get the nth item in a #PLIST_DICT node. 281 * Get the nth item in a #PLIST_DICT node.
diff --git a/src/plist.c b/src/plist.c
index 95bc43c..a9a6173 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -212,6 +212,14 @@ plist_t plist_array_get_item(plist_t node, uint32_t n)
212 return ret; 212 return ret;
213} 213}
214 214
215uint32_t plist_array_get_item_index(plist_t node)
216{
217 plist_t father = plist_get_parent(node);
218 if (PLIST_ARRAY == plist_get_node_type(father)) {
219 return g_node_child_position(father, node);
220 }
221}
222
215void plist_array_set_item(plist_t node, plist_t item, uint32_t n) 223void plist_array_set_item(plist_t node, plist_t item, uint32_t n)
216{ 224{
217 if (node && PLIST_ARRAY == plist_get_node_type(node)) { 225 if (node && PLIST_ARRAY == plist_get_node_type(node)) {
@@ -252,13 +260,38 @@ void plist_array_remove_item(plist_t node, uint32_t n)
252 return; 260 return;
253} 261}
254 262
255uint32_t plist_dict_get_size(plist_t node) 263void plist_dict_new_iter(plist_t node, plist_dict_iter *iter)
256{ 264{
257 uint32_t ret = 0; 265 if (iter && *iter == NULL) {
258 if (node && PLIST_DICT == plist_get_node_type(node)) { 266 *iter = malloc(sizeof(uint32_t));
259 ret = g_node_n_children(node) / 2; 267 **iter = 0;
268 }
269 return;
270}
271
272void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val)
273{
274 if (node && PLIST_DICT == plist_get_node_type(node) && *iter < g_node_n_children(node) / 2) {
275
276 if (key) {
277 plist_get_key_val((plist_t)g_node_nth_child(node, 2 * (*iter)), key);
278 }
279
280 if (val) {
281 val = (plist_t) g_node_nth_child(node, 2 * (*iter) + 1);
282 }
283
284 *iter += 2;
285 }
286 return;
287}
288
289void plist_dict_get_item_key(plist_t node, char **key)
290{
291 plist_t father = plist_get_parent(node);
292 if (PLIST_DICT == plist_get_node_type(father)) {
293 plist_get_key_val( (plist_t) g_node_prev_sibling(node), key);
260 } 294 }
261 return ret;
262} 295}
263 296
264plist_t plist_dict_get_item(plist_t node, const char* key) 297plist_t plist_dict_get_item(plist_t node, const char* key)