fixed wrong token count when tokens disappear with ICU normalization
[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.4 2007-10-24 13:23:34 marc 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     src8cstr_len = strlen(src8cstr);
271   
272     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
273                   &utf16_len,
274                   src8cstr, src8cstr_len, status);
275   
276     /* check for buffer overflow, resize and retry */
277     if (*status == U_BUFFER_OVERFLOW_ERROR)
278     {
279         icu_buf_utf16_resize(dest16, utf16_len * 2);
280         *status = U_ZERO_ERROR;
281         u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
282                       &utf16_len,
283                       src8cstr, src8cstr_len, status);
284     }
285
286     if (U_SUCCESS(*status)  
287         && utf16_len <= dest16->utf16_cap)
288         dest16->utf16_len = utf16_len;
289     else 
290         icu_buf_utf16_clear(dest16);
291   
292     return *status;
293 }
294
295
296
297
298 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
299                              struct icu_buf_utf16 * src16,
300                              UErrorCode * status)
301 {
302     int32_t utf8_len = 0;
303   
304     u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
305                 &utf8_len,
306                 src16->utf16, src16->utf16_len, status);
307   
308     /* check for buffer overflow, resize and retry */
309     if (*status == U_BUFFER_OVERFLOW_ERROR)
310     {
311         icu_buf_utf8_resize(dest8, utf8_len * 2);
312         *status = U_ZERO_ERROR;
313         u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
314                     &utf8_len,
315                     src16->utf16, src16->utf16_len, status);
316
317     }
318
319     if (U_SUCCESS(*status)  
320         && utf8_len <= dest8->utf8_cap)
321         dest8->utf8_len = utf8_len;
322     else 
323         icu_buf_utf8_clear(dest8);
324   
325     return *status;
326 }
327
328
329
330 struct icu_casemap * icu_casemap_create(const char *locale, char action,
331                                         UErrorCode *status)
332 {    
333     struct icu_casemap * casemap
334         = (struct icu_casemap *) malloc(sizeof(struct icu_casemap));
335     strcpy(casemap->locale, locale);
336     casemap->action = action;
337
338     switch(casemap->action) {    
339     case 'l':   
340     case 'L':   
341     case 'u':   
342     case 'U':   
343     case 't':  
344     case 'T':  
345     case 'f':  
346     case 'F':  
347         break;
348     default:
349         icu_casemap_destroy(casemap);
350         return 0;
351     }
352
353     return casemap;
354 }
355
356 void icu_casemap_destroy(struct icu_casemap * casemap)
357 {
358     if (casemap) 
359         free(casemap);
360 }
361
362
363 int icu_casemap_casemap(struct icu_casemap * casemap,
364                         struct icu_buf_utf16 * dest16,
365                         struct icu_buf_utf16 * src16,
366                         UErrorCode *status)
367 {
368     if(!casemap)
369         return 0;
370     
371     return icu_utf16_casemap(dest16, src16,
372                              casemap->locale, casemap->action, status);
373 }
374
375
376 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
377                       struct icu_buf_utf16 * src16,
378                       const char *locale, char action,
379                       UErrorCode *status)
380 {
381     int32_t dest16_len = 0;
382
383
384     if (!src16->utf16_len){           //guarding for empty source string
385         if (dest16->utf16)
386             dest16->utf16[0] = (UChar) 0;
387         dest16->utf16_len = 0;
388         return U_ZERO_ERROR;
389     }
390
391     
392     switch(action) {    
393     case 'l':    
394     case 'L':    
395         dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
396                                   src16->utf16, src16->utf16_len, 
397                                   locale, status);
398         break;
399     case 'u':    
400     case 'U':    
401         dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
402                                   src16->utf16, src16->utf16_len, 
403                                   locale, status);
404         break;
405     case 't':    
406     case 'T':    
407         dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
408                                   src16->utf16, src16->utf16_len,
409                                   0, locale, status);
410         break;
411     case 'f':    
412     case 'F':    
413         dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
414                                    src16->utf16, src16->utf16_len,
415                                    U_FOLD_CASE_DEFAULT, status);
416         break;
417         
418     default:
419         return U_UNSUPPORTED_ERROR;
420         break;
421     }
422
423     /* check for buffer overflow, resize and retry */
424     if (*status == U_BUFFER_OVERFLOW_ERROR
425         && dest16 != src16        /* do not resize if in-place conversion */
426         ){
427         icu_buf_utf16_resize(dest16, dest16_len * 2);
428         *status = U_ZERO_ERROR;
429
430     
431         switch(action) {    
432         case 'l':    
433         case 'L':    
434             dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
435                                       src16->utf16, src16->utf16_len, 
436                                       locale, status);
437             break;
438         case 'u':    
439         case 'U':    
440             dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
441                                       src16->utf16, src16->utf16_len, 
442                                       locale, status);
443             break;
444         case 't':    
445         case 'T':    
446             dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
447                                       src16->utf16, src16->utf16_len,
448                                       0, locale, status);
449             break;
450         case 'f':    
451         case 'F':    
452             dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
453                                        src16->utf16, src16->utf16_len,
454                                        U_FOLD_CASE_DEFAULT, status);
455             break;
456         
457         default:
458             return U_UNSUPPORTED_ERROR;
459             break;
460         }
461     }
462     
463     if (U_SUCCESS(*status)
464         && dest16_len <= dest16->utf16_cap)
465         dest16->utf16_len = dest16_len;
466     else {
467         if (dest16->utf16)
468             dest16->utf16[0] = (UChar) 0;
469         dest16->utf16_len = 0;
470     }
471   
472     return *status;
473 }
474
475
476
477 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
478                                    struct icu_buf_utf8 * dest8, 
479                                    struct icu_buf_utf16 * src16,
480                                    UErrorCode * status)
481
482   
483     int32_t sortkey_len = 0;
484
485     sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
486                                   dest8->utf8, dest8->utf8_cap);
487
488     /* check for buffer overflow, resize and retry */
489     if (sortkey_len > dest8->utf8_cap) {
490         icu_buf_utf8_resize(dest8, sortkey_len * 2);
491         sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
492                                       dest8->utf8, dest8->utf8_cap);
493     }
494
495     if (U_SUCCESS(*status)
496         && sortkey_len > 0)
497         dest8->utf8_len = sortkey_len;
498     else 
499         icu_buf_utf8_clear(dest8);
500     
501     return sortkey_len;
502 }
503
504
505
506 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
507                                             UErrorCode *status)
508 {
509     struct icu_tokenizer * tokenizer
510         = (struct icu_tokenizer *) malloc(sizeof(struct icu_tokenizer));
511
512     strcpy(tokenizer->locale, locale);
513     tokenizer->action = action;
514     tokenizer->bi = 0;
515     tokenizer->buf16 = 0;
516     tokenizer->token_count = 0;
517     tokenizer->token_id = 0;
518     tokenizer->token_start = 0;
519     tokenizer->token_end = 0;
520
521
522     switch(tokenizer->action) {    
523     case 'l':
524     case 'L':
525         tokenizer->bi
526             = ubrk_open(UBRK_LINE, tokenizer->locale,
527                         0, 0, status);
528         break;
529     case 's':
530     case 'S':
531         tokenizer->bi
532             = ubrk_open(UBRK_SENTENCE, tokenizer->locale,
533                         0, 0, status);
534         break;
535     case 'w':
536     case 'W':
537         tokenizer->bi 
538             = ubrk_open(UBRK_WORD, tokenizer->locale,
539                         0, 0, status);
540         break;
541     case 'c':
542     case 'C':
543         tokenizer->bi 
544             = ubrk_open(UBRK_CHARACTER, tokenizer->locale,
545                         0, 0, status);
546         break;
547     case 't':
548     case 'T':
549         tokenizer->bi 
550             = ubrk_open(UBRK_TITLE, tokenizer->locale,
551                         0, 0, status);
552         break;
553     default:
554         *status = U_UNSUPPORTED_ERROR;
555         return 0;
556         break;
557     }
558     
559     /* ICU error stuff is a very  funny business */
560     if (U_SUCCESS(*status))
561         return tokenizer;
562
563     /* freeing if failed */
564     icu_tokenizer_destroy(tokenizer);
565     return 0;
566 }
567
568 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
569 {
570     if (tokenizer) {
571         if (tokenizer->bi)
572             ubrk_close(tokenizer->bi);
573         free(tokenizer);
574     }
575 }
576
577 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer, 
578                          struct icu_buf_utf16 * src16, 
579                          UErrorCode *status)
580 {
581     if (!tokenizer || !tokenizer->bi || !src16)
582         return 0;
583
584
585     tokenizer->buf16 = src16;
586     tokenizer->token_count = 0;
587     tokenizer->token_id = 0;
588     tokenizer->token_start = 0;
589     tokenizer->token_end = 0;
590
591     ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
592     
593  
594     if (U_FAILURE(*status))
595         return 0;
596
597     return 1;
598 };
599
600 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer, 
601                          struct icu_buf_utf16 * tkn16, 
602                          UErrorCode *status)
603 {
604     int32_t tkn_start = 0;
605     int32_t tkn_end = 0;
606     int32_t tkn_len = 0;
607     
608
609     if (!tokenizer || !tokenizer->bi
610         || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
611         return 0;
612
613     /*
614     never change tokenizer->buf16 and keep always invariant
615     0 <= tokenizer->token_start 
616        <= tokenizer->token_end 
617        <= tokenizer->buf16->utf16_len
618     returns length of token
619     */
620
621     if (0 == tokenizer->token_end) /* first call */
622         tkn_start = ubrk_first(tokenizer->bi);
623     else /* successive calls */
624         tkn_start = tokenizer->token_end;
625
626     /* get next position */
627     tkn_end = ubrk_next(tokenizer->bi);
628
629     /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
630     if (UBRK_DONE == tkn_end)
631         tkn_end = tokenizer->buf16->utf16_len;
632
633     /* copy out if everything is well */
634     if(U_FAILURE(*status))
635         return 0;        
636     
637     /* everything OK, now update internal state */
638     tkn_len = tkn_end - tkn_start;
639
640     if (0 < tkn_len){
641         tokenizer->token_count++;
642         tokenizer->token_id++;
643     } else {
644         tokenizer->token_id = 0;    
645     }
646     tokenizer->token_start = tkn_start;
647     tokenizer->token_end = tkn_end;
648     
649
650     /* copying into token buffer if it exists */
651     if (tkn16){
652         if (tkn16->utf16_cap < tkn_len)
653             icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
654
655         u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start], 
656                   tkn_len);
657
658         tkn16->utf16_len = tkn_len;
659     }
660
661     return tkn_len;
662 }
663
664
665 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
666 {
667     return tokenizer->token_id;
668 }
669
670 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
671 {
672     return tokenizer->token_start;
673 }
674
675 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
676 {
677     return tokenizer->token_end;
678 }
679
680 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
681 {
682     return (tokenizer->token_end - tokenizer->token_start);
683 }
684
685 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
686 {
687     return tokenizer->token_count;
688 }
689
690
691
692 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
693                                               UErrorCode *status)
694 {
695
696     struct icu_normalizer * normalizer
697         = (struct icu_normalizer *) malloc(sizeof(struct icu_normalizer));
698
699     normalizer->action = action;
700     normalizer->trans = 0;
701     normalizer->rules16 =  icu_buf_utf16_create(0);
702     icu_utf16_from_utf8_cstr(normalizer->rules16, rules, status);
703      
704     switch(normalizer->action) {    
705     case 'f':
706     case 'F':
707         normalizer->trans
708             = utrans_openU(normalizer->rules16->utf16, 
709                            normalizer->rules16->utf16_len,
710                            UTRANS_FORWARD,
711                            0, 0, 
712                            normalizer->parse_error, status);
713         break;
714     case 'r':
715     case 'R':
716         normalizer->trans
717             = utrans_openU(normalizer->rules16->utf16,
718                            normalizer->rules16->utf16_len,
719                            UTRANS_REVERSE ,
720                            0, 0,
721                            normalizer->parse_error, status);
722         break;
723     default:
724         *status = U_UNSUPPORTED_ERROR;
725         return 0;
726         break;
727     }
728     
729     if (U_SUCCESS(*status))
730         return normalizer;
731
732     /* freeing if failed */
733     icu_normalizer_destroy(normalizer);
734     return 0;
735 }
736
737
738 void icu_normalizer_destroy(struct icu_normalizer * normalizer){
739     if (normalizer) {
740         if (normalizer->rules16) 
741             icu_buf_utf16_destroy(normalizer->rules16);
742         if (normalizer->trans)
743             utrans_close(normalizer->trans);
744         free(normalizer);
745     }
746 }
747
748
749
750 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
751                              struct icu_buf_utf16 * dest16,
752                              struct icu_buf_utf16 * src16,
753                              UErrorCode *status)
754 {
755     if (!normalizer || !normalizer->trans 
756         || !src16
757         || !dest16)
758         return 0;
759
760     if (!src16->utf16_len){           //guarding for empty source string
761         icu_buf_utf16_clear(dest16);
762         return 0;
763     }
764
765     if (!icu_buf_utf16_copy(dest16, src16))
766         return 0;
767
768    
769     utrans_transUChars (normalizer->trans, 
770                         dest16->utf16, &(dest16->utf16_len),
771                         dest16->utf16_cap,
772                         0, &(src16->utf16_len), status);
773
774     if (U_FAILURE(*status))
775         icu_buf_utf16_clear(dest16);
776     
777     return dest16->utf16_len;
778 }
779
780
781
782
783 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
784                                               enum icu_chain_step_type type,
785                                               const uint8_t * rule,
786                                               struct icu_buf_utf16 * buf16,
787                                               UErrorCode *status)
788 {
789     struct icu_chain_step * step = 0;
790     
791     if(!chain || !type || !rule)
792         return 0;
793
794     step = (struct icu_chain_step *) malloc(sizeof(struct icu_chain_step));
795
796     step->type = type;
797
798     step->buf16 = buf16;
799
800     /* create auxilary objects */
801     switch(step->type) {
802     case ICU_chain_step_type_display:
803         break;
804     case ICU_chain_step_type_index:
805         break;
806     case ICU_chain_step_type_sortkey:
807         break;
808     case ICU_chain_step_type_casemap:
809         step->u.casemap = icu_casemap_create((char *) chain->locale, 
810                                              (char) rule[0], status);
811         break;
812     case ICU_chain_step_type_normalize:
813         step->u.normalizer = icu_normalizer_create((char *) rule, 'f', status);
814         break;
815     case ICU_chain_step_type_tokenize:
816         step->u.tokenizer = icu_tokenizer_create((char *) chain->locale, 
817                                                  (char) rule[0], status);
818         break;
819     default:
820         break;
821     }
822
823     return step;
824 }
825
826
827 void icu_chain_step_destroy(struct icu_chain_step * step){
828     
829     if (!step)
830         return;
831
832     icu_chain_step_destroy(step->previous);
833
834     switch(step->type) {
835     case ICU_chain_step_type_display:
836         break;
837     case ICU_chain_step_type_index:
838         break;
839     case ICU_chain_step_type_sortkey:
840         break;
841     case ICU_chain_step_type_casemap:
842         icu_casemap_destroy(step->u.casemap);
843         icu_buf_utf16_destroy(step->buf16);
844         break;
845     case ICU_chain_step_type_normalize:
846         icu_normalizer_destroy(step->u.normalizer);
847         icu_buf_utf16_destroy(step->buf16);
848         break;
849     case ICU_chain_step_type_tokenize:
850         icu_tokenizer_destroy(step->u.tokenizer);
851         icu_buf_utf16_destroy(step->buf16);
852         break;
853     default:
854         break;
855     }
856     free(step);
857 }
858
859
860
861 struct icu_chain * icu_chain_create(const uint8_t * identifier,
862                                     const uint8_t * locale)
863 {
864
865     struct icu_chain * chain 
866         = (struct icu_chain *) malloc(sizeof(struct icu_chain));
867
868     strncpy((char *) chain->identifier, (const char *) identifier, 128);
869     chain->identifier[128 - 1] = '\0';
870     strncpy((char *) chain->locale, (const char *) locale, 16);    
871     chain->locale[16 - 1] = '\0';
872
873     chain->token_count = 0;
874
875     chain->display8 = icu_buf_utf8_create(0);
876     chain->norm8 = icu_buf_utf8_create(0);
877     chain->sort8 = icu_buf_utf8_create(0);
878
879     chain->src16 = icu_buf_utf16_create(0);
880
881     chain->steps = 0;
882
883     return chain;
884 }
885
886
887 void icu_chain_destroy(struct icu_chain * chain)
888 {
889     if (chain){
890         icu_buf_utf8_destroy(chain->display8);
891         icu_buf_utf8_destroy(chain->norm8);
892         icu_buf_utf8_destroy(chain->sort8);
893         
894         icu_buf_utf16_destroy(chain->src16);
895     
896         icu_chain_step_destroy(chain->steps);
897         free(chain);
898     }
899 }
900
901
902
903 struct icu_chain * icu_chain_xml_config(xmlNode *xml_node, 
904                                         UErrorCode * status){
905
906     xmlNode *node = 0;
907     struct icu_chain * chain = 0;
908    
909     if (!xml_node 
910         ||xml_node->type != XML_ELEMENT_NODE 
911         || strcmp((const char *) xml_node->name, "icu_chain"))
912
913         return 0;
914
915     {    
916         xmlChar *xml_id = xmlGetProp(xml_node, (xmlChar *) "id");
917         xmlChar *xml_locale = xmlGetProp(xml_node, (xmlChar *) "locale");
918         
919         if (!xml_id || !strlen((const char *) xml_id) 
920             || !xml_locale || !strlen((const char *) xml_locale))
921             return 0;
922         
923         chain = icu_chain_create((const uint8_t *) xml_id, 
924                                  (const uint8_t *) xml_locale);
925         
926         xmlFree(xml_id);
927         xmlFree(xml_locale);
928     }
929     if (!chain)
930         return 0;
931         
932     for (node = xml_node->children; node; node = node->next)
933     {
934         xmlChar *xml_rule;
935         struct icu_chain_step * step = 0;
936
937         if (node->type != XML_ELEMENT_NODE)
938             continue;
939
940         xml_rule = xmlGetProp(node, (xmlChar *) "rule");
941
942         if (!strcmp((const char *) node->name, 
943                     (const char *) "casemap")){
944             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
945                                          (const uint8_t *) xml_rule, status);
946         }
947         else if (!strcmp((const char *) node->name,
948                          (const char *) "normalize")){
949             step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize, 
950                                          (const uint8_t *) xml_rule, status);
951         }
952         else if (!strcmp((const char *) node->name,
953                          (const char *) "tokenize")){
954             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
955                                          (const uint8_t *) xml_rule, status);
956         }
957         else if (!strcmp((const char *) node->name,
958                          (const char *) "display")){
959             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
960                                          (const uint8_t *) "", status);
961         }
962         else if (!strcmp((const char *) node->name,
963                          (const char *) "index")){
964             step = icu_chain_insert_step(chain, ICU_chain_step_type_index, 
965                                          (const uint8_t *) "", status);
966         }
967         else if (!strcmp((const char *) node->name,
968                          (const char *) "sortkey")){
969             step = icu_chain_insert_step(chain, ICU_chain_step_type_sortkey, 
970                                          (const uint8_t *) "", status);
971         }
972
973         xmlFree(xml_rule);
974         if (!step || U_FAILURE(*status)){
975             icu_chain_destroy(chain);
976             return 0;
977         }
978         
979
980     }
981
982     return chain;
983 }
984
985
986
987 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
988                                               enum icu_chain_step_type type,
989                                               const uint8_t * rule,
990                                               UErrorCode *status)
991 {    
992     struct icu_chain_step * step = 0;
993     struct icu_buf_utf16 * src16 = 0;
994     struct icu_buf_utf16 * buf16 = 0;
995
996     if (!chain || !type || !rule)
997         return 0;
998
999     /* assign utf16 src buffers as needed */
1000     if (chain->steps && chain->steps->buf16)
1001         src16 = chain->steps->buf16;
1002     else if (chain->src16)
1003         src16 = chain->src16;
1004     else
1005         return 0;
1006
1007     
1008     /* create utf16 destination buffers as needed, or */
1009     switch(type) {
1010     case ICU_chain_step_type_display:
1011         buf16 = src16;
1012         break;
1013     case ICU_chain_step_type_index:
1014         buf16 = src16;
1015         break;
1016     case ICU_chain_step_type_sortkey:
1017         buf16 = src16;
1018         break;
1019     case ICU_chain_step_type_casemap:
1020         buf16 = icu_buf_utf16_create(0);
1021         break;
1022     case ICU_chain_step_type_normalize:
1023         buf16 = icu_buf_utf16_create(0);
1024         break;
1025     case ICU_chain_step_type_tokenize:
1026         buf16 = icu_buf_utf16_create(0);
1027         break;
1028     default:
1029         break;
1030     }
1031
1032     /* create actual chain step with this buffer */
1033     step = icu_chain_step_create(chain, type, rule, buf16, status);
1034
1035     step->previous = chain->steps;
1036     chain->steps = step;
1037
1038     return step;
1039 }
1040
1041
1042 int icu_chain_step_next_token(struct icu_chain * chain,
1043                               struct icu_chain_step * step,
1044                               UErrorCode *status)
1045 {
1046     struct icu_buf_utf16 * src16 = 0;
1047     int got_new_token = 0;
1048
1049     if (!chain || !chain->src16 || !step || !step->more_tokens)
1050         return 0;
1051
1052     /* assign utf16 src buffers as neeed, advance in previous steps
1053        tokens until non-zero token met, and setting stop condition */
1054
1055     if (step->previous){
1056         src16 = step->previous->buf16;
1057         /* tokens might be killed in previous steps, therefore looping */
1058         while (step->need_new_token 
1059                && step->previous->more_tokens
1060                && !got_new_token)
1061             got_new_token
1062                 = icu_chain_step_next_token(chain, step->previous, status);
1063     }
1064     else { /* first step can only work once on chain->src16 input buffer */
1065         src16 = chain->src16;
1066         step->more_tokens = 0;
1067         got_new_token = 1;
1068     }
1069
1070     if (!src16)
1071         return 0;
1072
1073     /* stop if nothing to process */
1074     if (step->need_new_token && !got_new_token){
1075         step->more_tokens = 0;
1076         return 0;
1077     }
1078
1079     /* either an old token not finished yet, or a new token, thus
1080        perform the work, eventually put this steps output in 
1081        step->buf16 or the chains UTF8 output buffers  */
1082
1083     switch(step->type) {
1084     case ICU_chain_step_type_display:
1085         icu_utf16_to_utf8(chain->display8, src16, status);
1086         break;
1087     case ICU_chain_step_type_index:
1088         icu_utf16_to_utf8(chain->norm8, src16, status);
1089         break;
1090     case ICU_chain_step_type_sortkey:
1091         icu_utf16_to_utf8(chain->sort8, src16, status);
1092         break;
1093     case ICU_chain_step_type_casemap:
1094         icu_casemap_casemap(step->u.casemap,
1095                             step->buf16, src16, status);
1096         break;
1097     case ICU_chain_step_type_normalize:
1098         icu_normalizer_normalize(step->u.normalizer,
1099                                  step->buf16, src16, status);
1100         break;
1101     case ICU_chain_step_type_tokenize:
1102         /* attach to new src16 token only first time during splitting */
1103         if (step->need_new_token){
1104             icu_tokenizer_attach(step->u.tokenizer, src16, status);
1105             step->need_new_token = 0;
1106         }
1107
1108
1109         /* splitting one src16 token into multiple buf16 tokens */
1110         step->more_tokens
1111             = icu_tokenizer_next_token(step->u.tokenizer,
1112                                        step->buf16, status);
1113
1114         /* make sure to get new previous token if this one had been used up
1115            by recursive call to _same_ step */
1116
1117         if (!step->more_tokens)
1118             step->more_tokens = icu_chain_step_next_token(chain, step, status);
1119
1120         //if (0 == step->more_tokens)
1121         //return 0;
1122         break;
1123     default:
1124         return 0;
1125         break;
1126     }
1127
1128     if (U_FAILURE(*status))
1129         return 0;
1130
1131     /* if token disappered into thin air, tell caller */
1132     if (!step->buf16->utf16_len)
1133         return 0;
1134
1135     return 1;
1136 }
1137
1138
1139 #if 0 /* backup */
1140 int icu_chain_step_next_token_BAK(struct icu_chain * chain,
1141                               struct icu_chain_step * step,
1142                               UErrorCode *status)
1143 {
1144     struct icu_buf_utf16 * src16 = 0;
1145     
1146     if (!chain || !chain->src16 || !step || !step->more_tokens)
1147         return 0;
1148
1149     /* assign utf16 src buffers as neeed, advance in previous steps
1150        tokens until non-zero token met, and setting stop condition
1151     */
1152     if (step->previous){
1153         src16 = step->previous->buf16;
1154         if (step->need_new_token)
1155             step->more_tokens 
1156                 = icu_chain_step_next_token(chain, step->previous, status);
1157     }
1158     else { /* first step can only work once on chain->src16 input buffer */
1159         src16 = chain->src16;
1160         step->more_tokens = 1;
1161     }
1162
1163     /* stop if nothing to process 
1164        i.e new token source was not properly assigned
1165        or I did not get any tokens from previous step
1166    */
1167     if (!step->more_tokens || !src16)
1168         return 0;
1169
1170     /* perform the work, eventually put this steps output in 
1171        step->buf16 or the chains UTF8 output buffers  */
1172     switch(step->type) {
1173     case ICU_chain_step_type_display:
1174         icu_utf16_to_utf8(chain->display8, src16, status);
1175         break;
1176     case ICU_chain_step_type_index:
1177         icu_utf16_to_utf8(chain->norm8, src16, status);
1178         break;
1179     case ICU_chain_step_type_sortkey:
1180         icu_utf16_to_utf8(chain->sort8, src16, status);
1181         break;
1182     case ICU_chain_step_type_casemap:
1183         icu_casemap_casemap(step->u.casemap,
1184                             step->buf16, src16, status);
1185         break;
1186     case ICU_chain_step_type_normalize:
1187         icu_normalizer_normalize(step->u.normalizer,
1188                                  step->buf16, src16, status);
1189         break;
1190     case ICU_chain_step_type_tokenize:
1191         /* attach to new src16 token only first time during splitting */
1192         if (step->need_new_token){
1193             icu_tokenizer_attach(step->u.tokenizer, src16, status);
1194             step->need_new_token = 0;
1195         }
1196         /* splitting one src16 token into multiple buf16 tokens */
1197         step->more_tokens
1198             = icu_tokenizer_next_token(step->u.tokenizer,
1199                                        step->buf16, status);
1200         /* make sure to get new previous token if this one had been used up */
1201         if (step->previous && !step->more_tokens){
1202             if (icu_chain_step_next_token(chain, step->previous, status)){
1203                 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1204                 step->need_new_token = 0;   
1205                 step->more_tokens
1206                     = icu_tokenizer_next_token(step->u.tokenizer,
1207                                                step->buf16, status);
1208             }
1209         }
1210         if (0 == step->more_tokens)
1211             return 0;
1212         break;
1213     default:
1214         return 0;
1215         break;
1216     }
1217
1218     /* stop further token processing if last step and 
1219        new tokens can ot be obtained from last non-existing step 
1220     */
1221     if (!step->previous)
1222         step->more_tokens = 0;
1223
1224     if (U_FAILURE(*status))
1225         return 0;
1226
1227     // result buf empty, got no token TODO: make while loop around all token
1228     // fetching operations
1229     //if (!step->buf16->utf16_len)
1230     //    return 0;
1231
1232     return 1;
1233 }
1234
1235 #endif /* backup */
1236
1237 int icu_chain_assign_cstr(struct icu_chain * chain,
1238                           const char * src8cstr, 
1239                           UErrorCode *status)
1240 {
1241     struct icu_chain_step * stp = 0; 
1242
1243     if (!chain || !src8cstr)
1244         return 0;
1245
1246     stp = chain->steps;
1247     
1248     /* clear token count */
1249     chain->token_count = 0;
1250
1251     /* clear all steps stop states */
1252     while (stp){
1253         stp->more_tokens = 1;
1254         stp->need_new_token = 1;
1255         stp = stp->previous;
1256     }
1257     
1258     /* finally convert UTF8 to UTF16 string */
1259     icu_utf16_from_utf8_cstr(chain->src16, src8cstr, status);
1260             
1261     if (U_FAILURE(*status))
1262         return 0;
1263
1264     return 1;
1265 }
1266
1267
1268
1269 int icu_chain_next_token(struct icu_chain * chain,
1270                          UErrorCode *status)
1271 {
1272     int got_token = 0;
1273     
1274     if (!chain || !chain->steps)
1275         return 0;
1276
1277     got_token = icu_chain_step_next_token(chain, chain->steps, status);
1278     
1279     if (got_token){
1280         chain->token_count++;
1281         return chain->token_count;
1282     }
1283
1284     return 0;
1285 }
1286
1287 int icu_chain_get_token_count(struct icu_chain * chain)
1288 {
1289     if (!chain)
1290         return 0;
1291     
1292     return chain->token_count;
1293 }
1294
1295
1296
1297 const char * icu_chain_get_display(struct icu_chain * chain)
1298 {
1299     if (chain->display8)
1300         return icu_buf_utf8_to_cstr(chain->display8);
1301     
1302     return 0;
1303 }
1304
1305 const char * icu_chain_get_norm(struct icu_chain * chain)
1306 {
1307     if (chain->norm8)
1308         return icu_buf_utf8_to_cstr(chain->norm8);
1309     
1310     return 0;
1311 }
1312
1313 const char * icu_chain_get_sort(struct icu_chain * chain)
1314 {
1315     if (chain->sort8)
1316         return icu_buf_utf8_to_cstr(chain->sort8);
1317     
1318     return 0;
1319 }
1320
1321
1322 #endif /* HAVE_ICU */
1323
1324
1325
1326
1327 /*
1328  * Local variables:
1329  * c-basic-offset: 4
1330  * indent-tabs-mode: nil
1331  * End:
1332  * vim: shiftwidth=4 tabstop=8 expandtab
1333  */