Added 'check only' option for MARC utilities. Option -n for yaz-marcdump
[yaz-moved-to-github.git] / src / marcdisp.c
index 1d408be..6c8a603 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: marcdisp.c,v 1.33 2006-08-28 12:34:40 adam Exp $
+ * $Id: marcdisp.c,v 1.36 2006-12-07 11:08:05 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,
@@ -383,6 +392,41 @@ static void yaz_marc_reset(yaz_marc_t mt)
     mt->subfield_pp = 0;
 }
 
+int yaz_marc_write_check(yaz_marc_t mt, WRBUF wr)
+{
+    struct yaz_marc_node *n;
+    int identifier_length;
+    const char *leader = 0;
+
+    for (n = mt->nodes; n; n = n->next)
+        if (n->which == YAZ_MARC_LEADER)
+        {
+            leader = n->u.leader;
+            break;
+        }
+    
+    if (!leader)
+        return -1;
+    if (!atoi_n_check(leader+11, 1, &identifier_length))
+        return -1;
+
+    for (n = mt->nodes; n; n = n->next)
+    {
+        switch(n->which)
+        {
+        case YAZ_MARC_COMMENT:
+            wrbuf_iconv_write(wr, mt->iconv_cd, 
+                              n->u.comment, strlen(n->u.comment));
+            wrbuf_puts(wr, ")\n");
+            break;
+        default:
+            break;
+        }
+    }
+    return 0;
+}
+
+
 int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr)
 {
     struct yaz_marc_node *n;
@@ -463,6 +507,8 @@ int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr)
         return yaz_marc_write_marcxchange(mt, wr, 0, 0); /* no format, type */
     case YAZ_MARC_ISO2709:
         return yaz_marc_write_iso2709(mt, wr);
+    case YAZ_MARC_CHECK:
+        return yaz_marc_write_check(mt, wr);
     }
     return -1;
 }
@@ -574,7 +620,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);
 }
@@ -1211,6 +1258,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