ICU: using icu_iter for existing interface
[yaz-moved-to-github.git] / src / icu_chain.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file
8  * \brief ICU chain
9  */
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #if YAZ_HAVE_ICU
16 #include <yaz/xmalloc.h>
17
18 #include <yaz/icu_I18N.h>
19
20 #include <yaz/log.h>
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include <unicode/ustring.h>  /* some more string fcns*/
28 #include <unicode/uchar.h>    /* char names           */
29
30 enum icu_chain_step_type {
31     ICU_chain_step_type_none,
32     ICU_chain_step_type_display,   /* convert to utf8 display format */
33     ICU_chain_step_type_casemap,   /* apply utf16 charmap */
34     ICU_chain_step_type_transform, /* apply utf16 transform */
35     ICU_chain_step_type_tokenize,  /* apply utf16 tokenization */
36     ICU_chain_step_type_transliterate  /* apply utf16 tokenization */
37 };
38
39 #define USE_ITER 1
40
41 struct icu_chain_step
42 {
43     /* type and action object */
44     enum icu_chain_step_type type;
45     union {
46         struct icu_casemap * casemap;
47         struct icu_transform * transform;
48         struct icu_tokenizer * tokenizer;  
49     } u;
50     struct icu_chain_step * previous;
51 #if USE_ITER
52 #else
53     /* temprary post-action utf16 buffer */
54     struct icu_buf_utf16 * buf16;  
55     int more_tokens;
56     int need_new_token;
57 #endif
58 };
59
60
61 struct icu_chain
62 {
63 #if USE_ITER
64     struct icu_iter *iter;
65 #endif
66
67     char *locale;
68     int sort;
69
70     UCollator * coll;
71     
72 #if USE_ITER
73 #else
74     const char * src8cstr;
75
76     /* number of tokens returned so far */
77     int32_t token_count;
78 #endif
79     
80     /* utf8 output buffers */
81     struct icu_buf_utf8 * norm8;
82 #if USE_ITER
83 #else
84     struct icu_buf_utf8 * display8;
85     struct icu_buf_utf8 * sort8;
86     
87     /* utf16 source buffer */
88     struct icu_buf_utf16 * src16;
89 #endif
90     
91     /* linked list of chain steps */
92     struct icu_chain_step * steps;
93 };
94
95 int icu_check_status(UErrorCode status)
96 {
97     if (U_FAILURE(status))
98     {
99         yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
100         return 0;   
101     }
102     return 1;
103 }
104
105 static struct icu_chain_step *icu_chain_step_create(
106     struct icu_chain * chain,  enum icu_chain_step_type type,
107     const uint8_t * rule, 
108 #if USE_ITER
109 #else
110     struct icu_buf_utf16 * buf16,
111 #endif
112     UErrorCode *status)
113 {
114     struct icu_chain_step * step = 0;
115     
116     if(!chain || !type || !rule)
117         return 0;
118
119     step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
120
121     step->type = type;
122 #if USE_ITER
123 #else
124     step->buf16 = buf16;
125 #endif
126     /* create auxilary objects */
127     switch (step->type)
128     {
129     case ICU_chain_step_type_display:
130         break;
131     case ICU_chain_step_type_casemap:
132         step->u.casemap = icu_casemap_create(rule[0], status);
133         break;
134     case ICU_chain_step_type_transform:
135         /* rule omitted. Only ID used */
136         step->u.transform = icu_transform_create((const char *) rule, 'f',
137                                                  0, status);
138         break;
139     case ICU_chain_step_type_tokenize:
140         step->u.tokenizer = icu_tokenizer_create((char *) chain->locale, 
141                                                  (char) rule[0], status);
142         break;
143     case ICU_chain_step_type_transliterate:
144         /* we pass a dummy ID to utrans_openU.. */
145         step->u.transform = icu_transform_create("custom", 'f',
146                                                  (const char *) rule, status);
147         break;
148     default:
149         break;
150     }
151     return step;
152 }
153
154
155 static void icu_chain_step_destroy(struct icu_chain_step * step)
156 {
157     if (!step)
158         return;
159
160     icu_chain_step_destroy(step->previous);
161
162     switch (step->type)
163     {
164     case ICU_chain_step_type_display:
165         break;
166     case ICU_chain_step_type_casemap:
167         icu_casemap_destroy(step->u.casemap);
168 #if USE_ITER
169 #else
170         icu_buf_utf16_destroy(step->buf16);
171 #endif
172         break;
173     case ICU_chain_step_type_transform:
174     case ICU_chain_step_type_transliterate:
175         icu_transform_destroy(step->u.transform);
176 #if USE_ITER
177 #else
178         icu_buf_utf16_destroy(step->buf16);
179 #endif
180         break;
181     case ICU_chain_step_type_tokenize:
182         icu_tokenizer_destroy(step->u.tokenizer);
183 #if USE_ITER
184 #else
185         icu_buf_utf16_destroy(step->buf16);
186 #endif
187         break;
188     default:
189         break;
190     }
191     xfree(step);
192 }
193
194 struct icu_chain *icu_chain_create(const char *locale, int sort,
195                                    UErrorCode * status)
196 {
197     struct icu_chain * chain 
198         = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
199
200     *status = U_ZERO_ERROR;
201
202 #if USE_ITER
203     chain->iter = 0;
204 #endif
205     chain->locale = xstrdup(locale);
206
207     chain->sort = sort;
208
209     chain->coll = ucol_open((const char *) chain->locale, status);
210
211     if (U_FAILURE(*status))
212         return 0;
213 #if USE_ITER
214 #else
215     chain->token_count = 0;
216     chain->src8cstr = 0;
217 #endif
218
219     chain->norm8 = icu_buf_utf8_create(0);
220 #if USE_ITER
221 #else
222     chain->display8 = icu_buf_utf8_create(0);
223     chain->sort8 = icu_buf_utf8_create(0);
224     chain->src16 = icu_buf_utf16_create(0);
225 #endif
226
227     chain->steps = 0;
228
229     return chain;
230 }
231
232 void icu_chain_destroy(struct icu_chain * chain)
233 {
234     if (chain)
235     {
236         if (chain->coll)
237             ucol_close(chain->coll);
238
239         icu_buf_utf8_destroy(chain->norm8);
240 #if USE_ITER
241         if (chain->iter)
242             icu_iter_destroy(chain->iter);
243 #else
244         icu_buf_utf8_destroy(chain->display8);
245         icu_buf_utf8_destroy(chain->sort8);
246         icu_buf_utf16_destroy(chain->src16);
247 #endif        
248     
249         icu_chain_step_destroy(chain->steps);
250         xfree(chain->locale);
251         xfree(chain);
252     }
253 }
254
255 static struct icu_chain_step *icu_chain_insert_step(
256     struct icu_chain * chain, enum icu_chain_step_type type,
257     const uint8_t * rule, UErrorCode *status);
258
259 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node, 
260                                         int sort,
261                                         UErrorCode * status)
262 {
263     xmlNode *node = 0;
264     struct icu_chain * chain = 0;
265    
266     *status = U_ZERO_ERROR;
267
268     if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
269         return 0;
270     
271     {
272         xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node, 
273                                           (xmlChar *) "locale");
274         
275         if (xml_locale)
276         {
277             chain = icu_chain_create((const char *) xml_locale, sort, status);
278             xmlFree(xml_locale);
279         }
280         
281     }
282     if (!chain)
283         return 0;
284
285     for (node = xml_node->children; node; node = node->next)
286     {
287         xmlChar *xml_rule;
288         struct icu_chain_step * step = 0;
289
290         if (node->type != XML_ELEMENT_NODE)
291             continue;
292
293         xml_rule = xmlGetProp(node, (xmlChar *) "rule");
294
295         if (!strcmp((const char *) node->name, "casemap"))
296             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
297                                          (const uint8_t *) xml_rule, status);
298         else if (!strcmp((const char *) node->name, "transform"))
299             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
300                                          (const uint8_t *) xml_rule, status);
301         else if (!strcmp((const char *) node->name, "transliterate"))
302             step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate, 
303                                          (const uint8_t *) xml_rule, status);
304         else if (!strcmp((const char *) node->name, "tokenize"))
305             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
306                                          (const uint8_t *) xml_rule, status);
307         else if (!strcmp((const char *) node->name, "display"))
308             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
309                                          (const uint8_t *) "", status);
310         else if (!strcmp((const char *) node->name, "normalize"))
311         {
312             yaz_log(YLOG_WARN, "Element %s is deprecated. "
313                     "Use transform instead", node->name);
314             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
315                                          (const uint8_t *) xml_rule, status);
316         }
317         else if (!strcmp((const char *) node->name, "index")
318                  || !strcmp((const char *) node->name, "sortkey"))
319         {
320             yaz_log(YLOG_WARN, "Element %s is no longer needed. "
321                     "Remove it from the configuration", node->name);
322         }
323         else
324         {
325             yaz_log(YLOG_WARN, "Unknown element %s", node->name);
326             icu_chain_destroy(chain);
327             return 0;
328         }
329         xmlFree(xml_rule);
330         if (step && U_FAILURE(*status))
331         {
332             icu_chain_destroy(chain);
333             return 0;
334         }
335     }
336     return chain;
337 }
338
339 static struct icu_chain_step *icu_chain_insert_step(
340     struct icu_chain * chain, enum icu_chain_step_type type,
341     const uint8_t * rule, UErrorCode *status)
342 {    
343     struct icu_chain_step * step = 0;
344 #if USE_ITER
345 #else
346     struct icu_buf_utf16 * src16 = 0;
347     struct icu_buf_utf16 * buf16 = 0;
348 #endif
349     if (!chain || !type || !rule)
350         return 0;
351
352 #if USE_ITER
353 #else
354     /* assign utf16 src buffers as needed */
355     if (chain->steps && chain->steps->buf16)
356         src16 = chain->steps->buf16;
357     else if (chain->src16)
358         src16 = chain->src16;
359     else
360         return 0;
361
362     /* create utf16 destination buffers as needed, or */
363     switch (type)
364     {
365     case ICU_chain_step_type_display:
366         buf16 = src16;
367         break;
368     case ICU_chain_step_type_casemap:
369         buf16 = icu_buf_utf16_create(0);
370         break;
371     case ICU_chain_step_type_transform:
372     case ICU_chain_step_type_transliterate:
373         buf16 = icu_buf_utf16_create(0);
374         break;
375     case ICU_chain_step_type_tokenize:
376         buf16 = icu_buf_utf16_create(0);
377         break;
378         break;
379     default:
380         break;
381     }
382 #endif
383     /* create actual chain step with this buffer */
384     step = icu_chain_step_create(chain, type, rule,
385 #if USE_ITER
386 #else
387                                  buf16,
388 #endif
389                                  status);
390
391     step->previous = chain->steps;
392     chain->steps = step;
393
394     return step;
395 }
396
397 #if USE_ITER
398 #else
399 static int icu_chain_step_next_token(struct icu_chain * chain,
400                                      struct icu_chain_step * step,
401                                      UErrorCode *status)
402 {
403     struct icu_buf_utf16 * src16 = 0;
404     int got_new_token = 0;
405
406     if (!chain || !chain->src16 || !step || !step->more_tokens)
407         return 0;
408
409     /* assign utf16 src buffers as needed, advance in previous steps
410        tokens until non-zero token met, and setting stop condition */
411
412     if (step->previous)
413     {
414         src16 = step->previous->buf16;
415         /* tokens might be killed in previous steps, therefore looping */
416
417         while (step->need_new_token 
418                && step->previous->more_tokens
419                && !got_new_token)
420             got_new_token
421                 = icu_chain_step_next_token(chain, step->previous, status);
422     }
423     else 
424     { /* first step can only work once on chain->src16 input buffer */
425         src16 = chain->src16;
426         step->more_tokens = 0;
427         got_new_token = 1;
428     }
429
430     if (!src16)
431         return 0;
432
433     /* stop if nothing to process */
434     if (step->need_new_token && !got_new_token)
435     {
436         step->more_tokens = 0;
437         return 0;
438     }
439
440     /* either an old token not finished yet, or a new token, thus
441        perform the work, eventually put this steps output in 
442        step->buf16 or the chains UTF8 output buffers  */
443
444     switch (step->type)
445     {
446     case ICU_chain_step_type_display:
447         icu_utf16_to_utf8(chain->display8, src16, status);
448         break;
449     case ICU_chain_step_type_casemap:
450         icu_casemap_casemap(step->u.casemap,
451                             step->buf16, src16, status,
452                             chain->locale);
453         break;
454     case ICU_chain_step_type_transform:
455     case ICU_chain_step_type_transliterate:
456         icu_transform_trans(step->u.transform,
457                             step->buf16, src16, status);
458         break;
459     case ICU_chain_step_type_tokenize:
460         /* attach to new src16 token only first time during splitting */
461         if (step->need_new_token)
462         {
463             icu_tokenizer_attach(step->u.tokenizer, src16, status);
464             step->need_new_token = 0;
465         }
466
467         /* splitting one src16 token into multiple buf16 tokens */
468         step->more_tokens
469             = icu_tokenizer_next_token(step->u.tokenizer,
470                                        step->buf16, status);
471
472         /* make sure to get new previous token if this one had been used up
473            by recursive call to _same_ step */
474
475         if (!step->more_tokens)
476         {
477             step->more_tokens = icu_chain_step_next_token(chain, step, status);
478             return step->more_tokens;  /* avoid one token count too much! */
479         }
480         break;
481     default:
482         return 0;
483         break;
484     }
485
486     if (U_FAILURE(*status))
487         return 0;
488
489     /* if token disappered into thin air, tell caller */
490     /* if (!step->buf16->utf16_len && !step->more_tokens) */ 
491     /*    return 0; */ 
492
493     return 1;
494 }
495 #endif
496
497 struct icu_iter {
498     struct icu_chain *chain;
499     struct icu_buf_utf16 *last;
500     UErrorCode status;
501     struct icu_buf_utf8 *display;
502     struct icu_buf_utf8 *sort8;
503     struct icu_buf_utf16 *input;
504     int token_count;
505 };
506
507 void icu_utf16_print(struct icu_buf_utf16 *src16)
508 {
509     UErrorCode status = U_ZERO_ERROR;
510     const char *p;
511     struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
512     icu_utf16_to_utf8(dst8, src16, &status);
513
514     assert(status != 1234);
515     if (U_FAILURE(status))
516     {
517         printf("failure");
518     }
519     else
520     {
521         p = icu_buf_utf8_to_cstr(dst8);
522         printf("%s", p);
523     }
524     icu_buf_utf8_destroy(dst8);
525 }
526
527 struct icu_buf_utf16 *icu_iter_invoke(struct icu_iter *iter,
528                                       struct icu_chain_step *step,
529                                       struct icu_buf_utf16 *src)
530 {
531     if (!step)
532         return src;
533     else
534     {
535         struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
536         
537         switch (step->type)
538         {
539         case ICU_chain_step_type_casemap:
540             if (dst)
541             {
542                 struct icu_buf_utf16 *src = dst;
543
544                 dst = icu_buf_utf16_create(0);
545                 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
546                                     iter->chain->locale);
547                 icu_buf_utf16_destroy(src);
548             }
549             break;
550         case ICU_chain_step_type_tokenize:
551             if (dst)
552             {
553                 struct icu_buf_utf16 *src = dst;
554
555                 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
556                 icu_buf_utf16_destroy(src);
557             }
558             dst = icu_buf_utf16_create(0);
559             iter->status = U_ZERO_ERROR;
560             if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
561             {
562                 icu_buf_utf16_destroy(dst);
563                 dst = 0;
564             }
565             break;
566         case ICU_chain_step_type_transform:
567         case ICU_chain_step_type_transliterate:
568             if (dst)
569             {
570                 struct icu_buf_utf16 *src = dst;
571                 dst = icu_buf_utf16_create(0);
572                 icu_transform_trans(step->u.transform, dst, src, &iter->status);
573                 icu_buf_utf16_destroy(src);
574             }
575             break;
576         case ICU_chain_step_type_display:
577             if (dst)
578                 icu_utf16_to_utf8(iter->display, dst, &iter->status);
579             break;
580         default:
581             assert(0);
582         }
583         return dst;
584     }
585 }
586
587 struct icu_iter *icu_iter_create(struct icu_chain *chain,
588                                  const char *src8cstr)
589 {
590     if (!src8cstr)
591         return 0;
592     else
593     {
594         struct icu_iter *iter = xmalloc(sizeof(*iter));
595         iter->chain = chain;
596         iter->status = U_ZERO_ERROR;
597         iter->display = icu_buf_utf8_create(0);
598         iter->sort8 = icu_buf_utf8_create(0);
599         iter->token_count = 0;
600         iter->last = 0; /* no last returned string (yet) */
601
602         /* fill and assign input string.. It will be 0 after
603            first iteration */
604         iter->input =  icu_buf_utf16_create(0);
605         icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
606         return iter;
607
608     }
609 }
610
611 void icu_iter_destroy(struct icu_iter *iter)
612 {
613     if (iter)
614     {
615         icu_buf_utf8_destroy(iter->display);
616         icu_buf_utf8_destroy(iter->sort8);
617         if (iter->input)
618             icu_buf_utf16_destroy(iter->input);
619         xfree(iter);
620     }
621 }
622
623 int icu_iter_next(struct icu_iter *iter, struct icu_buf_utf8 *result)
624 {
625     if (!iter->input && iter->last == 0)
626         return 0;
627     else
628     {
629         /* on first call, iter->input is the input string. Thereafter: 0. */
630         iter->last = icu_iter_invoke(iter, iter->chain->steps, iter->input);
631         iter->input = 0;
632         
633         if (!iter->last)
634             return 0;
635
636         iter->token_count++;
637
638         if (iter->chain->sort)
639         {        
640             icu_sortkey8_from_utf16(iter->chain->coll,
641                                     iter->sort8, iter->last,
642                                     &iter->status);
643         }
644         icu_utf16_to_utf8(result, iter->last, &iter->status);
645         icu_buf_utf16_destroy(iter->last);
646
647         return 1;
648     }
649 }
650
651 const char *icu_iter_get_sortkey(struct icu_iter *iter)
652 {
653     return icu_buf_utf8_to_cstr(iter->sort8);
654 }
655
656 const char *icu_iter_get_display(struct icu_iter *iter)
657
658     return icu_buf_utf8_to_cstr(iter->display);   
659 }
660
661 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr, 
662                           UErrorCode *status)
663 {
664 #if USE_ITER
665     if (chain->iter)
666         icu_iter_destroy(chain->iter);
667     chain->iter = icu_iter_create(chain, src8cstr);
668     return 1;
669 #else
670     struct icu_chain_step * stp = 0; 
671
672     if (!chain || !src8cstr)
673         return 0;
674
675     chain->src8cstr = src8cstr;
676
677     stp = chain->steps;
678     
679     /* clear token count */
680     chain->token_count = 0;
681
682     /* clear all steps stop states */
683     while (stp)
684     {
685         stp->more_tokens = 1;
686         stp->need_new_token = 1;
687         stp = stp->previous;
688     }
689     
690     /* finally convert UTF8 to UTF16 string if needed */
691     if (chain->steps || chain->sort)
692         icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
693             
694     if (U_FAILURE(*status))
695         return 0;
696
697     return 1;
698 #endif
699 }
700
701 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
702 {
703 #if USE_ITER
704     *status = U_ZERO_ERROR;
705     return icu_iter_next(chain->iter, chain->norm8);
706 #else
707     int got_token = 0;
708     
709     *status = U_ZERO_ERROR;
710
711     if (!chain)
712         return 0;
713
714     /* special case with no steps - same as index type binary */
715     if (!chain->steps)
716     {
717         if (chain->token_count)
718             return 0;
719         else
720         {
721             chain->token_count++;
722             
723             if (chain->sort)
724                 icu_sortkey8_from_utf16(chain->coll,
725                                         chain->sort8, chain->steps->buf16,
726                                         status);
727             return chain->token_count;
728         }
729     }
730     /* usual case, one or more icu chain steps existing */
731     else 
732     {
733         while (!got_token && chain->steps && chain->steps->more_tokens)
734             got_token = icu_chain_step_next_token(chain, chain->steps, status);
735
736         if (got_token)
737         {
738             chain->token_count++;
739
740             icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
741             
742             if (chain->sort)
743                 icu_sortkey8_from_utf16(chain->coll,
744                                         chain->sort8, chain->steps->buf16,
745                                         status);
746             return chain->token_count;
747         }
748     }
749         
750     return 0;
751 #endif
752 }
753
754 int icu_chain_token_number(struct icu_chain * chain)
755 {
756 #if USE_ITER
757     if (chain && chain->iter)
758         return chain->iter->token_count;
759     return 0;
760 #else
761     if (!chain)
762         return 0;
763     
764     return chain->token_count;
765 #endif
766 }
767
768 const char * icu_chain_token_display(struct icu_chain * chain)
769 {
770 #if USE_ITER
771     if (chain->iter)
772         return icu_iter_get_display(chain->iter);
773 #else
774     if (chain->display8)
775         return icu_buf_utf8_to_cstr(chain->display8);
776 #endif
777     return 0;
778 }
779
780 const char * icu_chain_token_norm(struct icu_chain * chain)
781 {
782 #if USE_ITER
783     if (chain->norm8)
784         return icu_buf_utf8_to_cstr(chain->norm8);
785 #else
786     if (!chain->steps)
787         return chain->src8cstr;
788
789     if (chain->norm8)
790         return icu_buf_utf8_to_cstr(chain->norm8);
791 #endif
792     return 0;
793 }
794
795 const char * icu_chain_token_sortkey(struct icu_chain * chain)
796 {
797 #if USE_ITER
798     if (chain->iter)
799         return icu_iter_get_sortkey(chain->iter);
800 #else
801     if (chain->sort8)
802         return icu_buf_utf8_to_cstr(chain->sort8);
803 #endif
804     return 0;
805 }
806
807 #endif /* YAZ_HAVE_ICU */
808
809 /*
810  * Local variables:
811  * c-basic-offset: 4
812  * c-file-style: "Stroustrup"
813  * indent-tabs-mode: nil
814  * End:
815  * vim: shiftwidth=4 tabstop=8 expandtab
816  */
817