74b42da2b4a36fe60fbb44e258e54a7560144833
[yaz-moved-to-github.git] / src / icu_I18N.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 icu_I18N.c
8  * \brief ICU utilities
9  */
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #define USE_TIMING 0
16 #if USE_TIMING
17 #include <yaz/timing.h>
18 #endif
19
20 #if YAZ_HAVE_ICU
21 #include <yaz/xmalloc.h>
22
23 #include <yaz/icu_I18N.h>
24
25 #include <yaz/log.h>
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include <unicode/ustring.h>  /* some more string fcns*/
32 #include <unicode/uchar.h>    /* char names           */
33 #include <unicode/ucol.h> 
34
35 int icu_check_status(UErrorCode status)
36 {
37     if (U_FAILURE(status))
38     {
39         yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
40         return 0;   
41     }
42     return 1;
43 }
44
45 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
46 {
47     struct icu_buf_utf16 * buf16 
48         = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16));
49
50     buf16->utf16 = 0;
51     buf16->utf16_len = 0;
52     buf16->utf16_cap = 0;
53
54     if (capacity > 0)
55     {
56         buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
57         buf16->utf16[0] = (UChar) 0;
58         buf16->utf16_cap = capacity;
59     }
60     return buf16;
61 }
62
63 struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16)
64 {
65     if (buf16)
66     {
67         if (buf16->utf16)
68             buf16->utf16[0] = (UChar) 0;
69         buf16->utf16_len = 0;
70     }
71     return buf16;
72 }
73
74 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
75                                             size_t capacity)
76 {
77     if (!buf16)
78         return 0;
79     
80     if (capacity >  0)
81     {
82         if (0 == buf16->utf16)
83             buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
84         else
85             buf16->utf16 
86                 = (UChar *) xrealloc(buf16->utf16, sizeof(UChar) * capacity);
87
88         icu_buf_utf16_clear(buf16);
89         buf16->utf16_cap = capacity;
90     } 
91     else
92     {
93         xfree(buf16->utf16);
94         buf16->utf16 = 0;
95         buf16->utf16_len = 0;
96         buf16->utf16_cap = 0;
97     }
98
99     return buf16;
100 }
101
102
103 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
104                                           struct icu_buf_utf16 * src16)
105 {
106     if (!dest16 || !src16 || dest16 == src16)
107         return 0;
108
109     if (dest16->utf16_cap < src16->utf16_len)
110         icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
111
112     u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
113     dest16->utf16_len = src16->utf16_len;
114
115     return dest16;
116 }
117
118 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
119 {
120     if (buf16)
121         xfree(buf16->utf16);
122     xfree(buf16);
123 }
124
125 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
126 {
127     struct icu_buf_utf8 * buf8 
128         = (struct icu_buf_utf8 *) xmalloc(sizeof(struct icu_buf_utf8));
129
130     buf8->utf8 = 0;
131     buf8->utf8_len = 0;
132     buf8->utf8_cap = 0;
133
134     if (capacity > 0)
135     {
136         buf8->utf8 = (uint8_t *) xmalloc(sizeof(uint8_t) * capacity);
137         buf8->utf8[0] = (uint8_t) 0;
138         buf8->utf8_cap = capacity;
139     }
140     return buf8;
141 }
142
143 struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8)
144 {
145     if (buf8)
146     {
147         if (buf8->utf8)
148             buf8->utf8[0] = (uint8_t) 0;
149         buf8->utf8_len = 0;
150     }
151     return buf8;
152 }
153
154 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
155                                           size_t capacity)
156 {
157     if (!buf8)
158         return 0;
159
160     if (capacity >  0){
161         if (0 == buf8->utf8)
162             buf8->utf8 = (uint8_t *) xmalloc(sizeof(uint8_t) * capacity);
163         else
164             buf8->utf8 
165                 = (uint8_t *) xrealloc(buf8->utf8, sizeof(uint8_t) * capacity);
166         
167         buf8->utf8_cap = capacity;
168     } 
169     else { 
170         xfree(buf8->utf8);
171         buf8->utf8 = 0;
172         buf8->utf8_len = 0;
173         buf8->utf8_cap = 0;
174     }
175     
176     return buf8;
177 }
178
179 const char *icu_buf_utf8_to_cstr(struct icu_buf_utf8 *src8)
180 {
181     if (!src8 || src8->utf8_len == 0)
182         return "";
183
184     if (src8->utf8_len == src8->utf8_cap)
185         src8 = icu_buf_utf8_resize(src8, src8->utf8_len * 2 + 1);
186
187     src8->utf8[src8->utf8_len] = '\0';
188
189     return (const char *) src8->utf8;
190 }
191
192 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
193 {
194     if (buf8)
195         xfree(buf8->utf8);
196     xfree(buf8);
197 }
198
199 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
200                                     const char * src8cstr,
201                                     UErrorCode * status)
202 {
203     size_t src8cstr_len = 0;
204     int32_t utf16_len = 0;
205
206     *status = U_ZERO_ERROR;
207     src8cstr_len = strlen(src8cstr);
208   
209     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
210                   &utf16_len,
211                   src8cstr, src8cstr_len, status);
212   
213     /* check for buffer overflow, resize and retry */
214     if (*status == U_BUFFER_OVERFLOW_ERROR)
215     {
216         icu_buf_utf16_resize(dest16, utf16_len * 2);
217         *status = U_ZERO_ERROR;
218         u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
219                       &utf16_len,
220                       src8cstr, src8cstr_len, status);
221     }
222
223     if (U_SUCCESS(*status)  
224         && utf16_len <= dest16->utf16_cap)
225         dest16->utf16_len = utf16_len;
226     else 
227         icu_buf_utf16_clear(dest16);
228   
229     return *status;
230 }
231
232 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
233                              struct icu_buf_utf16 * src16,
234                              UErrorCode * status)
235 {
236     int32_t utf8_len = 0;
237   
238     u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
239                 &utf8_len,
240                 src16->utf16, src16->utf16_len, status);
241   
242     /* check for buffer overflow, resize and retry */
243     if (*status == U_BUFFER_OVERFLOW_ERROR)
244     {
245         icu_buf_utf8_resize(dest8, utf8_len * 2);
246         *status = U_ZERO_ERROR;
247         u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
248                     &utf8_len,
249                     src16->utf16, src16->utf16_len, status);
250     }
251
252     if (U_SUCCESS(*status)  
253         && utf8_len <= dest8->utf8_cap)
254         dest8->utf8_len = utf8_len;
255     else 
256         icu_buf_utf8_clear(dest8);
257   
258     return *status;
259 }
260
261
262
263 struct icu_casemap * icu_casemap_create(char action, UErrorCode *status)
264 {    
265     struct icu_casemap * casemap
266         = (struct icu_casemap *) xmalloc(sizeof(struct icu_casemap));
267     casemap->action = action;
268
269     switch(casemap->action)
270     {
271     case 'l':   
272     case 'L':   
273     case 'u':   
274     case 'U':   
275     case 't':  
276     case 'T':  
277     case 'f':  
278     case 'F':  
279         break;
280     default:
281         icu_casemap_destroy(casemap);
282         return 0;
283     }
284     return casemap;
285 }
286
287 void icu_casemap_destroy(struct icu_casemap * casemap)
288 {
289     xfree(casemap);
290 }
291
292 int icu_casemap_casemap(struct icu_casemap * casemap,
293                         struct icu_buf_utf16 * dest16,
294                         struct icu_buf_utf16 * src16,
295                         UErrorCode *status,
296                         const char *locale)
297 {
298     if(!casemap)
299         return 0;
300     
301     return icu_utf16_casemap(dest16, src16, locale,
302                              casemap->action, status);
303 }
304
305 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
306                       struct icu_buf_utf16 * src16,
307                       const char *locale, char action,
308                       UErrorCode *status)
309 {
310     int32_t dest16_len = 0;
311
312     if (!src16->utf16_len)
313     {           /* guarding for empty source string */
314         if (dest16->utf16)
315             dest16->utf16[0] = (UChar) 0;
316         dest16->utf16_len = 0;
317         return U_ZERO_ERROR;
318     }
319     
320     switch(action)
321     {
322     case 'l':    
323     case 'L':    
324         dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
325                                   src16->utf16, src16->utf16_len, 
326                                   locale, status);
327         break;
328     case 'u':    
329     case 'U':    
330         dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
331                                   src16->utf16, src16->utf16_len, 
332                                   locale, status);
333         break;
334     case 't':    
335     case 'T':    
336         dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
337                                   src16->utf16, src16->utf16_len,
338                                   0, locale, status);
339         break;
340     case 'f':    
341     case 'F':    
342         dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
343                                    src16->utf16, src16->utf16_len,
344                                    U_FOLD_CASE_DEFAULT, status);
345         break;
346         
347     default:
348         return U_UNSUPPORTED_ERROR;
349         break;
350     }
351
352     /* check for buffer overflow, resize and retry */
353     if (*status == U_BUFFER_OVERFLOW_ERROR
354         && dest16 != src16        /* do not resize if in-place conversion */
355         )
356     {
357         icu_buf_utf16_resize(dest16, dest16_len * 2);
358         *status = U_ZERO_ERROR;
359
360         switch(action) {    
361         case 'l':    
362         case 'L':    
363             dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
364                                       src16->utf16, src16->utf16_len, 
365                                       locale, status);
366             break;
367         case 'u':    
368         case 'U':    
369             dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
370                                       src16->utf16, src16->utf16_len, 
371                                       locale, status);
372             break;
373         case 't':    
374         case 'T':    
375             dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
376                                       src16->utf16, src16->utf16_len,
377                                       0, locale, status);
378             break;
379         case 'f':    
380         case 'F':    
381             dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
382                                        src16->utf16, src16->utf16_len,
383                                        U_FOLD_CASE_DEFAULT, status);
384             break;
385         
386         default:
387             return U_UNSUPPORTED_ERROR;
388             break;
389         }
390     }
391     
392     if (U_SUCCESS(*status)
393         && dest16_len <= dest16->utf16_cap)
394         dest16->utf16_len = dest16_len;
395     else
396     {
397         if (dest16->utf16)
398             dest16->utf16[0] = (UChar) 0;
399         dest16->utf16_len = 0;
400     }
401   
402     return *status;
403 }
404
405 void icu_sortkey8_from_utf16(UCollator *coll,
406                              struct icu_buf_utf8 * dest8, 
407                              struct icu_buf_utf16 * src16,
408                              UErrorCode * status)
409
410     int32_t sortkey_len = 0;
411
412     sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
413                                   dest8->utf8, dest8->utf8_cap);
414
415     /* check for buffer overflow, resize and retry */
416     if (sortkey_len > dest8->utf8_cap)
417     {
418         icu_buf_utf8_resize(dest8, sortkey_len * 2);
419         sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
420                                       dest8->utf8, dest8->utf8_cap);
421     }
422
423     if (U_SUCCESS(*status)
424         && sortkey_len > 0)
425         dest8->utf8_len = sortkey_len;
426     else 
427         icu_buf_utf8_clear(dest8);
428 }
429
430 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
431                                             UErrorCode *status)
432 {
433     struct icu_tokenizer * tokenizer
434         = (struct icu_tokenizer *) xmalloc(sizeof(struct icu_tokenizer));
435
436     tokenizer->action = action;
437     tokenizer->bi = 0;
438     tokenizer->buf16 = 0;
439     tokenizer->token_count = 0;
440     tokenizer->token_id = 0;
441     tokenizer->token_start = 0;
442     tokenizer->token_end = 0;
443
444     switch(tokenizer->action)
445     {    
446     case 'l':
447     case 'L':
448         tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
449         break;
450     case 's':
451     case 'S':
452         tokenizer->bi = ubrk_open(UBRK_SENTENCE, locale, 0, 0, status);
453         break;
454     case 'w':
455     case 'W':
456         tokenizer->bi = ubrk_open(UBRK_WORD, locale, 0, 0, status);
457         break;
458     case 'c':
459     case 'C':
460         tokenizer->bi = ubrk_open(UBRK_CHARACTER, locale, 0, 0, status);
461         break;
462     case 't':
463     case 'T':
464         tokenizer->bi = ubrk_open(UBRK_TITLE, locale, 0, 0, status);
465         break;
466     default:
467         *status = U_UNSUPPORTED_ERROR;
468         return 0;
469         break;
470     }
471     
472     /* ICU error stuff is a very  funny business */
473     if (U_SUCCESS(*status))
474         return tokenizer;
475
476     /* freeing if failed */
477     icu_tokenizer_destroy(tokenizer);
478     return 0;
479 }
480
481 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
482 {
483     if (tokenizer) {
484         if (tokenizer->bi)
485             ubrk_close(tokenizer->bi);
486         xfree(tokenizer);
487     }
488 }
489
490 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer, 
491                          struct icu_buf_utf16 * src16, 
492                          UErrorCode *status)
493 {
494     if (!tokenizer || !tokenizer->bi || !src16)
495         return 0;
496
497     tokenizer->buf16 = src16;
498     tokenizer->token_count = 0;
499     tokenizer->token_id = 0;
500     tokenizer->token_start = 0;
501     tokenizer->token_end = 0;
502
503     ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
504      
505     if (U_FAILURE(*status))
506         return 0;
507
508     return 1;
509 }
510
511 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer, 
512                          struct icu_buf_utf16 * tkn16, 
513                          UErrorCode *status)
514 {
515     int32_t tkn_start = 0;
516     int32_t tkn_end = 0;
517     int32_t tkn_len = 0;
518
519     if (!tokenizer || !tokenizer->bi
520         || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
521         return 0;
522     /*
523     never change tokenizer->buf16 and keep always invariant
524     0 <= tokenizer->token_start 
525        <= tokenizer->token_end 
526        <= tokenizer->buf16->utf16_len
527     returns length of token
528     */
529
530     if (0 == tokenizer->token_end) /* first call */
531         tkn_start = ubrk_first(tokenizer->bi);
532     else /* successive calls */
533         tkn_start = tokenizer->token_end;
534
535     /* get next position */
536     tkn_end = ubrk_next(tokenizer->bi);
537
538     /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
539     if (UBRK_DONE == tkn_end)
540         tkn_end = tokenizer->buf16->utf16_len;
541
542     /* copy out if everything is well */
543     if (U_FAILURE(*status))
544         return 0;        
545     
546     /* everything OK, now update internal state */
547     tkn_len = tkn_end - tkn_start;
548
549     if (0 < tkn_len)
550     {
551         tokenizer->token_count++;
552         tokenizer->token_id++;
553     } else {
554         tokenizer->token_id = 0;    
555     }
556     tokenizer->token_start = tkn_start;
557     tokenizer->token_end = tkn_end;    
558
559     /* copying into token buffer if it exists */
560     if (tkn16){
561         if (tkn16->utf16_cap < tkn_len)
562             icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
563
564         u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start], 
565                   tkn_len);
566
567         tkn16->utf16_len = tkn_len;
568     }
569
570     return tkn_len;
571 }
572
573 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
574 {
575     return tokenizer->token_id;
576 }
577
578 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
579 {
580     return tokenizer->token_start;
581 }
582
583 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
584 {
585     return tokenizer->token_end;
586 }
587
588 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
589 {
590     return (tokenizer->token_end - tokenizer->token_start);
591 }
592
593 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
594 {
595     return tokenizer->token_count;
596 }
597
598 struct icu_transform * icu_transform_create(const char *id, char action,
599                                             const char *rules, 
600                                             UErrorCode *status)
601 {
602     struct icu_buf_utf16 *id16 = icu_buf_utf16_create(0);
603     struct icu_buf_utf16 *rules16 = icu_buf_utf16_create(0);
604
605     struct icu_transform * transform
606         = (struct icu_transform *) xmalloc(sizeof(struct icu_transform));
607
608     transform->action = action;
609     transform->trans = 0;
610
611     if (id)
612         icu_utf16_from_utf8_cstr(id16, id, status);
613     if (rules)
614         icu_utf16_from_utf8_cstr(rules16, rules, status);
615
616     switch(transform->action)
617     {
618     case 'f':
619     case 'F':
620         transform->trans
621             = utrans_openU(id16->utf16, 
622                            id16->utf16_len,
623                            UTRANS_FORWARD,
624                            rules16->utf16, 
625                            rules16->utf16_len,
626                            &transform->parse_error, status);
627         break;
628     case 'r':
629     case 'R':
630         transform->trans
631             = utrans_openU(id16->utf16,
632                            id16->utf16_len,
633                            UTRANS_REVERSE ,
634                            rules16->utf16, 
635                            rules16->utf16_len,
636                            &transform->parse_error, status);
637         break;
638     default:
639         *status = U_UNSUPPORTED_ERROR;
640         break;
641     }
642     icu_buf_utf16_destroy(rules16);
643     icu_buf_utf16_destroy(id16);
644     
645     if (U_SUCCESS(*status))
646         return transform;
647
648     /* freeing if failed */
649     icu_transform_destroy(transform);
650     return 0;
651 }
652
653 void icu_transform_destroy(struct icu_transform * transform)
654 {
655     if (transform)
656     {
657         if (transform->trans)
658             utrans_close(transform->trans);
659         xfree(transform);
660     }
661 }
662
663 int icu_transform_trans(struct icu_transform * transform,
664                         struct icu_buf_utf16 * dest16,
665                         struct icu_buf_utf16 * src16,
666                         UErrorCode *status)
667 {
668     if (!transform || !transform->trans 
669         || !src16  || !dest16)
670         return 0;
671
672     if (!src16->utf16_len)
673     {           /* guarding for empty source string */
674         icu_buf_utf16_clear(dest16);
675         return 0;
676     }
677
678     if (!icu_buf_utf16_copy(dest16, src16))
679         return 0;
680
681     utrans_transUChars (transform->trans, 
682                         dest16->utf16, &(dest16->utf16_len),
683                         dest16->utf16_cap,
684                         0, &(src16->utf16_len), status);
685
686     if (U_FAILURE(*status))
687         icu_buf_utf16_clear(dest16);
688     
689     return dest16->utf16_len;
690 }
691
692 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
693                                               enum icu_chain_step_type type,
694                                               const uint8_t * rule,
695                                               struct icu_buf_utf16 * buf16,
696                                               UErrorCode *status)
697 {
698     struct icu_chain_step * step = 0;
699     
700     if(!chain || !type || !rule)
701         return 0;
702
703     step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
704
705     step->type = type;
706
707     step->buf16 = buf16;
708
709     /* create auxilary objects */
710     switch(step->type)
711     {
712     case ICU_chain_step_type_display:
713         break;
714     case ICU_chain_step_type_casemap:
715         step->u.casemap = icu_casemap_create(rule[0], status);
716         break;
717     case ICU_chain_step_type_transform:
718         /* rule omitted. Only ID used */
719         step->u.transform = icu_transform_create((const char *) rule, 'f',
720                                                  0, status);
721         break;
722     case ICU_chain_step_type_tokenize:
723         step->u.tokenizer = icu_tokenizer_create((char *) chain->locale, 
724                                                  (char) rule[0], status);
725         break;
726     case ICU_chain_step_type_transliterate:
727         /* we pass a dummy ID to utrans_openU.. */
728         step->u.transform = icu_transform_create("custom", 'f',
729                                                  (const char *) rule, status);
730         break;
731     default:
732         break;
733     }
734     return step;
735 }
736
737
738 void icu_chain_step_destroy(struct icu_chain_step * step)
739 {
740     if (!step)
741         return;
742
743     icu_chain_step_destroy(step->previous);
744
745     switch(step->type)
746     {
747     case ICU_chain_step_type_display:
748         break;
749     case ICU_chain_step_type_casemap:
750         icu_casemap_destroy(step->u.casemap);
751         icu_buf_utf16_destroy(step->buf16);
752         break;
753     case ICU_chain_step_type_transform:
754     case ICU_chain_step_type_transliterate:
755         icu_transform_destroy(step->u.transform);
756         icu_buf_utf16_destroy(step->buf16);
757         break;
758     case ICU_chain_step_type_tokenize:
759         icu_tokenizer_destroy(step->u.tokenizer);
760         icu_buf_utf16_destroy(step->buf16);
761         break;
762     default:
763         break;
764     }
765     xfree(step);
766 }
767
768 struct icu_chain * icu_chain_create(const char *locale,  int sort,
769                                     UErrorCode * status)
770 {
771     struct icu_chain * chain 
772         = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
773
774     *status = U_ZERO_ERROR;
775
776     chain->locale = xstrdup(locale);
777
778     chain->sort = sort;
779
780     chain->coll = ucol_open((const char *) chain->locale, status);
781
782     if (U_FAILURE(*status))
783         return 0;
784
785     chain->token_count = 0;
786
787     chain->src8cstr = 0;
788
789     chain->display8 = icu_buf_utf8_create(0);
790     chain->norm8 = icu_buf_utf8_create(0);
791     chain->sort8 = icu_buf_utf8_create(0);
792
793     chain->src16 = icu_buf_utf16_create(0);
794
795     chain->steps = 0;
796
797     return chain;
798 }
799
800 void icu_chain_destroy(struct icu_chain * chain)
801 {
802     if (chain)
803     {
804         if (chain->coll)
805             ucol_close(chain->coll);
806
807         icu_buf_utf8_destroy(chain->display8);
808         icu_buf_utf8_destroy(chain->norm8);
809         icu_buf_utf8_destroy(chain->sort8);
810         
811         icu_buf_utf16_destroy(chain->src16);
812     
813         icu_chain_step_destroy(chain->steps);
814         xfree(chain->locale);
815         xfree(chain);
816     }
817 }
818
819 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node, 
820                                         int sort,
821                                         UErrorCode * status)
822 {
823     xmlNode *node = 0;
824     struct icu_chain * chain = 0;
825    
826     *status = U_ZERO_ERROR;
827
828     if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
829         return 0;
830     
831     {
832         xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node, 
833                                           (xmlChar *) "locale");
834         
835         if (xml_locale)
836         {
837             chain = icu_chain_create((const char *) xml_locale, sort, status);
838             xmlFree(xml_locale);
839         }
840         
841     }
842     if (!chain)
843         return 0;
844
845     for (node = xml_node->children; node; node = node->next)
846     {
847         xmlChar *xml_rule;
848         struct icu_chain_step * step = 0;
849
850         if (node->type != XML_ELEMENT_NODE)
851             continue;
852
853         xml_rule = xmlGetProp(node, (xmlChar *) "rule");
854
855         if (!strcmp((const char *) node->name, "casemap"))
856             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
857                                          (const uint8_t *) xml_rule, status);
858         else if (!strcmp((const char *) node->name, "transform"))
859             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
860                                          (const uint8_t *) xml_rule, status);
861         else if (!strcmp((const char *) node->name, "transliterate"))
862             step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate, 
863                                          (const uint8_t *) xml_rule, status);
864         else if (!strcmp((const char *) node->name, "tokenize"))
865             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
866                                          (const uint8_t *) xml_rule, status);
867         else if (!strcmp((const char *) node->name, "display"))
868             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
869                                          (const uint8_t *) "", status);
870         else if (!strcmp((const char *) node->name, "normalize"))
871         {
872             yaz_log(YLOG_WARN, "Element %s is deprecated. "
873                     "Use transform instead", node->name);
874             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
875                                          (const uint8_t *) xml_rule, status);
876         }
877         else if (!strcmp((const char *) node->name, "index")
878                  || !strcmp((const char *) node->name, "sortkey"))
879         {
880             yaz_log(YLOG_WARN, "Element %s is no longer needed. "
881                     "Remove it from the configuration", node->name);
882         }
883         else
884         {
885             yaz_log(YLOG_WARN, "Unknown element %s", node->name);
886             icu_chain_destroy(chain);
887             return 0;
888         }
889         xmlFree(xml_rule);
890         if (step && U_FAILURE(*status))
891         {
892             icu_chain_destroy(chain);
893             return 0;
894         }
895     }
896     return chain;
897 }
898
899 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
900                                               enum icu_chain_step_type type,
901                                               const uint8_t * rule,
902                                               UErrorCode *status)
903 {    
904     struct icu_chain_step * step = 0;
905     struct icu_buf_utf16 * src16 = 0;
906     struct icu_buf_utf16 * buf16 = 0;
907
908     if (!chain || !type || !rule)
909         return 0;
910
911     /* assign utf16 src buffers as needed */
912     if (chain->steps && chain->steps->buf16)
913         src16 = chain->steps->buf16;
914     else if (chain->src16)
915         src16 = chain->src16;
916     else
917         return 0;
918
919     /* create utf16 destination buffers as needed, or */
920     switch(type)
921     {
922     case ICU_chain_step_type_display:
923         buf16 = src16;
924         break;
925     case ICU_chain_step_type_casemap:
926         buf16 = icu_buf_utf16_create(0);
927         break;
928     case ICU_chain_step_type_transform:
929     case ICU_chain_step_type_transliterate:
930         buf16 = icu_buf_utf16_create(0);
931         break;
932     case ICU_chain_step_type_tokenize:
933         buf16 = icu_buf_utf16_create(0);
934         break;
935         break;
936     default:
937         break;
938     }
939     /* create actual chain step with this buffer */
940     step = icu_chain_step_create(chain, type, rule, buf16, status);
941
942     step->previous = chain->steps;
943     chain->steps = step;
944
945     return step;
946 }
947
948 static int icu_chain_step_next_token(struct icu_chain * chain,
949                                      struct icu_chain_step * step,
950                                      UErrorCode *status)
951 {
952     struct icu_buf_utf16 * src16 = 0;
953     int got_new_token = 0;
954
955     if (!chain || !chain->src16 || !step || !step->more_tokens)
956         return 0;
957
958     /* assign utf16 src buffers as needed, advance in previous steps
959        tokens until non-zero token met, and setting stop condition */
960
961     if (step->previous)
962     {
963         src16 = step->previous->buf16;
964         /* tokens might be killed in previous steps, therefore looping */
965
966         while (step->need_new_token 
967                && step->previous->more_tokens
968                && !got_new_token)
969             got_new_token
970                 = icu_chain_step_next_token(chain, step->previous, status);
971     }
972     else 
973     { /* first step can only work once on chain->src16 input buffer */
974         src16 = chain->src16;
975         step->more_tokens = 0;
976         got_new_token = 1;
977     }
978
979     if (!src16)
980         return 0;
981
982     /* stop if nothing to process */
983     if (step->need_new_token && !got_new_token)
984     {
985         step->more_tokens = 0;
986         return 0;
987     }
988
989     /* either an old token not finished yet, or a new token, thus
990        perform the work, eventually put this steps output in 
991        step->buf16 or the chains UTF8 output buffers  */
992
993     switch(step->type)
994     {
995     case ICU_chain_step_type_display:
996         icu_utf16_to_utf8(chain->display8, src16, status);
997         break;
998     case ICU_chain_step_type_casemap:
999         icu_casemap_casemap(step->u.casemap,
1000                             step->buf16, src16, status,
1001                             chain->locale);
1002         break;
1003     case ICU_chain_step_type_transform:
1004     case ICU_chain_step_type_transliterate:
1005         icu_transform_trans(step->u.transform,
1006                             step->buf16, src16, status);
1007         break;
1008     case ICU_chain_step_type_tokenize:
1009         /* attach to new src16 token only first time during splitting */
1010         if (step->need_new_token)
1011         {
1012             icu_tokenizer_attach(step->u.tokenizer, src16, status);
1013             step->need_new_token = 0;
1014         }
1015
1016         /* splitting one src16 token into multiple buf16 tokens */
1017         step->more_tokens
1018             = icu_tokenizer_next_token(step->u.tokenizer,
1019                                        step->buf16, status);
1020
1021         /* make sure to get new previous token if this one had been used up
1022            by recursive call to _same_ step */
1023
1024         if (!step->more_tokens)
1025         {
1026             step->more_tokens = icu_chain_step_next_token(chain, step, status);
1027             return step->more_tokens;  /* avoid one token count too much! */
1028         }
1029         break;
1030     default:
1031         return 0;
1032         break;
1033     }
1034
1035     if (U_FAILURE(*status))
1036         return 0;
1037
1038     /* if token disappered into thin air, tell caller */
1039     /* if (!step->buf16->utf16_len && !step->more_tokens) */ 
1040     /*    return 0; */ 
1041
1042     return 1;
1043 }
1044
1045 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr, 
1046                           UErrorCode *status)
1047 {
1048     struct icu_chain_step * stp = 0; 
1049
1050     if (!chain || !src8cstr)
1051         return 0;
1052
1053     chain->src8cstr = src8cstr;
1054
1055     stp = chain->steps;
1056     
1057     /* clear token count */
1058     chain->token_count = 0;
1059
1060     /* clear all steps stop states */
1061     while (stp)
1062     {
1063         stp->more_tokens = 1;
1064         stp->need_new_token = 1;
1065         stp = stp->previous;
1066     }
1067     
1068     /* finally convert UTF8 to UTF16 string if needed */
1069     if (chain->steps || chain->sort)
1070         icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
1071             
1072     if (U_FAILURE(*status))
1073         return 0;
1074
1075     return 1;
1076 }
1077
1078 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
1079 {
1080     int got_token = 0;
1081     
1082     *status = U_ZERO_ERROR;
1083
1084     if (!chain)
1085         return 0;
1086
1087     /* special case with no steps - same as index type binary */
1088     if (!chain->steps)
1089     {
1090         if (chain->token_count)
1091             return 0;
1092         else
1093         {
1094             chain->token_count++;
1095             
1096             if (chain->sort)
1097                 icu_sortkey8_from_utf16(chain->coll,
1098                                         chain->sort8, chain->steps->buf16,
1099                                         status);
1100             return chain->token_count;
1101         }
1102     }
1103     /* usual case, one or more icu chain steps existing */
1104     else 
1105     {
1106         while (!got_token && chain->steps && chain->steps->more_tokens)
1107             got_token = icu_chain_step_next_token(chain, chain->steps, status);
1108
1109         if (got_token)
1110         {
1111             chain->token_count++;
1112
1113             icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
1114             
1115             if (chain->sort)
1116                 icu_sortkey8_from_utf16(chain->coll,
1117                                         chain->sort8, chain->steps->buf16,
1118                                         status);
1119             return chain->token_count;
1120         }
1121     }
1122         
1123     return 0;
1124 }
1125
1126 int icu_chain_token_number(struct icu_chain * chain)
1127 {
1128     if (!chain)
1129         return 0;
1130     
1131     return chain->token_count;
1132 }
1133
1134 const char * icu_chain_token_display(struct icu_chain * chain)
1135 {
1136     if (chain->display8)
1137         return icu_buf_utf8_to_cstr(chain->display8);
1138     
1139     return 0;
1140 }
1141
1142 const char * icu_chain_token_norm(struct icu_chain * chain)
1143 {
1144     if (!chain->steps)
1145         return chain->src8cstr;
1146
1147     if (chain->norm8)
1148         return icu_buf_utf8_to_cstr(chain->norm8);
1149     
1150     return 0;
1151 }
1152
1153 const char * icu_chain_token_sortkey(struct icu_chain * chain)
1154 {
1155     if (chain->sort8)
1156         return icu_buf_utf8_to_cstr(chain->sort8);
1157     
1158     return 0;
1159 }
1160
1161 const UCollator * icu_chain_get_coll(struct icu_chain * chain)
1162 {
1163     return chain->coll;
1164 }
1165
1166 #endif /* YAZ_HAVE_ICU */
1167
1168 /*
1169  * Local variables:
1170  * c-basic-offset: 4
1171  * c-file-style: "Stroustrup"
1172  * indent-tabs-mode: nil
1173  * End:
1174  * vim: shiftwidth=4 tabstop=8 expandtab
1175  */
1176