Changed return value of function icu_sortkey8_from_utf16 to void.
[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.21 2007-11-30 11:43:47 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 void 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
493
494
495 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
496                                             UErrorCode *status)
497 {
498     struct icu_tokenizer * tokenizer
499         = (struct icu_tokenizer *) xmalloc(sizeof(struct icu_tokenizer));
500
501     tokenizer->action = action;
502     tokenizer->bi = 0;
503     tokenizer->buf16 = 0;
504     tokenizer->token_count = 0;
505     tokenizer->token_id = 0;
506     tokenizer->token_start = 0;
507     tokenizer->token_end = 0;
508
509
510     switch(tokenizer->action) {    
511     case 'l':
512     case 'L':
513         tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
514         break;
515     case 's':
516     case 'S':
517         tokenizer->bi = ubrk_open(UBRK_SENTENCE, locale, 0, 0, status);
518         break;
519     case 'w':
520     case 'W':
521         tokenizer->bi = ubrk_open(UBRK_WORD, locale, 0, 0, status);
522         break;
523     case 'c':
524     case 'C':
525         tokenizer->bi = ubrk_open(UBRK_CHARACTER, locale, 0, 0, status);
526         break;
527     case 't':
528     case 'T':
529         tokenizer->bi = ubrk_open(UBRK_TITLE, locale, 0, 0, status);
530         break;
531     default:
532         *status = U_UNSUPPORTED_ERROR;
533         return 0;
534         break;
535     }
536     
537     /* ICU error stuff is a very  funny business */
538     if (U_SUCCESS(*status))
539         return tokenizer;
540
541     /* freeing if failed */
542     icu_tokenizer_destroy(tokenizer);
543     return 0;
544 }
545
546 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
547 {
548     if (tokenizer) {
549         if (tokenizer->bi)
550             ubrk_close(tokenizer->bi);
551         xfree(tokenizer);
552     }
553 }
554
555 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer, 
556                          struct icu_buf_utf16 * src16, 
557                          UErrorCode *status)
558 {
559     if (!tokenizer || !tokenizer->bi || !src16)
560         return 0;
561
562
563     tokenizer->buf16 = src16;
564     tokenizer->token_count = 0;
565     tokenizer->token_id = 0;
566     tokenizer->token_start = 0;
567     tokenizer->token_end = 0;
568
569     ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
570     
571  
572     if (U_FAILURE(*status))
573         return 0;
574
575     return 1;
576 };
577
578 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer, 
579                          struct icu_buf_utf16 * tkn16, 
580                          UErrorCode *status)
581 {
582     int32_t tkn_start = 0;
583     int32_t tkn_end = 0;
584     int32_t tkn_len = 0;
585     
586
587     if (!tokenizer || !tokenizer->bi
588         || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
589         return 0;
590
591     /*
592     never change tokenizer->buf16 and keep always invariant
593     0 <= tokenizer->token_start 
594        <= tokenizer->token_end 
595        <= tokenizer->buf16->utf16_len
596     returns length of token
597     */
598
599     if (0 == tokenizer->token_end) /* first call */
600         tkn_start = ubrk_first(tokenizer->bi);
601     else /* successive calls */
602         tkn_start = tokenizer->token_end;
603
604     /* get next position */
605     tkn_end = ubrk_next(tokenizer->bi);
606
607     /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
608     if (UBRK_DONE == tkn_end)
609         tkn_end = tokenizer->buf16->utf16_len;
610
611     /* copy out if everything is well */
612     if(U_FAILURE(*status))
613         return 0;        
614     
615     /* everything OK, now update internal state */
616     tkn_len = tkn_end - tkn_start;
617
618     if (0 < tkn_len){
619         tokenizer->token_count++;
620         tokenizer->token_id++;
621     } else {
622         tokenizer->token_id = 0;    
623     }
624     tokenizer->token_start = tkn_start;
625     tokenizer->token_end = tkn_end;
626     
627
628     /* copying into token buffer if it exists */
629     if (tkn16){
630         if (tkn16->utf16_cap < tkn_len)
631             icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
632
633         u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start], 
634                   tkn_len);
635
636         tkn16->utf16_len = tkn_len;
637     }
638
639     return tkn_len;
640 }
641
642
643 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
644 {
645     return tokenizer->token_id;
646 }
647
648 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
649 {
650     return tokenizer->token_start;
651 }
652
653 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
654 {
655     return tokenizer->token_end;
656 }
657
658 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
659 {
660     return (tokenizer->token_end - tokenizer->token_start);
661 }
662
663 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
664 {
665     return tokenizer->token_count;
666 }
667
668
669
670 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
671                                               UErrorCode *status)
672 {
673
674     struct icu_normalizer * normalizer
675         = (struct icu_normalizer *) xmalloc(sizeof(struct icu_normalizer));
676
677     normalizer->action = action;
678     normalizer->trans = 0;
679     normalizer->rules16 =  icu_buf_utf16_create(0);
680     icu_utf16_from_utf8_cstr(normalizer->rules16, rules, status);
681      
682     switch(normalizer->action) {    
683     case 'f':
684     case 'F':
685         normalizer->trans
686             = utrans_openU(normalizer->rules16->utf16, 
687                            normalizer->rules16->utf16_len,
688                            UTRANS_FORWARD,
689                            0, 0, 
690                            normalizer->parse_error, status);
691         break;
692     case 'r':
693     case 'R':
694         normalizer->trans
695             = utrans_openU(normalizer->rules16->utf16,
696                            normalizer->rules16->utf16_len,
697                            UTRANS_REVERSE ,
698                            0, 0,
699                            normalizer->parse_error, status);
700         break;
701     default:
702         *status = U_UNSUPPORTED_ERROR;
703         return 0;
704         break;
705     }
706     
707     if (U_SUCCESS(*status))
708         return normalizer;
709
710     /* freeing if failed */
711     icu_normalizer_destroy(normalizer);
712     return 0;
713 }
714
715
716 void icu_normalizer_destroy(struct icu_normalizer * normalizer){
717     if (normalizer) {
718         if (normalizer->rules16) 
719             icu_buf_utf16_destroy(normalizer->rules16);
720         if (normalizer->trans)
721             utrans_close(normalizer->trans);
722         xfree(normalizer);
723     }
724 }
725
726
727
728 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
729                              struct icu_buf_utf16 * dest16,
730                              struct icu_buf_utf16 * src16,
731                              UErrorCode *status)
732 {
733     if (!normalizer || !normalizer->trans 
734         || !src16
735         || !dest16)
736         return 0;
737
738     if (!src16->utf16_len){           /* guarding for empty source string */
739         icu_buf_utf16_clear(dest16);
740         return 0;
741     }
742
743     if (!icu_buf_utf16_copy(dest16, src16))
744         return 0;
745
746    
747     utrans_transUChars (normalizer->trans, 
748                         dest16->utf16, &(dest16->utf16_len),
749                         dest16->utf16_cap,
750                         0, &(src16->utf16_len), status);
751
752     if (U_FAILURE(*status))
753         icu_buf_utf16_clear(dest16);
754     
755     return dest16->utf16_len;
756 }
757
758
759
760
761 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
762                                               enum icu_chain_step_type type,
763                                               const uint8_t * rule,
764                                               struct icu_buf_utf16 * buf16,
765                                               UErrorCode *status)
766 {
767     struct icu_chain_step * step = 0;
768     
769     if(!chain || !type || !rule)
770         return 0;
771
772     step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
773
774     step->type = type;
775
776     step->buf16 = buf16;
777
778     /* create auxilary objects */
779     switch(step->type) {
780     case ICU_chain_step_type_display:
781         break;
782     case ICU_chain_step_type_casemap:
783         step->u.casemap = icu_casemap_create(rule[0], status);
784         break;
785     case ICU_chain_step_type_normalize:
786         step->u.normalizer = icu_normalizer_create((char *) rule, 'f', status);
787         break;
788     case ICU_chain_step_type_tokenize:
789         step->u.tokenizer = icu_tokenizer_create((char *) chain->locale, 
790                                                  (char) rule[0], status);
791         break;
792     default:
793         break;
794     }
795
796     return step;
797 }
798
799
800 void icu_chain_step_destroy(struct icu_chain_step * step){
801     
802     if (!step)
803         return;
804
805     icu_chain_step_destroy(step->previous);
806
807     switch(step->type) {
808     case ICU_chain_step_type_display:
809         break;
810     case ICU_chain_step_type_casemap:
811         icu_casemap_destroy(step->u.casemap);
812         icu_buf_utf16_destroy(step->buf16);
813         break;
814     case ICU_chain_step_type_normalize:
815         icu_normalizer_destroy(step->u.normalizer);
816         icu_buf_utf16_destroy(step->buf16);
817         break;
818     case ICU_chain_step_type_tokenize:
819         icu_tokenizer_destroy(step->u.tokenizer);
820         icu_buf_utf16_destroy(step->buf16);
821         break;
822     default:
823         break;
824     }
825     xfree(step);
826 }
827
828
829
830 struct icu_chain * icu_chain_create(const char *locale,  int sort,
831                                     UErrorCode * status)
832 {
833     struct icu_chain * chain 
834         = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
835
836     *status = U_ZERO_ERROR;
837
838     chain->locale = xstrdup(locale);
839
840     chain->sort = sort;
841
842     chain->coll = ucol_open((const char *) chain->locale, status);
843
844     if (U_FAILURE(*status))
845         return 0;
846
847     chain->token_count = 0;
848
849     chain->src8cstr = 0;
850
851     chain->display8 = icu_buf_utf8_create(0);
852     chain->norm8 = icu_buf_utf8_create(0);
853     chain->sort8 = icu_buf_utf8_create(0);
854
855     chain->src16 = icu_buf_utf16_create(0);
856
857     chain->steps = 0;
858
859     return chain;
860 }
861
862
863 void icu_chain_destroy(struct icu_chain * chain)
864 {
865     if (chain)
866     {
867         if (chain->coll)
868             ucol_close(chain->coll);
869
870         icu_buf_utf8_destroy(chain->display8);
871         icu_buf_utf8_destroy(chain->norm8);
872         icu_buf_utf8_destroy(chain->sort8);
873         
874         icu_buf_utf16_destroy(chain->src16);
875     
876         icu_chain_step_destroy(chain->steps);
877         xfree(chain->locale);
878         xfree(chain);
879     }
880 }
881
882
883
884 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node, 
885                                         int sort,
886                                         UErrorCode * status)
887 {
888     xmlNode *node = 0;
889     struct icu_chain * chain = 0;
890    
891     *status = U_ZERO_ERROR;
892
893     if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
894         return 0;
895     
896     {
897         xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node, 
898                                           (xmlChar *) "locale");
899         
900         if (xml_locale)
901         {
902             chain = icu_chain_create((const char *) xml_locale, sort, status);
903             xmlFree(xml_locale);
904         }
905         
906     }
907     if (!chain)
908         return 0;
909
910     for (node = xml_node->children; node; node = node->next)
911     {
912         xmlChar *xml_rule;
913         struct icu_chain_step * step = 0;
914
915         if (node->type != XML_ELEMENT_NODE)
916             continue;
917
918         xml_rule = xmlGetProp(node, (xmlChar *) "rule");
919
920         if (!strcmp((const char *) node->name, "casemap"))
921             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
922                                          (const uint8_t *) xml_rule, status);
923         else if (!strcmp((const char *) node->name, "transform"))
924             step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize, 
925                                          (const uint8_t *) xml_rule, status);
926         else if (!strcmp((const char *) node->name, "tokenize"))
927             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
928                                          (const uint8_t *) xml_rule, status);
929         else if (!strcmp((const char *) node->name, "display"))
930             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
931                                          (const uint8_t *) "", status);
932         xmlFree(xml_rule);
933         if (!step || U_FAILURE(*status))
934         {
935             icu_chain_destroy(chain);
936             return 0;
937         }
938         
939
940     }
941     return chain;
942 }
943
944
945
946 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
947                                               enum icu_chain_step_type type,
948                                               const uint8_t * rule,
949                                               UErrorCode *status)
950 {    
951     struct icu_chain_step * step = 0;
952     struct icu_buf_utf16 * src16 = 0;
953     struct icu_buf_utf16 * buf16 = 0;
954
955     if (!chain || !type || !rule)
956         return 0;
957
958     /* assign utf16 src buffers as needed */
959     if (chain->steps && chain->steps->buf16)
960         src16 = chain->steps->buf16;
961     else if (chain->src16)
962         src16 = chain->src16;
963     else
964         return 0;
965
966     
967     /* create utf16 destination buffers as needed, or */
968     switch(type)
969     {
970     case ICU_chain_step_type_display:
971         buf16 = src16;
972         break;
973     case ICU_chain_step_type_casemap:
974         buf16 = icu_buf_utf16_create(0);
975         break;
976     case ICU_chain_step_type_normalize:
977         buf16 = icu_buf_utf16_create(0);
978         break;
979     case ICU_chain_step_type_tokenize:
980         buf16 = icu_buf_utf16_create(0);
981         break;
982     default:
983         break;
984     }
985
986     /* create actual chain step with this buffer */
987     step = icu_chain_step_create(chain, type, rule, buf16, status);
988
989     step->previous = chain->steps;
990     chain->steps = step;
991
992     return step;
993 }
994
995
996 int icu_chain_step_next_token(struct icu_chain * chain,
997                               struct icu_chain_step * step,
998                               UErrorCode *status)
999 {
1000     struct icu_buf_utf16 * src16 = 0;
1001     int got_new_token = 0;
1002
1003     if (!chain || !chain->src16 || !step || !step->more_tokens)
1004         return 0;
1005
1006     /* assign utf16 src buffers as neeed, advance in previous steps
1007        tokens until non-zero token met, and setting stop condition */
1008
1009     if (step->previous)
1010     {
1011         src16 = step->previous->buf16;
1012         /* tokens might be killed in previous steps, therefore looping */
1013
1014         while (step->need_new_token 
1015                && step->previous->more_tokens
1016                && !got_new_token)
1017             got_new_token
1018                 = icu_chain_step_next_token(chain, step->previous, status);
1019     }
1020     else 
1021     { /* first step can only work once on chain->src16 input buffer */
1022         src16 = chain->src16;
1023         step->more_tokens = 0;
1024         got_new_token = 1;
1025     }
1026
1027     if (!src16)
1028         return 0;
1029
1030     /* stop if nothing to process */
1031     if (step->need_new_token && !got_new_token)
1032     {
1033         step->more_tokens = 0;
1034         return 0;
1035     }
1036
1037     /* either an old token not finished yet, or a new token, thus
1038        perform the work, eventually put this steps output in 
1039        step->buf16 or the chains UTF8 output buffers  */
1040
1041     switch(step->type)
1042     {
1043     case ICU_chain_step_type_display:
1044         icu_utf16_to_utf8(chain->display8, src16, status);
1045         break;
1046     case ICU_chain_step_type_casemap:
1047         icu_casemap_casemap(step->u.casemap,
1048                             step->buf16, src16, status,
1049                             chain->locale);
1050         break;
1051     case ICU_chain_step_type_normalize:
1052         icu_normalizer_normalize(step->u.normalizer,
1053                                  step->buf16, src16, status);
1054         break;
1055     case ICU_chain_step_type_tokenize:
1056         /* attach to new src16 token only first time during splitting */
1057         if (step->need_new_token)
1058         {
1059             icu_tokenizer_attach(step->u.tokenizer, src16, status);
1060             step->need_new_token = 0;
1061         }
1062
1063         /* splitting one src16 token into multiple buf16 tokens */
1064         step->more_tokens
1065             = icu_tokenizer_next_token(step->u.tokenizer,
1066                                        step->buf16, status);
1067
1068         /* make sure to get new previous token if this one had been used up
1069            by recursive call to _same_ step */
1070
1071         if (!step->more_tokens)
1072         {
1073             step->more_tokens = icu_chain_step_next_token(chain, step, status);
1074             return step->more_tokens;  /* avoid one token count too much! */
1075         }
1076         break;
1077     default:
1078         return 0;
1079         break;
1080     }
1081
1082     if (U_FAILURE(*status))
1083         return 0;
1084
1085     /* if token disappered into thin air, tell caller */
1086     /* if (!step->buf16->utf16_len && !step->more_tokens) */ 
1087     /*    return 0; */ 
1088
1089     return 1;
1090 }
1091
1092
1093 int icu_chain_assign_cstr(struct icu_chain * chain,
1094                           const char * src8cstr, 
1095                           UErrorCode *status)
1096 {
1097     struct icu_chain_step * stp = 0; 
1098
1099     if (!chain || !src8cstr)
1100         return 0;
1101
1102     chain->src8cstr = src8cstr;
1103
1104     stp = chain->steps;
1105     
1106     /* clear token count */
1107     chain->token_count = 0;
1108
1109     /* clear all steps stop states */
1110     while (stp)
1111     {
1112         stp->more_tokens = 1;
1113         stp->need_new_token = 1;
1114         stp = stp->previous;
1115     }
1116     
1117     /* finally convert UTF8 to UTF16 string if needed */
1118     if (chain->steps || chain->sort)
1119         icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
1120             
1121     if (U_FAILURE(*status))
1122         return 0;
1123
1124     return 1;
1125 }
1126
1127
1128
1129 int icu_chain_next_token(struct icu_chain * chain,
1130                          UErrorCode *status)
1131 {
1132     int got_token = 0;
1133     
1134     *status = U_ZERO_ERROR;
1135
1136     if (!chain)
1137         return 0;
1138
1139     /* special case with no steps - same as index type binary */
1140     if (!chain->steps)
1141     {
1142         if (chain->token_count)
1143             return 0;
1144         else
1145         {
1146             chain->token_count++;
1147             
1148             if (chain->sort)
1149                 icu_sortkey8_from_utf16(chain->coll,
1150                                         chain->sort8, chain->steps->buf16,
1151                                         status);
1152             return chain->token_count;
1153         }
1154     }
1155     /* usual case, one or more icu chain steps existing */
1156     else 
1157     {
1158         while(!got_token && chain->steps && chain->steps->more_tokens)
1159             got_token = icu_chain_step_next_token(chain, chain->steps, status);
1160
1161         if (got_token)
1162         {
1163             chain->token_count++;
1164
1165             icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
1166             
1167             if (chain->sort)
1168                 icu_sortkey8_from_utf16(chain->coll,
1169                                         chain->sort8, chain->steps->buf16,
1170                                         status);
1171
1172             return chain->token_count;
1173         }
1174     }
1175         
1176     return 0;
1177 }
1178
1179 int icu_chain_token_number(struct icu_chain * chain)
1180 {
1181     if (!chain)
1182         return 0;
1183     
1184     return chain->token_count;
1185 }
1186
1187
1188 const char * icu_chain_token_display(struct icu_chain * chain)
1189 {
1190     if (chain->display8)
1191         return icu_buf_utf8_to_cstr(chain->display8);
1192     
1193     return 0;
1194 }
1195
1196 const char * icu_chain_token_norm(struct icu_chain * chain)
1197 {
1198     if (!chain->steps)
1199         return chain->src8cstr;
1200
1201     if (chain->norm8)
1202         return icu_buf_utf8_to_cstr(chain->norm8);
1203     
1204     return 0;
1205 }
1206
1207 const char * icu_chain_token_sortkey(struct icu_chain * chain)
1208 {
1209     if (chain->sort8)
1210         return icu_buf_utf8_to_cstr(chain->sort8);
1211     
1212     return 0;
1213 }
1214
1215 const UCollator * icu_chain_get_coll(struct icu_chain * chain)
1216 {
1217     return chain->coll;
1218 }
1219
1220 #endif /* YAZ_HAVE_ICU */
1221
1222 /*
1223  * Local variables:
1224  * c-basic-offset: 4
1225  * indent-tabs-mode: nil
1226  * End:
1227  * vim: shiftwidth=4 tabstop=8 expandtab
1228  */