5 #include <libxml/tree.h>
6 #include <libxml/parser.h>
11 // Jenkins one-at-a-time hash (from pp2 reclists.c, wikipedia)
12 static unsigned int hash(const unsigned char *key)
14 unsigned int hash = 0;
28 inline char *strtrimcat (char *dest, char *src)
35 // move to end of dest
38 // initialise last non-space charater
40 // skip leading whitespace
51 *(++last_nonspace) = '\0';
54 inline char *strtrimcpy (char *dest, char *src)
57 strtrimcat(dest, src);
60 struct marchash *marchash_create (NMEM nmem)
63 new = nmem_malloc(nmem, sizeof (struct marchash));
64 memset(new, 0, sizeof (struct marchash));
69 int marchash_ingest_marcxml (struct marchash *marchash, xmlNodePtr rec_node)
71 xmlNodePtr field_node;
73 field_node = rec_node->children;
74 struct marcfield *field;
78 if (field_node->type == XML_ELEMENT_NODE)
81 if (!strcmp(field_node->name, "controlfield"))
83 field = marchash_add_field(marchash, xmlGetProp(field_node, "tag"), xmlNodeGetContent(field_node));
85 else if (!strcmp(field_node->name, "datafield"))
87 field = marchash_add_field(marchash, xmlGetProp(field_node, "tag"), xmlNodeGetContent(field_node));
91 sub_node = field_node->children;
94 if ((sub_node->type == XML_ELEMENT_NODE) && (!strcmp(sub_node->name, "subfield")))
96 marchash_add_subfield(marchash, field, xmlGetProp(sub_node, "code")[0], xmlNodeGetContent(sub_node));
98 sub_node = sub_node->next;
102 field_node = field_node->next;
106 struct marcfield *marchash_add_field (struct marchash *marchash, char *key, char *val)
109 struct marcfield *new;
110 struct marcfield *last;
112 slot = hash(key) & MARCHASH_MASK;
113 new = marchash->table[slot];
122 new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
127 marchash->table[slot] = new;
130 new->subfields = NULL;
131 strncpy(new->key, key, 4);
133 // only 3 char in a marc field name
134 if (new->key[3] != '\0')
137 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
138 strtrimcpy(new->val, val);
143 struct marcsubfield *marchash_add_subfield (struct marchash *marchash, struct marcfield *field, char key, char *val)
145 struct marcsubfield *new;
146 struct marcsubfield *last;
148 new = field->subfields;
156 new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
161 field->subfields = new;
165 new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
166 strcpy(new->val, val);
170 struct marcfield *marchash_get_field (struct marchash *marchash, char *key, struct marcfield *last)
172 struct marcfield *cur;
176 cur = marchash->table[hash(key) & MARCHASH_MASK];
179 if (!strcmp(cur->key, key))
186 struct marcsubfield *marchash_get_subfield (char key, struct marcfield *field, struct marcsubfield *last)
188 struct marcsubfield *cur;
192 cur = field->subfields;
202 char *marchash_catenate_subfields (struct marcfield *field, char *delim, NMEM nmem)
205 struct marcsubfield *cur;
206 int delimsize = strlen(delim);
207 int outsize = 1-delimsize;
208 // maybe it would make sense to have an nmem strcpy/strcat?
209 cur = field -> subfields;
212 outsize += strlen(cur->val) + delimsize;
216 output = nmem_malloc(nmem, outsize);
220 cur = field -> subfields;
223 strtrimcat(output, cur->val);
225 strcat(output, delim);