Do not build for Debian lenny anymore
[idzebra-moved-to-github.git] / data1 / d1_marc.c
index ef3d301..a332b2a 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: d1_marc.c,v 1.5 2003-11-28 23:06:59 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
-   Index Data Aps
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2011 Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -15,24 +12,27 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 /* converts data1 tree to ISO2709/MARC record */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <yaz/oid.h>
 #include <yaz/log.h>
+#include <yaz/oid_db.h>
 #include <yaz/marcdisp.h>
 #include <yaz/readconf.h>
 #include <yaz/xmalloc.h>
 #include <yaz/tpath.h>
-#include <data1.h>
+#include <idzebra/data1.h>
 
 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
 {
@@ -44,13 +44,10 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
     int argc;
     
     if (!(f = data1_path_fopen(dh, file, "r")))
-    {
-       yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
        return 0;
-    }
 
     res->name = 0;
-    res->reference = VAL_NONE;
+    res->oid = 0;
     res->next = 0;
     res->length_data_entry = 4;
     res->length_starting = 5;
@@ -70,7 +67,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -80,13 +77,16 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
-           if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
+            res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
+                                              CLASS_TAGSET, argv[1], 
+                                              mem);
+           if (!res->oid)
            {
-               yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
+               yaz_log(YLOG_WARN, "%s:%d: Unknown tagset reference '%s'",
                        file, lineno, argv[1]);
                continue;
            }
@@ -95,7 +95,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -105,7 +105,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -115,7 +115,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -125,7 +125,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -135,7 +135,7 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
@@ -145,14 +145,29 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
        {
            if (argc != 2)
            {
-               yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
                        *argv);
                continue;
            }
            res->force_identifier_length = atoi(argv[1]);
        }
+       else if (!strcmp(*argv, "implementation-codes"))
+       {
+           if (argc != 2)
+           {
+               yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
+                       *argv);
+               continue;
+           }
+            /* up to 4 characters .. space pad */
+            if (strlen(argv[1]) > 4)
+               yaz_log(YLOG_WARN, "%s:%d: Max 4 characters for "
+                                   "implementation-codes", file, lineno);
+            else
+                memcpy(res->implementation_codes, argv[1], strlen(argv[1]));
+       }
        else
-           yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
+           yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
                    *argv);
 
     fclose(f);
@@ -160,40 +175,30 @@ data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
 }
 
 
-/*
- * Locate some data under this node. This routine should handle variants
- * prettily.
- */
-static char *get_data(data1_node *n, int *len)
+static void get_data2(data1_node *n, int *len, char *dst, size_t max)
 {
-    char *r;
+    *len = 0;
 
     while (n)
     {
         if (n->which == DATA1N_data)
         {
-            int i;
-            *len = n->u.data.len;
-
-            for (i = 0; i<*len; i++)
-                if (!d1_isspace(n->u.data.data[i]))
-                    break;
-            while (*len && d1_isspace(n->u.data.data[*len - 1]))
-                (*len)--;
-            *len = *len - i;
-            if (*len > 0)
-                return n->u.data.data + i;
+            if (dst && *len < max)
+            {
+                size_t copy_len = max - *len;
+                if (copy_len > n->u.data.len)
+                    copy_len = n->u.data.len;
+                memcpy(dst + *len, n->u.data.data, copy_len);
+            }
+            *len += n->u.data.len;
         }
-        if (n->which == DATA1N_tag)
+        if (n->which == DATA1N_tag && *len == 0)
             n = n->child;
        else if (n->which == DATA1N_data)
             n = n->next;
        else
             break;     
     }
-    r = "";
-    *len = strlen(r);
-    return r;
 }
 
 static void memint (char *p, int val, int len)
@@ -234,13 +239,12 @@ static int nodetomarc(data1_handle dh,
 #if 0
     data1_pr_tree(dh, n, stdout);
 #endif
-    yaz_log (LOG_DEBUG, "nodetomarc");
+    yaz_log (YLOG_DEBUG, "nodetomarc");
 
     memcpy (leader+5, p->record_status, 1);
     memcpy (leader+6, p->implementation_codes, 4);
     memint (leader+10, p->indicator_length, 1);
     memint (leader+11, p->identifier_length, 1);
-    memint (leader+12, base_address, 5);
     memcpy (leader+17, p->user_systems, 3);
     memint (leader+20, p->length_data_entry, 1);
     memint (leader+21, p->length_starting, 1);
@@ -266,11 +270,7 @@ static int nodetomarc(data1_handle dh,
        else if (!strcmp(field->u.tag.tag, "leader"))
        {
            int dlen = 0;
-           char *dbuf = get_data(subf, &dlen);
-           if (dlen > 24)
-               dlen = 24;
-           if (dbuf && dlen > 0)
-               memcpy (leader, dbuf, dlen);
+            get_data2(subf, &dlen, leader, 24);
            continue;
        }
        else if (!strcmp(field->u.tag.tag, "controlfield"))
@@ -310,8 +310,12 @@ static int nodetomarc(data1_handle dh,
         for (; subf; subf = subf->next)
         {
             if (!control_field)
+           {
+               if (marc_xml && subf->which != DATA1N_tag)
+                   continue; /* we skip comments, cdata .. */
                 len += p->identifier_length;
-           get_data(subf, &dlen);
+           }
+            get_data2(subf, &dlen, 0, 0);
             len += dlen;
         }
     }
@@ -322,6 +326,11 @@ static int nodetomarc(data1_handle dh,
        *buf = (char *)xrealloc(*buf, *size = len);
        
     op = *buf;
+
+    /* we know the base address now */
+    memint (leader+12, base_address, 5);
+
+    /* copy temp leader to real output buf op */
     memcpy (op, leader, 24);
     memint (op, len, 5);
     
@@ -387,9 +396,9 @@ static int nodetomarc(data1_handle dh,
                if (!strcmp(xa->name, "ind1"))
                    indicator_data[0] = xa->value[0];
                if (!strcmp(xa->name, "ind2"))
-                   indicator_data[1] = xa->value[1];
+                   indicator_data[1] = xa->value[0];
                if (!strcmp(xa->name, "ind3"))
-                   indicator_data[2] = xa->value[2];
+                   indicator_data[2] = xa->value[0];
            }
        }
        if (!control_field)
@@ -399,32 +408,31 @@ static int nodetomarc(data1_handle dh,
        }
        for (; subf; subf = subf->next)
         {
-           char *data;
-
             if (!control_field)
             {
                 const char *identifier = "a";
                if (marc_xml)
                {
-                   if (subf->which == DATA1N_tag &&
-                       !strcmp(subf->u.tag.tag, "subfield"))
-                   {
-                       data1_xattr *xa;
-                       for (xa = subf->u.tag.attributes; xa; xa = xa->next)
-                           if (!strcmp(xa->name, "code"))
-                               identifier = xa->value;
-                   }
+                   data1_xattr *xa;
+                   if (subf->which != DATA1N_tag)
+                       continue;
+                   if (strcmp(subf->u.tag.tag, "subfield"))
+                       yaz_log(YLOG_WARN, "Unhandled tag %s",
+                               subf->u.tag.tag);
+                   
+                   for (xa = subf->u.tag.attributes; xa; xa = xa->next)
+                       if (!strcmp(xa->name, "code"))
+                           identifier = xa->value;
                }
                else if (subf->which != DATA1N_tag)
-                    yaz_log(LOG_WARN, "Malformed fields for marc output.");
+                    yaz_log(YLOG_WARN, "Malformed fields for marc output.");
                 else
                     identifier = subf->u.tag.tag;
                 op[data_p] = ISO2709_IDFS;
                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
                 data_p += p->identifier_length;
             }
-           data = get_data(subf, &dlen);
-            memcpy (op + data_p, data, dlen);
+            get_data2(subf, &dlen, op + data_p, 100000);
             data_p += dlen;
         }
         op[data_p++] = ISO2709_FS;
@@ -469,3 +477,12 @@ char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
     *len = nodetomarc(dh, p, n, selected, buf, size);
     return *buf;
 }
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+