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