Command line tool yaz-record-conv
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 30 Apr 2010 19:44:48 +0000 (21:44 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 30 Apr 2010 19:44:48 +0000 (21:44 +0200)
This is a simple program that allows testing of the record-conv
utilities of YAZ (used by YAZ GFS and Metaproxy).

util/.gitignore
util/Makefile.am
util/yaz-record-conv.c [new file with mode: 0644]

index bdcde5e..352981d 100644 (file)
@@ -21,3 +21,4 @@ yaz-xmlquery
 yaz-illclient
 yaz-icu
 yaz-json-parse
+yaz-record-conv
index f1fa8dd..be5bf68 100644 (file)
@@ -11,7 +11,7 @@ AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) $(ICU_CPPFLAGS)
 
 bin_PROGRAMS = yaz-marcdump yaz-iconv yaz-illclient yaz-icu yaz-json-parse
 noinst_PROGRAMS = cclsh cql2pqf cql2xcql srwtst yaz-benchmark \
- yaz-xmlquery
+ yaz-xmlquery yaz-record-conv
 
 # MARC dumper utility
 yaz_marcdump_SOURCES = marcdump.c
@@ -48,3 +48,6 @@ 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
 
+yaz_record_conv_SOURCES = yaz-record-conv.c
+yaz_record_conv_LDADD = ../src/libyaz.la
+
diff --git a/util/yaz-record-conv.c b/util/yaz-record-conv.c
new file mode 100644 (file)
index 0000000..4f943b6
--- /dev/null
@@ -0,0 +1,112 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
+ * See the file LICENSE for details.
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <yaz/options.h>
+#include <yaz/record_conv.h>
+
+const char *prog = "yaz-record-conv";
+
+static void usage(void)
+{
+    fprintf(stderr, "%s: usage\nyaz-record-conf config file ..\n", prog);
+    exit(1);
+}
+int main (int argc, char **argv)
+{
+    int r;
+    char *arg;
+    yaz_record_conv_t p = 0;
+    int no_errors = 0;
+    while ((r = options("V", argv, argc, &arg)) != -2)
+    {
+        switch (r)
+        {
+        case 'V':
+            break;
+        case 0:
+            if (!p)
+            {
+                xmlDocPtr doc = xmlParseFile(arg);
+                int r = -1;
+
+                p = yaz_record_conv_create();
+                if (doc)
+                {
+                    xmlNodePtr ptr = xmlDocGetRootElement(doc);
+                    if (ptr)
+                    {
+                        r = yaz_record_conv_configure(p, ptr);
+                        if (r)
+                        {
+                            fprintf(stderr, "record conf error: %s\n",
+                                    yaz_record_conv_get_error(p));
+                        }
+                    }
+                }
+                xmlFreeDoc(doc);
+                if (r)
+                {
+                    yaz_record_conv_destroy(p);
+                    exit(2);
+                }
+            }
+            else
+            {
+                WRBUF input_record = wrbuf_alloc();
+                WRBUF output_record = wrbuf_alloc();
+                FILE *f = fopen(arg, "rb");
+                int c, r;
+                if (!f)
+                {
+                    fprintf(stderr, "%s: open failed: %s\n",
+                            prog, arg);
+                    exit(3);
+                }
+                while ((c = getc(f)) != EOF)
+                    wrbuf_putc(input_record, c);
+                
+                r = yaz_record_conv_record(p, 
+                                           wrbuf_buf(input_record),
+                                           wrbuf_len(input_record),
+                                           output_record);
+                if (r)
+                {
+                    fprintf(stderr, "%s: %s: Error %s\n",
+                            prog, arg, 
+                            yaz_record_conv_get_error(p));
+                    no_errors++;
+                }
+                else
+                {
+                    fwrite(wrbuf_buf(output_record), 1,
+                           wrbuf_len(output_record), stdout);
+                }
+                wrbuf_destroy(input_record);
+                wrbuf_destroy(output_record);
+                fclose(f);
+            }
+            break;
+        default:
+            usage();
+        }
+    }   
+    yaz_record_conv_destroy(p);
+    if (no_errors)
+        exit(1);
+    exit(0);
+}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+