From d174ba080c959cab3245fb1714c6d6340ebbac09 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 8 Jan 2009 21:42:52 +0100 Subject: Add edition and reading fonctions so that handling plist is less confusing. Fix indent. --- Makefile.am | 2 +- include/plist/plist.h | 21 ++++++ src/bplist.c | 26 ++++---- src/plist.c | 173 ++++++++++++++++++++++++++++++++++++++------------ src/plist.h | 1 - src/xplist.c | 14 ++-- 6 files changed, 176 insertions(+), 61 deletions(-) diff --git a/Makefile.am b/Makefile.am index e88e9be..264b0a3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,4 +9,4 @@ doc: doxygen doxygen.cfg indent: - indent -kr -ut -ts4 -l120 src/*.c src/*.h dev/*.c + indent -kr -ut -ts4 -l120 src/*.c src/*.h plutil/*.c diff --git a/include/plist/plist.h b/include/plist/plist.h index b6335f5..b496d6f 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h @@ -52,6 +52,17 @@ plist_t plist_new_array(); //Plist edition, create a new root if node is NULL plist_t plist_add_sub_element( plist_t node, plist_type type, void* value, uint64_t length); +//Plist edition, only work for dict and array node +void plist_add_sub_node(plist_t node, plist_t subnode); + +void plist_add_sub_key_el(plist_t node, char* val); +void plist_add_sub_string_el(plist_t node, char* val); +void plist_add_sub_bool_el(plist_t node, uint8_t val); +void plist_add_sub_uint_el(plist_t node, uint64_t val); +void plist_add_sub_real_el(plist_t node, double val); +void plist_add_sub_data_el(plist_t node, char* val, uint64_t length); + + //plist free void plist_free(plist_t plist); @@ -63,6 +74,16 @@ plist_t plist_get_prev_sibling(plist_t node); plist_t plist_find_node(plist_t plist, plist_type type, void *value, uint64_t length); void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length); +//Plist reading +plist_type plist_get_node_type(plist_t node); + +void plist_get_key_val(plist_t node, char** val); +void plist_get_string_val(plist_t node, char** val); +void plist_get_bool_val(plist_t node, uint8_t* val); +void plist_get_uint_val(plist_t node, uint64_t* val); +void plist_get_real_val(plist_t node, double* val); +void plist_get_data_val(plist_t node, char** val, uint64_t* length); + //import and export functions void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length); diff --git a/src/bplist.c b/src/bplist.c index cbe53cd..b633535 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -59,7 +59,7 @@ enum { BPLIST_MASK = 0xF0 }; -static void byte_convert(uint8_t *address, size_t size) +static void byte_convert(uint8_t * address, size_t size) { #if G_BYTE_ORDER == G_LITTLE_ENDIAN uint8_t i = 0, j = 0; @@ -124,7 +124,7 @@ static plist_t parse_real_node(char *bnode, uint8_t size) switch (size) { case sizeof(float): case sizeof(double): - data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping + data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping break; default: free(data); @@ -140,8 +140,8 @@ static plist_t parse_date_node(char *bnode, uint8_t size) plist_data_t data = plist_get_data(node); double time_real = data->realval; - data->timeval.tv_sec = (glong)time_real; - data->timeval.tv_usec = (time_real - (glong)time_real) * G_USEC_PER_SEC; + data->timeval.tv_sec = (glong) time_real; + data->timeval.tv_usec = (time_real - (glong) time_real) * G_USEC_PER_SEC; data->type = PLIST_DATE; return node; } @@ -475,7 +475,7 @@ static guint plist_data_hash(gconstpointer key) case PLIST_BOOLEAN: case PLIST_UINT: case PLIST_REAL: - buff = (char *) &data->intval; //works also for real as we use an union + buff = (char *) &data->intval; //works also for real as we use an union size = 8; break; case PLIST_KEY: @@ -574,7 +574,7 @@ static void serialize_plist(GNode * node, gpointer data) return; } //insert new ref - uint64_t* index_val = (uint64_t*) malloc(sizeof(uint64_t)); + uint64_t *index_val = (uint64_t *) malloc(sizeof(uint64_t)); *index_val = current_index; g_hash_table_insert(ser->ref_table, node, index_val); @@ -586,9 +586,9 @@ static void serialize_plist(GNode * node, gpointer data) return; } -static gboolean free_index (gpointer key, gpointer value, gpointer user_data) +static gboolean free_index(gpointer key, gpointer value, gpointer user_data) { - free((uint64_t*)value); + free((uint64_t *) value); return TRUE; } @@ -618,7 +618,7 @@ static void write_real(GByteArray * bplist, double val) static void write_date(GByteArray * bplist, double val) { - uint64_t size = 8; //dates always use 8 bytes + uint64_t size = 8; //dates always use 8 bytes uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); buff[0] = BPLIST_DATE | Log2(size); memcpy(buff + 1, &val, size); @@ -651,7 +651,7 @@ static void write_data(GByteArray * bplist, uint8_t * val, uint64_t size) static void write_string(GByteArray * bplist, char *val) { uint64_t size = strlen(val); - write_raw_data(bplist, BPLIST_STRING, (uint8_t*) val, size); + write_raw_data(bplist, BPLIST_STRING, (uint8_t *) val, size); } static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size) @@ -672,7 +672,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl GNode *cur = NULL; uint64_t i = 0; for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { - idx = *(uint64_t*)(g_hash_table_lookup(ref_table, cur)); + idx = *(uint64_t *) (g_hash_table_lookup(ref_table, cur)); memcpy(buff + i * dict_param_size, &idx, dict_param_size); byte_convert(buff + i * dict_param_size, dict_param_size); } @@ -787,7 +787,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); break; case PLIST_DATE: - write_date(bplist_buff, data->timeval.tv_sec + (double)data->timeval.tv_usec / G_USEC_PER_SEC ); + write_date(bplist_buff, data->timeval.tv_sec + (double) data->timeval.tv_usec / G_USEC_PER_SEC); break; default: break; @@ -795,7 +795,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) } //free intermediate objects - g_hash_table_foreach_remove (ref_table, free_index, NULL); + g_hash_table_foreach_remove(ref_table, free_index, NULL); //write offsets offset_size = get_needed_bytes(bplist_buff->len); 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() plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length) { - //only structured types are allowed to have nulll value - if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { - //now handle value - plist_data_t data = plist_new_plist_data(); - data->type = type; - data->length = length; - - switch (type) { - case PLIST_BOOLEAN: - data->boolval = *((char *) value); - break; - case PLIST_UINT: - data->intval = *((uint64_t *) value); - break; - case PLIST_REAL: - data->realval = *((double *) value); - break; - case PLIST_KEY: - case PLIST_STRING: - data->strval = strdup((char *) value); - break; - case PLIST_UNICODE: - data->unicodeval = wcsdup((wchar_t *) value); - break; - case PLIST_DATA: - memcpy(data->buff, value, length); - break; - case PLIST_ARRAY: - case PLIST_DICT: - case PLIST_DATE: - default: - break; - } + //only structured types can have children + plist_type node_type = plist_get_node_type(node); + if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) { + //only structured types are allowed to have nulll value + if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { + //now handle value + plist_data_t data = plist_new_plist_data(); + data->type = type; + data->length = length; - plist_t subnode = plist_new_node(data); - if (node) - g_node_append(node, subnode); - return subnode; - } else - return NULL; + switch (type) { + case PLIST_BOOLEAN: + data->boolval = *((char *) value); + break; + case PLIST_UINT: + data->intval = *((uint64_t *) value); + break; + case PLIST_REAL: + data->realval = *((double *) value); + break; + case PLIST_KEY: + case PLIST_STRING: + data->strval = strdup((char *) value); + break; + case PLIST_UNICODE: + data->unicodeval = wcsdup((wchar_t *) value); + break; + case PLIST_DATA: + memcpy(data->buff, value, length); + break; + case PLIST_ARRAY: + case PLIST_DICT: + case PLIST_DATE: + default: + break; + } + + plist_t subnode = plist_new_node(data); + if (node) + g_node_append(node, subnode); + return subnode; + } else + return NULL; + } } void plist_free(plist_t plist) @@ -243,3 +247,94 @@ uint64_t plist_get_node_uint_val(plist_t node) else return 0; } + +void plist_add_sub_node(plist_t node, plist_t subnode) +{ + if (node && subnode) { + plist_type type = plist_get_node_type(node); + if (type == PLIST_DICT || type == PLIST_ARRAY) + g_node_append(node, subnode); + } +} + +void plist_add_sub_key_el(plist_t node, char *val) +{ + plist_add_sub_element(node, PLIST_KEY, val, strlen(val)); +} + +void plist_add_sub_string_el(plist_t node, char *val) +{ + plist_add_sub_element(node, PLIST_STRING, val, strlen(val)); +} + +void plist_add_sub_bool_el(plist_t node, uint8_t val) +{ + plist_add_sub_element(node, PLIST_BOOLEAN, &val, sizeof(uint8_t)); +} + +void plist_add_sub_uint_el(plist_t node, uint64_t val) +{ + plist_add_sub_element(node, PLIST_UINT, &val, sizeof(uint64_t)); +} + +void plist_add_sub_real_el(plist_t node, double val) +{ + plist_add_sub_element(node, PLIST_REAL, &val, sizeof(double)); +} + +void plist_add_sub_data_el(plist_t node, char *val, uint64_t length) +{ + plist_add_sub_element(node, PLIST_DATA, val, length); +} + +void plist_get_key_val(plist_t node, char **val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_KEY == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == strlen(*val)); +} + +void plist_get_string_val(plist_t node, char **val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_STRING == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == strlen(*val)); +} + +void plist_get_bool_val(plist_t node, uint8_t * val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_BOOLEAN == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == sizeof(uint8_t)); +} + +void plist_get_uint_val(plist_t node, uint64_t * val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_UINT == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == sizeof(uint64_t)); +} + +void plist_get_real_val(plist_t node, double *val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_REAL == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == sizeof(double)); +} + +void plist_get_data_val(plist_t node, char **val, uint64_t * length) +{ + plist_type type = plist_get_node_type(node); + if (PLIST_UINT == type) + plist_get_type_and_value(node, &type, (void *) val, length); +} diff --git a/src/plist.h b/src/plist.h index bd12e3a..449a3f7 100644 --- a/src/plist.h +++ b/src/plist.h @@ -56,7 +56,6 @@ plist_t plist_new_node(plist_data_t data); plist_data_t plist_get_data(const plist_t node); plist_data_t plist_new_plist_data(); void plist_free_plist_data(plist_data_t node); -plist_type plist_get_node_type(plist_t node); uint64_t plist_get_node_uint_val(plist_t node); diff --git a/src/xplist.c b/src/xplist.c index 3a019a9..e0d3768 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -256,7 +256,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) } if (!xmlStrcmp(node->name, XPLIST_INT)) { - char *strval = (char*)xmlNodeGetContent(node); + char *strval = (char *) xmlNodeGetContent(node); data->intval = g_ascii_strtoull(strval, NULL, 0); data->type = PLIST_UINT; data->length = 8; @@ -264,7 +264,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) } if (!xmlStrcmp(node->name, XPLIST_REAL)) { - char *strval = (char*)xmlNodeGetContent(node); + char *strval = (char *) xmlNodeGetContent(node); data->realval = atof(strval); data->type = PLIST_REAL; data->length = 8; @@ -272,21 +272,21 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) } if (!xmlStrcmp(node->name, XPLIST_DATE)) { - g_time_val_from_iso8601((char*)xmlNodeGetContent(node), &data->timeval); + g_time_val_from_iso8601((char *) xmlNodeGetContent(node), &data->timeval); data->type = PLIST_DATE; data->length = sizeof(GTimeVal); continue; //TODO : handle date tag } if (!xmlStrcmp(node->name, XPLIST_STRING)) { - data->strval = strdup( (char*) xmlNodeGetContent(node)); + data->strval = strdup((char *) xmlNodeGetContent(node)); data->type = PLIST_STRING; data->length = strlen(data->strval); continue; } if (!xmlStrcmp(node->name, XPLIST_KEY)) { - data->strval = strdup( (char*) xmlNodeGetContent(node)); + data->strval = strdup((char *) xmlNodeGetContent(node)); data->type = PLIST_KEY; data->length = strlen(data->strval); continue; @@ -294,7 +294,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) if (!xmlStrcmp(node->name, XPLIST_DATA)) { gsize size = 0; - data->buff = g_base64_decode((char*)xmlNodeGetContent(node), &size); + data->buff = g_base64_decode((char *) xmlNodeGetContent(node), &size); data->length = size; data->type = PLIST_DATA; continue; @@ -326,7 +326,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) int size = 0; xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); - if (size >=0 ) + if (size >= 0) *length = size; free_plist(plist_doc); } -- cgit v1.1-32-gdbae