diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-22 16:32:59 -0600 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-22 16:32:59 -0600 |
commit | baf9c39797fce24b89398a3e94ff27ae4074867a (patch) | |
tree | 6103a99867b21a5d844bb84eb49e84c4f393d1ec | |
parent | 742a1cd7c66d64d45e25a016e506d555834f087b (diff) | |
download | yajl-baf9c39797fce24b89398a3e94ff27ae4074867a.tar.gz |
be terse & piss all over that tree.
-rw-r--r-- | example/parse_config.c | 4 | ||||
-rw-r--r-- | src/api/yajl_tree.h | 109 | ||||
-rw-r--r-- | src/yajl_tree.c | 132 |
3 files changed, 98 insertions, 147 deletions
diff --git a/example/parse_config.c b/example/parse_config.c index ae95be1..f5d4339 100644 --- a/example/parse_config.c +++ b/example/parse_config.c @@ -25,7 +25,7 @@ int main(void) { size_t rd; - yajl_value_t * node; + yajl_val node; char errbuf[1024]; /* null plug buffers */ @@ -59,7 +59,7 @@ main(void) /* now extract a nested value from the config file */ { const char * path[] = { "Logging", "timeFormat", (const char *) 0 }; - yajl_value_t * v = yajl_tree_get(node, path, YAJL_TYPE_STRING); + yajl_val v = yajl_tree_get(node, path, YAJL_TYPE_STRING); if (v) printf("Logging/timeFomat: %s\n", YAJL_TO_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 7cfdad2..7ba095a 100644 --- a/src/api/yajl_tree.h +++ b/src/api/yajl_tree.h @@ -31,15 +31,14 @@ #include <yajl/yajl_common.h> -/* Forward declaration, because "yajl_value_object_t" and "yajl_value_array_t" - * contain "yajl_value_t" and "yajl_value_t" can be an object or an array. */ -struct yajl_value_s; -typedef struct yajl_value_s yajl_value_t; +/* Forward declaration, because "yajl_val_object_t" and "yajl_val_array" + * contain "yajl_val" and "yajl_val" can be an object or an array. */ +typedef struct yajl_val_s * yajl_val; #define YAJL_NUMBER_INT_VALID 0x01 #define YAJL_NUMBER_DOUBLE_VALID 0x02 /** Structure describing a JSON number. */ -struct yajl_value_number_s +typedef struct yajl_val_number_s { /** Holds the raw value of the number, in string form. */ char *value_raw; @@ -50,38 +49,35 @@ struct yajl_value_number_s /** 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; -}; -typedef struct yajl_value_number_s yajl_value_number_t; +} yajl_val_number; /** * Structure describing a JSON object. * - * \sa yajl_value_array_s + * \sa yajl_val_array_s */ -struct yajl_value_object_s +typedef struct yajl_val_object_s { /** Array of keys in the JSON object. */ - yajl_value_t **keys; + yajl_val *keys; /** Array of values in the JSON object. */ - yajl_value_t **values; + yajl_val *values; /** Number of key-value-pairs in the JSON object. */ - size_t children_num; -}; -typedef struct yajl_value_object_s yajl_value_object_t; + size_t len; +} yajl_val_object; /** * Structure describing a JSON array. * - * \sa yajl_value_object_s + * \sa yajl_val_object_s */ -struct yajl_value_array_s +typedef struct yajl_val_array_s { /** Array of elements in the JSON array. */ - yajl_value_t **values; + yajl_val *values; /** Number of elements in the JSON array. */ - size_t values_num; -}; -typedef struct yajl_value_array_s yajl_value_array_t; + size_t len; +} yajl_val_array; #define YAJL_TYPE_STRING 1 #define YAJL_TYPE_NUMBER 2 @@ -99,10 +95,10 @@ typedef struct yajl_value_array_s yajl_value_array_t; * the "YAJL_IS_*" and "YAJL_TO_*" macros below to check for the correct type * and cast the struct. * - * \sa yajl_value_string_t, yajl_value_number_t, yajl_value_object_t, - * yajl_value_array_t + * \sa yajl_val_string, yajl_val_number, yajl_val_object, + * yajl_val_array */ -struct yajl_value_s +struct yajl_val_s { /** Type of the value contained. Use the "YAJL_IS_*" macors to check for a * specific type. */ @@ -112,9 +108,9 @@ struct yajl_value_s union { char * string; - yajl_value_number_t number; - yajl_value_object_t object; - yajl_value_array_t array; + yajl_val_number number; + yajl_val_object object; + yajl_val_array array; } data; }; @@ -140,8 +136,8 @@ struct yajl_value_s * null terminated message describing the error in more detail is stored in * \em error_buffer if it is not \c NULL. */ -YAJL_API yajl_value_t *yajl_tree_parse (const char *input, - char *error_buffer, size_t error_buffer_size); +YAJL_API yajl_val yajl_tree_parse (const char *input, + char *error_buffer, size_t error_buffer_size); /** * Free a parse tree. @@ -151,68 +147,26 @@ YAJL_API yajl_value_t *yajl_tree_parse (const char *input, * \param v Pointer to a JSON value returned by "yajl_tree_parse". Passing NULL * is valid and results in a no-op. */ -YAJL_API void yajl_tree_free (yajl_value_t *v); +YAJL_API void yajl_tree_free (yajl_val v); /** * Access a nested value. */ -YAJL_API yajl_value_t * yajl_tree_get(yajl_value_t * parent, - const char ** path, - int type); +YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, int type); -/** - * Checks if value is a string. - * - * Returns true if the value is a string, false otherwise. - */ +/* Various convenience macros to check the type of a `yajl_val` */ #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_STRING)) - -/** - * Checks if value is a number. - * - * Returns true if the value is a number, false otherwise. - */ #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NUMBER)) - -/** - * Checks if value is an object. - * - * Returns true if the value is a object, false otherwise. - */ #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_OBJECT)) - -/** - * Checks if value is an array. - * - * Returns true if the value is a array, false otherwise. - */ #define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_ARRAY )) - -/** - * Checks if value is true. - * - * Returns true if the value is a boolean and true, false otherwise. - */ #define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_TRUE )) - -/** - * Checks if value is false. - * - * Returns true if the value is a boolean and false, false otherwise. - */ #define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_FALSE )) - -/** - * Checks if value is null. - * - * Returns true if the value is null, false otherwise. - */ #define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NULL )) /** * Convert value to string. * - * Returns a pointer to a yajl_value_string_t or NULL if the value is not a + * 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) @@ -220,7 +174,7 @@ YAJL_API yajl_value_t * yajl_tree_get(yajl_value_t * parent, /** * Convert value to number. * - * Returns a pointer to a yajl_value_number_t or NULL if the value is not a + * 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) @@ -228,7 +182,7 @@ YAJL_API yajl_value_t * yajl_tree_get(yajl_value_t * parent, /** * Convert value to object. * - * Returns a pointer to a yajl_value_object_t or NULL if the value is not an + * 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) @@ -236,10 +190,9 @@ YAJL_API yajl_value_t * yajl_tree_get(yajl_value_t * parent, /** * Convert value to array. * - * Returns a pointer to a yajl_value_array_t or NULL if the value is not an + * 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) #endif /* YAJL_TREE_H */ -/* vim: set sw=2 sts=2 et : */ diff --git a/src/yajl_tree.c b/src/yajl_tree.c index d52f9d7..f2c7dcf 100644 --- a/src/yajl_tree.c +++ b/src/yajl_tree.c @@ -30,15 +30,15 @@ struct stack_elem_s; typedef struct stack_elem_s stack_elem_t; struct stack_elem_s { - yajl_value_t *key; - yajl_value_t *value; + yajl_val key; + yajl_val value; stack_elem_t *next; }; struct context_s { stack_elem_t *stack; - yajl_value_t *root; + yajl_val root; char *errbuf; size_t errbuf_size; }; @@ -50,9 +50,9 @@ typedef struct context_s context_t; return (retval); \ } while (0) \ -static yajl_value_t *value_alloc (uint8_t type) +static yajl_val value_alloc (uint8_t type) { - yajl_value_t *v; + yajl_val v; v = malloc (sizeof (*v)); if (v == NULL) return (NULL); @@ -62,15 +62,15 @@ static yajl_value_t *value_alloc (uint8_t type) return (v); } -static void yajl_object_free (yajl_value_t *v) +static void yajl_object_free (yajl_val v) { - yajl_value_object_t *o; + yajl_val_object *o; size_t i; o = YAJL_TO_OBJECT (v); if (o == NULL) return; - for (i = 0; i < o->children_num; i++) + for (i = 0; i < o->len; i++) { yajl_tree_free (o->keys[i]); o->keys[i] = NULL; @@ -83,15 +83,15 @@ static void yajl_object_free (yajl_value_t *v) free (v); } -static void yajl_array_free (yajl_value_t *v) +static void yajl_array_free (yajl_val v) { - yajl_value_array_t *a; + yajl_val_array *a; size_t i; a = YAJL_TO_ARRAY (v); if (a == NULL) return; - for (i = 0; i < a->values_num; i++) + for (i = 0; i < a->len; i++) { yajl_tree_free (a->values[i]); a->values[i] = NULL; @@ -108,7 +108,7 @@ static void yajl_array_free (yajl_value_t *v) * reached (an appropriate closing bracket has been read), the value is popped * off the stack and added to the enclosing object using "context_add_value". */ -static int context_push (context_t *ctx, yajl_value_t *v) +static int context_push(context_t *ctx, yajl_val v) { stack_elem_t *stack; @@ -128,10 +128,10 @@ static int context_push (context_t *ctx, yajl_value_t *v) return (0); } -static yajl_value_t *context_pop (context_t *ctx) +static yajl_val context_pop(context_t *ctx) { stack_elem_t *stack; - yajl_value_t *v; + yajl_val v; if (ctx->stack == NULL) RETURN_ERROR (ctx, NULL, "context_pop: " @@ -147,11 +147,11 @@ static yajl_value_t *context_pop (context_t *ctx) return (v); } -static int object_add_keyval (context_t *ctx, - yajl_value_t *obj, yajl_value_t *key, yajl_value_t *value) +static int object_add_keyval(context_t *ctx, + yajl_val obj, yajl_val key, yajl_val value) { - yajl_value_object_t *o; - yajl_value_t **tmp; + yajl_val_object *o; + yajl_val *tmp; /* We're checking for NULL in "context_add_value" or its callers. */ assert (ctx != NULL); @@ -166,28 +166,28 @@ static int object_add_keyval (context_t *ctx, o = YAJL_TO_OBJECT (obj); assert (o != NULL); - tmp = realloc (o->keys, sizeof (*o->keys) * (o->children_num + 1)); + tmp = realloc (o->keys, sizeof (*o->keys) * (o->len + 1)); if (tmp == NULL) RETURN_ERROR (ctx, ENOMEM, "Out of memory"); o->keys = tmp; - tmp = realloc (o->values, sizeof (*o->values) * (o->children_num + 1)); + tmp = realloc (o->values, sizeof (*o->values) * (o->len + 1)); if (tmp == NULL) RETURN_ERROR (ctx, ENOMEM, "Out of memory"); o->values = tmp; - o->keys[o->children_num] = key; - o->values[o->children_num] = value; - o->children_num++; + o->keys[o->len] = key; + o->values[o->len] = value; + o->len++; return (0); } static int array_add_value (context_t *ctx, - yajl_value_t *array, yajl_value_t *value) + yajl_val array, yajl_val value) { - yajl_value_array_t *a; - yajl_value_t **tmp; + yajl_val_array *a; + yajl_val *tmp; /* We're checking for NULL pointers in "context_add_value" or its * callers. */ @@ -199,12 +199,12 @@ static int array_add_value (context_t *ctx, a = YAJL_TO_ARRAY (array); assert (a != NULL); - tmp = realloc (a->values, sizeof (*a->values) * (a->values_num + 1)); + tmp = realloc (a->values, sizeof (*a->values) * (a->len + 1)); if (tmp == NULL) RETURN_ERROR (ctx, ENOMEM, "Out of memory"); a->values = tmp; - a->values[a->values_num] = value; - a->values_num++; + a->values[a->len] = value; + a->len++; return (0); } @@ -213,7 +213,7 @@ static int array_add_value (context_t *ctx, * Add a value to the value on top of the stack or the "root" member in the * context if the end of the parsing process is reached. */ -static int context_add_value (context_t *ctx, yajl_value_t *v) +static int context_add_value (context_t *ctx, yajl_val v) { /* We're checking for NULL values in all the calling functions. */ assert (ctx != NULL); @@ -250,7 +250,7 @@ static int context_add_value (context_t *ctx, yajl_value_t *v) } else /* if (ctx->key != NULL) */ { - yajl_value_t *key; + yajl_val key; key = ctx->stack->key; ctx->stack->key = NULL; @@ -272,7 +272,7 @@ static int context_add_value (context_t *ctx, yajl_value_t *v) static int handle_string (void *ctx, const unsigned char *string, size_t string_length) { - yajl_value_t *v; + yajl_val v; v = value_alloc (YAJL_TYPE_STRING); if (v == NULL) @@ -292,8 +292,8 @@ static int handle_string (void *ctx, static int handle_number (void *ctx, const char *string, size_t string_length) { - yajl_value_t *v; - yajl_value_number_t *n; + yajl_val v; + yajl_val_number *n; char *endptr; v = value_alloc (YAJL_TYPE_NUMBER); @@ -329,8 +329,8 @@ static int handle_number (void *ctx, const char *string, size_t string_length) static int handle_start_map (void *ctx) { - yajl_value_t *v; - yajl_value_object_t *o; + yajl_val v; + yajl_val_object *o; v = value_alloc (YAJL_TYPE_OBJECT); if (v == NULL) @@ -339,14 +339,14 @@ static int handle_start_map (void *ctx) o = YAJL_TO_OBJECT (v); o->keys = NULL; o->values = NULL; - o->children_num = 0; + o->len = 0; return ((context_push (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); } static int handle_end_map (void *ctx) { - yajl_value_t *v; + yajl_val v; v = context_pop (ctx); if (v == NULL) @@ -357,8 +357,8 @@ static int handle_end_map (void *ctx) static int handle_start_array (void *ctx) { - yajl_value_t *v; - yajl_value_array_t *a; + yajl_val v; + yajl_val_array *a; v = value_alloc (YAJL_TYPE_ARRAY); if (v == NULL) @@ -366,14 +366,14 @@ static int handle_start_array (void *ctx) a = YAJL_TO_ARRAY (v); a->values = NULL; - a->values_num = 0; + a->len = 0; return ((context_push (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); } static int handle_end_array (void *ctx) { - yajl_value_t *v; + yajl_val v; v = context_pop (ctx); if (v == NULL) @@ -384,7 +384,7 @@ static int handle_end_array (void *ctx) static int handle_boolean (void *ctx, int boolean_value) { - yajl_value_t *v; + yajl_val v; v = value_alloc (boolean_value ? YAJL_TYPE_TRUE : YAJL_TYPE_FALSE); if (v == NULL) @@ -395,7 +395,7 @@ static int handle_boolean (void *ctx, int boolean_value) static int handle_null (void *ctx) { - yajl_value_t *v; + yajl_val v; v = value_alloc (YAJL_TYPE_NULL); if (v == NULL) @@ -407,8 +407,8 @@ static int handle_null (void *ctx) /* * Public functions */ -yajl_value_t *yajl_tree_parse (const char *input, - char *error_buffer, size_t error_buffer_size) +yajl_val yajl_tree_parse (const char *input, + char *error_buffer, size_t error_buffer_size) { static const yajl_callbacks callbacks = { @@ -442,9 +442,9 @@ yajl_value_t *yajl_tree_parse (const char *input, handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, 1); - status = yajl_parse (handle, - (unsigned char *) input, - strlen (input)); + status = yajl_parse(handle, + (unsigned char *) input, + strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { @@ -462,53 +462,51 @@ yajl_value_t *yajl_tree_parse (const char *input, return (ctx.root); } -yajl_value_t * yajl_tree_get(yajl_value_t * n, - const char ** path, - int type) +yajl_val yajl_tree_get(yajl_val n, const char ** path, int type) { if (!path) return NULL; while (n && *path) { unsigned int i; if (n->type != YAJL_TYPE_OBJECT) return NULL; - for (i = 0; i < n->data.object.children_num; i++) { + for (i = 0; i < n->data.object.len; i++) { if (!strcmp(*path, n->data.object.keys[i]->data.string)) { n = n->data.object.values[i]; break; } } - if (i == n->data.object.children_num) return NULL; + if (i == n->data.object.len) return NULL; path++; } return n; } -void yajl_tree_free (yajl_value_t *v) +void yajl_tree_free (yajl_val v) { if (v == NULL) return; - if (YAJL_IS_STRING (v)) + if (YAJL_IS_STRING(v)) { - free (v->data.string); - free (v); + free(v->data.string); + free(v); } - else if (YAJL_IS_NUMBER (v)) + else if (YAJL_IS_NUMBER(v)) { - yajl_value_number_t *n = YAJL_TO_NUMBER (v); + yajl_val_number *n = YAJL_TO_NUMBER(v); - free (n->value_raw); - free (v); + free(n->value_raw); + free(v); } - else if (YAJL_TO_OBJECT (v)) + else if (YAJL_TO_OBJECT(v)) { - yajl_object_free (v); + yajl_object_free(v); } - else if (YAJL_TO_ARRAY (v)) + else if (YAJL_TO_ARRAY(v)) { - yajl_array_free (v); + yajl_array_free(v); } else /* if (YAJL_TYPE_TRUE or YAJL_TYPE_FALSE or YAJL_TYPE_NULL) */ { - free (v); + free(v); } } |