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