diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-22 16:52:23 -0600 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-22 16:52:23 -0600 |
commit | dd18f6182e58bb81368b30d33262e58fca7a532c (patch) | |
tree | e7767ad0ded6924ee6d9af4b59842148f9409cd6 | |
parent | aedaa9e449e03af866c0e594014cd7e78dc7c98b (diff) | |
download | yajl-dd18f6182e58bb81368b30d33262e58fca7a532c.tar.gz |
add a couple convenience routines for dealing with numbers, more copious yajl_tree reformatting
-rw-r--r-- | example/parse_config.c | 2 | ||||
-rw-r--r-- | src/api/yajl_tree.h | 54 | ||||
-rw-r--r-- | src/yajl_tree.c | 34 |
3 files changed, 40 insertions, 50 deletions
diff --git a/example/parse_config.c b/example/parse_config.c index f5d4339..e5e32b4 100644 --- a/example/parse_config.c +++ b/example/parse_config.c @@ -60,7 +60,7 @@ main(void) { const char * path[] = { "Logging", "timeFormat", (const char *) 0 }; yajl_val v = yajl_tree_get(node, path, YAJL_TYPE_STRING); - if (v) printf("Logging/timeFomat: %s\n", YAJL_TO_STRING(v)); + if (v) printf("Logging/timeFomat: %s\n", YAJL_GET_STRING(v)); else printf("no such node: %s/%s\n", path[0], path[1]); } diff --git a/src/api/yajl_tree.h b/src/api/yajl_tree.h index ea5f1c1..301c9b6 100644 --- a/src/api/yajl_tree.h +++ b/src/api/yajl_tree.h @@ -41,11 +41,11 @@ typedef struct yajl_val_s * yajl_val; typedef struct yajl_val_number_s { /** Holds the raw value of the number, in string form. */ - char *value_raw; + char *r; /** Holds the integer value of the number, if possible. */ - int64_t value_int; + long long i; /** Holds the double value of the number, if possible. */ - double value_double; + double d; /** Signals whether the \em value_int and \em value_double members are * valid. See \c YAJL_NUMBER_INT_VALID and \c YAJL_NUMBER_DOUBLE_VALID. */ unsigned int flags; @@ -157,42 +157,34 @@ YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, int type); /* Various convenience macros to check the type of a `yajl_val` */ #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_STRING)) #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NUMBER)) +#define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->data.flags & YAJL_NUMBER_INT_VALID)) +#define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->data.flags & YAJL_NUMBER_DOUBLE_VALID)) #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_OBJECT)) #define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_ARRAY )) #define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_TRUE )) #define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_FALSE )) #define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NULL )) -/** - * Convert value to string. - * - * Returns a pointer to a yajl_val_string or NULL if the value is not a - * string. - */ -#define YAJL_TO_STRING(v) (YAJL_IS_STRING(v) ? (v)->data.string : NULL) +/** Given a yajl_val_string return a ptr to the bare string it contains, + * or NULL if the value is not a string. */ +#define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->data.string : NULL) -/** - * Convert value to number. - * - * Returns a pointer to a yajl_val_number or NULL if the value is not a - * number. - */ -#define YAJL_TO_NUMBER(v) (YAJL_IS_NUMBER(v) ? &(v)->data.number : NULL) +/** Get the string representation of a number. You should check type first, + * perhaps using YAJL_IS_NUMBER */ +#define YAJL_GET_NUMBER(v) ((v)->data.number.r) -/** - * Convert value to object. - * - * Returns a pointer to a yajl_val_object or NULL if the value is not an - * object. - */ -#define YAJL_TO_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->data.object : NULL) +/** Get the double representation of a number. You should check type first, + * perhaps using YAJL_IS_DOUBLE */ +#define YAJL_GET_DOUBLE(v) ((v)->data.number.d) -/** - * Convert value to array. - * - * Returns a pointer to a yajl_val_array or NULL if the value is not an - * array. - */ -#define YAJL_TO_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->data.array : NULL) +/** Get the 64bit (long long) integer representation of a number. You should + * check type first, perhaps using YAJL_IS_INTEGER */ +#define YAJL_GET_INTEGER(v) ((v)->data.number.i) + +/** Get a pointer to a yajl_val_object or NULL if the value is not an object. */ +#define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->data.object : NULL) + +/** Get a pointer to a yajl_val_array or NULL if the value is not an object. */ +#define YAJL_GET_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->data.array : NULL) #endif /* YAJL_TREE_H */ diff --git a/src/yajl_tree.c b/src/yajl_tree.c index e4c8c8a..8760b15 100644 --- a/src/yajl_tree.c +++ b/src/yajl_tree.c @@ -67,7 +67,7 @@ static void yajl_object_free (yajl_val v) yajl_val_object *o; size_t i; - o = YAJL_TO_OBJECT (v); + o = YAJL_GET_OBJECT(v); if (o == NULL) return; for (i = 0; i < o->len; i++) @@ -88,7 +88,7 @@ static void yajl_array_free (yajl_val v) yajl_val_array *a; size_t i; - a = YAJL_TO_ARRAY (v); + a = YAJL_GET_ARRAY (v); if (a == NULL) return; for (i = 0; i < a->len; i++) @@ -161,7 +161,7 @@ static int object_add_keyval(context_t *ctx, assert (value != NULL); /* We're assuring that "obj" is an object in "context_add_value". */ - o = YAJL_TO_OBJECT (obj); + o = YAJL_GET_OBJECT (obj); assert (o != NULL); tmpk = realloc (o->keys, sizeof (*o->keys) * (o->len + 1)); @@ -194,7 +194,7 @@ static int array_add_value (context_t *ctx, assert (value != NULL); /* "context_add_value" will only call us with array values. */ - a = YAJL_TO_ARRAY (array); + a = YAJL_GET_ARRAY (array); assert (a != NULL); tmp = realloc (a->values, sizeof (*a->values) * (a->len + 1)); @@ -299,28 +299,28 @@ static int handle_number (void *ctx, const char *string, size_t string_length) v = value_alloc (YAJL_TYPE_NUMBER); if (v == NULL) RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); - n = YAJL_TO_NUMBER (v); + n = &(v->data.number); - n->value_raw = malloc (string_length + 1); - if (n->value_raw == NULL) + n->r = malloc (string_length + 1); + if (n->r == NULL) { free (v); RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); } - memcpy (n->value_raw, string, string_length); - n->value_raw[string_length] = 0; + memcpy (n->r, string, string_length); + n->r[string_length] = 0; n->flags = 0; endptr = NULL; errno = 0; - n->value_int = (int64_t) strtoll (n->value_raw, &endptr, /* base = */ 10); + n->i = (int64_t) strtoll (n->r, &endptr, /* base = */ 10); if ((errno == 0) && (endptr != NULL) && (*endptr == 0)) n->flags |= YAJL_NUMBER_INT_VALID; endptr = NULL; errno = 0; - n->value_double = strtod (n->value_raw, &endptr); + n->d = strtod (n->r, &endptr); if ((errno == 0) && (endptr != NULL) && (*endptr == 0)) n->flags |= YAJL_NUMBER_DOUBLE_VALID; @@ -336,7 +336,7 @@ static int handle_start_map (void *ctx) if (v == NULL) RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); - o = YAJL_TO_OBJECT (v); + o = YAJL_GET_OBJECT (v); o->keys = NULL; o->values = NULL; o->len = 0; @@ -364,7 +364,7 @@ static int handle_start_array (void *ctx) if (v == NULL) RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); - a = YAJL_TO_ARRAY (v); + a = YAJL_GET_ARRAY (v); a->values = NULL; a->len = 0; @@ -492,16 +492,14 @@ void yajl_tree_free (yajl_val v) } else if (YAJL_IS_NUMBER(v)) { - yajl_val_number *n = YAJL_TO_NUMBER(v); - - free(n->value_raw); + free(v->data.number.r); free(v); } - else if (YAJL_TO_OBJECT(v)) + else if (YAJL_GET_OBJECT(v)) { yajl_object_free(v); } - else if (YAJL_TO_ARRAY(v)) + else if (YAJL_GET_ARRAY(v)) { yajl_array_free(v); } |