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