Fixed compilation and tests for case where Libxml2 is unavailable.
[yaz-moved-to-github.git] / util / marcdump.c
index 040a51b..b329696 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 1995-2006, Index Data ApS
+ * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: marcdump.c,v 1.44 2006-12-07 11:08:05 adam Exp $
+ * $Id: marcdump.c,v 1.48 2007-02-17 10:53:06 adam Exp $
  */
 
 #define _FILE_OFFSET_BITS 64
@@ -50,11 +50,52 @@ static char *prog;
 
 static void usage(const char *prog)
 {
-    fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-X] [-e] "
-             "[-I] [-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] file...\n",
+    fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] "
+             "[-i format] [-o format] "
+             "[-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] file...\n",
              prog);
 } 
 
+static int getbyte_stream(void *client_data)
+{
+    FILE *f = (FILE*) client_data;
+
+    int c = fgetc(f);
+    if (c == EOF)
+        return 0;
+    return c;
+}
+
+static void ungetbyte_stream(int c, void *client_data)
+{
+    FILE *f = (FILE*) client_data;
+
+    if (c == 0)
+        c = EOF;
+    ungetc(c, f);
+}
+
+static void marcdump_read_line(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);
+    }
+    
+    while (yaz_marc_read_line(mt, getbyte_stream,
+                              ungetbyte_stream, inf) == 0)
+    {
+        WRBUF wrbuf = wrbuf_alloc();
+        yaz_marc_write_mode(mt, wrbuf);
+        fputs(wrbuf_buf(wrbuf), stdout);
+        wrbuf_free(wrbuf, 1);
+    }
+    fclose(inf);
+}
+
 #if YAZ_HAVE_XML2
 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
 {
@@ -84,7 +125,8 @@ static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
 #endif
 
 static void dump(const char *fname, const char *from, const char *to,
-                 int read_xml, int xml,
+                 int input_format, int output_format,
+                 int write_using_libxml2,
                  int print_offset, const char *split_fname, int split_chunk,
                  int verbose, FILE *cfile, const char *leader_spec)
 {
@@ -109,18 +151,21 @@ static void dump(const char *fname, const char *from, const char *to,
         }
         yaz_marc_iconv(mt, cd);
     }
-    yaz_marc_xml(mt, xml);
+    yaz_marc_xml(mt, output_format);
+    yaz_marc_write_using_libxml2(mt, write_using_libxml2);
     yaz_marc_debug(mt, verbose);
 
-    if (read_xml)
+    if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
     {
 #if YAZ_HAVE_XML2
         marcdump_read_xml(mt, fname);
-#else
-        return;
 #endif
     }
-    else
+    else if (input_format == YAZ_MARC_LINE)
+    {
+        marcdump_read_line(mt, fname);
+    }
+    else if (input_format == YAZ_MARC_ISO2709)
     {
         FILE *inf = fopen(fname, "rb");
         int num = 1;
@@ -265,13 +310,14 @@ int main (int argc, char **argv)
     char *arg;
     int verbose = 0;
     int no = 0;
-    int xml = 0;
+    int output_format = YAZ_MARC_LINE;
     FILE *cfile = 0;
     char *from = 0, *to = 0;
-    int read_xml = 0;
+    int input_format = YAZ_MARC_ISO2709;
     int split_chunk = 1;
     const char *split_fname = 0;
     const char *leader_spec = 0;
+    int write_using_libxml2 = 0;
     
 #if HAVE_LOCALE_H
     setlocale(LC_CTYPE, "");
@@ -283,11 +329,43 @@ int main (int argc, char **argv)
 #endif
 
     prog = *argv;
-    while ((r = options("C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
+    while ((r = options("i:o:C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
     {
         no++;
         switch (r)
         {
+        case 'i':
+            input_format = yaz_marc_decode_formatstr(arg);
+            if (input_format == -1)
+            {
+                fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
+                exit(1);
+            }
+#if YAZ_HAVE_XML2
+#else
+            if (input_format == YAZ_MARC_MARCXML 
+                || input_format == YAZ_MARC_XCHANGE)
+            {
+                fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
+                exit(3);
+            }
+#endif
+            break;
+        case 'o':
+            /* dirty hack so we can make Libxml2 do the writing ..
+               rather than WRBUF */
+            if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
+            {
+                arg = arg + 4;
+                write_using_libxml2 = 1;
+            }
+            output_format = yaz_marc_decode_formatstr(arg);
+            if (output_format == -1)
+            {
+                fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
+                exit(1);
+            }
+            break;
         case 'l':
             leader_spec = arg;
             break;
@@ -303,13 +381,9 @@ int main (int argc, char **argv)
             cfile = fopen(arg, "w");
             break;
         case 'x':
-#if YAZ_HAVE_XML2
-            read_xml = 1;
-#else
-            fprintf(stderr, "%s: -x not supported."
-                    " YAZ not compiled with Libxml2 support\n", prog);
-            exit(3);
-#endif
+            fprintf(stderr, "%s: -x no longer supported. "
+                    "Use -i marcxml instead\n", prog);
+            exit(1);
             break;
         case 'O':
             fprintf(stderr, "%s: OAI MARC no longer supported."
@@ -317,16 +391,22 @@ int main (int argc, char **argv)
             exit(1);
             break;
         case 'e':
-            xml = YAZ_MARC_XCHANGE;
+            fprintf(stderr, "%s: -e no longer supported. "
+                    "Use -o marcxchange instead\n", prog);
+            exit(1);
             break;
         case 'X':
-            xml = YAZ_MARC_MARCXML;
+            fprintf(stderr, "%s: -X no longer supported. "
+                    "Use -o marcxml instead\n", prog);
+            exit(1);
             break;
         case 'I':
-            xml = YAZ_MARC_ISO2709;
+            fprintf(stderr, "%s: -I no longer supported. "
+                    "Use -o marc instead\n", prog);
+            exit(1);
             break;
         case 'n':
-            xml = YAZ_MARC_CHECK;
+            output_format = YAZ_MARC_CHECK;
             break;
         case 'p':
             print_offset = 1;
@@ -338,7 +418,8 @@ int main (int argc, char **argv)
             split_chunk = atoi(arg);
             break;
         case 0:
-            dump(arg, from, to, read_xml, xml,
+            dump(arg, from, to, input_format, output_format,
+                 write_using_libxml2,
                  print_offset, split_fname, split_chunk,
                  verbose, cfile, leader_spec);
             break;
@@ -347,7 +428,7 @@ int main (int argc, char **argv)
             break;
         default:
             usage(prog);
-            exit (1);
+            exit(1);
         }
     }
     if (cfile)