Avoid using 'icu_chain' in non-YAZ_HAVE_ICU mode
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2012 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 charsets.c
21     \brief Pazpar2 Character set facilities
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
30 #include <yaz/log.h>
31 #include <yaz/yaz-version.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <string.h>
35
36 #include "charsets.h"
37 #include "normalize7bit.h"
38
39 typedef struct pp2_charset_s *pp2_charset_t;
40 static pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node);
41 static pp2_charset_t pp2_charset_create(void);
42 static pp2_charset_t pp2_charset_create_a_to_z(void);
43 static void pp2_charset_destroy(pp2_charset_t pct);
44 static pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct);
45
46 #if YAZ_HAVE_ICU
47 #include <yaz/icu.h>
48 static pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn);
49 #endif
50
51 /* charset handle */
52 struct pp2_charset_s {
53     const char *(*token_next_handler)(pp2_charset_token_t prt);
54     const char *(*get_sort_handler)(pp2_charset_token_t prt);
55     const char *(*get_display_handler)(pp2_charset_token_t prt);
56 #if YAZ_HAVE_ICU
57     struct icu_chain * icu_chn;
58     UErrorCode icu_sts;
59 #endif
60 };
61
62 static const char *pp2_charset_token_null(pp2_charset_token_t prt);
63 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt);
64 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt);
65 static const char *pp2_get_display_ascii(pp2_charset_token_t prt);
66
67 #if YAZ_HAVE_ICU
68 static const char *pp2_charset_token_icu(pp2_charset_token_t prt);
69 static const char *pp2_get_sort_icu(pp2_charset_token_t prt);
70 static const char *pp2_get_display_icu(pp2_charset_token_t prt);
71 #endif
72
73 /* tokenzier handle */
74 struct pp2_charset_token_s {
75     const char *cp;     /* unnormalized buffer we're tokenizing */
76     const char *last_cp;  /* pointer to last token we're dealing with */
77     pp2_charset_t pct;  /* our main charset handle (type+config) */
78     WRBUF norm_str;     /* normized string we return (temporarily) */
79     WRBUF sort_str;     /* sort string we return (temporarily) */
80 #if YAZ_HAVE_ICU
81     yaz_icu_iter_t iter;
82 #endif
83 };
84
85 struct pp2_charset_fact_s {
86     struct pp2_charset_entry *list;
87     int ref_count;
88 };
89
90 struct pp2_charset_entry {
91     struct pp2_charset_entry *next;
92     pp2_charset_t pct;
93     char *name;
94 };
95
96
97 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
98                                 pp2_charset_t pct, const char *default_id);
99
100 pp2_charset_fact_t pp2_charset_fact_create(void)
101 {
102     pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
103     pft->list = 0;
104     pft->ref_count = 1;
105
106     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
107     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
108     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
109     pp2_charset_fact_add(pft, pp2_charset_create(), "facet");
110     return pft;
111 }
112
113 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
114 {
115     if (pft)
116     {
117         assert(pft->ref_count >= 1);
118         --(pft->ref_count);
119         if (pft->ref_count == 0)
120         {
121             struct pp2_charset_entry *pce = pft->list;
122             while (pce)
123             {
124                 struct pp2_charset_entry *next = pce->next;
125                 pp2_charset_destroy(pce->pct);
126                 xfree(pce->name);
127                 xfree(pce);
128                 pce = next;
129             }
130             xfree(pft);
131         }
132     }
133 }
134
135 int pp2_charset_fact_add(pp2_charset_fact_t pft,
136                          pp2_charset_t pct, const char *default_id)
137 {
138     struct pp2_charset_entry *pce;
139
140     for (pce = pft->list; pce; pce = pce->next)
141         if (!strcmp(default_id, pce->name))
142             break;
143
144     if (!pce)
145     {
146         pce = xmalloc(sizeof(*pce));
147         pce->name = xstrdup(default_id);
148         pce->next = pft->list;
149         pft->list = pce;
150     }
151     else
152     {
153         pp2_charset_destroy(pce->pct);
154     }
155     pce->pct = pct;
156     return 0;
157 }
158
159 int pp2_charset_fact_define(pp2_charset_fact_t pft,
160                             xmlNode *xml_node, const char *default_id)
161 {
162     int r;
163     pp2_charset_t pct;
164     xmlChar *id = 0;
165
166     assert(xml_node);
167     pct = pp2_charset_create_xml(xml_node);
168     if (!pct)
169         return -1;
170     if (!default_id)
171     {
172         id = xmlGetProp(xml_node, (xmlChar*) "id");
173         if (!id)
174         {
175             yaz_log(YLOG_WARN, "Missing id for icu_chain");
176             pp2_charset_destroy(pct);
177             return -1;
178         }
179         default_id = (const char *) id;
180     }
181     r = pp2_charset_fact_add(pft, pct, default_id);
182     if (id)
183         xmlFree(id);
184     return r;
185 }
186
187 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
188 {
189     (pft->ref_count)++;
190 }
191
192 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
193 {
194 #if YAZ_HAVE_ICU
195     UErrorCode status = U_ZERO_ERROR;
196     struct icu_chain *chain = 0;
197     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
198         xml_node = xml_node->next;
199     chain = icu_chain_xml_config(xml_node, 1, &status);
200     if (!chain || U_FAILURE(status)){
201         //xmlDocPtr icu_doc = 0;
202         //xmlChar *xmlstr = 0;
203                 //int size = 0;
204                 //xmlDocDumpMemory(icu_doc, size);
205
206         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
207                 "<%s>\n ... \n</%s>",
208                 xml_node->name, xml_node->name);
209         return 0;
210     }
211     return pp2_charset_create_icu(chain);
212 #else // YAZ_HAVE_ICU
213     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
214             "<%s>\n ... \n</%s>",
215             xml_node->name, xml_node->name);
216     yaz_log(YLOG_FATAL,
217             "But no ICU support is compiled into the YAZ library.");
218     return 0;
219 #endif // YAZ_HAVE_ICU
220 }
221
222 pp2_charset_t pp2_charset_create(void)
223 {
224     pp2_charset_t pct = xmalloc(sizeof(*pct));
225
226     pct->token_next_handler = pp2_charset_token_null;
227     pct->get_sort_handler  = pp2_get_sort_ascii;
228     pct->get_display_handler  = pp2_get_display_ascii;
229 #if YAZ_HAVE_ICU
230     pct->icu_chn = 0;
231 #endif // YAZ_HAVE_ICU
232     return pct;
233 }
234
235 pp2_charset_t pp2_charset_create_a_to_z(void)
236 {
237     pp2_charset_t pct = pp2_charset_create();
238     pct->token_next_handler = pp2_charset_token_a_to_z;
239     return pct;
240 }
241
242 #if YAZ_HAVE_ICU
243 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
244 {
245     pp2_charset_t pct = pp2_charset_create();
246     if (icu_chn)
247     {
248         pct->icu_chn = icu_chn;
249         pct->icu_sts = U_ZERO_ERROR;
250         pct->token_next_handler = pp2_charset_token_icu;
251         pct->get_sort_handler = pp2_get_sort_icu;
252         pct->get_display_handler = pp2_get_display_icu;
253     }
254     return pct;
255 }
256 #endif // YAZ_HAVE_ICU
257
258 void pp2_charset_destroy(pp2_charset_t pct)
259 {
260 #if YAZ_HAVE_ICU
261     icu_chain_destroy(pct->icu_chn);
262 #endif
263     xfree(pct);
264 }
265
266 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
267                                                const char *id)
268 {
269     struct pp2_charset_entry *pce;
270     for (pce = pft->list; pce; pce = pce->next)
271         if (!strcmp(id, pce->name))
272             return pp2_charset_tokenize(pce->pct);
273     return 0;
274 }
275
276 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
277 {
278     pp2_charset_token_t prt = xmalloc(sizeof(*prt));
279
280     assert(pct);
281
282     prt->norm_str = wrbuf_alloc();
283     prt->sort_str = wrbuf_alloc();
284     prt->cp = 0;
285     prt->last_cp = 0;
286     prt->pct = pct;
287
288 #if YAZ_HAVE_ICU
289     prt->iter = 0;
290     if (pct->icu_chn)
291         prt->iter = icu_iter_create(pct->icu_chn);
292 #endif
293     return prt;
294 }
295
296 void pp2_charset_token_first(pp2_charset_token_t prt,
297                              const char *buf, int skip_article)
298 {
299     if (skip_article)
300     {
301         const char *p = buf;
302         char firstword[64];
303         char *pout = firstword;
304         char articles[] = "the den der die des an a "; // must end in space
305
306         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
307             *pout++ = tolower(*(unsigned char *)p);
308         *pout++ = ' ';
309         *pout++ = '\0';
310         if (strstr(articles, firstword))
311             buf = p;
312     }
313
314     wrbuf_rewind(prt->norm_str);
315     wrbuf_rewind(prt->sort_str);
316     prt->cp = buf;
317     prt->last_cp = 0;
318
319 #if YAZ_HAVE_ICU
320     if (prt->iter)
321     {
322         icu_iter_first(prt->iter, buf);
323     }
324 #endif // YAZ_HAVE_ICU
325 }
326
327 void pp2_charset_token_destroy(pp2_charset_token_t prt)
328 {
329     assert(prt);
330 #if YAZ_HAVE_ICU
331     if (prt->iter)
332         icu_iter_destroy(prt->iter);
333 #endif
334     if(prt->norm_str)
335         wrbuf_destroy(prt->norm_str);
336     if(prt->sort_str)
337         wrbuf_destroy(prt->sort_str);
338     xfree(prt);
339 }
340
341 const char *pp2_charset_token_next(pp2_charset_token_t prt)
342 {
343     assert(prt);
344     return (prt->pct->token_next_handler)(prt);
345 }
346
347 const char *pp2_get_sort(pp2_charset_token_t prt)
348 {
349     return prt->pct->get_sort_handler(prt);
350 }
351
352 const char *pp2_get_display(pp2_charset_token_t prt)
353 {
354     return prt->pct->get_display_handler(prt);
355 }
356
357 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
358 /* original tokenizer with our tokenize interface, but we
359    add +1 to ensure no '\0' are in our string (except for EOF)
360 */
361 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
362 {
363     const char *cp = prt->cp;
364     int c;
365
366     /* skip white space */
367     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
368         cp++;
369     if (*cp == '\0')
370     {
371         prt->cp = cp;
372         prt->last_cp = 0;
373         return 0;
374     }
375     /* now read the term itself */
376
377     prt->last_cp = cp;
378     wrbuf_rewind(prt->norm_str);
379     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
380     {
381         wrbuf_putc(prt->norm_str, c);
382         cp++;
383     }
384     prt->cp = cp;
385     return wrbuf_cstr(prt->norm_str);
386 }
387
388 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
389 {
390     if (prt->last_cp == 0)
391         return 0;
392     else
393     {
394         char *tmp = xstrdup(prt->last_cp);
395         char *result = 0;
396         result = normalize7bit_mergekey(tmp);
397
398         wrbuf_rewind(prt->sort_str);
399         wrbuf_puts(prt->sort_str, result);
400         xfree(tmp);
401         return wrbuf_cstr(prt->sort_str);
402     }
403 }
404
405 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
406 {
407     if (prt->last_cp == 0)
408         return 0;
409     else
410     {
411         return wrbuf_cstr(prt->norm_str);
412     }
413 }
414
415 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
416 {
417     const char *cp = prt->cp;
418
419     prt->last_cp = *cp ? cp : 0;
420     while (*cp)
421         cp++;
422     prt->cp = cp;
423     return prt->last_cp;
424 }
425
426 #if YAZ_HAVE_ICU
427 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
428 {
429     if (icu_iter_next(prt->iter))
430     {
431         return icu_iter_get_norm(prt->iter);
432     }
433     return 0;
434 }
435
436 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
437 {
438     return icu_iter_get_sortkey(prt->iter);
439 }
440
441 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
442 {
443     return icu_iter_get_display(prt->iter);
444 }
445
446 #endif // YAZ_HAVE_ICU
447
448
449 /*
450  * Local variables:
451  * c-basic-offset: 4
452  * c-file-style: "Stroustrup"
453  * indent-tabs-mode: nil
454  * End:
455  * vim: shiftwidth=4 tabstop=8 expandtab
456  */
457