diff options
author | Jonathan Beck | 2009-10-11 14:00:31 +0200 |
---|---|---|
committer | Jonathan Beck | 2009-10-11 14:00:31 +0200 |
commit | 583bb249458fcca6cdf2581896681e618cf4547e (patch) | |
tree | b46b47170728b3b063cc2620c7ed7b7f987e0ad7 | |
parent | 120b7348e7b9d4c92e463590de5a4de4e6bc1228 (diff) | |
download | libplist-583bb249458fcca6cdf2581896681e618cf4547e.tar.gz libplist-583bb249458fcca6cdf2581896681e618cf4547e.tar.bz2 |
Add function to change a node's type.
-rw-r--r-- | include/plist/plist.h | 8 | ||||
-rw-r--r-- | src/plist.c | 38 |
2 files changed, 43 insertions, 3 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h index bfd934f..55ae9e7 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h @@ -411,6 +411,14 @@ extern "C" { ********************************************/ /** + * Forces type of node. Changing type of structured nodes is only allowed if node is empty. + * Reset value of node; + * @param node the node + * @param type the key value + */ + PLIST_API void plist_set_type(plist_t node, plist_type type); + +/** * Set the value of a node. * Forces type of node to #PLIST_KEY * diff --git a/src/plist.c b/src/plist.c index 3b0387e..df98a7a 100644 --- a/src/plist.c +++ b/src/plist.c @@ -44,10 +44,8 @@ plist_data_t plist_new_plist_data(void) return data; } -static void plist_free_node(GNode * node, gpointer none) +static void plist_free_data(plist_data_t data) { - g_node_unlink(node); - plist_data_t data = plist_get_data(node); if (data) { switch (data->type) { case PLIST_KEY: @@ -62,6 +60,13 @@ static void plist_free_node(GNode * node, gpointer none) } free(data); } +} + +static void plist_free_node(GNode * node, gpointer none) +{ + g_node_unlink(node); + plist_data_t data = plist_get_data(node); + plist_free_data(data); node->data = NULL; g_node_children_foreach(node, G_TRAVERSE_ALL, plist_free_node, NULL); } @@ -664,6 +669,33 @@ static plist_t plist_set_element_val(plist_t node, plist_type type, const void * } } +void plist_set_type(plist_t node, plist_type type) +{ + if ( g_node_n_children(node) == 0 ) { + plist_data_t data = plist_get_data(node); + plist_free_data( data ); + data = plist_new_plist_data(); + data->type = type; + switch(type){ + case PLIST_BOOLEAN: + data->length = sizeof(uint8_t); + break; + case PLIST_UINT: + data->length = sizeof(uint64_t); + break; + case PLIST_REAL: + data->length = sizeof(double); + break; + case PLIST_DATE: + data->length = sizeof(GTimeVal); + break; + default: + data->length = 0; + break; + } + } +} + void plist_set_key_val(plist_t node, const char *val) { plist_set_element_val(node, PLIST_KEY, val, strlen(val)); |