Refactor ICU WRT locale.
[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.14 2007-11-08 13:35:13 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 YAZ_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     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                         const char *locale)
368 {
369     if(!casemap)
370         return 0;
371     
372     return icu_utf16_casemap(dest16, src16, locale,
373                              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                                         int sort,
913                                         UErrorCode * status)
914 {
915     xmlNode *node = 0;
916     struct icu_chain * chain = 0;
917    
918     *status = U_ZERO_ERROR;
919
920     if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
921         return 0;
922     
923     {
924         xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node, 
925                                           (xmlChar *) "locale");
926         
927         if (xml_locale)
928         {
929             chain = icu_chain_create((const char *) xml_locale, sort, status);
930             xmlFree(xml_locale);
931         }
932         
933     }
934     if (!chain)
935         return 0;
936
937     for (node = xml_node->children; node; node = node->next)
938     {
939         xmlChar *xml_rule;
940         struct icu_chain_step * step = 0;
941
942         if (node->type != XML_ELEMENT_NODE)
943             continue;
944
945         xml_rule = xmlGetProp(node, (xmlChar *) "rule");
946
947         if (!strcmp((const char *) node->name, 
948                     (const char *) "casemap")){
949             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
950                                          (const uint8_t *) xml_rule, status);
951         }
952         else if (!strcmp((const char *) node->name,
953                          (const char *) "normalize")){
954             step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize, 
955                                          (const uint8_t *) xml_rule, status);
956         }
957         else if (!strcmp((const char *) node->name,
958                          (const char *) "tokenize")){
959             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
960                                          (const uint8_t *) xml_rule, status);
961         }
962         else if (!strcmp((const char *) node->name,
963                          (const char *) "display")){
964             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
965                                          (const uint8_t *) "", status);
966         }
967         xmlFree(xml_rule);
968         if (!step || U_FAILURE(*status)){
969             icu_chain_destroy(chain);
970             return 0;
971         }
972         
973
974     }
975
976     return chain;
977 }
978
979
980
981 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
982                                               enum icu_chain_step_type type,
983                                               const uint8_t * rule,
984                                               UErrorCode *status)
985 {    
986     struct icu_chain_step * step = 0;
987     struct icu_buf_utf16 * src16 = 0;
988     struct icu_buf_utf16 * buf16 = 0;
989
990     if (!chain || !type || !rule)
991         return 0;
992
993     /* assign utf16 src buffers as needed */
994     if (chain->steps && chain->steps->buf16)
995         src16 = chain->steps->buf16;
996     else if (chain->src16)
997         src16 = chain->src16;
998     else
999         return 0;
1000
1001     
1002     /* create utf16 destination buffers as needed, or */
1003     switch(type) {
1004     case ICU_chain_step_type_display:
1005         buf16 = src16;
1006         break;
1007     case ICU_chain_step_type_casemap:
1008         buf16 = icu_buf_utf16_create(0);
1009         break;
1010     case ICU_chain_step_type_normalize:
1011         buf16 = icu_buf_utf16_create(0);
1012         break;
1013     case ICU_chain_step_type_tokenize:
1014         buf16 = icu_buf_utf16_create(0);
1015         break;
1016     default:
1017         break;
1018     }
1019
1020     /* create actual chain step with this buffer */
1021     step = icu_chain_step_create(chain, type, rule, buf16, status);
1022
1023     step->previous = chain->steps;
1024     chain->steps = step;
1025
1026     return step;
1027 }
1028
1029
1030 int icu_chain_step_next_token(struct icu_chain * chain,
1031                               struct icu_chain_step * step,
1032                               UErrorCode *status)
1033 {
1034     struct icu_buf_utf16 * src16 = 0;
1035     int got_new_token = 0;
1036
1037     if (!chain || !chain->src16 || !step || !step->more_tokens)
1038         return 0;
1039
1040     /* assign utf16 src buffers as neeed, advance in previous steps
1041        tokens until non-zero token met, and setting stop condition */
1042
1043     if (step->previous){
1044         src16 = step->previous->buf16;
1045         /* tokens might be killed in previous steps, therefore looping */
1046
1047         while (step->need_new_token 
1048                && step->previous->more_tokens
1049                && !got_new_token)
1050             got_new_token
1051                 = icu_chain_step_next_token(chain, step->previous, status);
1052     }
1053     else { /* first step can only work once on chain->src16 input buffer */
1054         src16 = chain->src16;
1055         step->more_tokens = 0;
1056         got_new_token = 1;
1057     }
1058
1059     if (!src16)
1060         return 0;
1061
1062     /* stop if nothing to process */
1063     if (step->need_new_token && !got_new_token){
1064         step->more_tokens = 0;
1065         return 0;
1066     }
1067
1068     /* either an old token not finished yet, or a new token, thus
1069        perform the work, eventually put this steps output in 
1070        step->buf16 or the chains UTF8 output buffers  */
1071
1072     switch(step->type) {
1073     case ICU_chain_step_type_display:
1074         icu_utf16_to_utf8(chain->display8, src16, status);
1075         break;
1076     case ICU_chain_step_type_casemap:
1077         icu_casemap_casemap(step->u.casemap,
1078                             step->buf16, src16, status,
1079                             chain->locale);
1080         break;
1081     case ICU_chain_step_type_normalize:
1082         icu_normalizer_normalize(step->u.normalizer,
1083                                  step->buf16, src16, status);
1084         break;
1085     case ICU_chain_step_type_tokenize:
1086         /* attach to new src16 token only first time during splitting */
1087         if (step->need_new_token){
1088             icu_tokenizer_attach(step->u.tokenizer, src16, status);
1089             step->need_new_token = 0;
1090         }
1091
1092
1093         /* splitting one src16 token into multiple buf16 tokens */
1094         step->more_tokens
1095             = icu_tokenizer_next_token(step->u.tokenizer,
1096                                        step->buf16, status);
1097
1098         /* make sure to get new previous token if this one had been used up
1099            by recursive call to _same_ step */
1100
1101         if (!step->more_tokens){
1102             step->more_tokens = icu_chain_step_next_token(chain, step, status);
1103             return step->more_tokens;  // avoid one token count too much!
1104         }
1105
1106         break;
1107     default:
1108         return 0;
1109         break;
1110     }
1111
1112     if (U_FAILURE(*status))
1113         return 0;
1114
1115     /* if token disappered into thin air, tell caller */
1116     /* if (!step->buf16->utf16_len && !step->more_tokens) */ 
1117     /*    return 0; */ 
1118
1119     return 1;
1120 }
1121
1122
1123 int icu_chain_assign_cstr(struct icu_chain * chain,
1124                           const char * src8cstr, 
1125                           UErrorCode *status)
1126 {
1127     struct icu_chain_step * stp = 0; 
1128
1129     if (!chain || !src8cstr)
1130         return 0;
1131
1132     chain->src8cstr = src8cstr;
1133
1134     stp = chain->steps;
1135     
1136     /* clear token count */
1137     chain->token_count = 0;
1138
1139     /* clear all steps stop states */
1140     while (stp){
1141         stp->more_tokens = 1;
1142         stp->need_new_token = 1;
1143         stp = stp->previous;
1144     }
1145     
1146     /* finally convert UTF8 to UTF16 string if needed */
1147     if (chain->steps || chain->sort)
1148         icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
1149             
1150     if (U_FAILURE(*status))
1151         return 0;
1152
1153     return 1;
1154 }
1155
1156
1157
1158 int icu_chain_next_token(struct icu_chain * chain,
1159                          UErrorCode *status)
1160 {
1161     int got_token = 0;
1162     
1163     *status = U_ZERO_ERROR;
1164
1165     if (!chain)
1166         return 0;
1167
1168     /* special case with no steps - same as index type binary */
1169     if (!chain->steps){
1170         if (chain->token_count)
1171             return 0;
1172         else {
1173             chain->token_count++;
1174             
1175             if (chain->sort)
1176                 icu_sortkey8_from_utf16(chain->coll,
1177                                         chain->sort8, chain->steps->buf16,
1178                                         status);
1179             return chain->token_count;
1180         }
1181     }
1182     /* usual case, one or more icu chain steps existing */
1183     else {
1184
1185         while(!got_token && chain->steps && chain->steps->more_tokens)
1186             got_token = icu_chain_step_next_token(chain, chain->steps, status);
1187
1188         if (got_token){
1189             chain->token_count++;
1190
1191             icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
1192             
1193             if (chain->sort)
1194                 icu_sortkey8_from_utf16(chain->coll,
1195                                         chain->sort8, chain->steps->buf16,
1196                                         status);
1197             
1198             return chain->token_count;
1199         }
1200     }
1201         
1202     return 0;
1203 }
1204
1205 int icu_chain_token_number(struct icu_chain * chain)
1206 {
1207     if (!chain)
1208         return 0;
1209     
1210     return chain->token_count;
1211 }
1212
1213
1214 const char * icu_chain_token_display(struct icu_chain * chain)
1215 {
1216     if (chain->display8)
1217         return icu_buf_utf8_to_cstr(chain->display8);
1218     
1219     return 0;
1220 }
1221
1222 const char * icu_chain_token_norm(struct icu_chain * chain)
1223 {
1224     if (!chain->steps)
1225         return chain->src8cstr;
1226
1227     if (chain->norm8)
1228         return icu_buf_utf8_to_cstr(chain->norm8);
1229     
1230     return 0;
1231 }
1232
1233 const char * icu_chain_token_sortkey(struct icu_chain * chain)
1234 {
1235     if (chain->sort8)
1236         return icu_buf_utf8_to_cstr(chain->sort8);
1237     
1238     return 0;
1239 }
1240
1241 const UCollator * icu_chain_get_coll(struct icu_chain * chain)
1242 {
1243     return chain->coll;
1244 }
1245
1246
1247 #endif /* YAZ_HAVE_ICU */
1248
1249
1250
1251
1252 /*
1253  * Local variables:
1254  * c-basic-offset: 4
1255  * indent-tabs-mode: nil
1256  * End:
1257  * vim: shiftwidth=4 tabstop=8 expandtab
1258  */