Commit c39685d3 authored by Jonathan Beck's avatar Jonathan Beck

Refine API and fix some warnings.

parent e220e2cf
...@@ -45,11 +45,14 @@ typedef enum { ...@@ -45,11 +45,14 @@ typedef enum {
PLIST_NONE PLIST_NONE
} plist_type; } plist_type;
//Plist edition //Plist creation and edition
void plist_new_dict(plist_t * plist); //utilitary functions to create root nodes (supposed to be dict or array)
void plist_new_array(plist_t * plist); plist_t plist_new_dict();
void plist_new_dict_in_plist(plist_t plist, plist_t * dict); plist_t plist_new_array();
void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length); //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 free
void plist_free(plist_t plist); void plist_free(plist_t plist);
//plist navigation //plist navigation
...@@ -57,20 +60,16 @@ plist_t plist_get_first_child(plist_t node); ...@@ -57,20 +60,16 @@ plist_t plist_get_first_child(plist_t node);
plist_t plist_get_next_sibling(plist_t node); plist_t plist_get_next_sibling(plist_t node);
plist_t plist_get_prev_sibling(plist_t node); plist_t plist_get_prev_sibling(plist_t node);
plist_t plist_find_node(plist_t plist, plist_type type, void *value);
void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length);
//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);
void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist); void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist);
void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist); void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist);
plist_t plist_find_query_node(plist_t plist, char *key, char *request);
plist_t plist_find_node(plist_t plist, plist_type type, void *value);
void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
INCLUDES = -I$(top_srcdir)/include INCLUDES = -I$(top_srcdir)/include
AM_CFLAGS = $(libxml2_CFLAGS) $(libglib2_CFLAGS) AM_CFLAGS = $(libxml2_CFLAGS) $(libglib2_CFLAGS) -D_GNU_SOURCE
AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS) AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS)
lib_LTLIBRARIES = libplist.la lib_LTLIBRARIES = libplist.la
......
...@@ -80,7 +80,7 @@ void byte_convert(char *address, size_t size) ...@@ -80,7 +80,7 @@ void byte_convert(char *address, size_t size)
#define be64dec(x) bswap_64( *(uint64_t*)(x) ) #define be64dec(x) bswap_64( *(uint64_t*)(x) )
#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8))) #define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8)))
#define get_real_bytes(x) (x >> 32 ? 4 : 8) #define get_real_bytes(x) (x >> 32 ? 4 : 8)
plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object)
...@@ -446,7 +446,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) ...@@ -446,7 +446,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
guint plist_data_hash(gconstpointer key) guint plist_data_hash(gconstpointer key)
{ {
plist_data_t data = plist_get_data(key); plist_data_t data = plist_get_data((plist_t) key);
guint hash = data->type; guint hash = data->type;
guint i = 0; guint i = 0;
...@@ -467,14 +467,14 @@ guint plist_data_hash(gconstpointer key) ...@@ -467,14 +467,14 @@ guint plist_data_hash(gconstpointer key)
size = strlen(buff); size = strlen(buff);
break; break;
case PLIST_UNICODE: case PLIST_UNICODE:
buff = data->unicodeval; buff = (char *) data->unicodeval;
size = strlen(buff) * sizeof(wchar_t); size = strlen(buff) * sizeof(wchar_t);
break; break;
case PLIST_DATA: case PLIST_DATA:
case PLIST_ARRAY: case PLIST_ARRAY:
case PLIST_DICT: case PLIST_DICT:
//for these types only hash pointer //for these types only hash pointer
buff = &key; buff = (char *) &key;
size = sizeof(gconstpointer); size = sizeof(gconstpointer);
break; break;
case PLIST_DATE: case PLIST_DATE:
...@@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) ...@@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
if (!((GNode *) a)->data || !((GNode *) b)->data) if (!((GNode *) a)->data || !((GNode *) b)->data)
return FALSE; return FALSE;
plist_data_t val_a = plist_get_data(a); plist_data_t val_a = plist_get_data((plist_t) a);
plist_data_t val_b = plist_get_data(b); plist_data_t val_b = plist_get_data((plist_t) b);
if (val_a->type != val_b->type) if (val_a->type != val_b->type)
return FALSE; return FALSE;
...@@ -519,7 +519,7 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) ...@@ -519,7 +519,7 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
else else
return FALSE; return FALSE;
case PLIST_UNICODE: case PLIST_UNICODE:
if (!strcmp(val_a->unicodeval, val_b->unicodeval)) if (!wcscmp(val_a->unicodeval, val_b->unicodeval))
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
......
...@@ -30,14 +30,14 @@ ...@@ -30,14 +30,14 @@
plist_t plist_new_node(plist_data_t data) plist_t plist_new_node(plist_data_t data)
{ {
return (plist_t)g_node_new(data); return (plist_t) g_node_new(data);
} }
plist_data_t plist_get_data(plist_t node) plist_data_t plist_get_data(const plist_t node)
{ {
if (!node) if (!node)
return NULL; return NULL;
return ((GNode*)node)->data; return ((GNode *) node)->data;
} }
plist_data_t plist_new_plist_data() plist_data_t plist_new_plist_data()
...@@ -48,81 +48,57 @@ plist_data_t plist_new_plist_data() ...@@ -48,81 +48,57 @@ plist_data_t plist_new_plist_data()
void plist_free_plist_data(plist_data_t data) void plist_free_plist_data(plist_data_t data)
{ {
if (data) {
switch (data->type) {
default:
break;
}
free(data); free(data);
}
} }
void plist_new_dict(plist_t * plist) plist_t plist_new_dict()
{ {
if (*plist != NULL)
return;
plist_data_t data = plist_new_plist_data(); plist_data_t data = plist_new_plist_data();
data->type = PLIST_DICT; data->type = PLIST_DICT;
*plist = plist_new_node(data); return plist_new_node(data);
} }
void plist_new_array(plist_t * plist) plist_t plist_new_array()
{ {
if (*plist != NULL)
return;
plist_data_t data = plist_new_plist_data(); plist_data_t data = plist_new_plist_data();
data->type = PLIST_ARRAY; data->type = PLIST_ARRAY;
*plist = plist_new_node(data); return plist_new_node(data);
}
void plist_new_dict_in_plist(plist_t plist, plist_t * dict)
{
if (!plist || *dict)
return;
plist_data_t data = plist_new_plist_data();
data->type = PLIST_DICT;
*dict = plist_new_node(data);
g_node_append(plist, *dict);
} }
plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length)
/** Adds a new key pair to a dict.
*
* @param dict The dict node in the plist.
* @param key the key name of the key pair.
* @param type The the type of the value in the key pair.
* @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value
*
*/
void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length)
{ {
if (!dict || !key || !value) //only structured types are allowed to have nulll value
return; if (!value && (type == PLIST_DICT || type == PLIST_ARRAY)) {
plist_data_t data = plist_new_plist_data();
data->type = PLIST_KEY;
data->strval = strdup(key);
plist_t keynode = plist_new_node(data);
g_node_append(dict, keynode);
//now handle value //now handle value
plist_data_t val = plist_new_plist_data(); plist_data_t data = plist_new_plist_data();
val->type = type; data->type = type;
val->length = length; data->length = length;
switch (type) { switch (type) {
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
val->boolval = *((char *) value); data->boolval = *((char *) value);
break; break;
case PLIST_UINT: case PLIST_UINT:
val->intval = *((uint64_t *) value); data->intval = *((uint64_t *) value);
break; break;
case PLIST_REAL: case PLIST_REAL:
val->realval = *((double *) value); data->realval = *((double *) value);
break; break;
case PLIST_STRING: case PLIST_STRING:
val->strval = strdup((char *) value); data->strval = strdup((char *) value);
break; break;
case PLIST_UNICODE: case PLIST_UNICODE:
val->unicodeval = wcsdup((wchar_t *) value); data->unicodeval = wcsdup((wchar_t *) value);
break; break;
case PLIST_DATA: case PLIST_DATA:
memcpy(val->buff, value, length); memcpy(data->buff, value, length);
break; break;
case PLIST_ARRAY: case PLIST_ARRAY:
case PLIST_DICT: case PLIST_DICT:
...@@ -130,8 +106,13 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu ...@@ -130,8 +106,13 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu
default: default:
break; break;
} }
plist_t valnode = plist_new_node(val);
g_node_append(dict, valnode); 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) void plist_free(plist_t plist)
...@@ -141,44 +122,17 @@ void plist_free(plist_t plist) ...@@ -141,44 +122,17 @@ void plist_free(plist_t plist)
plist_t plist_get_first_child(plist_t node) plist_t plist_get_first_child(plist_t node)
{ {
return (plist_t)g_node_first_child( (GNode*)node ); return (plist_t) g_node_first_child((GNode *) node);
} }
plist_t plist_get_next_sibling(plist_t node) plist_t plist_get_next_sibling(plist_t node)
{ {
return (plist_t)g_node_next_sibling( (GNode*)node ); return (plist_t) g_node_next_sibling((GNode *) node);
} }
plist_t plist_get_prev_sibling(plist_t node) plist_t plist_get_prev_sibling(plist_t node)
{ {
return (plist_t)g_node_prev_sibling( (GNode*)node ); return (plist_t) g_node_prev_sibling((GNode *) node);
}
plist_t plist_find_query_node(plist_t plist, char *key, char *request)
{
if (!plist)
return NULL;
plist_t current = NULL;
plist_t next = NULL;
for (current = plist_get_first_child(plist); current; current = next) {
next = plist_get_next_sibling(current);
plist_data_t data = plist_get_data(current);
if (data->type == PLIST_KEY && !strcmp(data->strval, key) && next) {
data = plist_get_data(next);
if (data->type == PLIST_STRING && !strcmp(data->strval, request))
return next;
}
if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) {
plist_t sub = plist_find_query_node(current, key, request);
if (sub)
return sub;
}
}
return NULL;
} }
char compare_node_value(plist_type type, plist_data_t data, void *value) char compare_node_value(plist_type type, plist_data_t data, void *value)
......
...@@ -49,10 +49,10 @@ struct plist_data_s { ...@@ -49,10 +49,10 @@ struct plist_data_s {
plist_type type; plist_type type;
}; };
typedef struct plist_data_s* plist_data_t; typedef struct plist_data_s *plist_data_t;
plist_t plist_new_node(plist_data_t data); plist_t plist_new_node(plist_data_t data);
plist_data_t plist_get_data(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);
......
...@@ -221,7 +221,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -221,7 +221,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
break; break;
plist_data_t data = plist_new_plist_data(); plist_data_t data = plist_new_plist_data();
GNode *subnode = g_node_new(data); plist_t subnode = plist_new_node(data);
if (*plist_node) if (*plist_node)
g_node_append(*plist_node, subnode); g_node_append(*plist_node, subnode);
else else
......
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