schema mapping supports duplicate maps (copy instead of pointer swap)
[yaz-moved-to-github.git] / retrieval / d1_marc.c
index 4957bfb..75acbb9 100644 (file)
@@ -1,51 +1,36 @@
 /*
- * Copyright (c) 1995, Index Data.
+ * Copyright (c) 1995-2002, Index Data.
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
- * $Log: d1_marc.c,v $
- * Revision 1.4  1996-03-25 10:18:03  quinn
- * Removed trailing whitespace from data elements
- *
- * Revision 1.3  1995/11/01  16:34:57  quinn
- * Making data1 look for tables in data1_tabpath
- *
- * Revision 1.2  1995/11/01  13:54:48  quinn
- * Minor adjustments
- *
- * Revision 1.1  1995/11/01  11:56:08  quinn
- * Added Retrieval (data management) functions en masse.
- *
- *
+ * $Id: d1_marc.c,v 1.19 2002-05-28 21:09:44 adam Exp $
  */
 
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
-#include <oid.h>
-#include <log.h>
-#include <readconf.h>
-#include <xmalloc.h>
-#include <data1.h>
-#include <tpath.h>
+#include <yaz/oid.h>
+#include <yaz/log.h>
+#include <yaz/marcdisp.h>
+#include <yaz/readconf.h>
+#include <yaz/xmalloc.h>
+#include <yaz/data1.h>
+#include <yaz/tpath.h>
 
-#define ISO2709_RS 035
-#define ISO2709_FS 036
-#define ISO2709_IDFS 037
-
-data1_marctab *data1_read_marctab(char *file)
+data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
 {
     FILE *f;
-    data1_marctab *res = xmalloc(sizeof(*res));
+    NMEM mem = data1_nmem_get (dh);
+    data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
     char line[512], *argv[50];
+    int lineno = 0;
     int argc;
     
-    if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
+    if (!(f = data1_path_fopen(dh, file, "r")))
     {
-       logf(LOG_WARN|LOG_ERRNO, "%s", file);
+       yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
        return 0;
     }
 
@@ -61,71 +46,99 @@ data1_marctab *data1_read_marctab(char *file)
     strcpy(res->implementation_codes, "    ");
     res->indicator_length = 2;
     res->identifier_length = 2;
+    res->force_indicator_length = -1;
+    res->force_identifier_length = -1;
     strcpy(res->user_systems, "z  ");
-
-    while ((argc = readconf_line(f, line, 512, argv, 50)))
-       if (!strcmp(argv[0], "name"))
+    
+    while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
+       if (!strcmp(*argv, "name"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad name directive");
+               yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
-           res->name = xmalloc(strlen(argv[1])+1);
-           strcpy(res->name, argv[1]);
+           res->name = nmem_strdup(mem, argv[1]);
        }
-       else if (!strcmp(argv[0], "reference"))
+       else if (!strcmp(*argv, "reference"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad name directive");
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
            if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
            {
-               logf(LOG_WARN, "%s: Unknown tagset ref '%s' in %s", file,
-                   argv[1]);
+               yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
+                       file, lineno, argv[1]);
                continue;
            }
        }
-       else if (!strcmp(argv[0], "length-data-entry"))
+       else if (!strcmp(*argv, "length-data-entry"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad data-length-entry");
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
            res->length_data_entry = atoi(argv[1]);
        }
-       else if (!strcmp(argv[0], "length-starting"))
+       else if (!strcmp(*argv, "length-starting"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad length-starting");
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
            res->length_starting = atoi(argv[1]);
        }
-       else if (!strcmp(argv[0], "length-implementation"))
+       else if (!strcmp(*argv, "length-implementation"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad length-implentation");
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
            res->length_implementation = atoi(argv[1]);
        }
-       else if (!strcmp(argv[0], "future-use"))
+       else if (!strcmp(*argv, "future-use"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s: Bad future-use");
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
                continue;
            }
            strncpy(res->future_use, argv[1], 2);
        }
+       else if (!strcmp(*argv, "force-indicator-length"))
+       {
+           if (argc != 2)
+           {
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
+               continue;
+           }
+           res->force_indicator_length = atoi(argv[1]);
+       }
+       else if (!strcmp(*argv, "force-identifier-length"))
+       {
+           if (argc != 2)
+           {
+               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
+               continue;
+           }
+           res->force_identifier_length = atoi(argv[1]);
+       }
        else
-           logf(LOG_WARN, "%s: Bad directive '%s'", file, argv[0]);
+           yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
+                   *argv);
 
     fclose(f);
     return res;
@@ -141,7 +154,8 @@ static char *get_data(data1_node *n, int *len)
 
     while (n->which != DATA1N_data && n->child)
        n = n->child;
-    if (n->which != DATA1N_data || n->u.data.what != DATA1I_text)
+    if (n->which != DATA1N_data || 
+       (n->u.data.what != DATA1I_text && n->u.data.what != DATA1I_num))
     {
        r = "[Structured/included data]";
        *len = strlen(r);
@@ -149,14 +163,14 @@ static char *get_data(data1_node *n, int *len)
     }
 
     *len = n->u.data.len;
-    while (*len && isspace(n->u.data.data[*len - 1]))
+    while (*len && d1_isspace(n->u.data.data[*len - 1]))
        (*len)--;
     return n->u.data.data;
 }
 
 static void memint (char *p, int val, int len)
 {
-    static char buf[9];
+    char buf[10];
 
     if (len == 1)
         *p = val + '0';
@@ -167,6 +181,19 @@ static void memint (char *p, int val, int len)
     }
 }
 
+static int is_indicator (data1_marctab *p, data1_node *subf)
+{
+#if 1
+    if (p->indicator_length != 2 ||
+       (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
+       return 1;
+#else
+    if (subf && subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
+       return 1;
+#endif
+    return 0;
+}
+
 static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
     char **buf, int *size)
 {
@@ -177,11 +204,14 @@ static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
     char *op;
     data1_node *field, *subf;
 
+    yaz_log (LOG_DEBUG, "nodetomarc");
     for (field = n->child; field; field = field->next)
     {
+        int is00X = 0;
+
        if (field->which != DATA1N_tag)
        {
-           logf(LOG_WARN, "Malformed field composition for marc output.");
+           yaz_log(LOG_WARN, "Malformed field composition for marc output.");
            return -1;
        }
        if (selected && !field->u.tag.node_selected)
@@ -190,17 +220,20 @@ static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
             + p->length_implementation;
         base_address += 3 + p->length_data_entry + p->length_starting
             + p->length_implementation;
-       if (strncmp(field->u.tag.tag, "00", 2))
-            len += p->indicator_length;      /* this is fairly bogus */
-        for (subf = field->child; subf; subf = subf->next)
+       subf = field->child;
+
+       if (subf->which == DATA1N_data)
+            is00X = 1;
+       
+        if (!is00X)
+            len += p->indicator_length;  
+       /*  we'll allow no indicator if length is not 2 */
+       if (is_indicator (p, subf))
+           subf = subf->child;
+
+        for (; subf; subf = subf->next)
         {
-           if (subf->which != DATA1N_tag)
-           {
-               logf(LOG_WARN,
-                   "Malformed subfield composition for marc output.");
-               return -1;
-           }
-            if (strncmp(field->u.tag.tag, "00", 2))
+            if (!is00X)
                 len += p->identifier_length;
            get_data(subf, &dlen);
             len += dlen;
@@ -208,9 +241,9 @@ static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
     }
 
     if (!*buf)
-       *buf = xmalloc(*size = len);
+       *buf = (char *)xmalloc(*size = len);
     else if (*size <= len)
-       *buf = xrealloc(*buf, *size = len);
+       *buf = (char *)xrealloc(*buf, *size = len);
        
     op = *buf;
     memint (op, len, 5);
@@ -230,22 +263,41 @@ static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
 
     for (field = n->child; field; field = field->next)
     {
+        int is00X = 0;
+
         int data_0 = data_p;
+       char *indicator_data = "    ";
        if (selected && !field->u.tag.node_selected)
            continue;
-        if (strncmp(field->u.tag.tag, "00", 2))   /* bogus */
+
+       subf = field->child;
+
+        if (subf->which == DATA1N_data)
+            is00X = 1;
+
+       if (is_indicator (p, subf))
+       {
+            indicator_data = subf->u.tag.tag;
+           subf = subf->child;
+       }
+        if (!is00X)
         {
-            memcpy (op + data_p, "  ", p->indicator_length);
+            memcpy (op + data_p, indicator_data, p->indicator_length);
             data_p += p->indicator_length;
         }
-        for (subf = field->child; subf; subf = subf->next)
+        for (; subf; subf = subf->next)
         {
            char *data;
 
-            if (strncmp(field->u.tag.tag, "00", 2))
+            if (!is00X)
             {
+                const char *identifier = "a";
+                if (subf->which != DATA1N_tag)
+                    yaz_log(LOG_WARN, "Malformed fields for marc output.");
+                else
+                    identifier = subf->u.tag.tag;
                 op[data_p] = ISO2709_IDFS;
-                memcpy (op + data_p+1, subf->u.tag.tag, p->identifier_length-1);
+                memcpy (op + data_p+1, identifier, p->identifier_length-1);
                 data_p += p->identifier_length;
             }
            data = get_data(subf, &dlen);
@@ -269,11 +321,12 @@ static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
     return len;
 }
 
-char *data1_nodetomarc(data1_marctab *p, data1_node *n, int selected, int *len)
+char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
+                      int selected, int *len)
 {
-    static char *buf = 0;
-    static int size = 0;
+    int *size;
+    char **buf = data1_get_map_buf (dh, &size);
 
-    *len = nodetomarc(p, n, selected, &buf, &size);
-    return buf;
+    *len = nodetomarc(p, n, selected, buf, size);
+    return *buf;
 }