OAI MARC fix
[yaz-moved-to-github.git] / util / marcdisp.c
index 03bb37c..342e5d1 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2002, Index Data
  * See the file LICENSE for details.
  *
- * $Id: marcdisp.c,v 1.17 2002-02-01 14:50:29 adam Exp $
+ * $Id: marcdisp.c,v 1.22 2002-10-04 11:24:55 adam Exp $
  */
 
 #if HAVE_CONFIG_H
@@ -16,8 +16,7 @@
 #include <yaz/wrbuf.h>
 #include <yaz/yaz-util.h>
 
-int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
-                       int bsize)
+int yaz_marc_decode (const char *buf, WRBUF wr, int debug, int bsize, int xml)
 {
     int entry_p;
     int record_length;
@@ -57,9 +56,47 @@ int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
     length_starting = atoi_n (buf+21, 1);
     length_implementation = atoi_n (buf+22, 1);
 
+    if (xml)
+    {
+        char str[80];
+        int i;
+        if (xml > 1)
+        {
+            wrbuf_puts(
+                wr,
+                "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
+                "\n"
+                " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
+                "\n"
+                " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
+                "\n"
+                );
+            
+            sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
+                     buf[5], buf[6], buf[7]);
+            wrbuf_puts (wr, str);
+        }
+        else
+        {
+            wrbuf_puts (wr, "<iso2709\n");
+            sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
+            wrbuf_puts (wr, str);
+            sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
+            wrbuf_puts (wr, str);
+            for (i = 1; i<=19; i++)
+            {
+                sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
+                wrbuf_puts (wr, str);
+            }
+            wrbuf_puts (wr, ">\n");
+        }
+    }
     if (debug)
     {
        char str[40];
+
+        if (xml)
+            wrbuf_puts (wr, "<!--\n");
        sprintf (str, "Record length         %5d\n", record_length);
        wrbuf_puts (wr, str);
        sprintf (str, "Indicator length      %5d\n", indicator_length);
@@ -74,7 +111,10 @@ int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
        wrbuf_puts (wr, str);
        sprintf (str, "Length implementation %5d\n", length_implementation);
        wrbuf_puts (wr, str);
+        if (xml)
+            wrbuf_puts (wr, "-->\n");
     }
+
     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
     {
         entry_p += 3+length_data_entry+length_starting;
@@ -94,10 +134,6 @@ int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
         memcpy (tag, buf+entry_p, 3);
        entry_p += 3;
         tag[3] = '\0';
-       if (debug)
-           wrbuf_puts (wr, "Tag: ");
-       wrbuf_puts (wr, tag);
-       wrbuf_puts (wr, " ");
        data_length = atoi_n (buf+entry_p, length_data_entry);
        entry_p += length_data_entry;
        data_offset = atoi_n (buf+entry_p, length_starting);
@@ -112,48 +148,149 @@ int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
         }
         else if (!memcmp (tag, "00", 2))
             identifier_flag = 0;
+
+
+        if (xml)
+        {
+            if (xml > 1)
+            {
+                if (identifier_flag)
+                    wrbuf_puts (wr, "<varfield id=\"");
+                else
+                    wrbuf_puts (wr, "<fixfield id=\"");
+                wrbuf_puts (wr, tag);
+                wrbuf_puts (wr, "\"");
+            }
+            else
+            {
+                wrbuf_puts (wr, "<field tag=\"");
+                wrbuf_puts (wr, tag);
+                wrbuf_puts (wr, "\"");
+            }
+        }
+        else
+        {
+            if (debug)
+                wrbuf_puts (wr, "Tag: ");
+            wrbuf_puts (wr, tag);
+            wrbuf_puts (wr, " ");
+        }
         
         if (identifier_flag)
        {
-            if (debug)
+            if (debug && !xml)
                 wrbuf_puts (wr, " Ind: ");
             for (j = 0; j<indicator_length; j++, i++)
-               wrbuf_putc (wr, buf[i]);
+            {
+                if (xml)
+                {
+                    char nostr[30];
+                    if (xml > 1)
+                        sprintf (nostr, " i%d=\"%c\"", j+1, buf[i]);
+                    else
+                        sprintf (nostr, " Indicator%d=\"%c\"", j+1, buf[i]);
+                    wrbuf_puts (wr, nostr);
+                }
+                else
+                    wrbuf_putc (wr, buf[i]);
+            }
        }
-        if (debug)
-            wrbuf_puts (wr, " Fields: ");
+        if (xml)
+        {
+            wrbuf_puts (wr, ">");
+            if (identifier_flag)
+                wrbuf_puts (wr, "\n");
+        }
+        else
+        {
+            if (debug && !xml)
+                wrbuf_puts (wr, " Fields: ");
+        }
        while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
        {
             if (identifier_flag)
            {
                i++;
-               wrbuf_puts (wr, " $"); 
-                for (j = 1; j<identifier_length; j++, i++)
-                   wrbuf_putc (wr, buf[i]);
-               wrbuf_putc (wr, ' ');
+                if (xml)
+                {
+                    if (xml > 1)
+                        wrbuf_puts (wr, "  <subfield label=\"");
+                    else
+                        wrbuf_puts (wr, "  <subfield code=\"");
+                    for (j = 1; j<identifier_length; j++, i++)
+                        wrbuf_putc (wr, buf[i]);
+                    wrbuf_puts (wr, "\">");
+                }
+                else
+                {
+                    wrbuf_puts (wr, " $"); 
+                    for (j = 1; j<identifier_length; j++, i++)
+                        wrbuf_putc (wr, buf[i]);
+                    wrbuf_putc (wr, ' ');
+                }
                while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
                       buf[i] != ISO2709_FS && i < end_offset)
                {
-                   wrbuf_putc (wr, buf[i]);
+                    if (xml && buf[i] == '<')
+                        wrbuf_puts(wr, "&lt;");
+                    else if (xml && buf[i] == '&')
+                        wrbuf_puts(wr, "&amp;");
+                    else
+                        wrbuf_putc (wr, buf[i]);
                    i++;
                }
+                if (xml)
+                    wrbuf_puts (wr, "</subfield>\n");
            }
            else
            {
-               wrbuf_putc (wr, buf[i]);
+                if (xml && buf[i] == '<')
+                    wrbuf_puts(wr, "&lt;");
+                else if (xml && buf[i] == '&')
+                    wrbuf_puts(wr, "&amp;");
+                else if (xml && buf[i] == '"')
+                    wrbuf_puts(wr, "&quot;");
+                else
+                    wrbuf_putc (wr, buf[i]);
                i++;
            }
        }
-       wrbuf_putc (wr, '\n');
+        if (!xml)
+            wrbuf_putc (wr, '\n');
        if (i < end_offset)
-           wrbuf_puts (wr, "-- separator but not at end of field\n");
+           wrbuf_puts (wr, "  <!-- separator but not at end of field -->\n");
        if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
-           wrbuf_puts (wr, "-- no separator at end of field\n");
+           wrbuf_puts (wr, "  <!-- no separator at end of field -->\n");
+        if (xml)
+        {
+            if (xml > 1)
+            {
+                if (identifier_flag)
+                    wrbuf_puts (wr, "</varfield>\n");
+                else
+                    wrbuf_puts (wr, "</fixfield>\n");
+            }
+            else
+                wrbuf_puts (wr, "</field>\n");
+        }
+    }
+    if (xml)
+    {
+        if (xml > 1)
+            wrbuf_puts (wr, "</oai_marc>\n");
+        else
+            wrbuf_puts (wr, "</iso2709>\n");
     }
     wrbuf_puts (wr, "");
     return record_length;
 }
 
+int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
+                        int bsize)
+{
+    return yaz_marc_decode (buf, wr, debug, bsize, 0);
+}
+
 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
 {
     int record_length;
@@ -168,7 +305,6 @@ int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
     return record_length;
 }
 
-
 int marc_display_ex (const char *buf, FILE *outf, int debug)
 {
     return marc_display_exl (buf, outf, debug, -1);