1 /* This file is part of Pazpar2.
2 Copyright (C) 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)
27 /* disable inline if AC_C_INLINE is not in use (Windows) */
36 #include <libxml/tree.h>
37 #include <libxml/parser.h>
40 #include "jenkins_hash.h"
43 static inline void strtrimcat(char *dest, const char *src)
50 // move to end of dest
53 // initialise last non-space charater
55 // skip leading whitespace
66 *(++last_nonspace) = '\0';
69 static inline void strtrimcpy(char *dest, const char *src)
72 strtrimcat(dest, src);
75 struct marchash *marchash_create(NMEM nmem)
78 new = nmem_malloc(nmem, sizeof (struct marchash));
79 memset(new, 0, sizeof (struct marchash));
84 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
86 xmlNodePtr field_node;
88 struct marcfield *field;
89 field_node = rec_node->children;
93 if (field_node->type == XML_ELEMENT_NODE)
96 if (!strcmp((const char *) field_node->name, "controlfield"))
98 xmlChar *content = xmlNodeGetContent(field_node);
99 xmlChar *tag = xmlGetProp(field_node, BAD_CAST "tag");
101 field = marchash_add_field(
102 marchash, (const char *) tag, (const char *) content);
106 else if (!strcmp((const char *) field_node->name, "datafield"))
108 xmlChar *content = xmlNodeGetContent(field_node);
109 xmlChar *tag = xmlGetProp(field_node, BAD_CAST "tag");
111 field = marchash_add_field(
112 marchash, (const char *) tag, (const char *) content);
118 sub_node = field_node->children;
121 if ((sub_node->type == XML_ELEMENT_NODE) &&
122 !strcmp((const char *) sub_node->name, "subfield"))
124 xmlChar *content = xmlNodeGetContent(sub_node);
125 xmlChar *code = xmlGetProp(sub_node, BAD_CAST "code");
127 marchash_add_subfield(
129 code[0], (const char *) content);
133 sub_node = sub_node->next;
137 field_node = field_node->next;
141 struct marcfield *marchash_add_field(struct marchash *marchash,
142 const char *key, const char *val)
145 struct marcfield *new;
146 struct marcfield *last;
148 slot = jenkins_hash((const unsigned char *) key) & MARCHASH_MASK;
149 new = marchash->table[slot];
158 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
163 marchash->table[slot] = new;
166 new->subfields = NULL;
167 strncpy(new->key, key, 4);
169 // only 3 char in a marc field name
170 if (new->key[3] != '\0')
173 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
174 strtrimcpy(new->val, val);
179 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
180 struct marcfield *field,
181 const char key, const char *val)
183 struct marcsubfield *new;
184 struct marcsubfield *last;
186 new = field->subfields;
194 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
199 field->subfields = new;
203 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
204 strcpy(new->val, val);
208 struct marcfield *marchash_get_field (struct marchash *marchash,
209 const char *key, struct marcfield *last)
211 struct marcfield *cur;
215 cur = marchash->table[jenkins_hash((const unsigned char *)key) & MARCHASH_MASK];
218 if (!strcmp(cur->key, key))
225 struct marcsubfield *marchash_get_subfield(char key,
226 struct marcfield *field,
227 struct marcsubfield *last)
229 struct marcsubfield *cur;
233 cur = field->subfields;
243 char *marchash_catenate_subfields(struct marcfield *field,
244 const char *delim, NMEM nmem)
247 struct marcsubfield *cur;
248 int delimsize = strlen(delim);
249 int outsize = 1-delimsize;
250 // maybe it would make sense to have an nmem strcpy/strcat?
251 cur = field -> subfields;
254 outsize += strlen(cur->val) + delimsize;
258 output = nmem_malloc(nmem, outsize);
262 cur = field -> subfields;
265 strtrimcat(output, cur->val);
267 strcat(output, delim);
275 * c-file-style: "Stroustrup"
276 * indent-tabs-mode: nil
278 * vim: shiftwidth=4 tabstop=8 expandtab