1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief MARC MAP utilities (hash lookup etc)
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
33 #include "jenkins_hash.h"
36 inline void strtrimcat(char *dest, const char *src)
43 // move to end of dest
46 // initialise last non-space charater
48 // skip leading whitespace
59 *(++last_nonspace) = '\0';
62 inline void strtrimcpy(char *dest, const char *src)
65 strtrimcat(dest, src);
68 struct marchash *marchash_create(NMEM nmem)
71 new = nmem_malloc(nmem, sizeof (struct marchash));
72 memset(new, 0, sizeof (struct marchash));
77 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
79 xmlNodePtr field_node;
81 struct marcfield *field;
82 field_node = rec_node->children;
86 if (field_node->type == XML_ELEMENT_NODE)
89 if (!strcmp((const char *) field_node->name, "controlfield"))
91 field = marchash_add_field(
93 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
94 (const char *) xmlNodeGetContent(field_node));
96 else if (!strcmp((const char *) field_node->name, "datafield"))
98 field = marchash_add_field(
100 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
101 (const char *) xmlNodeGetContent(field_node));
105 sub_node = field_node->children;
108 if ((sub_node->type == XML_ELEMENT_NODE) &&
109 !strcmp((const char *) sub_node->name, "subfield"))
111 marchash_add_subfield(
113 xmlGetProp(sub_node, BAD_CAST "code")[0],
114 (const char *) xmlNodeGetContent(sub_node));
116 sub_node = sub_node->next;
120 field_node = field_node->next;
124 struct marcfield *marchash_add_field(struct marchash *marchash,
125 const char *key, const char *val)
128 struct marcfield *new;
129 struct marcfield *last;
131 slot = jenkins_hash((const unsigned char *) key) & MARCHASH_MASK;
132 new = marchash->table[slot];
141 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
146 marchash->table[slot] = new;
149 new->subfields = NULL;
150 strncpy(new->key, key, 4);
152 // only 3 char in a marc field name
153 if (new->key[3] != '\0')
156 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
157 strtrimcpy(new->val, val);
162 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
163 struct marcfield *field,
164 const char key, const char *val)
166 struct marcsubfield *new;
167 struct marcsubfield *last;
169 new = field->subfields;
177 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
182 field->subfields = new;
186 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
187 strcpy(new->val, val);
191 struct marcfield *marchash_get_field (struct marchash *marchash,
192 const char *key, struct marcfield *last)
194 struct marcfield *cur;
198 cur = marchash->table[jenkins_hash((const unsigned char *)key) & MARCHASH_MASK];
201 if (!strcmp(cur->key, key))
208 struct marcsubfield *marchash_get_subfield(char key,
209 struct marcfield *field,
210 struct marcsubfield *last)
212 struct marcsubfield *cur;
216 cur = field->subfields;
226 char *marchash_catenate_subfields(struct marcfield *field,
227 const char *delim, NMEM nmem)
230 struct marcsubfield *cur;
231 int delimsize = strlen(delim);
232 int outsize = 1-delimsize;
233 // maybe it would make sense to have an nmem strcpy/strcat?
234 cur = field -> subfields;
237 outsize += strlen(cur->val) + delimsize;
241 output = nmem_malloc(nmem, outsize);
245 cur = field -> subfields;
248 strtrimcat(output, cur->val);
250 strcat(output, delim);
258 * c-file-style: "Stroustrup"
259 * indent-tabs-mode: nil
261 * vim: shiftwidth=4 tabstop=8 expandtab