1 /* $Id: icu_I18N.c,v 1.4 2007-05-02 14:01:36 marc Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
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
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
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
28 #include <yaz/timing.h>
39 #include <unicode/ustring.h> /* some more string fcns*/
40 #include <unicode/uchar.h> /* char names */
43 //#include <unicode/ustdio.h>
44 //#include <unicode/utypes.h> /* Basic ICU data types */
45 #include <unicode/ucol.h>
46 //#include <unicode/ucnv.h> /* C Converter API */
47 //#include <unicode/uloc.h>
48 //#include <unicode/ubrk.h>
49 /* #include <unicode/unistr.h> */
52 // forward declarations for helper functions
54 int icu_check_status (UErrorCode status);
56 UChar* icu_utf16_from_utf8(UChar *utf16,
61 UChar* icu_utf16_from_utf8n(UChar *utf16,
68 char* icu_utf16_to_utf8(char *utf8,
75 int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap,
76 const UChar *src16, int32_t src16_len,
77 const char *locale, char action);
80 // source code of all functions
82 int icu_check_status (UErrorCode status)
84 //if(U_FAILURE(status))
85 if(!U_SUCCESS(status))
87 "ICU: %d %s\n", status, u_errorName(status));
92 UChar* icu_utf16_from_utf8(UChar *utf16,
97 size_t utf8_len = strlen(utf8);
98 return icu_utf16_from_utf8n(utf16, utf16_cap, utf16_len,
103 UChar* icu_utf16_from_utf8n(UChar *utf16,
109 UErrorCode status = U_ZERO_ERROR;
110 u_strFromUTF8(utf16, utf16_cap, utf16_len, utf8, (int32_t) utf8_len,
112 if (U_ZERO_ERROR != icu_check_status(status))
119 char* icu_utf16_to_utf8(char *utf8,
125 UErrorCode status = U_ZERO_ERROR;
126 u_strToUTF8(utf8, (int32_t) utf8_cap, (int32_t *)utf8_len,
127 utf16, utf16_len, &status);
128 if (U_ZERO_ERROR != icu_check_status(status))
135 int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap,
136 const UChar *src16, int32_t src16_len,
137 const char *locale, char action)
139 UErrorCode status = U_ZERO_ERROR;
140 int32_t dest16_len = 0;
144 dest16_len = u_strToLower(dest16, dest16_cap, src16, src16_len,
148 dest16_len = u_strToUpper(dest16, dest16_cap, src16, src16_len,
152 dest16_len = u_strToTitle(dest16, dest16_cap, src16, src16_len,
156 dest16_len = u_strFoldCase(dest16, dest16_cap, src16, src16_len,
157 U_FOLD_CASE_DEFAULT, &status);
165 if (U_ZERO_ERROR != icu_check_status(status))
172 char * icu_casemap(NMEM nmem, char *buf, size_t buf_cap,
173 size_t *dest8_len, const char *src8,
174 const char *locale, char action)
176 size_t src8_len = strlen(src8);
183 if (!buf || !(buf_cap > 0) || !src8_len)
186 // converting buf to utf16
187 buf = (char *)icu_utf16_from_utf8n((UChar *) buf,
188 (int32_t) buf_cap, &buf_len,
192 buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap,
193 (const UChar *)buf, (int32_t) buf_len,
196 // converting buf to utf8
197 buf = icu_utf16_to_utf8(buf, buf_cap, (size_t *) &buf_len,
198 (const UChar *) buf, (int32_t) buf_len);
201 // copying out to nmem
205 *dest8_len = buf_len;
207 dest8 = nmem_strdup(nmem, buf);
212 struct icu_termmap * icu_termmap_create(NMEM nmem)
214 struct icu_termmap *itmp = nmem_malloc(nmem, sizeof(*itmp));
221 int icu_termmap_cmp(const void *vp1, const void *vp2)
223 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
224 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
226 return strcmp(itmp1->sort_key, itmp2->sort_key);
231 char * icu_sortmap(NMEM nmem, char *buf, size_t buf_cap,
232 size_t *dest8_len, const char *src8,
235 size_t src8_len = strlen(src8);
242 if (!buf || !(buf_cap > 0) || !src8_len)
245 // converting buf to utf16
246 buf = (char *)icu_utf16_from_utf8n((UChar *) buf,
247 (int32_t) buf_cap, &buf_len,
251 //buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap,
252 // (const UChar *)buf, (int32_t) buf_len,
257 UErrorCode status = U_ZERO_ERROR;
259 UCollator * coll = ucol_open (locale, &status);
260 if (U_ZERO_ERROR != icu_check_status(status))
263 ucol_getSortKey(coll, (const UChar *) buf, (int32_t) buf_len,
264 (uint8_t *) buf, (int32_t) buf_cap);
270 // copying out to nmem
274 *dest8_len = buf_len;
276 dest8 = nmem_strdup(nmem, buf);
291 * indent-tabs-mode: nil
293 * vim: shiftwidth=4 tabstop=8 expandtab