X-Git-Url: http://git.indexdata.com/?p=pazpar2-moved-to-github.git;a=blobdiff_plain;f=src%2Fmarcmap.c;h=f79b3232a123349fc6b1fc5c549680f0348e24b5;hp=14cd21f392d6967cb2eec6e25f910452d7d144f4;hb=HEAD;hpb=096341d55d0f82fbfdf4c1389c2216c368d385f3 diff --git a/src/marcmap.c b/src/marcmap.c index 14cd21f..f79b323 100644 --- a/src/marcmap.c +++ b/src/marcmap.c @@ -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 +#endif + #include #include #include @@ -30,14 +34,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include -#include -#include +#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); @@ -151,7 +160,7 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in) if ((mmcur->subfield == '$') && (s = field->val)) { meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s); - xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz); + xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz); } // catenate all subfields else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem))) @@ -160,10 +169,10 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in) xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz); } // subfield value - else if (mmcur->subfield) + else if (mmcur->subfield) { subfield = 0; - while ((subfield = + while ((subfield = marchash_get_subfield(mmcur->subfield, field, subfield)) != 0) { @@ -174,7 +183,7 @@ xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in) } } } - + } mmcur = mmcur->next; } @@ -204,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;