Commit d174ba08 authored by Jonathan Beck's avatar Jonathan Beck

Add edition and reading fonctions so that handling plist is less confusing.

Fix indent.
parent c8d5e261
...@@ -9,4 +9,4 @@ doc: ...@@ -9,4 +9,4 @@ doc:
doxygen doxygen.cfg doxygen doxygen.cfg
indent: indent:
indent -kr -ut -ts4 -l120 src/*.c src/*.h dev/*.c indent -kr -ut -ts4 -l120 src/*.c src/*.h plutil/*.c
...@@ -52,6 +52,17 @@ plist_t plist_new_array(); ...@@ -52,6 +52,17 @@ plist_t plist_new_array();
//Plist edition, create a new root if node is NULL //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_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 //plist free
void plist_free(plist_t plist); void plist_free(plist_t plist);
...@@ -63,6 +74,16 @@ plist_t plist_get_prev_sibling(plist_t node); ...@@ -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); 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); 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 //import and export functions
void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); 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); void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length);
......
...@@ -59,7 +59,7 @@ enum { ...@@ -59,7 +59,7 @@ enum {
BPLIST_MASK = 0xF0 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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
uint8_t i = 0, j = 0; uint8_t i = 0, j = 0;
...@@ -140,8 +140,8 @@ static plist_t parse_date_node(char *bnode, uint8_t size) ...@@ -140,8 +140,8 @@ static plist_t parse_date_node(char *bnode, uint8_t size)
plist_data_t data = plist_get_data(node); plist_data_t data = plist_get_data(node);
double time_real = data->realval; double time_real = data->realval;
data->timeval.tv_sec = (glong)time_real; data->timeval.tv_sec = (glong) time_real;
data->timeval.tv_usec = (time_real - (glong)time_real) * G_USEC_PER_SEC; data->timeval.tv_usec = (time_real - (glong) time_real) * G_USEC_PER_SEC;
data->type = PLIST_DATE; data->type = PLIST_DATE;
return node; return node;
} }
...@@ -574,7 +574,7 @@ static void serialize_plist(GNode * node, gpointer data) ...@@ -574,7 +574,7 @@ static void serialize_plist(GNode * node, gpointer data)
return; return;
} }
//insert new ref //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; *index_val = current_index;
g_hash_table_insert(ser->ref_table, node, index_val); g_hash_table_insert(ser->ref_table, node, index_val);
...@@ -586,9 +586,9 @@ static void serialize_plist(GNode * node, gpointer data) ...@@ -586,9 +586,9 @@ static void serialize_plist(GNode * node, gpointer data)
return; 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; return TRUE;
} }
...@@ -651,7 +651,7 @@ static void write_data(GByteArray * bplist, uint8_t * val, uint64_t 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) static void write_string(GByteArray * bplist, char *val)
{ {
uint64_t size = strlen(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) 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 ...@@ -672,7 +672,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
GNode *cur = NULL; GNode *cur = NULL;
uint64_t i = 0; uint64_t i = 0;
for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) { 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); memcpy(buff + i * dict_param_size, &idx, dict_param_size);
byte_convert(buff + i * dict_param_size, 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) ...@@ -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); write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
break; break;
case PLIST_DATE: 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; break;
default: default:
break; break;
...@@ -795,7 +795,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) ...@@ -795,7 +795,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
} }
//free intermediate objects //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 //write offsets
offset_size = get_needed_bytes(bplist_buff->len); offset_size = get_needed_bytes(bplist_buff->len);
......
...@@ -74,6 +74,9 @@ plist_t plist_new_array() ...@@ -74,6 +74,9 @@ plist_t plist_new_array()
plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length) plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length)
{ {
//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 //only structured types are allowed to have nulll value
if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) {
//now handle value //now handle value
...@@ -114,6 +117,7 @@ plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64 ...@@ -114,6 +117,7 @@ plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64
return subnode; return subnode;
} else } else
return NULL; return NULL;
}
} }
void plist_free(plist_t plist) void plist_free(plist_t plist)
...@@ -243,3 +247,94 @@ uint64_t plist_get_node_uint_val(plist_t node) ...@@ -243,3 +247,94 @@ uint64_t plist_get_node_uint_val(plist_t node)
else else
return 0; 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);
}
...@@ -56,7 +56,6 @@ plist_t plist_new_node(plist_data_t data); ...@@ -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_get_data(const plist_t node);
plist_data_t plist_new_plist_data(); plist_data_t plist_new_plist_data();
void plist_free_plist_data(plist_data_t node); 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); uint64_t plist_get_node_uint_val(plist_t node);
......
...@@ -256,7 +256,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -256,7 +256,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
} }
if (!xmlStrcmp(node->name, XPLIST_INT)) { 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->intval = g_ascii_strtoull(strval, NULL, 0);
data->type = PLIST_UINT; data->type = PLIST_UINT;
data->length = 8; data->length = 8;
...@@ -264,7 +264,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -264,7 +264,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
} }
if (!xmlStrcmp(node->name, XPLIST_REAL)) { if (!xmlStrcmp(node->name, XPLIST_REAL)) {
char *strval = (char*)xmlNodeGetContent(node); char *strval = (char *) xmlNodeGetContent(node);
data->realval = atof(strval); data->realval = atof(strval);
data->type = PLIST_REAL; data->type = PLIST_REAL;
data->length = 8; data->length = 8;
...@@ -272,21 +272,21 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -272,21 +272,21 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
} }
if (!xmlStrcmp(node->name, XPLIST_DATE)) { 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->type = PLIST_DATE;
data->length = sizeof(GTimeVal); data->length = sizeof(GTimeVal);
continue; //TODO : handle date tag continue; //TODO : handle date tag
} }
if (!xmlStrcmp(node->name, XPLIST_STRING)) { if (!xmlStrcmp(node->name, XPLIST_STRING)) {
data->strval = strdup( (char*) xmlNodeGetContent(node)); data->strval = strdup((char *) xmlNodeGetContent(node));
data->type = PLIST_STRING; data->type = PLIST_STRING;
data->length = strlen(data->strval); data->length = strlen(data->strval);
continue; continue;
} }
if (!xmlStrcmp(node->name, XPLIST_KEY)) { if (!xmlStrcmp(node->name, XPLIST_KEY)) {
data->strval = strdup( (char*) xmlNodeGetContent(node)); data->strval = strdup((char *) xmlNodeGetContent(node));
data->type = PLIST_KEY; data->type = PLIST_KEY;
data->length = strlen(data->strval); data->length = strlen(data->strval);
continue; continue;
...@@ -294,7 +294,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -294,7 +294,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!xmlStrcmp(node->name, XPLIST_DATA)) { if (!xmlStrcmp(node->name, XPLIST_DATA)) {
gsize size = 0; 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->length = size;
data->type = PLIST_DATA; data->type = PLIST_DATA;
continue; continue;
...@@ -326,7 +326,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) ...@@ -326,7 +326,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
int size = 0; int size = 0;
xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size);
if (size >=0 ) if (size >= 0)
*length = size; *length = size;
free_plist(plist_doc); free_plist(plist_doc);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment