X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fmarcdump.c;h=850331b48e8a4b7ca3670229f75903ef716e882e;hp=891174b3bc2e8885b1cd995b6d4219fd047cf875;hb=e3e6a89b0ebc5270208139c22d68735ee6626a19;hpb=f05461c79b402d35bb6ca5feb75a848f4a5a3c7d diff --git a/util/marcdump.c b/util/marcdump.c index 891174b..850331b 100644 --- a/util/marcdump.c +++ b/util/marcdump.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2013 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ @@ -42,6 +42,7 @@ #endif #include +#include #include #include #include @@ -117,6 +118,53 @@ static void marcdump_read_line(yaz_marc_t mt, const char *fname) fclose(inf); } +static void marcdump_read_json(yaz_marc_t mt, const char *fname) +{ + FILE *inf = fopen(fname, "rb"); + if (!inf) + { + fprintf(stderr, "%s: cannot open %s:%s\n", + prog, fname, strerror(errno)); + exit(1); + } + else + { + const char *errmsg; + size_t errpos; + WRBUF w = wrbuf_alloc(); + struct json_node *n; + int c; + + while ((c = getc(inf)) != EOF) + wrbuf_putc(w, c); + n = json_parse2(wrbuf_cstr(w), &errmsg, &errpos); + if (n) + { + int r = yaz_marc_read_json_node(mt, n); + if (r == 0) + { + wrbuf_rewind(w); + yaz_marc_write_mode(mt, w); + fputs(wrbuf_cstr(w), stdout); + wrbuf_rewind(w); + } + else + { + fprintf(stderr, "%s: JSON MARC parsing failed ret=%d\n", fname, + r); + } + } + else + { + fprintf(stderr, "%s: JSON parse error: %s . pos=%ld\n", fname, + errmsg, (long) errpos); + } + wrbuf_destroy(w); + fclose(inf); + } +} + + #if YAZ_HAVE_XML2 static void marcdump_read_xml(yaz_marc_t mt, const char *fname) { @@ -245,6 +293,10 @@ static void dump(const char *fname, const char *from, const char *to, { marcdump_read_line(mt, fname); } + else if (input_format == YAZ_MARC_JSON) + { + marcdump_read_json(mt, fname); + } else if (input_format == YAZ_MARC_ISO2709) { FILE *inf = fopen(fname, "rb");