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 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 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 field = marchash_add_field(
97 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
98 (const char *) xmlNodeGetContent(field_node));
100 else if (!strcmp((const char *) field_node->name, "datafield"))
102 field = marchash_add_field(
104 (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
105 (const char *) xmlNodeGetContent(field_node));
109 sub_node = field_node->children;
112 if ((sub_node->type == XML_ELEMENT_NODE) &&
113 !strcmp((const char *) sub_node->name, "subfield"))
115 marchash_add_subfield(
117 xmlGetProp(sub_node, BAD_CAST "code")[0],
118 (const char *) xmlNodeGetContent(sub_node));
120 sub_node = sub_node->next;
124 field_node = field_node->next;
128 struct marcfield *marchash_add_field(struct marchash *marchash,
129 const char *key, const char *val)
132 struct marcfield *new;
133 struct marcfield *last;
135 slot = jenkins_hash((const unsigned char *) key) & MARCHASH_MASK;
136 new = marchash->table[slot];
145 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
150 marchash->table[slot] = new;
153 new->subfields = NULL;
154 strncpy(new->key, key, 4);
156 // only 3 char in a marc field name
157 if (new->key[3] != '\0')
160 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
161 strtrimcpy(new->val, val);
166 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
167 struct marcfield *field,
168 const char key, const char *val)
170 struct marcsubfield *new;
171 struct marcsubfield *last;
173 new = field->subfields;
181 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
186 field->subfields = new;
190 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
191 strcpy(new->val, val);
195 struct marcfield *marchash_get_field (struct marchash *marchash,
196 const char *key, struct marcfield *last)
198 struct marcfield *cur;
202 cur = marchash->table[jenkins_hash((const unsigned char *)key) & MARCHASH_MASK];
205 if (!strcmp(cur->key, key))
212 struct marcsubfield *marchash_get_subfield(char key,
213 struct marcfield *field,
214 struct marcsubfield *last)
216 struct marcsubfield *cur;
220 cur = field->subfields;
230 char *marchash_catenate_subfields(struct marcfield *field,
231 const char *delim, NMEM nmem)
234 struct marcsubfield *cur;
235 int delimsize = strlen(delim);
236 int outsize = 1-delimsize;
237 // maybe it would make sense to have an nmem strcpy/strcat?
238 cur = field -> subfields;
241 outsize += strlen(cur->val) + delimsize;
245 output = nmem_malloc(nmem, outsize);
249 cur = field -> subfields;
252 strtrimcat(output, cur->val);
254 strcat(output, delim);
262 * c-file-style: "Stroustrup"
263 * indent-tabs-mode: nil
265 * vim: shiftwidth=4 tabstop=8 expandtab