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