Happy new year
[pazpar2-moved-to-github.git] / src / marcmap.c
index b3a447c..f79b323 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) Index Data
 
 Pazpar2 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
@@ -21,6 +21,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     \brief MARC map implementation
 */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -30,14 +34,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <yaz/nmem.h>
 
-#include <marcmap.h>
-#include <marchash.h>
+#include "marcmap.h"
+#include "marchash.h"
 
-struct marcmap *marcmap_load(char *filename, NMEM nmem) {
+struct marcmap *marcmap_load(const char *filename, NMEM nmem)
+{
     struct marcmap *mm;
     struct marcmap *mmhead;
     FILE *fp;
-    char c;
+    int c;
     char buf[256];
     int len;
     int field;
@@ -49,19 +54,21 @@ struct marcmap *marcmap_load(char *filename, NMEM nmem) {
     mm = NULL;
     mmhead = NULL;
     fp = fopen(filename, "r");
+    if (!fp)
+        return mmhead;
 
-    while ((c = getc(fp) ) != EOF) 
+    while ((c = getc(fp) ) != EOF)
     {
         // allocate some space
         if (newrec)
         {
-            if (mm != NULL) 
+            if (mm != NULL)
             {
                 mm->next = nmem_malloc(nmem, sizeof(struct marcmap));
                 mm = mm->next;
             }
             // first one!
-            else 
+            else
             { mm = nmem_malloc(nmem, sizeof(struct marcmap));
                 mmhead = mm;
             }
@@ -88,8 +95,8 @@ struct marcmap *marcmap_load(char *filename, NMEM nmem) {
                 mm->subfield = buf[len-2];
             }
             // third, pz fieldname
-            else if (field == 2) 
-            { 
+            else if (field == 2)
+            {
                 mm->pz = nmem_malloc(nmem, len * sizeof(char));
                 strncpy(mm->pz, buf, len);
             }
@@ -126,16 +133,18 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
     xmlDocPtr xml_out;
     xmlNodePtr xml_out_root;
     xmlNodePtr rec_node;
-    xmlNodePtr meta_node; 
+    xmlNodePtr meta_node;
     struct marchash *marchash;
     struct marcfield *field;
     struct marcsubfield *subfield;
     struct marcmap *mmcur;
-     
+
     xml_out = xmlNewDoc(BAD_CAST "1.0");
+    xml_out->encoding = xmlCharStrdup("UTF-8");
     xml_out_root = xmlNewNode(NULL, BAD_CAST "record");
     xmlDocSetRootElement(xml_out, xml_out_root);
-    ns_pz = xmlNewNs(xml_out_root, BAD_CAST "http://www.indexdata.com/pazpar2/1.0", BAD_CAST "pz"); 
+    ns_pz = xmlNewNs(xml_out_root, BAD_CAST "http://www.indexdata.com/pazpar2/1.0", BAD_CAST "pz");
+    xmlSetNs(xml_out_root, ns_pz);
     nmem = nmem_create();
     rec_node = xmlDocGetRootElement(xml_in);
     marchash = marchash_create(nmem);
@@ -144,36 +153,38 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
     mmcur = marcmap;
     while (mmcur != NULL)
     {
-        if (field = marchash_get_field(marchash, mmcur->field, NULL))
-            do
+        field = 0;
+        while ((field = marchash_get_field(marchash, mmcur->field, field)) != 0)
+        {
+            // field value
+            if ((mmcur->subfield == '$') && (s = field->val))
             {
-                // field value
-                if ((mmcur->subfield == '$') && (s = field->val))
-                {
-                    meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
-                    xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz); 
-                }
-                // catenate all subfields
-                else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem)))
-                {
-                    meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
-                    xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz);
-                }
-                // subfield value
-                else if (mmcur->subfield) 
+                meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
+                xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
+            }
+            // catenate all subfields
+            else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem)))
+            {
+                meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
+                xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
+            }
+            // subfield value
+            else if (mmcur->subfield)
+            {
+                subfield = 0;
+                while ((subfield =
+                        marchash_get_subfield(mmcur->subfield,
+                                              field, subfield)) != 0)
                 {
-                    if (subfield = marchash_get_subfield(mmcur->subfield, field, NULL)) 
-                        do
-                            if (s = subfield->val)
-                            {
-                                meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
-                                xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz);
-                            }
-                        while (subfield = marchash_get_subfield(mmcur->subfield, field, subfield));
+                    if ((s = subfield->val) != 0)
+                    {
+                        meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
+                        xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
+                    }
                 }
-                
             }
-            while (field = marchash_get_field(marchash, mmcur->field, field));
+
+        }
         mmcur = mmcur->next;
     }
 
@@ -202,12 +213,12 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
     if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
         strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
     strncat(mergekey, " author ", 1023 - strlen(mergekey));
-    if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
+    if ((field = marchash_get_field(marchash, "100", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
         strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
     strncat(mergekey, " medium ", 1023 - strlen(mergekey));
     strncat(mergekey, medium, 1023 - strlen(mergekey));
 
-    xmlSetProp(xml_out_root, BAD_CAST "mergekey", BAD_CAST mergekey);
+//    xmlSetProp(xml_out_root, BAD_CAST "mergekey", BAD_CAST mergekey);
 
     nmem_destroy(nmem);
     return xml_out;