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