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