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)
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
34 // Jenkins one-at-a-time hash (from pp2 reclists.c, wikipedia)
35 static unsigned int hash(const unsigned char *key)
37 unsigned int hash = 0;
51 inline char *strtrimcat (char *dest, char *src)
58 // move to end of dest
61 // initialise last non-space charater
63 // skip leading whitespace
74 *(++last_nonspace) = '\0';
77 inline char *strtrimcpy (char *dest, char *src)
80 strtrimcat(dest, src);
83 struct marchash *marchash_create (NMEM nmem)
86 new = nmem_malloc(nmem, sizeof (struct marchash));
87 memset(new, 0, sizeof (struct marchash));
92 int marchash_ingest_marcxml (struct marchash *marchash, xmlNodePtr rec_node)
94 xmlNodePtr field_node;
96 field_node = rec_node->children;
97 struct marcfield *field;
101 if (field_node->type == XML_ELEMENT_NODE)
104 if (!strcmp(field_node->name, "controlfield"))
106 field = marchash_add_field(marchash, xmlGetProp(field_node, "tag"), xmlNodeGetContent(field_node));
108 else if (!strcmp(field_node->name, "datafield"))
110 field = marchash_add_field(marchash, xmlGetProp(field_node, "tag"), xmlNodeGetContent(field_node));
114 sub_node = field_node->children;
117 if ((sub_node->type == XML_ELEMENT_NODE) && (!strcmp(sub_node->name, "subfield")))
119 marchash_add_subfield(marchash, field, xmlGetProp(sub_node, "code")[0], xmlNodeGetContent(sub_node));
121 sub_node = sub_node->next;
125 field_node = field_node->next;
129 struct marcfield *marchash_add_field (struct marchash *marchash, char *key, char *val)
132 struct marcfield *new;
133 struct marcfield *last;
135 slot = hash(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, struct marcfield *field, char key, char *val)
168 struct marcsubfield *new;
169 struct marcsubfield *last;
171 new = field->subfields;
179 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
184 field->subfields = new;
188 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
189 strcpy(new->val, val);
193 struct marcfield *marchash_get_field (struct marchash *marchash, char *key, struct marcfield *last)
195 struct marcfield *cur;
199 cur = marchash->table[hash(key) & MARCHASH_MASK];
202 if (!strcmp(cur->key, key))
209 struct marcsubfield *marchash_get_subfield (char key, struct marcfield *field, struct marcsubfield *last)
211 struct marcsubfield *cur;
215 cur = field->subfields;
225 char *marchash_catenate_subfields (struct marcfield *field, char *delim, NMEM nmem)
228 struct marcsubfield *cur;
229 int delimsize = strlen(delim);
230 int outsize = 1-delimsize;
231 // maybe it would make sense to have an nmem strcpy/strcat?
232 cur = field -> subfields;
235 outsize += strlen(cur->val) + delimsize;
239 output = nmem_malloc(nmem, outsize);
243 cur = field -> subfields;
246 strtrimcat(output, cur->val);
248 strcat(output, delim);
256 * c-file-style: "Stroustrup"
257 * indent-tabs-mode: nil
259 * vim: shiftwidth=4 tabstop=8 expandtab