Towards 2.1.40.
[yaz-moved-to-github.git] / src / marcdisp.c
index 1680ea5..970eed7 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: marcdisp.c,v 1.32 2006-08-01 09:28:04 adam Exp $
+ * $Id: marcdisp.c,v 1.35 2006-10-27 12:19:15 adam Exp $
  */
 
 /**
@@ -25,6 +25,7 @@
 #include <yaz/marcdisp.h>
 #include <yaz/wrbuf.h>
 #include <yaz/yaz-util.h>
+#include <yaz/nmem_xml.h>
 
 #if YAZ_HAVE_XML2
 #include <libxml/parser.h>
@@ -87,6 +88,7 @@ struct yaz_marc_t_ {
     yaz_iconv_t iconv_cd;
     char subfield_str[8];
     char endline_str[8];
+    char *leader_spec;
     struct yaz_marc_node *nodes;
     struct yaz_marc_node **nodes_pp;
     struct yaz_marc_subfield **subfield_pp;
@@ -99,6 +101,7 @@ yaz_marc_t yaz_marc_create(void)
     mt->debug = 0;
     mt->m_wr = wrbuf_alloc();
     mt->iconv_cd = 0;
+    mt->leader_spec = 0;
     strcpy(mt->subfield_str, " $");
     strcpy(mt->endline_str, "\n");
 
@@ -112,10 +115,15 @@ void yaz_marc_destroy(yaz_marc_t mt)
     if (!mt)
         return ;
     nmem_destroy(mt->nmem);
-    wrbuf_free (mt->m_wr, 1);
-    xfree (mt);
+    wrbuf_free(mt->m_wr, 1);
+    xfree(mt->leader_spec);
+    xfree(mt);
 }
 
+static int marc_exec_leader(const char *leader_spec, char *leader,
+                            size_t size);
+
+
 struct yaz_marc_node *yaz_marc_add_node(yaz_marc_t mt)
 {
     struct yaz_marc_node *n = nmem_malloc(mt->nmem, sizeof(*n));
@@ -158,6 +166,7 @@ void yaz_marc_add_leader(yaz_marc_t mt, const char *leader, size_t leader_len)
     struct yaz_marc_node *n = yaz_marc_add_node(mt);
     n->which = YAZ_MARC_LEADER;
     n->u.leader = nmem_strdupn(mt->nmem, leader, leader_len);
+    marc_exec_leader(mt->leader_spec, n->u.leader, leader_len);
 }
 
 void yaz_marc_add_controlfield(yaz_marc_t mt, const char *tag,
@@ -422,15 +431,20 @@ int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr)
                 wrbuf_puts (wr, mt->subfield_str); 
                 wrbuf_iconv_write(wr, mt->iconv_cd, s->code_data, 
                                   using_code_len);
-                wrbuf_printf(wr, " ");
+                wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
                 wrbuf_iconv_puts(wr, mt->iconv_cd, 
                                  s->code_data + using_code_len);
+                wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
+                wr->pos--;
             }
             wrbuf_puts (wr, mt->endline_str);
             break;
         case YAZ_MARC_CONTROLFIELD:
-            wrbuf_printf(wr, "%s ", n->u.controlfield.tag);
+            wrbuf_printf(wr, "%s", n->u.controlfield.tag);
+            wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
             wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data);
+            wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
+            wr->pos--;
             wrbuf_puts (wr, mt->endline_str);
             break;
         case YAZ_MARC_COMMENT:
@@ -499,6 +513,7 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
     for (n = mt->nodes; n; n = n->next)
     {
         struct yaz_marc_subfield *s;
+
         switch(n->which)
         {
         case YAZ_MARC_DATAFIELD:
@@ -514,7 +529,7 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
                     wrbuf_printf(wr, " ind%d=\"", i+1);
                     wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                           n->u.datafield.indicator+i, 1);
-                    wrbuf_printf(wr, "\"");
+                    wrbuf_iconv_puts(wr, mt->iconv_cd, "\"");
                 }
             }
             wrbuf_printf(wr, ">\n");
@@ -528,14 +543,15 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
                     :
                     cdata_one_character(mt, s->code_data);
                 
-                wrbuf_puts(wr, "    <subfield code=\"");
+                wrbuf_iconv_puts(wr, mt->iconv_cd, "    <subfield code=\"");
                 wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                         s->code_data, using_code_len);
-                wrbuf_puts(wr, "\">");
+                wrbuf_iconv_puts(wr, mt->iconv_cd, "\">");
                 wrbuf_iconv_write_cdata(wr, mt->iconv_cd,
                                         s->code_data + using_code_len,
                                         strlen(s->code_data + using_code_len));
-                wrbuf_puts(wr, "</subfield>\n");
+                wrbuf_iconv_puts(wr, mt->iconv_cd, "</subfield>");
+                wrbuf_puts(wr, "\n");
             }
             wrbuf_printf(wr, "  </datafield>\n");
             break;
@@ -543,9 +559,10 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
             wrbuf_printf(wr, "  <controlfield tag=\"");
             wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag,
                                     strlen(n->u.controlfield.tag));
-            wrbuf_printf(wr, "\">");
+            wrbuf_iconv_puts(wr, mt->iconv_cd, "\">");
             wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data);
-            wrbuf_printf(wr, "</controlfield>\n");
+            wrbuf_iconv_puts(wr, mt->iconv_cd, "</controlfield>");
+            wrbuf_puts(wr, "\n");
             break;
         case YAZ_MARC_COMMENT:
             wrbuf_printf(wr, "<!-- ");
@@ -566,7 +583,8 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
 
 int yaz_marc_write_marcxml(yaz_marc_t mt, WRBUF wr)
 {
-    yaz_marc_modify_leader(mt, 9, "a");
+    if (!mt->leader_spec)
+        yaz_marc_modify_leader(mt, 9, "a");
     return yaz_marc_write_marcxml_ns(mt, wr, "http://www.loc.gov/MARC21/slim",
                                      0, 0);
 }
@@ -616,18 +634,22 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
     {
         int data_length = 0;
         struct yaz_marc_subfield *s;
+
         switch(n->which)
         {
         case YAZ_MARC_DATAFIELD:
             wrbuf_printf(wr_dir, "%.3s", n->u.datafield.tag);
             data_length += indicator_length;
+            wrbuf_rewind(wr_data_tmp);
             for (s = n->u.datafield.subfields; s; s = s->next)
             {
-                wrbuf_rewind(wr_data_tmp);
+                /* write dummy IDFS + content */
+                wrbuf_iconv_putchar(wr_data_tmp, mt->iconv_cd, ' ');
                 wrbuf_iconv_puts(wr_data_tmp, mt->iconv_cd, s->code_data);
-                data_length += 1+wrbuf_len(wr_data_tmp);
             }
-            data_length++;
+            /* write dummy FS (makes MARC-8 to become ASCII) */
+            wrbuf_iconv_putchar(wr_data_tmp, mt->iconv_cd, ' ');
+            data_length += wrbuf_len(wr_data_tmp);
             break;
         case YAZ_MARC_CONTROLFIELD:
             wrbuf_printf(wr_dir, "%.3s", n->u.controlfield.tag);
@@ -635,8 +657,8 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
             wrbuf_rewind(wr_data_tmp);
             wrbuf_iconv_puts(wr_data_tmp, mt->iconv_cd, 
                              n->u.controlfield.data);
+            wrbuf_iconv_putchar(wr_data_tmp, mt->iconv_cd, ' ');/* field sep */
             data_length += wrbuf_len(wr_data_tmp);
-            data_length++;
             break;
         case YAZ_MARC_COMMENT:
             break;
@@ -676,6 +698,7 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
     for (n = mt->nodes; n; n = n->next)
     {
         struct yaz_marc_subfield *s;
+
         switch(n->which)
         {
         case YAZ_MARC_DATAFIELD:
@@ -683,14 +706,20 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
                          n->u.datafield.indicator);
             for (s = n->u.datafield.subfields; s; s = s->next)
             {
-                wrbuf_printf(wr, "%c", ISO2709_IDFS);
+                wrbuf_putc(wr, ISO2709_IDFS);
                 wrbuf_iconv_puts(wr, mt->iconv_cd, s->code_data);
+                /* write dummy blank - makes MARC-8 to become ASCII */
+                wrbuf_iconv_putchar(wr, mt->iconv_cd, ' ');
+                wr->pos--;
             }
-            wrbuf_printf(wr, "%c", ISO2709_FS);
+            wrbuf_putc(wr, ISO2709_FS);
             break;
         case YAZ_MARC_CONTROLFIELD:
             wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data);
-            wrbuf_printf(wr, "%c", ISO2709_FS);
+            /* write dummy blank - makes MARC-8 to become ASCII */
+            wrbuf_iconv_putchar(wr, mt->iconv_cd, ' ');
+            wr->pos--;
+            wrbuf_putc(wr, ISO2709_FS);
             break;
         case YAZ_MARC_COMMENT:
             break;
@@ -1192,6 +1221,64 @@ int marc_display (const char *buf, FILE *outf)
     return marc_display_ex (buf, outf, 0);
 }
 
+int yaz_marc_leader_spec(yaz_marc_t mt, const char *leader_spec)
+{
+    xfree(mt->leader_spec);
+    mt->leader_spec = 0;
+    if (leader_spec)
+    {
+        char dummy_leader[24];
+        if (marc_exec_leader(leader_spec, dummy_leader, 24))
+            return -1;
+        mt->leader_spec = xstrdup(leader_spec);
+    }
+    return 0;
+}
+
+static int marc_exec_leader(const char *leader_spec, char *leader, size_t size)
+{
+    const char *cp = leader_spec;
+    while (cp)
+    {
+        char val[21];
+        int pos;
+        int no_read = 0, no = 0;
+
+        no = sscanf(cp, "%d=%20[^,]%n", &pos, val, &no_read);
+        if (no < 2 || no_read < 3)
+            return -1;
+        if (pos < 0 || pos >= size)
+            return -1;
+
+        if (*val == '\'')
+        {
+            const char *vp = strchr(val+1, '\'');
+            size_t len;
+            
+            if (!vp)
+                return -1;
+            len = vp-val-1;
+            if (len + pos > size)
+                return -1;
+            memcpy(leader + pos, val+1, len);
+        }
+        else if (*val >= '0' && *val <= '9')
+        {
+            int ch = atoi(val);
+            leader[pos] = ch;
+        }
+        else
+            return -1;
+        cp += no_read;
+        if (*cp != ',')
+            break;
+
+        cp++;
+    }
+    return 0;
+}
+
+
 /*
  * Local variables:
  * c-basic-offset: 4