DRY out multiple hash functions.
[pazpar2-moved-to-github.git] / src / marchash.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 /** \file 
21     \brief MARC MAP utilities (hash lookup etc)
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <yaz/nmem.h>
32
33 #include "jenkins_hash.h"
34 #include <marchash.h>
35
36 inline void strtrimcat(char *dest, const char *src)
37 {
38     const char *in;
39     char *out;
40     char *last_nonspace;
41     in = src;
42     out = dest;
43     // move to end of dest
44     while (*out)
45         out++;
46     // initialise last non-space charater
47     last_nonspace = out;
48     // skip leading whitespace
49     while (isspace(*in))
50         in++;
51     while (*in)
52     {
53         *out = *in;
54         if (!isspace(*in))
55             last_nonspace = out;
56         out++;
57         in++;
58     }
59     *(++last_nonspace) = '\0';
60 }
61
62 inline void strtrimcpy(char *dest, const char *src)
63 {
64     *dest = '\0';
65     strtrimcat(dest, src);
66 }
67
68 struct marchash *marchash_create(NMEM nmem)
69 {
70     struct marchash *new;
71     new = nmem_malloc(nmem, sizeof (struct marchash));
72     memset(new, 0, sizeof (struct marchash));
73     new->nmem = nmem;
74     return new;
75 }
76
77 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
78 {
79      xmlNodePtr field_node;
80      xmlNodePtr sub_node;
81      struct marcfield *field;
82      field_node = rec_node->children;
83
84      while (field_node)
85      {
86          if (field_node->type == XML_ELEMENT_NODE)
87          {
88              field = NULL;
89              if (!strcmp((const char *) field_node->name, "controlfield"))
90              {
91                  field = marchash_add_field(
92                      marchash, 
93                      (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
94                      (const char *) xmlNodeGetContent(field_node));
95              }
96              else if (!strcmp((const char *) field_node->name, "datafield"))
97              {
98                  field = marchash_add_field(
99                      marchash,
100                      (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
101                      (const char *) xmlNodeGetContent(field_node));
102              }
103              if (field)
104              {
105                  sub_node = field_node->children;
106                  while (sub_node) 
107                  {
108                      if ((sub_node->type == XML_ELEMENT_NODE) &&
109                          !strcmp((const char *) sub_node->name, "subfield"))
110                      {
111                          marchash_add_subfield(
112                              marchash, field,
113                              xmlGetProp(sub_node, BAD_CAST "code")[0],
114                              (const char *) xmlNodeGetContent(sub_node));
115                      }
116                      sub_node = sub_node->next;
117                  } 
118              }
119          }
120          field_node = field_node->next;
121      }
122 }
123
124 struct marcfield *marchash_add_field(struct marchash *marchash,
125                                      const char *key, const char *val)
126 {
127     int slot;
128     struct marcfield *new;
129     struct marcfield *last;
130     
131     slot = jenkins_hash((const unsigned char *) key) & MARCHASH_MASK;
132     new = marchash->table[slot];
133     last = NULL;
134     
135     while (new) 
136     {
137         last = new; 
138         new = new->next;     
139     }
140
141     new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
142
143     if (last)
144         last->next = new;
145     else
146         marchash->table[slot] = new;
147
148     new->next = NULL;
149     new->subfields = NULL;
150     strncpy(new->key, key, 4);
151     
152     // only 3 char in a marc field name 
153     if (new->key[3] != '\0')
154         return 0;
155
156     new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
157     strtrimcpy(new->val, val);
158
159     return new;
160 }
161
162 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
163                                            struct marcfield *field,
164                                            const char key, const char *val)
165 {
166     struct marcsubfield *new;
167     struct marcsubfield *last;
168     last = NULL;
169     new = field->subfields;
170
171     while (new)
172     {
173         last = new;
174         new = new->next;
175     }
176
177     new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
178
179     if (last)
180         last->next = new;
181     else
182         field->subfields = new;
183
184     new->next = NULL;
185     new->key = key;
186     new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
187     strcpy(new->val, val);
188     return new;
189 }
190
191 struct marcfield *marchash_get_field (struct marchash *marchash,
192                                       const char *key, struct marcfield *last)
193 {
194     struct marcfield *cur;
195     if (last)
196         cur = last->next;
197     else 
198         cur = marchash->table[jenkins_hash((const unsigned char *)key) & MARCHASH_MASK];
199     while (cur)
200     {
201         if (!strcmp(cur->key, key))
202             return cur;
203         cur = cur->next;
204     }
205     return NULL;
206 }
207
208 struct marcsubfield *marchash_get_subfield(char key,
209                                            struct marcfield *field,
210                                            struct marcsubfield *last)
211 {
212     struct marcsubfield *cur;
213     if (last)
214         cur = last->next;
215     else
216         cur = field->subfields;
217     while (cur)
218     {
219         if (cur->key == key)
220           return cur;
221         cur = cur->next;
222     }
223     return NULL;
224 }
225
226 char *marchash_catenate_subfields(struct marcfield *field,
227                                   const char *delim, NMEM nmem)
228 {
229     char *output;
230     struct marcsubfield *cur;
231     int delimsize = strlen(delim);
232     int outsize = 1-delimsize;
233     // maybe it would make sense to have an nmem strcpy/strcat?
234     cur = field -> subfields;
235     while (cur)
236     {
237         outsize += strlen(cur->val) + delimsize;
238         cur = cur->next;
239     }  
240     if (outsize > 0)
241         output = nmem_malloc(nmem, outsize); 
242     else
243         return NULL;
244     *output = '\0';
245     cur = field -> subfields;
246     while (cur)
247     {
248         strtrimcat(output, cur->val);
249         if (cur->next)
250             strcat(output, delim); 
251         cur = cur->next;
252     } 
253     return output;
254 }
255 /*
256  * Local variables:
257  * c-basic-offset: 4
258  * c-file-style: "Stroustrup"
259  * indent-tabs-mode: nil
260  * End:
261  * vim: shiftwidth=4 tabstop=8 expandtab
262  */