Skip to content

Commit

Permalink
[libu] json += missing doxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tho committed May 27, 2010
1 parent dac025c commit bbf1dce
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/toolbox/json.h
Expand Up @@ -43,7 +43,7 @@ int u_json_parse (const char *json, u_json_obj_t **pjo);
int u_json_obj_new (u_json_obj_t **pjo);
int u_json_obj_set_key (u_json_obj_t *jo, const char *key);
int u_json_obj_set_val (u_json_obj_t *jo, const char *val);
int u_json_obj_set_type (u_json_obj_t *jo, int type);
int u_json_obj_set_type (u_json_obj_t *jo, u_json_type_t type);
void u_json_obj_free (u_json_obj_t *jo);
int u_json_obj_add (u_json_obj_t *head, u_json_obj_t *jo);

Expand Down
93 changes: 56 additions & 37 deletions srcs/toolbox/json.c
Expand Up @@ -316,6 +316,62 @@ int u_json_encode (u_json_obj_t *jo, const char **ps)
return ~0;
}

/**
* \brief Pre/post-order tree walker
*
* Traverse the supplied JSON object \p jo in pre/post-order, depending on
* \p strategy, invoking the callback function \p cb on each node.
*
* \param jo Pointer to ::u_json_obj_t object to traverse
* \param strategy one of ::U_JSON_WALK_PREORDER or ::U_JSON_WALK_POSTORDER
* \param l depth level in the JSON tree (the root is at depth 0)
* \param cb function to invoke on each traversed node
*
* \return nothing
*/
void u_json_obj_walk (u_json_obj_t *jo, int strategy, size_t l,
void (*cb)(u_json_obj_t *, size_t))
{
dbg_return_if (strategy != U_JSON_WALK_PREORDER &&
strategy != U_JSON_WALK_POSTORDER, );

if (jo == NULL)
return;

if (strategy == U_JSON_WALK_PREORDER && cb)
cb(jo, l);

/* When recurring into the children branch, increment depth by one. */
u_json_obj_walk(TAILQ_FIRST(&jo->children), strategy, l + 1, cb);

/* Siblings are at the same depth as the current node. */
u_json_obj_walk(TAILQ_NEXT(jo, siblings), strategy, l, cb);

if (strategy == U_JSON_WALK_POSTORDER && cb)
cb(jo, l);

return;
}

/**
* \brief Print to stderr the internal representation of a JSON object
*
* Print to stderr the supplied JSON object \p jo
*
* \param jo Pointer to the ::u_json_obj_t object that must be printed
*
* \return nothing
*/
void u_json_obj_print (u_json_obj_t *jo)
{
dbg_return_if (jo == NULL, );

/* Tree root is at '0' depth. */
u_json_obj_walk(jo, U_JSON_WALK_PREORDER, 0, u_json_obj_do_print);

return;
}

/**
* \}
*/
Expand Down Expand Up @@ -366,43 +422,6 @@ static int u_json_do_encode (u_json_obj_t *jo, u_string_t *s)
return ~0;
}


/* {Pre,post}-order tree walker, depending on 'strategy'. */
void u_json_obj_walk (u_json_obj_t *jo, int strategy, size_t l,
void (*cb)(u_json_obj_t *, size_t))
{
dbg_return_if (
strategy != U_JSON_WALK_PREORDER &&
strategy != U_JSON_WALK_POSTORDER, );

if (jo == NULL)
return;

if (strategy == U_JSON_WALK_PREORDER && cb)
cb(jo, l);

/* When recurring into the children branch, increment depth by one. */
u_json_obj_walk(TAILQ_FIRST(&jo->children), strategy, l + 1, cb);

/* Siblings are at the same depth as the current node. */
u_json_obj_walk(TAILQ_NEXT(jo, siblings), strategy, l, cb);

if (strategy == U_JSON_WALK_POSTORDER && cb)
cb(jo, l);

return;
}

void u_json_obj_print (u_json_obj_t *jo)
{
dbg_return_if (jo == NULL, );

/* Tree root is at '0' depth. */
u_json_obj_walk(jo, U_JSON_WALK_PREORDER, 0, u_json_obj_do_print);

return;
}

static int u_json_match_value (u_lexer_t *jl, u_json_obj_t *jo)
{
dbg_return_if (jl == NULL, ~0);
Expand Down

0 comments on commit bbf1dce

Please sign in to comment.