diff options
author | Jonathan Beck <jonabeck@gmail.com> | 2009-01-29 20:49:00 +0100 |
---|---|---|
committer | Jonathan Beck <jonabeck@gmail.com> | 2009-01-29 20:49:00 +0100 |
commit | f1d587b73eb3d8a7379e7c44f03257e6e553ca1b (patch) | |
tree | d5fd5843816d6e23275f53eb0e213a565995088f | |
parent | 296a38509d12306e423800ed2c583a94a31eddff (diff) | |
download | libplist-f1d587b73eb3d8a7379e7c44f03257e6e553ca1b.tar.gz |
Add setter and getter for Date and Unicode types.
-rw-r--r-- | include/plist/plist.h | 6 | ||||
-rw-r--r-- | src/bplist.c | 10 | ||||
-rw-r--r-- | src/plist.c | 74 |
3 files changed, 80 insertions, 10 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h index c06d38b..76ce0ec 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h @@ -60,7 +60,8 @@ 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, const char* val, uint64_t length); - +void plist_add_sub_unicode_el(plist_t node, const char* val); +void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec); //plist free void plist_free(plist_t plist); @@ -83,6 +84,9 @@ 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); +void plist_get_unicode_val(plist_t node, char** val); +void plist_get_date_val(plist_t node, int32_t* sec, int32_t* usec); + //import and export functions void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); diff --git a/src/bplist.c b/src/bplist.c index d05c2e5..50840c2 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -332,6 +332,9 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data) dstdata->intval = srcdata->intval; break; case PLIST_DATE: + dstdata->timeval.tv_sec = srcdata->timeval.tv_sec; + dstdata->timeval.tv_usec = srcdata->timeval.tv_usec; + break; case PLIST_REAL: dstdata->realval = srcdata->realval; break; @@ -495,6 +498,9 @@ static guint plist_data_hash(gconstpointer key) size = sizeof(gconstpointer); break; case PLIST_DATE: + buff = (char *) &(data->timeval); + size = data->length; + break; default: break; } @@ -551,6 +557,10 @@ static gboolean plist_data_compare(gconstpointer a, gconstpointer b) return FALSE; break; case PLIST_DATE: + if (!memcmp(&(val_a->timeval), &(val_b->timeval),sizeof(GTimeVal))) + return TRUE; + else + return FALSE; default: break; } diff --git a/src/plist.c b/src/plist.c index 0f6d741..71059e4 100644 --- a/src/plist.c +++ b/src/plist.c @@ -82,6 +82,11 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * data->type = type; data->length = length; + glong len = 0; + glong items_read = 0; + glong items_written = 0; + GError *error = NULL; + switch (type) { case PLIST_BOOLEAN: data->boolval = *((char *) value); @@ -97,15 +102,19 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * data->strval = strdup((char *) value); break; case PLIST_UNICODE: - data->unicodeval = (gunichar2*) malloc(length * sizeof(gunichar2)); - memcpy(data->unicodeval, value, length * sizeof(gunichar2)); + len = strlen((char*)value); + data->unicodeval = g_utf8_to_utf16((char*) value, len, &items_read, &items_written, &error); + data->length = items_written; break; case PLIST_DATA: memcpy(data->buff, value, length); break; + case PLIST_DATE: + data->timeval.tv_sec = ((GTimeVal*)value)->tv_sec; + data->timeval.tv_usec = ((GTimeVal*)value)->tv_usec; + break; case PLIST_ARRAY: case PLIST_DICT: - case PLIST_DATE: default: break; } @@ -161,11 +170,13 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v res = !memcpy(data->unicodeval, value, length); break; case PLIST_DATA: - res = memcmp(data->buff, (char *) value, length); + res = !memcmp(data->buff, (char *) value, length); + break; + case PLIST_DATE: + res = !memcmp(&(data->timeval), value, sizeof(GTimeVal)); break; case PLIST_ARRAY: case PLIST_DICT: - case PLIST_DATE: default: break; } @@ -209,6 +220,12 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu if (!node) return; + //for unicode + glong len = 0; + glong items_read = 0; + glong items_written = 0; + GError *error = NULL; + plist_data_t data = plist_get_data(node); *type = data->type; @@ -229,13 +246,20 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu *((char **) value) = strdup(data->strval); break; case PLIST_UNICODE: - *((gunichar2 **) value) = malloc (*length * sizeof(gunichar2)); - memcpy(*((gunichar2 **) value), data->unicodeval, *length * sizeof(gunichar2)); + len = data->length; + *((char **) value) = g_utf16_to_utf8(data->unicodeval, len, &items_read, &items_written, &error); break; case PLIST_DATA: + *((uint8_t**) value) = (uint8_t*) malloc( *length * sizeof(uint8_t)); + memcpy(value, data->buff, *length * sizeof(uint8_t)); + break; + case PLIST_DATE: + //exception : here we use memory on the stack since it is just a temporary buffer + (*((GTimeVal**) value))->tv_sec = data->timeval.tv_sec; + (*((GTimeVal**) value))->tv_usec = data->timeval.tv_usec; + break; case PLIST_ARRAY: case PLIST_DICT: - case PLIST_DATE: default: break; } @@ -290,6 +314,17 @@ void plist_add_sub_data_el(plist_t node, const char *val, uint64_t length) plist_add_sub_element(node, PLIST_DATA, val, length); } +void plist_add_sub_unicode_el(plist_t node, const char* val) +{ + plist_add_sub_element(node, PLIST_UNICODE, val, strlen(val)); +} + +void plist_add_sub_date_el(plist_t node, int32_t sec, int32_t usec) +{ + GTimeVal val = {sec, usec}; + plist_add_sub_element(node, PLIST_UNICODE, &val, sizeof(GTimeVal)); +} + void plist_get_key_val(plist_t node, char **val) { plist_type type = plist_get_node_type(node); @@ -338,6 +373,27 @@ void plist_get_real_val(plist_t node, double *val) 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) + if (PLIST_DATA == type) plist_get_type_and_value(node, &type, (void *) val, length); } + +void plist_get_unicode_val(plist_t node, char** val) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + if (PLIST_UNICODE == type) + plist_get_type_and_value(node, &type, (void *) val, &length); + assert(length == strlen(*val)); +} + +void plist_get_date_val(plist_t node, int32_t* sec, int32_t* usec) +{ + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + GTimeVal val = {0, 0}; + if (PLIST_DATE == type) + plist_get_type_and_value(node, &type, (void *) &val, &length); + assert(length == sizeof(GTimeVal)); + *sec = val.tv_sec; + *usec = val.tv_usec; +} |