6dd150ea77d67ad9d9dd836e3a84217a2f0bfd5f
[pazpar2-moved-to-github.git] / src / icu_I18N.c
1 /* $Id: icu_I18N.c,v 1.5 2007-05-07 09:31:36 marc Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #if HAVE_CONFIG_H
23 #include "cconfig.h"
24 #endif
25
26 #define USE_TIMING 0
27 #if USE_TIMING
28 #include <yaz/timing.h>
29 #endif
30
31
32 #ifdef HAVE_ICU
33 #include "icu_I18N.h"
34
35 #include <yaz/log.h>
36
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40
41 #include <unicode/ustring.h>  /* some more string fcns*/
42 #include <unicode/uchar.h>    /* char names           */
43
44
45 //#include <unicode/ustdio.h>
46 //#include <unicode/utypes.h>   /* Basic ICU data types */
47 #include <unicode/ucol.h> 
48 //#include <unicode/ucnv.h>     /* C   Converter API    */
49 //#include <unicode/uloc.h>
50 //#include <unicode/ubrk.h>
51 /* #include <unicode/unistr.h> */
52
53
54
55
56 int icu_check_status (UErrorCode status)
57 {
58     //if(U_FAILURE(status))
59     if(!U_SUCCESS(status))
60       yaz_log(YLOG_WARN, 
61               "ICU: %d %s\n", status, u_errorName(status));
62   return status;
63 }
64
65
66
67 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
68 {
69   struct icu_buf_utf16 * buf16 
70     = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16));
71
72   buf16->utf16 = 0;
73   buf16->utf16_len = 0;
74   buf16->utf16_cap = 0;
75
76   if (capacity > 0){
77     buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
78     buf16->utf16[0] = (UChar) 0;
79     buf16->utf16_cap = capacity;
80   }
81   return buf16;
82 };
83
84
85 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
86                                             size_t capacity)
87 {
88   if (buf16){
89     if (capacity >  0){
90       if (0 == buf16->utf16)
91         buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
92       else
93         buf16->utf16 
94           = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity);
95       buf16->utf16[0] = (UChar) 0;
96       buf16->utf16_len = 0;
97       buf16->utf16_cap = capacity;
98     } 
99     else { 
100       if (buf16->utf16)
101         free(buf16->utf16);
102       buf16->utf16 = 0;
103       buf16->utf16_len = 0;
104       buf16->utf16_cap = 0;
105     }
106   }
107
108   return buf16;
109 };
110
111
112 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
113 {
114   if (buf16){
115     if (buf16->utf16)
116       free(buf16->utf16);
117     free(buf16);
118   }
119 };
120
121
122
123
124
125
126 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
127 {
128   struct icu_buf_utf8 * buf8 
129     = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8));
130
131   buf8->utf8 = 0;
132   buf8->utf8_len = 0;
133   buf8->utf8_cap = 0;
134
135   if (capacity > 0){
136     buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
137     buf8->utf8[0] = (uint8_t) 0;
138     buf8->utf8_cap = capacity;
139   }
140   return buf8;
141 };
142
143
144
145 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
146                                           size_t capacity)
147 {
148   if (buf8){
149     if (capacity >  0){
150       if (0 == buf8->utf8)
151         buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
152       else
153         buf8->utf8 
154           = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity);
155       buf8->utf8[0] = (uint8_t) 0;
156       buf8->utf8_len = 0;
157       buf8->utf8_cap = capacity;
158     } 
159     else { 
160       if (buf8->utf8)
161         free(buf8->utf8);
162       buf8->utf8 = 0;
163       buf8->utf8_len = 0;
164       buf8->utf8_cap = 0;
165     }
166   }
167
168   return buf8;
169 };
170
171
172
173 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
174 {
175   if (buf8){
176     if (buf8->utf8)
177       free(buf8->utf8);
178     free(buf8);
179   }
180 };
181
182
183
184 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
185                                struct icu_buf_utf8 * src8,
186                                UErrorCode * status)
187 {
188   int32_t utf16_len = 0;
189   
190   u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
191                 &utf16_len,
192                 (const char *) src8->utf8, src8->utf8_len, status);
193   
194   // check for buffer overflow, resize and retry
195   if (*status == U_BUFFER_OVERFLOW_ERROR
196       //|| dest16->utf16_len > dest16->utf16_cap
197       ){
198     icu_buf_utf16_resize(dest16, utf16_len * 2);
199     *status = U_ZERO_ERROR;
200     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
201                   &utf16_len,
202                   (const char *) src8->utf8, src8->utf8_len, status);
203   }
204
205   if (*status != U_BUFFER_OVERFLOW_ERROR
206       && utf16_len < dest16->utf16_cap)
207     dest16->utf16_len = utf16_len;
208   else {
209     dest16->utf16[0] = (UChar) 0;
210     dest16->utf16_len = 0;
211   }
212   
213   return *status;
214 };
215
216  
217
218 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
219                                     const char * src8cstr,
220                                     UErrorCode * status)
221 {
222   size_t src8cstr_len = 0;
223   int32_t utf16_len = 0;
224
225   src8cstr_len = strlen(src8cstr);
226   
227   u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
228                 &utf16_len,
229                 src8cstr, src8cstr_len, status);
230   
231   // check for buffer overflow, resize and retry
232   if (*status == U_BUFFER_OVERFLOW_ERROR
233       //|| dest16->utf16_len > dest16->utf16_cap
234       ){
235     icu_buf_utf16_resize(dest16, utf16_len * 2);
236     *status = U_ZERO_ERROR;
237     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
238                   &utf16_len,
239                   src8cstr, src8cstr_len, status);
240   }
241
242   if (*status != U_BUFFER_OVERFLOW_ERROR
243       && utf16_len < dest16->utf16_cap)
244     dest16->utf16_len = utf16_len;
245   else {
246     dest16->utf16[0] = (UChar) 0;
247     dest16->utf16_len = 0;
248   }
249   
250   return *status;
251 };
252
253
254 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
255                                    struct icu_buf_utf8 * dest8, 
256                                    struct icu_buf_utf16 * src16,
257                                    UErrorCode * status)
258
259   
260   int32_t sortkey_len = 0;
261
262   sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
263                                 dest8->utf8, dest8->utf8_cap);
264
265   // check for buffer overflow, resize and retry
266   if (sortkey_len > dest8->utf8_cap) {
267     icu_buf_utf8_resize(dest8, sortkey_len * 2);
268     sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
269                                   dest8->utf8, dest8->utf8_cap);
270   }
271
272   if (sortkey_len > 0)
273     dest8->utf8_len = sortkey_len;
274  
275   return *status;
276 };
277
278
279
280
281
282 /// CRAP FOLLOWING HERE ...
283
284 #if 0
285
286 // forward declarations for helper functions
287
288 int icu_check_status (UErrorCode status);
289
290 UChar* icu_utf16_from_utf8(UChar *utf16,
291                            int32_t utf16_cap,
292                            int32_t *utf16_len,
293                            const char *utf8);
294
295 UChar* icu_utf16_from_utf8n(UChar *utf16,
296                             int32_t utf16_cap,
297                             int32_t *utf16_len,
298                             const char *utf8, 
299                             size_t utf8_len);
300
301
302 char* icu_utf16_to_utf8(char *utf8,           
303                         size_t utf8_cap,
304                         size_t *utf8_len,
305                         const UChar *utf16, 
306                         int32_t utf16_len);
307
308
309 int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap,
310                           const UChar *src16, int32_t src16_len,
311                           const char *locale, char action);
312
313
314 // source code of all functions
315
316 int icu_check_status (UErrorCode status)
317 {
318     //if(U_FAILURE(status))
319     if(!U_SUCCESS(status))
320       yaz_log(YLOG_WARN, 
321               "ICU: %d %s\n", status, u_errorName(status));
322   return status;
323 }
324
325
326 UChar* icu_utf16_from_utf8(UChar *utf16,             
327                            int32_t utf16_cap,
328                            int32_t *utf16_len,
329                            const char *utf8)
330 {
331     size_t utf8_len = strlen(utf8);
332     return icu_utf16_from_utf8n(utf16, utf16_cap, utf16_len,
333                                   utf8, utf8_len);    
334 }
335
336
337 UChar* icu_utf16_from_utf8n(UChar *utf16,             
338                             int32_t utf16_cap,
339                             int32_t *utf16_len,
340                             const char *utf8, 
341                             size_t utf8_len)
342 {
343     UErrorCode status = U_ZERO_ERROR;
344     u_strFromUTF8(utf16, utf16_cap, utf16_len, utf8, (int32_t) utf8_len,
345                   &status);
346     if (U_ZERO_ERROR != icu_check_status(status))
347         return 0;
348     else
349         return utf16;
350 }
351
352
353 char* icu_utf16_to_utf8(char *utf8,           
354                         size_t utf8_cap,
355                         size_t *utf8_len,
356                         const UChar *utf16, 
357                         int32_t utf16_len)
358 {
359     UErrorCode status = U_ZERO_ERROR;
360     u_strToUTF8(utf8, (int32_t) utf8_cap, (int32_t *)utf8_len, 
361                 utf16, utf16_len, &status);
362     if (U_ZERO_ERROR != icu_check_status(status))
363         return 0;
364     else
365         return utf8;
366 }
367
368
369 int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap,
370                           const UChar *src16, int32_t src16_len,
371                           const char *locale, char action)
372 {
373     UErrorCode status = U_ZERO_ERROR;
374     int32_t dest16_len = 0;
375     
376     switch(action) {    
377     case 'l':    
378         dest16_len = u_strToLower(dest16, dest16_cap, src16, src16_len, 
379                                   locale, &status);
380         break;
381     case 'u':    
382         dest16_len = u_strToUpper(dest16, dest16_cap, src16, src16_len, 
383                                   locale, &status);
384         break;
385     case 't':    
386         dest16_len = u_strToTitle(dest16, dest16_cap, src16, src16_len,
387                                   0, locale, &status);
388         break;
389     case 'f':    
390         dest16_len = u_strFoldCase(dest16, dest16_cap, src16, src16_len,
391                                    U_FOLD_CASE_DEFAULT, &status);
392         break;
393         
394     default:
395         return 0;
396         break;
397     }
398
399     if (U_ZERO_ERROR != icu_check_status(status))
400         return 0;
401     else
402         return dest16_len;
403 }
404
405
406 char * icu_casemap(NMEM nmem, char *buf, size_t buf_cap, 
407                    size_t *dest8_len,  const char *src8,
408                    const char *locale, char action)
409 {
410     size_t src8_len = strlen(src8);
411     int32_t buf_len = 0;
412     char * dest8 = 0;
413     
414     if (dest8_len)
415         *dest8_len = 0;
416
417     if (!buf || !(buf_cap > 0) || !src8_len)
418         return 0;
419
420     // converting buf to utf16
421     buf = (char *)icu_utf16_from_utf8n((UChar *) buf, 
422                                        (int32_t) buf_cap, &buf_len,
423                                        src8, src8_len);
424     
425     // case mapping
426     buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap,
427                                          (const UChar *)buf, (int32_t) buf_len,
428                                          locale, action);
429
430     // converting buf to utf8
431     buf = icu_utf16_to_utf8(buf, buf_cap, (size_t *) &buf_len,
432                             (const UChar *) buf, (int32_t) buf_len);
433
434     
435     // copying out to nmem
436     buf[buf_len] = '\0';
437
438     if(dest8_len)
439         *dest8_len = buf_len;
440
441     dest8 =  nmem_strdup(nmem, buf);
442     return dest8;
443 }
444
445
446 struct icu_termmap * icu_termmap_create(NMEM nmem)
447 {
448     struct icu_termmap *itmp =  nmem_malloc(nmem, sizeof(*itmp));
449     itmp->sort_key = 0;
450     itmp->norm_term = 0;
451     itmp->disp_term = 0;
452     return itmp;
453 };
454
455 int icu_termmap_cmp(const void *vp1, const void *vp2)
456 {
457     struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
458     struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
459
460     return strcmp(itmp1->sort_key, itmp2->sort_key);
461 }
462
463
464
465 char * icu_sortmap(NMEM nmem, char *buf, size_t buf_cap, 
466                    size_t *dest8_len,  const char *src8,
467                    const char *locale)
468 {
469     size_t src8_len = strlen(src8);
470     int32_t buf_len = 0;
471     char * dest8 = 0;
472     
473     if (dest8_len)
474         *dest8_len = 0;
475
476     if (!buf || !(buf_cap > 0) || !src8_len)
477         return 0;
478
479     // converting buf to utf16
480     buf = (char *)icu_utf16_from_utf8n((UChar *) buf, 
481                                        (int32_t) buf_cap, &buf_len,
482                                        src8, src8_len);
483     
484     // sort mapping
485     //buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap,
486     //                                     (const UChar *)buf, (int32_t) buf_len,
487     //                                      locale, action);
488     
489     
490     {
491         UErrorCode status = U_ZERO_ERROR;
492
493         UCollator * coll = ucol_open (locale, &status);
494         if (U_ZERO_ERROR != icu_check_status(status))
495             buf_len = 0;
496
497         ucol_getSortKey(coll, (const UChar *) buf, (int32_t) buf_len, 
498                         (uint8_t *) buf, (int32_t) buf_cap);
499         
500         ucol_close(coll);
501     }
502     
503
504     // copying out to nmem
505     buf[buf_len] = '\0';
506
507     if(dest8_len)
508         *dest8_len = buf_len;
509
510     dest8 =  nmem_strdup(nmem, buf);
511     return dest8;
512 }
513
514 #endif
515
516
517
518 #endif // HAVE_ICU    
519
520
521
522
523 /*
524  * Local variables:
525  * c-basic-offset: 4
526  * indent-tabs-mode: nil
527  * End:
528  * vim: shiftwidth=4 tabstop=8 expandtab
529  */