summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Dolan <mu@netsoc.tcd.ie>2012-11-26 22:22:45 +0000
committerStephen Dolan <mu@netsoc.tcd.ie>2012-11-26 22:22:45 +0000
commitd56370f734a2195fb473b8809f40358b024bb073 (patch)
tree84b18607d348a8f6328ac9af862a24c9cdb3dd8d
parent334a79b7040df4b5942da9688fbee0e5d3af9fd7 (diff)
Move some higher-level JSON manipulation functions into jv_aux.{c,h}
-rw-r--r--Makefile2
-rw-r--r--execute.c1
-rw-r--r--jv.h61
-rw-r--r--jv_aux.c62
-rw-r--r--jv_aux.h11
5 files changed, 75 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index 9c5e1635..0c2d3177 100644
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@ version.gen.h: VERSION
sed 's/.*/#define JQ_VERSION "&"/' $^ > $@
main.c: version.gen.h
-JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c
+JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c
jq_test: $(JQ_SRC) jq_test.c
diff --git a/execute.c b/execute.c
index e1e9b511..fa6b1b79 100644
--- a/execute.c
+++ b/execute.c
@@ -14,6 +14,7 @@
#include "locfile.h"
#include "jv.h"
+#include "jv_aux.h"
#include "parser.h"
#include "builtin.h"
diff --git a/jv.h b/jv.h
index e67d614a..c81b2e24 100644
--- a/jv.h
+++ b/jv.h
@@ -112,67 +112,6 @@ jv jv_parse(const char* string);
jv jv_parse_sized(const char* string, int length);
-static jv jv_lookup(jv t, jv k) {
- jv v;
- if (jv_get_kind(t) == JV_KIND_OBJECT && jv_get_kind(k) == JV_KIND_STRING) {
- v = jv_object_get(t, k);
- if (!jv_is_valid(v)) {
- jv_free(v);
- v = jv_null();
- }
- } else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
- // FIXME: don't do lookup for noninteger index
- v = jv_array_get(t, (int)jv_number_value(k));
- if (!jv_is_valid(v)) {
- jv_free(v);
- v = jv_null();
- }
- } else if (jv_get_kind(t) == JV_KIND_NULL &&
- (jv_get_kind(k) == JV_KIND_STRING || jv_get_kind(k) == JV_KIND_NUMBER)) {
- jv_free(t);
- jv_free(k);
- v = jv_null();
- } else {
- v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
- jv_kind_name(jv_get_kind(t)),
- jv_kind_name(jv_get_kind(k))));
- jv_free(t);
- jv_free(k);
- }
- return v;
-}
-
-static jv jv_modify(jv t, jv k, jv v) {
- int isnull = jv_get_kind(t) == JV_KIND_NULL;
- if (jv_get_kind(k) == JV_KIND_STRING &&
- (jv_get_kind(t) == JV_KIND_OBJECT || isnull)) {
- if (isnull) t = jv_object();
- t = jv_object_set(t, k, v);
- } else if (jv_get_kind(k) == JV_KIND_NUMBER &&
- (jv_get_kind(t) == JV_KIND_ARRAY || isnull)) {
- if (isnull) t = jv_array();
- t = jv_array_set(t, (int)jv_number_value(k), v);
- } else {
- jv err = jv_invalid_with_msg(jv_string_fmt("Cannot update field at %s index of %s",
- jv_kind_name(jv_get_kind(t)),
- jv_kind_name(jv_get_kind(v))));
- jv_free(t);
- jv_free(k);
- jv_free(v);
- t = err;
- }
- return t;
-}
-
-static jv jv_insert(jv root, jv value, jv* path, int pathlen) {
- if (pathlen == 0) {
- jv_free(root);
- return value;
- }
- return jv_modify(root, jv_copy(*path),
- jv_insert(jv_lookup(jv_copy(root), jv_copy(*path)), value, path+1, pathlen-1));
-}
-
diff --git a/jv_aux.c b/jv_aux.c
new file mode 100644
index 00000000..483a75ef
--- /dev/null
+++ b/jv_aux.c
@@ -0,0 +1,62 @@
+#include "jv_aux.h"
+
+jv jv_lookup(jv t, jv k) {
+ jv v;
+ if (jv_get_kind(t) == JV_KIND_OBJECT && jv_get_kind(k) == JV_KIND_STRING) {
+ v = jv_object_get(t, k);
+ if (!jv_is_valid(v)) {
+ jv_free(v);
+ v = jv_null();
+ }
+ } else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
+ // FIXME: don't do lookup for noninteger index
+ v = jv_array_get(t, (int)jv_number_value(k));
+ if (!jv_is_valid(v)) {
+ jv_free(v);
+ v = jv_null();
+ }
+ } else if (jv_get_kind(t) == JV_KIND_NULL &&
+ (jv_get_kind(k) == JV_KIND_STRING || jv_get_kind(k) == JV_KIND_NUMBER)) {
+ jv_free(t);
+ jv_free(k);
+ v = jv_null();
+ } else {
+ v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
+ jv_kind_name(jv_get_kind(t)),
+ jv_kind_name(jv_get_kind(k))));
+ jv_free(t);
+ jv_free(k);
+ }
+ return v;
+}
+
+jv jv_modify(jv t, jv k, jv v) {
+ int isnull = jv_get_kind(t) == JV_KIND_NULL;
+ if (jv_get_kind(k) == JV_KIND_STRING &&
+ (jv_get_kind(t) == JV_KIND_OBJECT || isnull)) {
+ if (isnull) t = jv_object();
+ t = jv_object_set(t, k, v);
+ } else if (jv_get_kind(k) == JV_KIND_NUMBER &&
+ (jv_get_kind(t) == JV_KIND_ARRAY || isnull)) {
+ if (isnull) t = jv_array();
+ t = jv_array_set(t, (int)jv_number_value(k), v);
+ } else {
+ jv err = jv_invalid_with_msg(jv_string_fmt("Cannot update field at %s index of %s",
+ jv_kind_name(jv_get_kind(t)),
+ jv_kind_name(jv_get_kind(v))));
+ jv_free(t);
+ jv_free(k);
+ jv_free(v);
+ t = err;
+ }
+ return t;
+}
+
+jv jv_insert(jv root, jv value, jv* path, int pathlen) {
+ if (pathlen == 0) {
+ jv_free(root);
+ return value;
+ }
+ return jv_modify(root, jv_copy(*path),
+ jv_insert(jv_lookup(jv_copy(root), jv_copy(*path)), value, path+1, pathlen-1));
+}
diff --git a/jv_aux.h b/jv_aux.h
new file mode 100644
index 00000000..9b91b664
--- /dev/null
+++ b/jv_aux.h
@@ -0,0 +1,11 @@
+#ifndef JV_AUX_H
+#define JV_AUX_H
+
+#include "jv.h"
+
+jv jv_lookup(jv t, jv k);
+jv jv_modify(jv t, jv k, jv v);
+jv jv_insert(jv root, jv value, jv* path, int pathlen);
+
+
+#endif