Isolated ICU locale out of XML config file. Locale is now the second argument to...
[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
45 /* declared structs and functions */
46
47 int icu_check_status (UErrorCode status);
48
49 struct icu_buf_utf16
50 {
51   UChar * utf16;
52   int32_t utf16_len;
53   int32_t utf16_cap;
54 };
55
56 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity);
57
58 struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16);
59
60 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
61                                             size_t capacity);
62
63 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
64                                           struct icu_buf_utf16 * src16);
65
66 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16);
67
68
69
70 struct icu_buf_utf8
71 {
72     uint8_t * utf8;
73     int32_t utf8_len;
74     int32_t utf8_cap;
75 };
76
77 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity);
78
79 struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8);
80
81 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
82                                           size_t capacity);
83
84 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8);
85
86
87 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
88                                struct icu_buf_utf8 * src8,
89                                UErrorCode * status);
90
91 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
92                                     const char * src8cstr,
93                                     UErrorCode * status);
94
95
96 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
97                              struct icu_buf_utf16 * src16,
98                              UErrorCode * status);
99
100 struct icu_casemap
101 {
102     char locale[16];
103     char action;
104 };
105
106 struct icu_casemap * icu_casemap_create(const char *locale, char action,
107                                             UErrorCode *status);
108
109 void icu_casemap_destroy(struct icu_casemap * casemap);
110
111 int icu_casemap_casemap(struct icu_casemap * casemap,
112                         struct icu_buf_utf16 * dest16,
113                         struct icu_buf_utf16 * src16,
114                         UErrorCode *status);
115
116 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
117                       struct icu_buf_utf16 * src16,
118                       const char *locale, char action,
119                       UErrorCode *status);
120
121 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
122                                    struct icu_buf_utf8 * dest8, 
123                                    struct icu_buf_utf16 * src16,
124                                    UErrorCode * status);
125
126 struct icu_tokenizer
127 {
128     char locale[16];
129     char action;
130     UBreakIterator* bi;
131     struct icu_buf_utf16 * buf16;
132     int32_t token_count;
133     int32_t token_id;
134     int32_t token_start;
135     int32_t token_end;
136 /*
137   keep always invariant
138   0 <= token_start 
139   <= token_end 
140   <= buf16->utf16_len
141   and invariant
142   0 <= token_id <= token_count
143 */
144 };
145
146 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
147                                             UErrorCode *status);
148
149 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer);
150
151 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer, 
152                          struct icu_buf_utf16 * src16, UErrorCode *status);
153
154 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer, 
155                                  struct icu_buf_utf16 * tkn16, 
156                                  UErrorCode *status);
157
158 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer);
159 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer);
160 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer);
161 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer);
162 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer);
163
164
165
166 struct icu_normalizer
167 {
168     char action;
169     struct icu_buf_utf16 * rules16;
170     UParseError parse_error[256];
171     UTransliterator * trans;
172 };
173
174 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
175                                               UErrorCode *status);
176
177
178 void icu_normalizer_destroy(struct icu_normalizer * normalizer);
179
180 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
181                              struct icu_buf_utf16 * dest16,
182                              struct icu_buf_utf16 * src16,
183                              UErrorCode *status);
184
185 enum icu_chain_step_type {
186     ICU_chain_step_type_none,
187     ICU_chain_step_type_display,   /* convert to utf8 display format */
188     ICU_chain_step_type_index,     /* convert to utf8 index format  */
189     ICU_chain_step_type_sortkey,   /* convert to utf8 sortkey format */
190     ICU_chain_step_type_casemap,   /* apply utf16 charmap */
191     ICU_chain_step_type_normalize, /* apply utf16 normalization */
192     ICU_chain_step_type_tokenize   /* apply utf16 tokenization */
193 };
194
195
196
197 struct icu_chain_step
198 {
199     /* type and action object */
200     enum icu_chain_step_type type;
201     union {
202         struct icu_casemap * casemap;
203         struct icu_normalizer * normalizer;
204         struct icu_tokenizer * tokenizer;  
205     } u;
206     /* temprary post-action utf16 buffer */
207     struct icu_buf_utf16 * buf16;  
208     struct icu_chain_step * previous;
209     int more_tokens;
210     int need_new_token;
211 };
212
213
214 struct icu_chain;
215
216 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
217                                               enum icu_chain_step_type type,
218                                               const uint8_t * rule,
219                                               struct icu_buf_utf16 * buf16,
220                                               UErrorCode *status);
221
222
223 void icu_chain_step_destroy(struct icu_chain_step * step);
224
225
226 struct icu_chain
227 {
228     uint8_t identifier[128];
229     uint8_t locale[16];
230     
231     /* number of tokens returned so far */
232     int32_t token_count;
233     
234     /* utf8 output buffers */
235     struct icu_buf_utf8 * display8;
236     struct icu_buf_utf8 * norm8;
237     struct icu_buf_utf8 * sort8;
238     
239     /* utf16 source buffer */
240     struct icu_buf_utf16 * src16;
241     
242     /* linked list of chain steps */
243     struct icu_chain_step * steps;
244 };
245
246 struct icu_chain * icu_chain_create( // const uint8_t * identifier, 
247                                     const uint8_t * locale);
248
249 void icu_chain_destroy(struct icu_chain * chain);
250
251 struct icu_chain * icu_chain_xml_config(xmlNode *xml_node,
252                                         const uint8_t * locale, 
253                                         UErrorCode * status);
254
255
256 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
257                                               enum icu_chain_step_type type,
258                                               const uint8_t * rule,
259                                               UErrorCode *status);
260
261
262 int icu_chain_step_next_token(struct icu_chain * chain,
263                               struct icu_chain_step * step,
264                               UErrorCode *status);
265
266 int icu_chain_assign_cstr(struct icu_chain * chain,
267                           const char * src8cstr, 
268                           UErrorCode *status);
269
270 int icu_chain_next_token(struct icu_chain * chain,
271                          UErrorCode *status);
272
273 int icu_chain_get_token_count(struct icu_chain * chain);
274
275 const char * icu_chain_get_display(struct icu_chain * chain);
276
277 const char * icu_chain_get_norm(struct icu_chain * chain);
278
279 const char * icu_chain_get_sort(struct icu_chain * chain);
280
281 #endif /* ICU_I18NL_H */
282
283 /*
284  * Local variables:
285  * c-basic-offset: 4
286  * indent-tabs-mode: nil
287  * End:
288  * vim: shiftwidth=4 tabstop=8 expandtab
289  */