6673270584c5f9c8ae521033bfdb1eda0612bfc7
[pazpar2-moved-to-github.git] / src / icu_bug_2.c
1 // Make command on debian 64 bit testing dist  
2 /*
3 gcc -g -Wall `icu-config --cppflags`  `icu-config --ldflags` -o icu_bug_2 icu_bug_2.c
4 snatched from http://www.icu-project.org/userguide/Collate_API.html 
5 and changed.
6 added a struct icu_termmap such that I actually can see the output
7 */
8
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 #include <unicode/ustring.h>  /* some more string fcns*/
14 #include <unicode/uchar.h>    /* char names           */
15
16
17 //#include <unicode/ustdio.h>
18 //#include <unicode/utypes.h>   /* Basic ICU data types */
19 #include <unicode/ucol.h> 
20 //#include <unicode/ucnv.h>     /* C   Converter API    */
21 //#include <unicode/uloc.h>
22 //#include <unicode/ubrk.h>
23 //#include <unicode/unistr.h> 
24
25
26 #define MAX_KEY_SIZE 256
27
28 struct icu_buf_utf16
29 {
30   UChar * utf16;
31   int32_t utf16_len;
32   int32_t utf16_cap;
33 };
34
35
36 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
37 {
38   struct icu_buf_utf16 * buf16 
39     = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16));
40
41   buf16->utf16 = 0;
42   buf16->utf16_len = 0;
43   buf16->utf16_cap = 0;
44
45   if (capacity > 0){
46     buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
47     buf16->utf16[0] = (UChar) 0;
48     buf16->utf16_cap = capacity;
49   }
50   return buf16;
51 };
52
53
54 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
55                                             size_t capacity)
56 {
57   if (buf16){
58     if (capacity >  0){
59       if (0 == buf16->utf16)
60         buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
61       else
62         buf16->utf16 
63           = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity);
64       buf16->utf16[0] = (UChar) 0;
65       buf16->utf16_len = 0;
66       buf16->utf16_cap = capacity;
67     } 
68     else { 
69       if (buf16->utf16)
70         free(buf16->utf16);
71       buf16->utf16 = 0;
72       buf16->utf16_len = 0;
73       buf16->utf16_cap = 0;
74     }
75   }
76
77   return buf16;
78 };
79
80
81 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
82 {
83   if (buf16){
84     if (buf16->utf16)
85       free(buf16->utf16);
86     free(buf16);
87   }
88 };
89
90
91
92 struct icu_buf_utf8
93 {
94   uint8_t * utf8;
95   int32_t utf8_len;
96   int32_t utf8_cap;
97 };
98
99
100
101 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
102 {
103   struct icu_buf_utf8 * buf8 
104     = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8));
105
106   buf8->utf8 = 0;
107   buf8->utf8_len = 0;
108   buf8->utf8_cap = 0;
109
110   if (capacity > 0){
111     buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
112     buf8->utf8[0] = (uint8_t) 0;
113     buf8->utf8_cap = capacity;
114   }
115   return buf8;
116 };
117
118
119
120 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
121                                           size_t capacity)
122 {
123   if (buf8){
124     if (capacity >  0){
125       if (0 == buf8->utf8)
126         buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
127       else
128         buf8->utf8 
129           = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity);
130       buf8->utf8[0] = (uint8_t) 0;
131       buf8->utf8_len = 0;
132       buf8->utf8_cap = capacity;
133     } 
134     else { 
135       if (buf8->utf8)
136         free(buf8->utf8);
137       buf8->utf8 = 0;
138       buf8->utf8_len = 0;
139       buf8->utf8_cap = 0;
140     }
141   }
142
143   return buf8;
144 };
145
146
147
148 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
149 {
150   if (buf8){
151     if (buf8->utf8)
152       free(buf8->utf8);
153     free(buf8);
154   }
155 };
156
157
158
159 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
160                                struct icu_buf_utf8 * src8,
161                                UErrorCode * status)
162 {
163   int32_t utf16_len = 0;
164   
165   u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
166                 &utf16_len,
167                 (const char *) src8->utf8, src8->utf8_len, status);
168   
169   // check for buffer overflow, resize and retry
170   if (*status == U_BUFFER_OVERFLOW_ERROR
171       //|| dest16->utf16_len > dest16->utf16_cap
172       ){
173     icu_buf_utf16_resize(dest16, utf16_len * 2);
174     *status = U_ZERO_ERROR;
175     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
176                   &utf16_len,
177                   (const char *) src8->utf8, src8->utf8_len, status);
178   }
179
180   if (*status != U_BUFFER_OVERFLOW_ERROR
181       && utf16_len < dest16->utf16_cap)
182     dest16->utf16_len = utf16_len;
183   else {
184     dest16->utf16[0] = (UChar) 0;
185     dest16->utf16_len = 0;
186   }
187   
188   return *status;
189 };
190
191  
192
193 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
194                                     const char * src8cstr,
195                                     UErrorCode * status)
196 {
197   size_t src8cstr_len = 0;
198   int32_t utf16_len = 0;
199
200   src8cstr_len = strlen(src8cstr);
201   
202   u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
203                 &utf16_len,
204                 src8cstr, src8cstr_len, status);
205   
206   // check for buffer overflow, resize and retry
207   if (*status == U_BUFFER_OVERFLOW_ERROR
208       //|| dest16->utf16_len > dest16->utf16_cap
209       ){
210     icu_buf_utf16_resize(dest16, utf16_len * 2);
211     *status = U_ZERO_ERROR;
212     u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
213                   &utf16_len,
214                   src8cstr, src8cstr_len, status);
215   }
216
217   if (*status != U_BUFFER_OVERFLOW_ERROR
218       && utf16_len < dest16->utf16_cap)
219     dest16->utf16_len = utf16_len;
220   else {
221     dest16->utf16[0] = (UChar) 0;
222     dest16->utf16_len = 0;
223   }
224   
225   return *status;
226 };
227
228
229 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
230                                    struct icu_buf_utf8 * dest8, 
231                                    struct icu_buf_utf16 * src16,
232                                    UErrorCode * status)
233
234   
235   int32_t sortkey_len = 0;
236
237   sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
238                                 dest8->utf8, dest8->utf8_cap);
239
240   // check for buffer overflow, resize and retry
241   if (sortkey_len > dest8->utf8_cap) {
242     icu_buf_utf8_resize(dest8, sortkey_len * 2);
243     sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
244                                   dest8->utf8, dest8->utf8_cap);
245   }
246
247   if (sortkey_len > 0)
248     dest8->utf8_len = sortkey_len;
249  
250   return *status;
251 };
252
253  
254
255
256 struct icu_termmap
257 {
258   uint8_t sort_key[MAX_KEY_SIZE]; // standard C string '\0' terminated
259   char disp_term[MAX_KEY_SIZE];  // standard C utf-8 string
260 };
261
262
263
264 int icu_termmap_cmp(const void *vp1, const void *vp2)
265 {
266   struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
267   struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
268
269   int cmp = 0;
270     
271   cmp = strcmp((const char *)itmp1->sort_key, 
272                (const char *)itmp2->sort_key);
273   return cmp;
274 }
275
276
277 int icu_check_status(UErrorCode status)
278 {
279   if(!U_SUCCESS(status))
280     printf("ICU status: %d %s\n", status, u_errorName(status));
281   return status;
282 }
283
284
285
286 int icu_coll_sort(const char * locale, int src_list_len,
287                   const char ** src_list, const char ** chk_list)
288 {
289   UErrorCode status = U_ZERO_ERROR;
290   int success = 1;
291
292   struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
293   struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
294
295   int i;
296
297   struct icu_termmap * list[src_list_len];
298
299   UCollator *coll = ucol_open(locale, &status); 
300   icu_check_status(status);
301
302   if(!U_SUCCESS(status))
303     return 0;
304
305   // assigning display terms and sort keys using buf 8 and buf16
306   for( i = 0; i < src_list_len; i++) 
307     {
308
309       list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
310
311       // copy display term
312       strcpy(list[i]->disp_term, src_list[i]);    
313
314       // transforming to UTF16
315       icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
316       icu_check_status(status);
317
318       // computing sortkeys
319       icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
320       icu_check_status(status);
321     
322       // assigning sortkeys
323       memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);    
324       //strncpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);    
325       //strcpy((char *) list[i]->sort_key, (const char *) buf8->utf8);
326     } 
327
328
329   // do the sorting
330   qsort(list, src_list_len, 
331         sizeof(struct icu_termmap *), icu_termmap_cmp);
332
333   // checking correct sorting
334   for (i = 0; i < src_list_len; i++){
335     if (0 != strcmp(list[i]->disp_term, chk_list[i])){
336       success = 0;
337     }
338   }
339
340   if(!success){
341   printf("\nERROR\n"); 
342   printf("Input str: '%s' : ", locale); 
343   for (i = 0; i < src_list_len; i++) {
344     printf(" '%s'", list[i]->disp_term); 
345   }
346   printf("\n");
347   printf("ICU sort:  '%s' : ", locale); 
348   for (i = 0; i < src_list_len; i++) {
349     printf(" '%s'", list[i]->disp_term); 
350     //printf("(%d|%d)", list[i]->sort_key[0],list[i]->sort_key[1]); 
351   }
352   printf("\n"); 
353   printf("Expected:  '%s' : ", locale); 
354   for (i = 0; i < src_list_len; i++) {
355     printf(" '%s'", chk_list[i]); 
356   }
357   printf("\n"); 
358   }
359   
360   
361   ucol_close(coll);
362
363   icu_buf_utf8_destroy(buf8);
364   icu_buf_utf16_destroy(buf16);
365
366   return success;
367 };
368
369
370 int main(int argc, char **argv)
371 {
372   
373   size_t en_1_len = 6;
374   const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
375   const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
376   icu_coll_sort("en", en_1_len, en_1_src, en_1_cck);
377   icu_coll_sort("en_AU", en_1_len, en_1_src, en_1_cck);
378   icu_coll_sort("en_CA", en_1_len, en_1_src, en_1_cck);
379   icu_coll_sort("en_GB", en_1_len, en_1_src, en_1_cck);
380   icu_coll_sort("en_US", en_1_len, en_1_src, en_1_cck);
381     
382     
383   size_t da_1_len = 6;
384   const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
385   const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
386   icu_coll_sort("da", da_1_len, da_1_src, da_1_cck);
387   icu_coll_sort("da_DK", da_1_len, da_1_src, da_1_cck);
388
389
390   size_t de_1_len = 9;
391   const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
392   const char * de_1_cck[9] = {"a", "ä", "o", "ö", "s", "ß", "t", "u", "ü"};
393   icu_coll_sort("de", de_1_len, de_1_src, de_1_cck);
394   icu_coll_sort("de_AT", de_1_len, de_1_src, de_1_cck);
395   icu_coll_sort("de_DE", de_1_len, de_1_src, de_1_cck);
396
397   return 0;
398 };
399