added special case with an empty ICU chain for 'raw' index processing without call...
[yaz-moved-to-github.git] / include / yaz / icu_I18N.h
1 /*
2  * Copyright (c) 1995-2007, Index Data
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef ICU_I18NL_H
29 #define ICU_I18NL_H
30
31 #include <yaz/nmem.h>
32
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35
36 #include <unicode/utypes.h>   /* Basic ICU data types */
37 #include <unicode/uchar.h>    /* char names           */
38
39 #include <unicode/ucol.h> 
40 #include <unicode/ubrk.h>
41 #include <unicode/utrans.h>
42
43
44 /* declared structs and functions */
45
46 int icu_check_status (UErrorCode status);
47
48 struct icu_buf_utf16
49 {
50   UChar * utf16;
51   int32_t utf16_len;
52   int32_t utf16_cap;
53 };
54
55 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity);
56
57 struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16);
58
59 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
60                                             size_t capacity);
61
62 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
63                                           struct icu_buf_utf16 * src16);
64
65 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16);
66
67
68
69 struct icu_buf_utf8
70 {
71     uint8_t * utf8;
72     int32_t utf8_len;
73     int32_t utf8_cap;
74 };
75
76 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity);
77
78 struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8);
79
80 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
81                                           size_t capacity);
82
83 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8);
84
85
86 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
87                                struct icu_buf_utf8 * src8,
88                                UErrorCode * status);
89
90 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
91                                     const char * src8cstr,
92                                     UErrorCode * status);
93
94
95 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
96                              struct icu_buf_utf16 * src16,
97                              UErrorCode * status);
98
99 struct icu_casemap
100 {
101     char locale[16];
102     char action;
103 };
104
105 struct icu_casemap * icu_casemap_create(const char *locale, char action,
106                                             UErrorCode *status);
107
108 void icu_casemap_destroy(struct icu_casemap * casemap);
109
110 int icu_casemap_casemap(struct icu_casemap * casemap,
111                         struct icu_buf_utf16 * dest16,
112                         struct icu_buf_utf16 * src16,
113                         UErrorCode *status);
114
115 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
116                       struct icu_buf_utf16 * src16,
117                       const char *locale, char action,
118                       UErrorCode *status);
119
120 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
121                                    struct icu_buf_utf8 * dest8, 
122                                    struct icu_buf_utf16 * src16,
123                                    UErrorCode * status);
124
125 struct icu_tokenizer
126 {
127     char locale[16];
128     char action;
129     UBreakIterator* bi;
130     struct icu_buf_utf16 * buf16;
131     int32_t token_count;
132     int32_t token_id;
133     int32_t token_start;
134     int32_t token_end;
135 /*
136   keep always invariant
137   0 <= token_start 
138   <= token_end 
139   <= buf16->utf16_len
140   and invariant
141   0 <= token_id <= token_count
142 */
143 };
144
145 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
146                                             UErrorCode *status);
147
148 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer);
149
150 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer, 
151                          struct icu_buf_utf16 * src16, UErrorCode *status);
152
153 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer, 
154                                  struct icu_buf_utf16 * tkn16, 
155                                  UErrorCode *status);
156
157 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer);
158 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer);
159 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer);
160 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer);
161 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer);
162
163
164
165 struct icu_normalizer
166 {
167     char action;
168     struct icu_buf_utf16 * rules16;
169     UParseError parse_error[256];
170     UTransliterator * trans;
171 };
172
173 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
174                                               UErrorCode *status);
175
176
177 void icu_normalizer_destroy(struct icu_normalizer * normalizer);
178
179 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
180                              struct icu_buf_utf16 * dest16,
181                              struct icu_buf_utf16 * src16,
182                              UErrorCode *status);
183
184 enum icu_chain_step_type {
185     ICU_chain_step_type_none,
186     ICU_chain_step_type_display,   /* convert to utf8 display format */
187     ICU_chain_step_type_casemap,   /* apply utf16 charmap */
188     ICU_chain_step_type_normalize, /* apply utf16 normalization */
189     ICU_chain_step_type_tokenize   /* apply utf16 tokenization */
190 };
191
192
193
194 struct icu_chain_step
195 {
196     /* type and action object */
197     enum icu_chain_step_type type;
198     union {
199         struct icu_casemap * casemap;
200         struct icu_normalizer * normalizer;
201         struct icu_tokenizer * tokenizer;  
202     } u;
203     /* temprary post-action utf16 buffer */
204     struct icu_buf_utf16 * buf16;  
205     struct icu_chain_step * previous;
206     int more_tokens;
207     int need_new_token;
208 };
209
210
211 struct icu_chain;
212
213 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
214                                               enum icu_chain_step_type type,
215                                               const uint8_t * rule,
216                                               struct icu_buf_utf16 * buf16,
217                                               UErrorCode *status);
218
219
220 void icu_chain_step_destroy(struct icu_chain_step * step);
221
222
223 struct icu_chain
224 {
225     uint8_t locale[16];
226     int sort;
227
228     const char * src8cstr;
229
230     UCollator * coll;
231     
232     /* number of tokens returned so far */
233     int32_t token_count;
234     
235     /* utf8 output buffers */
236     struct icu_buf_utf8 * display8;
237     struct icu_buf_utf8 * norm8;
238     struct icu_buf_utf8 * sort8;
239     
240     /* utf16 source buffer */
241     struct icu_buf_utf16 * src16;
242     
243     /* linked list of chain steps */
244     struct icu_chain_step * steps;
245 };
246
247 struct icu_chain * icu_chain_create(const uint8_t * locale,
248                                     int sort,
249                                     UErrorCode * status);
250
251 void icu_chain_destroy(struct icu_chain * chain);
252
253 struct icu_chain * icu_chain_xml_config(xmlNode *xml_node,
254                                         const uint8_t * locale,
255                                         int sort,
256                                         UErrorCode * status);
257
258 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
259                                               enum icu_chain_step_type type,
260                                               const uint8_t * rule,
261                                               UErrorCode *status);
262
263 int icu_chain_step_next_token(struct icu_chain * chain,
264                               struct icu_chain_step * step,
265                               UErrorCode *status);
266
267 int icu_chain_assign_cstr(struct icu_chain * chain,
268                           const char * src8cstr, 
269                           UErrorCode *status);
270
271 int icu_chain_next_token(struct icu_chain * chain,
272                          UErrorCode *status);
273
274 int icu_chain_get_token_count(struct icu_chain * chain);
275
276 const char * icu_chain_get_display(struct icu_chain * chain);
277
278 const char * icu_chain_get_norm(struct icu_chain * chain);
279
280 const char * icu_chain_get_sort(struct icu_chain * chain);
281
282 const UCollator * icu_chain_get_coll(struct icu_chain * chain);
283
284 #endif /* ICU_I18NL_H */
285
286 /*
287  * Local variables:
288  * c-basic-offset: 4
289  * indent-tabs-mode: nil
290  * End:
291  * vim: shiftwidth=4 tabstop=8 expandtab
292  */