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