Add yaz-json-parse utility program
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 26 Jan 2010 12:39:37 +0000 (13:39 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 26 Jan 2010 12:39:37 +0000 (13:39 +0100)
util/.gitignore
util/Makefile.am
util/json-parse.c [new file with mode: 0644]

index e952621..bdcde5e 100644 (file)
@@ -20,3 +20,4 @@ yaz-benchmark
 yaz-xmlquery
 yaz-illclient
 yaz-icu
 yaz-xmlquery
 yaz-illclient
 yaz-icu
+yaz-json-parse
index bccbaca..f1fa8dd 100644 (file)
@@ -1,7 +1,7 @@
 ## This file is part of the YAZ toolkit.
 ## Copyright (C) 1995-2010 Index Data
 
 ## This file is part of the YAZ toolkit.
 ## Copyright (C) 1995-2010 Index Data
 
-bin_SCRIPTS = yaz-asncomp yaz-config
+bin_SCRIPTS = yaz-asncomp yaz-config 
 
 EXTRA_DIST = yaz-asncomp yaz-icu-example.xml
 
 
 EXTRA_DIST = yaz-asncomp yaz-icu-example.xml
 
@@ -9,7 +9,7 @@ DISTCLEANFILES = yaz-config
 
 AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) $(ICU_CPPFLAGS)
 
 
 AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) $(ICU_CPPFLAGS)
 
-bin_PROGRAMS = yaz-marcdump yaz-iconv yaz-illclient yaz-icu
+bin_PROGRAMS = yaz-marcdump yaz-iconv yaz-illclient yaz-icu yaz-json-parse
 noinst_PROGRAMS = cclsh cql2pqf cql2xcql srwtst yaz-benchmark \
  yaz-xmlquery
 
 noinst_PROGRAMS = cclsh cql2pqf cql2xcql srwtst yaz-benchmark \
  yaz-xmlquery
 
@@ -44,3 +44,7 @@ yaz_illclient_LDADD = ../src/libyaz.la
 
 yaz_icu_SOURCES = yaz-icu.c
 yaz_icu_LDADD =../src/libyaz_icu.la ../src/libyaz.la $(ICU_LIBS)
 
 yaz_icu_SOURCES = yaz-icu.c
 yaz_icu_LDADD =../src/libyaz_icu.la ../src/libyaz.la $(ICU_LIBS)
+
+yaz_json_parse_SOURCES = json-parse.c
+yaz_json_parse_LDADD = ../src/libyaz.la
+
diff --git a/util/json-parse.c b/util/json-parse.c
new file mode 100644 (file)
index 0000000..db3e473
--- /dev/null
@@ -0,0 +1,77 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
+ * See the file LICENSE for details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <yaz/json.h>
+#include <yaz/wrbuf.h>
+#include <yaz/options.h>
+
+void usage(const char *prog)
+{
+    fprintf(stderr, "%s: [-p]\n", prog);
+    exit(1);
+}
+
+static struct json_node *do_parse_from_stdin(void)
+{
+    FILE *f = stdin;
+    WRBUF w = wrbuf_alloc();
+    struct json_node *n;
+    const char *json_str;
+    const char *err_msg;
+    int c;
+
+    while ((c = getc(f)) != EOF)
+        wrbuf_putc(w, c);
+    json_str = wrbuf_cstr(w);
+    n = json_parse(json_str, &err_msg);
+    if (!n)
+        fprintf(stderr, "JSON parse error: %s\n", err_msg);
+    wrbuf_destroy(w);
+    return n;
+}
+
+int main(int argc, char **argv)
+{
+    struct json_node *n;
+    int print = 0;
+    int ret;
+    char *arg;
+    while ((ret = options("p", argv, argc, &arg)) != YAZ_OPTIONS_EOF)
+    {
+        switch (ret)
+        {
+        case 'p':
+            print = 1;
+            break;
+        default:
+            usage(argv[0]);
+        }
+    }
+    n = do_parse_from_stdin();
+    if (!n)
+        exit(1);
+    if (print)
+    {
+        WRBUF result = wrbuf_alloc();
+        json_write_wrbuf(n, result);
+        puts(wrbuf_cstr(result));
+        wrbuf_destroy(result);
+    }
+    json_remove_node(n);
+    return 0;
+}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+