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)
33 #include <libxml/tree.h>
34 #include <libxml/parser.h>
37 #include "jenkins_hash.h"
40 static inline void strtrimcat(char *dest, const char *src)
47 // move to end of dest
50 // initialise last non-space charater
52 // skip leading whitespace
63 *(++last_nonspace) = '\0';
66 static inline void strtrimcpy(char *dest, const char *src)
69 strtrimcat(dest, src);
72 struct marchash *marchash_create(NMEM nmem)
75 new = nmem_malloc(nmem, sizeof (struct marchash));
76 memset(new, 0, sizeof (struct marchash));
81 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
83 xmlNodePtr field_node;
85 struct marcfield *field;
86 field_node = rec_node->children;
90 if (field_node->type == XML_ELEMENT_NODE)
93 if (!strcmp((const char *) field_node->name, "controlfield"))
95 xmlChar *content = xmlNodeGetContent(field_node);
96 xmlChar *tag = xmlGetProp(field_node, BAD_CAST "tag");
98 field = marchash_add_field(
99 marchash, (const char *) tag, (const char *) content);
103 else if (!strcmp((const char *) field_node->name, "datafield"))
105 xmlChar *content = xmlNodeGetContent(field_node);
106 xmlChar *tag = xmlGetProp(field_node, BAD_CAST "tag");
108 field = marchash_add_field(
109 marchash, (const char *) tag, (const char *) content);
115 sub_node = field_node->children;
118 if ((sub_node->type == XML_ELEMENT_NODE) &&
119 !strcmp((const char *) sub_node->name, "subfield"))
121 xmlChar *content = xmlNodeGetContent(sub_node);
122 xmlChar *code = xmlGetProp(sub_node, BAD_CAST "code");
124 marchash_add_subfield(
126 code[0], (const char *) content);
130 sub_node = sub_node->next;
134 field_node = field_node->next;
138 struct marcfield *marchash_add_field(struct marchash *marchash,
139 const char *key, const char *val)
142 struct marcfield *new;
143 struct marcfield *last;
145 slot = jenkins_hash((const unsigned char *) key) & MARCHASH_MASK;
146 new = marchash->table[slot];
155 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
160 marchash->table[slot] = new;
163 new->subfields = NULL;
164 strncpy(new->key, key, 4);
166 // only 3 char in a marc field name
167 if (new->key[3] != '\0')
170 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
171 strtrimcpy(new->val, val);
176 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
177 struct marcfield *field,
178 const char key, const char *val)
180 struct marcsubfield *new;
181 struct marcsubfield *last;
183 new = field->subfields;
191 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
196 field->subfields = new;
200 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
201 strcpy(new->val, val);
205 struct marcfield *marchash_get_field (struct marchash *marchash,
206 const char *key, struct marcfield *last)
208 struct marcfield *cur;
212 cur = marchash->table[jenkins_hash((const unsigned char *)key) & MARCHASH_MASK];
215 if (!strcmp(cur->key, key))
222 struct marcsubfield *marchash_get_subfield(char key,
223 struct marcfield *field,
224 struct marcsubfield *last)
226 struct marcsubfield *cur;
230 cur = field->subfields;
240 char *marchash_catenate_subfields(struct marcfield *field,
241 const char *delim, NMEM nmem)
244 struct marcsubfield *cur;
245 int delimsize = strlen(delim);
246 int outsize = 1-delimsize;
247 // maybe it would make sense to have an nmem strcpy/strcat?
248 cur = field -> subfields;
251 outsize += strlen(cur->val) + delimsize;
255 output = nmem_malloc(nmem, outsize);
259 cur = field -> subfields;
262 strtrimcat(output, cur->val);
264 strcat(output, delim);
272 * c-file-style: "Stroustrup"
273 * indent-tabs-mode: nil
275 * vim: shiftwidth=4 tabstop=8 expandtab