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>
35 // Jenkins one-at-a-time hash (from pp2 reclists.c, wikipedia)
36 static unsigned int hash(const unsigned char *key)
38 unsigned int hash = 0;
52 inline void strtrimcat(char *dest, const char *src)
59 // move to end of dest
62 // initialise last non-space charater
64 // skip leading whitespace
75 *(++last_nonspace) = '\0';
78 inline void strtrimcpy(char *dest, const char *src)
81 strtrimcat(dest, src);
84 struct marchash *marchash_create(NMEM nmem)
87 new = nmem_malloc(nmem, sizeof (struct marchash));
88 memset(new, 0, sizeof (struct marchash));
93 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
95 xmlNodePtr field_node;
97 struct marcfield *field;
98 field_node = rec_node->children;
102 if (field_node->type == XML_ELEMENT_NODE)
105 if (!strcmp((const char *) field_node->name, "controlfield"))
107 field = marchash_add_field(
109 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
110 (const char *) xmlNodeGetContent(field_node));
112 else if (!strcmp((const char *) field_node->name, "datafield"))
114 field = marchash_add_field(
116 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
117 (const char *) xmlNodeGetContent(field_node));
121 sub_node = field_node->children;
124 if ((sub_node->type == XML_ELEMENT_NODE) &&
125 !strcmp((const char *) sub_node->name, "subfield"))
127 marchash_add_subfield(
129 xmlGetProp(sub_node, BAD_CAST "code")[0],
130 (const char *) xmlNodeGetContent(sub_node));
132 sub_node = sub_node->next;
136 field_node = field_node->next;
140 struct marcfield *marchash_add_field(struct marchash *marchash,
141 const char *key, const char *val)
144 struct marcfield *new;
145 struct marcfield *last;
147 slot = hash((const unsigned char *) key) & MARCHASH_MASK;
148 new = marchash->table[slot];
157 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
162 marchash->table[slot] = new;
165 new->subfields = NULL;
166 strncpy(new->key, key, 4);
168 // only 3 char in a marc field name
169 if (new->key[3] != '\0')
172 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
173 strtrimcpy(new->val, val);
178 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
179 struct marcfield *field,
180 const char key, const char *val)
182 struct marcsubfield *new;
183 struct marcsubfield *last;
185 new = field->subfields;
193 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
198 field->subfields = new;
202 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
203 strcpy(new->val, val);
207 struct marcfield *marchash_get_field (struct marchash *marchash,
208 const char *key, struct marcfield *last)
210 struct marcfield *cur;
214 cur = marchash->table[hash((const unsigned char *)key) & MARCHASH_MASK];
217 if (!strcmp(cur->key, key))
224 struct marcsubfield *marchash_get_subfield(char key,
225 struct marcfield *field,
226 struct marcsubfield *last)
228 struct marcsubfield *cur;
232 cur = field->subfields;
242 char *marchash_catenate_subfields(struct marcfield *field,
243 const char *delim, NMEM nmem)
246 struct marcsubfield *cur;
247 int delimsize = strlen(delim);
248 int outsize = 1-delimsize;
249 // maybe it would make sense to have an nmem strcpy/strcat?
250 cur = field -> subfields;
253 outsize += strlen(cur->val) + delimsize;
257 output = nmem_malloc(nmem, outsize);
261 cur = field -> subfields;
264 strtrimcat(output, cur->val);
266 strcat(output, delim);
274 * c-file-style: "Stroustrup"
275 * indent-tabs-mode: nil
277 * vim: shiftwidth=4 tabstop=8 expandtab