Clean-up to avoid GCC warnings, constify.
[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 <marchash.h>
34
35 // Jenkins one-at-a-time hash (from pp2 reclists.c, wikipedia)
36 static unsigned int hash(const unsigned char *key)
37 {
38     unsigned int hash = 0;
39
40     while (*key)
41     {
42         hash += *(key++);
43         hash += (hash << 10);
44         hash ^= (hash >> 6);
45     }
46     hash += (hash << 3);
47     hash ^= (hash >> 11);
48     hash += (hash << 15);
49     return hash;
50 }
51
52 inline void strtrimcat(char *dest, const char *src)
53 {
54     const char *in;
55     char *out;
56     char *last_nonspace;
57     in = src;
58     out = dest;
59     // move to end of dest
60     while (*out)
61         out++;
62     // initialise last non-space charater
63     last_nonspace = out;
64     // skip leading whitespace
65     while (isspace(*in))
66         in++;
67     while (*in)
68     {
69         *out = *in;
70         if (!isspace(*in))
71             last_nonspace = out;
72         out++;
73         in++;
74     }
75     *(++last_nonspace) = '\0';
76 }
77
78 inline void strtrimcpy(char *dest, const char *src)
79 {
80     *dest = '\0';
81     strtrimcat(dest, src);
82 }
83
84 struct marchash *marchash_create(NMEM nmem)
85 {
86     struct marchash *new;
87     new = nmem_malloc(nmem, sizeof (struct marchash));
88     memset(new, 0, sizeof (struct marchash));
89     new->nmem = nmem;
90     return new;
91 }
92
93 void marchash_ingest_marcxml(struct marchash *marchash, xmlNodePtr rec_node)
94 {
95      xmlNodePtr field_node;
96      xmlNodePtr sub_node;
97      struct marcfield *field;
98      field_node = rec_node->children;
99
100      while (field_node)
101      {
102          if (field_node->type == XML_ELEMENT_NODE)
103          {
104              field = NULL;
105              if (!strcmp((const char *) field_node->name, "controlfield"))
106              {
107                  field = marchash_add_field(
108                      marchash, 
109                      (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
110                      (const char *) xmlNodeGetContent(field_node));
111              }
112              else if (!strcmp((const char *) field_node->name, "datafield"))
113              {
114                  field = marchash_add_field(
115                      marchash,
116                      (const char *) xmlGetProp(field_node, BAD_CAST "tag"),
117                      (const char *) xmlNodeGetContent(field_node));
118              }
119              if (field)
120              {
121                  sub_node = field_node->children;
122                  while (sub_node) 
123                  {
124                      if ((sub_node->type == XML_ELEMENT_NODE) &&
125                          !strcmp((const char *) sub_node->name, "subfield"))
126                      {
127                          marchash_add_subfield(
128                              marchash, field,
129                              xmlGetProp(sub_node, BAD_CAST "code")[0],
130                              (const char *) xmlNodeGetContent(sub_node));
131                      }
132                      sub_node = sub_node->next;
133                  } 
134              }
135          }
136          field_node = field_node->next;
137      }
138 }
139
140 struct marcfield *marchash_add_field(struct marchash *marchash,
141                                      const char *key, const char *val)
142 {
143     int slot;
144     struct marcfield *new;
145     struct marcfield *last;
146     
147     slot = hash((const unsigned char *) key) & MARCHASH_MASK;
148     new = marchash->table[slot];
149     last = NULL;
150     
151     while (new) 
152     {
153         last = new; 
154         new = new->next;     
155     }
156
157     new = nmem_malloc(marchash->nmem, sizeof (struct marcfield));
158
159     if (last)
160         last->next = new;
161     else
162         marchash->table[slot] = new;
163
164     new->next = NULL;
165     new->subfields = NULL;
166     strncpy(new->key, key, 4);
167     
168     // only 3 char in a marc field name 
169     if (new->key[3] != '\0')
170         return 0;
171
172     new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
173     strtrimcpy(new->val, val);
174
175     return new;
176 }
177
178 struct marcsubfield *marchash_add_subfield(struct marchash *marchash,
179                                            struct marcfield *field,
180                                            const char key, const char *val)
181 {
182     struct marcsubfield *new;
183     struct marcsubfield *last;
184     last = NULL;
185     new = field->subfields;
186
187     while (new)
188     {
189         last = new;
190         new = new->next;
191     }
192
193     new = nmem_malloc(marchash->nmem, sizeof (struct marcsubfield));
194
195     if (last)
196         last->next = new;
197     else
198         field->subfields = new;
199
200     new->next = NULL;
201     new->key = key;
202     new->val = nmem_malloc(marchash->nmem, sizeof (char) * strlen(val) + 1);
203     strcpy(new->val, val);
204     return new;
205 }
206
207 struct marcfield *marchash_get_field (struct marchash *marchash,
208                                       const char *key, struct marcfield *last)
209 {
210     struct marcfield *cur;
211     if (last)
212         cur = last->next;
213     else 
214         cur = marchash->table[hash((const unsigned char *)key) & MARCHASH_MASK];
215     while (cur)
216     {
217         if (!strcmp(cur->key, key))
218             return cur;
219         cur = cur->next;
220     }
221     return NULL;
222 }
223
224 struct marcsubfield *marchash_get_subfield(char key,
225                                            struct marcfield *field,
226                                            struct marcsubfield *last)
227 {
228     struct marcsubfield *cur;
229     if (last)
230         cur = last->next;
231     else
232         cur = field->subfields;
233     while (cur)
234     {
235         if (cur->key == key)
236           return cur;
237         cur = cur->next;
238     }
239     return NULL;
240 }
241
242 char *marchash_catenate_subfields(struct marcfield *field,
243                                   const char *delim, NMEM nmem)
244 {
245     char *output;
246     struct marcsubfield *cur;
247     int delimsize = strlen(delim);
248     int outsize = 1-delimsize;
249     // maybe it would make sense to have an nmem strcpy/strcat?
250     cur = field -> subfields;
251     while (cur)
252     {
253         outsize += strlen(cur->val) + delimsize;
254         cur = cur->next;
255     }  
256     if (outsize > 0)
257         output = nmem_malloc(nmem, outsize); 
258     else
259         return NULL;
260     *output = '\0';
261     cur = field -> subfields;
262     while (cur)
263     {
264         strtrimcat(output, cur->val);
265         if (cur->next)
266             strcat(output, delim); 
267         cur = cur->next;
268     } 
269     return output;
270 }
271 /*
272  * Local variables:
273  * c-basic-offset: 4
274  * c-file-style: "Stroustrup"
275  * indent-tabs-mode: nil
276  * End:
277  * vim: shiftwidth=4 tabstop=8 expandtab
278  */