diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2013-12-29 18:03:10 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2013-12-29 18:03:10 +0000 |
commit | bb372e308e2332766d038cef7bcdf74df7b0e31c (patch) | |
tree | 3193b449d204cd4467fdc5d9fbbd595a127a94ad /main.c | |
parent | 054eb236c524a237f7cd156e8e71151b1b4e0ace (diff) | |
download | jsonpath-bb372e308e2332766d038cef7bcdf74df7b0e31c.tar.gz |
eliminate global variables and use a private parser/lexer state
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 24 |
1 files changed, 14 insertions, 10 deletions
@@ -76,23 +76,25 @@ static void export_json(struct json_object *jsobj, char *expr) { bool first; - struct jp_opcode *tree; + struct jp_state *state; struct json_object *res; - const char *error, *prefix; + const char *prefix; - tree = jp_parse(expr, &error); + state = jp_parse(expr); - if (error) + if (!state || state->error) { - fprintf(stderr, "In expression '%s': %s\n", expr, error); - return; + fprintf(stderr, "In expression '%s': %s\n", + expr, state ? state->error : "Out of memory"); + + goto out; } - res = jp_match(tree, jsobj); + res = jp_match(state->path, jsobj); - if (tree->type == T_LABEL) + if (state->path->type == T_LABEL) { - prefix = tree->str; + prefix = state->path->str; switch (json_object_get_type(res)) { @@ -163,7 +165,9 @@ export_json(struct json_object *jsobj, char *expr) printf("%s\n", json_object_to_json_string(res)); } - jp_free(); +out: + if (state) + jp_free(state); } int main(int argc, char **argv) |