merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / unx / nlsupport.c
blob245483688365f41419b5ad9c1676518c378f8881
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: nlsupport.c,v $
10 * $Revision: 1.36 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/nlsupport.h>
32 #include <osl/diagnose.h>
33 #include <osl/process.h>
34 #include <rtl/memory.h>
36 #if defined(LINUX) || defined(SOLARIS) || defined(IRIX) || defined(NETBSD) || defined(FREEBSD) || defined(MACOSX)
37 #include <pthread.h>
38 #ifndef MACOSX
39 #include <locale.h>
40 #include <langinfo.h>
41 #else
42 #include <osl/module.h>
43 #include <osl/thread.h>
44 #endif /* !MACOSX */
45 #endif /* LINUX || SOLARIS || IRIX || NETBSD || MACOSX */
47 #include <string.h>
49 /*****************************************************************************
50 typedefs
51 *****************************************************************************/
54 typedef struct {
55 const char *key;
56 const rtl_TextEncoding value;
57 } _pair;
60 /*****************************************************************************
61 compare function for binary search
62 *****************************************************************************/
64 static int
65 _pair_compare (const char *key, const _pair *pair)
67 int result = rtl_str_compareIgnoreAsciiCase( key, pair->key );
68 return result;
71 /*****************************************************************************
72 binary search on encoding tables
73 *****************************************************************************/
75 static const _pair*
76 _pair_search (const char *key, const _pair *base, unsigned int member )
78 unsigned int lower = 0;
79 unsigned int upper = member;
80 unsigned int current;
81 int comparison;
83 /* check for validity of input */
84 if ( (key == NULL) || (base == NULL) || (member == 0) )
85 return NULL;
87 /* binary search */
88 while ( lower < upper )
90 current = (lower + upper) / 2;
91 comparison = _pair_compare( key, base + current );
92 if (comparison < 0)
93 upper = current;
94 else if (comparison > 0)
95 lower = current + 1;
96 else
97 return base + current;
100 return NULL;
104 /*****************************************************************************
105 convert rtl_Locale to locale string
106 *****************************************************************************/
108 static char * _compose_locale( rtl_Locale * pLocale, char * buffer, size_t n )
110 /* check if a valid locale is specified */
111 if( pLocale && pLocale->Language && (pLocale->Language->length == 2) )
113 size_t offset = 0;
115 /* convert language code to ascii */
117 rtl_String *pLanguage = NULL;
119 rtl_uString2String( &pLanguage,
120 pLocale->Language->buffer, pLocale->Language->length,
121 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
123 if( SAL_INT_CAST(sal_uInt32, pLanguage->length) < n )
125 strcpy( buffer, pLanguage->buffer );
126 offset = pLanguage->length;
129 rtl_string_release( pLanguage );
132 /* convert country code to ascii */
133 if( pLocale->Country && (pLocale->Country->length == 2) )
135 rtl_String *pCountry = NULL;
137 rtl_uString2String( &pCountry,
138 pLocale->Country->buffer, pLocale->Country->length,
139 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
141 if( offset + pCountry->length + 1 < n )
143 strcpy( buffer + offset++, "_" );
144 strcpy( buffer + offset, pCountry->buffer );
145 offset += pCountry->length;
148 rtl_string_release( pCountry );
151 /* convert variant to ascii - check if there is enough space for the variant string */
152 if( pLocale->Variant && pLocale->Variant->length &&
153 ( SAL_INT_CAST(sal_uInt32, pLocale->Variant->length) < n - 6 ) )
155 rtl_String *pVariant = NULL;
157 rtl_uString2String( &pVariant,
158 pLocale->Variant->buffer, pLocale->Variant->length,
159 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
161 if( offset + pVariant->length + 1 < n )
163 strcpy( buffer + offset, pVariant->buffer );
164 offset += pVariant->length;
167 rtl_string_release( pVariant );
170 return buffer;
173 return NULL;
176 /*****************************************************************************
177 convert locale string to rtl_Locale
178 *****************************************************************************/
180 static rtl_Locale * _parse_locale( const char * locale )
182 static sal_Unicode c_locale[2] = { (sal_Unicode) 'C', 0 };
184 /* check if locale contains a valid string */
185 if( locale )
187 size_t len = strlen( locale );
189 if( len >= 2 )
191 rtl_uString * pLanguage = NULL;
192 rtl_uString * pCountry = NULL;
193 rtl_uString * pVariant = NULL;
195 size_t offset = 2;
197 rtl_Locale * ret;
199 /* convert language code to unicode */
200 rtl_string2UString( &pLanguage, locale, 2, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
201 OSL_ASSERT(pLanguage != NULL);
203 /* convert country code to unicode */
204 if( len >= 5 && '_' == locale[2] )
206 rtl_string2UString( &pCountry, locale + 3, 2, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
207 OSL_ASSERT(pCountry != NULL);
208 offset = 5;
211 /* convert variant code to unicode - do not rely on "." as delimiter */
212 if( len > offset ) {
213 rtl_string2UString( &pVariant, locale + offset, len - offset, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
214 OSL_ASSERT(pVariant != NULL);
217 ret = rtl_locale_register( pLanguage->buffer, pCountry ? pCountry->buffer : c_locale + 1, pVariant ? pVariant->buffer : c_locale + 1 );
219 if (pVariant) rtl_uString_release(pVariant);
220 if (pCountry) rtl_uString_release(pCountry);
221 if (pLanguage) rtl_uString_release(pLanguage);
223 return ret;
225 else
226 return rtl_locale_register( c_locale, c_locale + 1, c_locale + 1 );
229 return NULL;
232 #if defined(LINUX) || defined(SOLARIS) || defined(IRIX) || defined(NETBSD) || defined(FREEBSD)
235 * This implementation of osl_getTextEncodingFromLocale maps
236 * from nl_langinfo(CODESET) to rtl_textencoding defines.
237 * nl_langinfo() is supported only on Linux and Solaris.
238 * nl_langinfo() is supported only on Linux, Solaris and IRIX,
239 * >= NetBSD 1.6 and >= FreeBSD 4.4
241 * This routine is SLOW because of the setlocale call, so
242 * grab the result and cache it.
244 * XXX this code has the usual mt problems aligned with setlocale() XXX
247 #ifdef LINUX
248 #if !defined(CODESET)
249 #define CODESET _NL_CTYPE_CODESET_NAME
250 #endif
251 #endif
254 * _nl_language_list[] is an array list of supported encodings. Because
255 * we are using a binary search, the list has to be in ascending order.
256 * We are comparing the encodings case insensitiv, so the list has
257 * to be completly upper- , or lowercase.
260 #if defined(SOLARIS)
262 /* The values in the below list can be obtained with a script like
263 * #!/bin/sh
264 * for i in `locale -a`; do
265 * LC_ALL=$i locale -k code_set_name
266 * done
268 const _pair _nl_language_list[] = {
269 { "5601", RTL_TEXTENCODING_EUC_KR }, /* ko_KR.EUC */
270 { "646", RTL_TEXTENCODING_ISO_8859_1 }, /* fake: ASCII_US */
271 { "ANSI-1251", RTL_TEXTENCODING_MS_1251 }, /* ru_RU.ANSI1251 */
272 { "BIG5", RTL_TEXTENCODING_BIG5 }, /* zh_CN.BIG5 */
273 { "BIG5-HKSCS", RTL_TEXTENCODING_BIG5_HKSCS }, /* zh_CN.BIG5HK */
274 { "CNS11643", RTL_TEXTENCODING_EUC_TW }, /* zh_TW.EUC */
275 { "EUCJP", RTL_TEXTENCODING_EUC_JP }, /* ja_JP.eucjp */
276 { "GB18030", RTL_TEXTENCODING_GB_18030 }, /* zh_CN.GB18030 */
277 { "GB2312", RTL_TEXTENCODING_GB_2312 }, /* zh_CN */
278 { "GBK", RTL_TEXTENCODING_GBK }, /* zh_CN.GBK */
279 { "ISO8859-1", RTL_TEXTENCODING_ISO_8859_1 },
280 { "ISO8859-10", RTL_TEXTENCODING_ISO_8859_10 },
281 { "ISO8859-13", RTL_TEXTENCODING_ISO_8859_13 }, /* lt_LT lv_LV */
282 { "ISO8859-14", RTL_TEXTENCODING_ISO_8859_14 },
283 { "ISO8859-15", RTL_TEXTENCODING_ISO_8859_15 },
284 { "ISO8859-2", RTL_TEXTENCODING_ISO_8859_2 },
285 { "ISO8859-3", RTL_TEXTENCODING_ISO_8859_3 },
286 { "ISO8859-4", RTL_TEXTENCODING_ISO_8859_4 },
287 { "ISO8859-5", RTL_TEXTENCODING_ISO_8859_5 },
288 { "ISO8859-6", RTL_TEXTENCODING_ISO_8859_6 },
289 { "ISO8859-7", RTL_TEXTENCODING_ISO_8859_7 },
290 { "ISO8859-8", RTL_TEXTENCODING_ISO_8859_8 },
291 { "ISO8859-9", RTL_TEXTENCODING_ISO_8859_9 },
292 { "KOI8-R", RTL_TEXTENCODING_KOI8_R },
293 { "KOI8-U", RTL_TEXTENCODING_KOI8_U },
294 { "PCK", RTL_TEXTENCODING_MS_932 },
295 { "SUN_EU_GREEK", RTL_TEXTENCODING_ISO_8859_7 }, /* 8859-7 + Euro */
296 { "TIS620.2533", RTL_TEXTENCODING_MS_874 }, /* th_TH.TIS620 */
297 { "UTF-8", RTL_TEXTENCODING_UTF8 }
300 /* XXX MS-874 is an extension to tis620, so this is not
301 * really equivalent */
302 #elif defined(IRIX)
304 const _pair _nl_language_list[] = {
305 { "big5", RTL_TEXTENCODING_BIG5 }, /* China - Traditional Chinese */
306 { "eucCN", RTL_TEXTENCODING_EUC_CN }, /* China */
307 { "eucgbk", RTL_TEXTENCODING_DONTKNOW }, /* China - Simplified Chinese */
308 { "eucJP", RTL_TEXTENCODING_EUC_JP }, /* Japan */
309 { "eucKR", RTL_TEXTENCODING_EUC_KR }, /* Korea */
310 { "eucTW", RTL_TEXTENCODING_EUC_TW }, /* Taiwan - Traditional Chinese */
311 { "gbk", RTL_TEXTENCODING_GBK }, /* China - Simplified Chinese */
312 { "ISO8859-1", RTL_TEXTENCODING_ISO_8859_1 }, /* Western */
313 { "ISO8859-2", RTL_TEXTENCODING_ISO_8859_2 }, /* Central European */
314 { "ISO8859-5", RTL_TEXTENCODING_ISO_8859_5 }, /* Cyrillic */
315 { "ISO8859-7", RTL_TEXTENCODING_ISO_8859_7 }, /* Greek */
316 { "ISO8859-9", RTL_TEXTENCODING_ISO_8859_9 }, /* Turkish */
317 { "ISO8859-15", RTL_TEXTENCODING_ISO_8859_15 }, /* Western Updated (w/Euro sign) */
318 { "sjis", RTL_TEXTENCODING_SHIFT_JIS } /* Japan */
321 #elif defined(LINUX) || defined(NETBSD)
323 const _pair _nl_language_list[] = {
324 { "ANSI_X3.110-1983", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-99 NAPLPS */
325 { "ANSI_X3.4-1968", RTL_TEXTENCODING_ISO_8859_1 }, /* fake: ASCII_US */
326 { "ASMO_449", RTL_TEXTENCODING_DONTKNOW }, /* ISO_9036 ARABIC7 */
327 { "BALTIC", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-179 */
328 { "BIG5", RTL_TEXTENCODING_BIG5 }, /* locale: zh_TW */
329 { "BIG5-HKSCS", RTL_TEXTENCODING_BIG5_HKSCS }, /* locale: zh_CN.BIG5HK */
330 { "BIG5HKSCS", RTL_TEXTENCODING_BIG5_HKSCS }, /* depricated */
331 { "BS_4730", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-4 ISO646-GB */
332 { "BS_VIEWDATA", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-47 */
333 { "CP1250", RTL_TEXTENCODING_MS_1250 }, /* MS-EE */
334 { "CP1251", RTL_TEXTENCODING_MS_1251 }, /* MS-CYRL */
335 { "CP1252", RTL_TEXTENCODING_MS_1252 }, /* MS-ANSI */
336 { "CP1253", RTL_TEXTENCODING_MS_1253 }, /* MS-GREEK */
337 { "CP1254", RTL_TEXTENCODING_MS_1254 }, /* MS-TURK */
338 { "CP1255", RTL_TEXTENCODING_MS_1255 }, /* MS-HEBR */
339 { "CP1256", RTL_TEXTENCODING_MS_1256 }, /* MS-ARAB */
340 { "CP1257", RTL_TEXTENCODING_MS_1257 }, /* WINBALTRIM */
341 { "CSA_Z243.4-1985-1", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-121 */
342 { "CSA_Z243.4-1985-2", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-122 CSA7-2 */
343 { "CSA_Z243.4-1985-GR", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-123 */
344 { "CSN_369103", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-139 */
345 { "CWI", RTL_TEXTENCODING_DONTKNOW }, /* CWI-2 CP-HU */
346 { "DEC-MCS", RTL_TEXTENCODING_DONTKNOW }, /* DEC */
347 { "DIN_66003", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-21 */
348 { "DS_2089", RTL_TEXTENCODING_DONTKNOW }, /* DS2089 ISO646-DK */
349 { "EBCDIC-AT-DE", RTL_TEXTENCODING_DONTKNOW },
350 { "EBCDIC-AT-DE-A", RTL_TEXTENCODING_DONTKNOW },
351 { "EBCDIC-CA-FR", RTL_TEXTENCODING_DONTKNOW },
352 { "EBCDIC-DK-NO", RTL_TEXTENCODING_DONTKNOW },
353 { "EBCDIC-DK-NO-A", RTL_TEXTENCODING_DONTKNOW },
354 { "EBCDIC-ES", RTL_TEXTENCODING_DONTKNOW },
355 { "EBCDIC-ES-A", RTL_TEXTENCODING_DONTKNOW },
356 { "EBCDIC-ES-S", RTL_TEXTENCODING_DONTKNOW },
357 { "EBCDIC-FI-SE", RTL_TEXTENCODING_DONTKNOW },
358 { "EBCDIC-FI-SE-A", RTL_TEXTENCODING_DONTKNOW },
359 { "EBCDIC-FR", RTL_TEXTENCODING_DONTKNOW },
360 { "EBCDIC-IS-FRISS", RTL_TEXTENCODING_DONTKNOW }, /* FRISS */
361 { "EBCDIC-IT", RTL_TEXTENCODING_DONTKNOW },
362 { "EBCDIC-PT", RTL_TEXTENCODING_DONTKNOW },
363 { "EBCDIC-UK", RTL_TEXTENCODING_DONTKNOW },
364 { "EBCDIC-US", RTL_TEXTENCODING_DONTKNOW },
365 { "ECMA-CYRILLIC", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-111 */
366 { "ES", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-17 */
367 { "ES2", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-85 */
368 { "EUC-JP", RTL_TEXTENCODING_EUC_JP }, /* locale: ja_JP.eucjp */
369 { "EUC-KR", RTL_TEXTENCODING_EUC_KR }, /* locale: ko_KR.euckr */
370 { "EUC-TW", RTL_TEXTENCODING_EUC_TW }, /* locale: zh_TW.euctw */
371 { "GB18030", RTL_TEXTENCODING_GB_18030 }, /* locale: zh_CN.gb18030 */
372 { "GB2312", RTL_TEXTENCODING_GB_2312 }, /* locale: zh_CN */
373 { "GB_1988-80", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-57 */
374 { "GBK", RTL_TEXTENCODING_GBK }, /* locale: zh_CN.GBK */
375 { "GOST_19768-74", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-153 */
376 { "GREEK-CCITT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-150 */
377 { "GREEK7", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-88 */
378 { "GREEK7-OLD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-18 */
379 { "HP-ROMAN8", RTL_TEXTENCODING_DONTKNOW }, /* ROMAN8 R8 */
380 { "IBM037", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-[US|CA|WT] */
381 { "IBM038", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-INT CP038 */
382 { "IBM1004", RTL_TEXTENCODING_DONTKNOW }, /* CP1004 OS2LATIN1 */
383 { "IBM1026", RTL_TEXTENCODING_DONTKNOW }, /* CP1026 1026 */
384 { "IBM1047", RTL_TEXTENCODING_DONTKNOW }, /* CP1047 1047 */
385 { "IBM256", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-INT1 */
386 { "IBM273", RTL_TEXTENCODING_DONTKNOW }, /* CP273 */
387 { "IBM274", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-BE CP274 */
388 { "IBM275", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-BR CP275 */
389 { "IBM277", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CP-[DK|NO] */
390 { "IBM278", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CP-[FISE]*/
391 { "IBM280", RTL_TEXTENCODING_DONTKNOW }, /* CP280 EBCDIC-CP-IT*/
392 { "IBM281", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-JP-E CP281 */
393 { "IBM284", RTL_TEXTENCODING_DONTKNOW }, /* CP284 EBCDIC-CP-ES */
394 { "IBM285", RTL_TEXTENCODING_DONTKNOW }, /* CP285 EBCDIC-CP-GB */
395 { "IBM290", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-JP-KANA */
396 { "IBM297", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CP-FR */
397 { "IBM420", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CP-AR1 */
398 { "IBM423", RTL_TEXTENCODING_DONTKNOW }, /* CP423 EBCDIC-CP-GR */
399 { "IBM424", RTL_TEXTENCODING_DONTKNOW }, /* CP424 EBCDIC-CP-HE */
400 { "IBM437", RTL_TEXTENCODING_IBM_437 }, /* CP437 437 */
401 { "IBM500", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CP-[BE|CH] */
402 { "IBM850", RTL_TEXTENCODING_IBM_850 }, /* CP850 850 */
403 { "IBM851", RTL_TEXTENCODING_DONTKNOW }, /* CP851 851 */
404 { "IBM852", RTL_TEXTENCODING_IBM_852 }, /* CP852 852 */
405 { "IBM855", RTL_TEXTENCODING_IBM_855 }, /* CP855 855 */
406 { "IBM857", RTL_TEXTENCODING_IBM_857 }, /* CP857 857 */
407 { "IBM860", RTL_TEXTENCODING_IBM_860 }, /* CP860 860 */
408 { "IBM861", RTL_TEXTENCODING_IBM_861 }, /* CP861 861 CP-IS */
409 { "IBM862", RTL_TEXTENCODING_IBM_862 }, /* CP862 862 */
410 { "IBM863", RTL_TEXTENCODING_IBM_863 }, /* CP863 863 */
411 { "IBM864", RTL_TEXTENCODING_IBM_864 }, /* CP864 */
412 { "IBM865", RTL_TEXTENCODING_IBM_865 }, /* CP865 865 */
413 { "IBM866", RTL_TEXTENCODING_IBM_866 }, /* CP866 866 */
414 { "IBM868", RTL_TEXTENCODING_DONTKNOW }, /* CP868 CP-AR */
415 { "IBM869", RTL_TEXTENCODING_IBM_869 }, /* CP869 869 CP-GR */
416 { "IBM870", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-[ROECE|YU] */
417 { "IBM871", RTL_TEXTENCODING_DONTKNOW }, /* CP871 EBCDIC-CP-IS */
418 { "IBM875", RTL_TEXTENCODING_DONTKNOW }, /* CP875 EBCDIC-GREEK */
419 { "IBM880", RTL_TEXTENCODING_DONTKNOW }, /* EBCDIC-CYRILLIC */
420 { "IBM891", RTL_TEXTENCODING_DONTKNOW }, /* CP891 */
421 { "IBM903", RTL_TEXTENCODING_DONTKNOW }, /* CP903 */
422 { "IBM904", RTL_TEXTENCODING_DONTKNOW }, /* CP904 904 */
423 { "IBM905", RTL_TEXTENCODING_DONTKNOW }, /* CP905 EBCDIC-CP-TR */
424 { "IBM918", RTL_TEXTENCODING_DONTKNOW }, /* CP918 EBCDIC-AR2 */
425 { "IEC_P27-1", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-143 */
426 { "INIS", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-49 */
427 { "INIS-8", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-50 */
428 { "INIS-CYRILLIC", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-51 */
429 { "INVARIANT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-170 */
430 { "ISO-8859-1", RTL_TEXTENCODING_ISO_8859_1 }, /* ISO-IR-100 CP819 */
431 { "ISO-8859-10", RTL_TEXTENCODING_ISO_8859_10 }, /* ISO-IR-157 LATIN6 */
432 { "ISO-8859-13", RTL_TEXTENCODING_ISO_8859_13 }, /* ISO-IR-179 LATIN7 */
433 { "ISO-8859-14", RTL_TEXTENCODING_ISO_8859_14 }, /* LATIN8 L8 */
434 { "ISO-8859-15", RTL_TEXTENCODING_ISO_8859_15 },
435 { "ISO-8859-2", RTL_TEXTENCODING_ISO_8859_2 }, /* LATIN2 L2 */
436 { "ISO-8859-3", RTL_TEXTENCODING_ISO_8859_3 }, /* LATIN3 L3 */
437 { "ISO-8859-4", RTL_TEXTENCODING_ISO_8859_4 }, /* LATIN4 L4 */
438 { "ISO-8859-5", RTL_TEXTENCODING_ISO_8859_5 }, /* CYRILLIC */
439 { "ISO-8859-6", RTL_TEXTENCODING_ISO_8859_6 }, /* ECMA-114 ARABIC */
440 { "ISO-8859-7", RTL_TEXTENCODING_ISO_8859_7 }, /* ECMA-118 GREEK8 */
441 { "ISO-8859-8", RTL_TEXTENCODING_ISO_8859_8 }, /* ISO_8859-8 HEBREW */
442 { "ISO-8859-9", RTL_TEXTENCODING_ISO_8859_9 }, /* ISO_8859-9 LATIN5 */
443 { "ISO-IR-90", RTL_TEXTENCODING_DONTKNOW }, /* ISO_6937-2:1983 */
444 { "ISO_10367-BOX", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-155 */
445 { "ISO_2033-1983", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-98 E13B */
446 { "ISO_5427", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-37 KOI-7 */
447 { "ISO_5427-EXT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-54 */
448 { "ISO_5428", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-55 */
449 { "ISO_646.BASIC", RTL_TEXTENCODING_ASCII_US }, /* REF */
450 { "ISO_646.IRV", RTL_TEXTENCODING_ASCII_US }, /* ISO-IR-2 IRV */
451 { "ISO_646.IRV:1983", RTL_TEXTENCODING_ISO_8859_1 }, /* fake: ASCII_US, used for "C" locale*/
452 { "ISO_6937", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-156 ISO6937*/
453 { "ISO_6937-2-25", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-152 */
454 { "ISO_6937-2-ADD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-142 */
455 { "ISO_8859-SUPP", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-154 */
456 { "IT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-15 */
457 { "JIS_C6220-1969-JP", RTL_TEXTENCODING_DONTKNOW }, /* KATAKANA X0201-7 */
458 { "JIS_C6220-1969-RO", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-14 */
459 { "JIS_C6229-1984-A", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-91 */
460 { "JIS_C6229-1984-B", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-92 */
461 { "JIS_C6229-1984-B-ADD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-93 */
462 { "JIS_C6229-1984-HAND", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-94 */
463 { "JIS_C6229-1984-HAND-ADD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-95 */
464 { "JIS_C6229-1984-KANA", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-96 */
465 { "JIS_X0201", RTL_TEXTENCODING_DONTKNOW }, /* X0201 */
466 { "JUS_I.B1.002", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-141 */
467 { "JUS_I.B1.003-MAC", RTL_TEXTENCODING_DONTKNOW }, /* MACEDONIAN */
468 { "JUS_I.B1.003-SERB", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-146 SERBIAN */
469 { "KOI-8", RTL_TEXTENCODING_DONTKNOW },
470 { "KOI8-R", RTL_TEXTENCODING_KOI8_R },
471 { "KOI8-U", RTL_TEXTENCODING_KOI8_U },
472 { "KSC5636", RTL_TEXTENCODING_DONTKNOW }, /* ISO646-KR */
473 { "LATIN-GREEK", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-19 */
474 { "LATIN-GREEK-1", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-27 */
475 { "MAC-IS", RTL_TEXTENCODING_APPLE_ROMAN },
476 { "MAC-UK", RTL_TEXTENCODING_APPLE_ROMAN },
477 { "MACINTOSH", RTL_TEXTENCODING_APPLE_ROMAN }, /* MAC */
478 { "MSZ_7795.3", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-86 */
479 { "NATS-DANO", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-9-1 */
480 { "NATS-DANO-ADD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-9-2 */
481 { "NATS-SEFI", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-8-1 */
482 { "NATS-SEFI-ADD", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-8-2 */
483 { "NC_NC00-10", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-151 */
484 { "NEXTSTEP", RTL_TEXTENCODING_DONTKNOW }, /* NEXT */
485 { "NF_Z_62-010", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-69 */
486 { "NF_Z_62-010_(1973)", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-25 */
487 { "NS_4551-1", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-60 */
488 { "NS_4551-2", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-61 */
489 { "PT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-16 */
490 { "PT2", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-84 */
491 { "SAMI", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-158 */
492 { "SEN_850200_B", RTL_TEXTENCODING_DONTKNOW }, /* ISO646-[FI|SE] */
493 { "SEN_850200_C", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-11 */
494 { "T.101-G2", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-128 */
495 { "T.61-7BIT", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-102 */
496 { "T.61-8BIT", RTL_TEXTENCODING_DONTKNOW }, /* T.61 ISO-IR-103 */
497 { "TIS-620", RTL_TEXTENCODING_MS_874 }, /* locale: th_TH */
498 { "UTF-8", RTL_TEXTENCODING_UTF8 }, /* ISO-10646/UTF-8 */
499 { "VIDEOTEX-SUPPL", RTL_TEXTENCODING_DONTKNOW }, /* ISO-IR-70 */
500 { "WIN-SAMI-2", RTL_TEXTENCODING_DONTKNOW } /* WS2 */
503 #elif defined(FREEBSD)
505 const _pair _nl_language_list[] = {
506 { "ASCII", RTL_TEXTENCODING_ASCII_US }, /* US-ASCII */
507 { "BIG5", RTL_TEXTENCODING_BIG5 }, /* China - Traditional Chinese */
508 { "CP1251", RTL_TEXTENCODING_MS_1251 }, /* MS-CYRL */
509 { "CP866", RTL_TEXTENCODING_IBM_866 }, /* CP866 866 */
510 { "EUCCN", RTL_TEXTENCODING_EUC_CN }, /* China - Simplified Chinese */
511 { "EUCJP", RTL_TEXTENCODING_EUC_JP }, /* Japan */
512 { "EUCKR", RTL_TEXTENCODING_EUC_KR }, /* Korea */
513 { "ISO8859-1", RTL_TEXTENCODING_ISO_8859_1 }, /* Western */
514 { "ISO8859-15", RTL_TEXTENCODING_ISO_8859_15 }, /* Western Updated (w/Euro sign) */
515 { "ISO8859-2", RTL_TEXTENCODING_ISO_8859_2 }, /* Central European */
516 { "ISO8859-4", RTL_TEXTENCODING_ISO_8859_4 }, /* LATIN4 L4 */
517 { "ISO8859-5", RTL_TEXTENCODING_ISO_8859_5 }, /* Cyrillic */
518 { "ISO8859-7", RTL_TEXTENCODING_ISO_8859_7 }, /* Greek */
519 { "ISO8859-9", RTL_TEXTENCODING_ISO_8859_9 }, /* Turkish */
520 { "KOI8-R", RTL_TEXTENCODING_KOI8_R }, /* KOI8-R */
521 { "KOI8-U", RTL_TEXTENCODING_KOI8_U }, /* KOI8-U */
522 { "SJIS", RTL_TEXTENCODING_SHIFT_JIS }, /* Japan */
523 { "US-ASCII", RTL_TEXTENCODING_ASCII_US }, /* US-ASCII */
524 { "UTF-8", RTL_TEXTENCODING_UTF8 } /* ISO-10646/UTF-8 */
527 #elif defined(NETBSD)
529 const _pair _nl_language_list[] = {
530 { "ASCII", RTL_TEXTENCODING_ASCII_US }, /* US-ASCII */
531 { "BIG5", RTL_TEXTENCODING_BIG5 }, /* China - Traditional Chinese */
532 { "CP1251", RTL_TEXTENCODING_MS_1251 }, /* MS-CYRL */
533 { "CP866", RTL_TEXTENCODING_IBM_866 }, /* CP866 866 */
534 { "CTEXT", RTL_TEXTENCODING_ASCII_US }, /* US-ASCII */
535 { "EUCCN", RTL_TEXTENCODING_EUC_CN }, /* China - Simplified Chinese */
536 { "EUCJP", RTL_TEXTENCODING_EUC_JP }, /* Japan */
537 { "EUCKR", RTL_TEXTENCODING_EUC_KR }, /* Korea */
538 { "EUCTW", RTL_TEXTENCODING_EUC_TW }, /* China - Traditional Chinese */
539 { "ISO-2022-JP", RTL_TEXTENCODING_DONTKNOW }, /* */
540 { "ISO-2022-JP-2", RTL_TEXTENCODING_DONTKNOW }, /* */
541 { "ISO8859-1", RTL_TEXTENCODING_ISO_8859_1 }, /* Western */
542 { "ISO8859-15", RTL_TEXTENCODING_ISO_8859_15 }, /* Western Updated (w/Euro sign) */
543 { "ISO8859-2", RTL_TEXTENCODING_ISO_8859_2 }, /* Central European */
544 { "ISO8859-4", RTL_TEXTENCODING_ISO_8859_4 }, /* LATIN4 L4 */
545 { "ISO8859-5", RTL_TEXTENCODING_ISO_8859_5 }, /* Cyrillic */
546 { "ISO8859-7", RTL_TEXTENCODING_ISO_8859_7 }, /* Greek */
547 { "ISO8859-9", RTL_TEXTENCODING_ISO_8859_9 }, /* Turkish */
548 { "KOI8-R", RTL_TEXTENCODING_KOI8_R }, /* KOI8-R */
549 { "KOI8-U", RTL_TEXTENCODING_KOI8_U }, /* KOI8-U */
550 { "SJIS", RTL_TEXTENCODING_SHIFT_JIS }, /* Japan */
551 { "US-ASCII", RTL_TEXTENCODING_ASCII_US }, /* US-ASCII */
552 { "UTF-8", RTL_TEXTENCODING_UTF8 } /* ISO-10646/UTF-8 */
555 #endif /* ifdef SOLARIS IRIX LINUX FREEBSD NETBSD */
557 static pthread_mutex_t aLocalMutex = PTHREAD_MUTEX_INITIALIZER;
559 /*****************************************************************************
560 return the text encoding corresponding to the given locale
561 *****************************************************************************/
563 rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
565 const _pair *language=0;
567 char locale_buf[64] = "";
568 char codeset_buf[64];
570 char *ctype_locale = 0;
571 char *codeset = 0;
573 /* default to process locale if pLocale == NULL */
574 if( NULL == pLocale )
575 osl_getProcessLocale( &pLocale );
577 /* convert rtl_Locale to locale string */
578 _compose_locale( pLocale, locale_buf, 64 );
580 /* basic thread safeness */
581 pthread_mutex_lock( &aLocalMutex );
583 /* remember the charset as indicated by the LC_CTYPE locale */
584 ctype_locale = setlocale( LC_CTYPE, NULL );
586 /* set the desired LC_CTYPE locale */
587 if( NULL == setlocale( LC_CTYPE, locale_buf ) )
589 pthread_mutex_unlock(&aLocalMutex);
590 return RTL_TEXTENCODING_DONTKNOW;
593 /* get the charset as indicated by the LC_CTYPE locale */
594 #if defined(NETBSD) && !defined(CODESET)
595 codeset = NULL;
596 #else
597 codeset = nl_langinfo( CODESET );
598 #endif
600 if ( codeset != NULL )
602 /* get codeset into mt save memory */
603 strncpy( codeset_buf, codeset, sizeof(codeset_buf) );
604 codeset_buf[sizeof(codeset_buf) - 1] = 0;
605 codeset = codeset_buf;
608 /* restore the original value of locale */
609 if ( ctype_locale != NULL )
610 setlocale( LC_CTYPE, ctype_locale );
612 pthread_mutex_unlock( &aLocalMutex );
614 /* search the codeset in our language list */
615 if ( codeset != NULL )
617 const unsigned int members = sizeof(_nl_language_list) / sizeof(_pair);
618 language = _pair_search (codeset, _nl_language_list, members);
621 OSL_ASSERT( language && ( RTL_TEXTENCODING_DONTKNOW != language->value ) );
623 /* a matching item in our list provides a mapping from codeset to
624 * rtl-codeset */
625 if ( language != NULL )
626 return language->value;
628 return RTL_TEXTENCODING_DONTKNOW;
631 /*****************************************************************************
632 return the current process locale
633 *****************************************************************************/
635 void _imp_getProcessLocale( rtl_Locale ** ppLocale )
637 char * locale;
639 /* basic thread safeness */
640 pthread_mutex_lock( &aLocalMutex );
642 /* set the locale defined by the env vars */
643 locale = setlocale( LC_CTYPE, "" );
645 /* fallback to the current locale */
646 if( NULL == locale )
647 locale = setlocale( LC_CTYPE, NULL );
649 /* return the LC_CTYPE locale */
650 *ppLocale = _parse_locale( locale );
652 pthread_mutex_unlock( &aLocalMutex );
655 /*****************************************************************************
656 set the current process locale
657 *****************************************************************************/
659 int _imp_setProcessLocale( rtl_Locale * pLocale )
661 char locale_buf[64] = "";
662 int ret = 0;
664 /* convert rtl_Locale to locale string */
665 _compose_locale( pLocale, locale_buf, 64 );
667 /* basic thread safeness */
668 pthread_mutex_lock( &aLocalMutex );
670 /* try to set LC_ALL locale */
671 if( NULL == setlocale( LC_ALL, locale_buf ) )
672 ret = -1;
674 pthread_mutex_unlock( &aLocalMutex );
675 return ret;
678 #else /* ifdef LINUX || SOLARIS || MACOSX || NETBSD */
681 * This implementation of osl_getTextEncodingFromLocale maps
682 * from the ISO language codes.
685 const _pair _full_locale_list[] = {
686 { "ja_JP.eucJP", RTL_TEXTENCODING_EUC_JP },
687 { "ja_JP.EUC", RTL_TEXTENCODING_EUC_JP },
688 { "ko_KR.EUC", RTL_TEXTENCODING_EUC_KR },
689 { "zh_CN.EUC", RTL_TEXTENCODING_EUC_CN },
690 { "zh_TW.EUC", RTL_TEXTENCODING_EUC_TW }
693 const _pair _locale_extension_list[] = {
694 { "big5", RTL_TEXTENCODING_BIG5 },
695 { "big5hk", RTL_TEXTENCODING_BIG5_HKSCS },
696 { "gb18030", RTL_TEXTENCODING_GB_18030 },
697 { "euc", RTL_TEXTENCODING_EUC_JP },
698 { "iso8859-1", RTL_TEXTENCODING_ISO_8859_1 },
699 { "iso8859-10", RTL_TEXTENCODING_ISO_8859_10 },
700 { "iso8859-13", RTL_TEXTENCODING_ISO_8859_13 },
701 { "iso8859-14", RTL_TEXTENCODING_ISO_8859_14 },
702 { "iso8859-15", RTL_TEXTENCODING_ISO_8859_15 },
703 { "iso8859-2", RTL_TEXTENCODING_ISO_8859_2 },
704 { "iso8859-3", RTL_TEXTENCODING_ISO_8859_3 },
705 { "iso8859-4", RTL_TEXTENCODING_ISO_8859_4 },
706 { "iso8859-5", RTL_TEXTENCODING_ISO_8859_5 },
707 { "iso8859-6", RTL_TEXTENCODING_ISO_8859_6 },
708 { "iso8859-7", RTL_TEXTENCODING_ISO_8859_7 },
709 { "iso8859-8", RTL_TEXTENCODING_ISO_8859_8 },
710 { "iso8859-9", RTL_TEXTENCODING_ISO_8859_9 },
711 { "koi8-r", RTL_TEXTENCODING_KOI8_R },
712 { "koi8-u", RTL_TEXTENCODING_KOI8_U },
713 { "pck", RTL_TEXTENCODING_MS_932 },
714 #if (0)
715 { "sun_eu_greek", RTL_TEXTENCODING_DONTKNOW },
716 #endif
717 { "utf-16", RTL_TEXTENCODING_UNICODE },
718 { "utf-7", RTL_TEXTENCODING_UTF7 },
719 { "utf-8", RTL_TEXTENCODING_UTF8 }
722 const _pair _iso_language_list[] = {
723 { "af", RTL_TEXTENCODING_ISO_8859_1 },
724 { "ar", RTL_TEXTENCODING_ISO_8859_6 },
725 { "az", RTL_TEXTENCODING_ISO_8859_9 },
726 { "be", RTL_TEXTENCODING_ISO_8859_5 },
727 { "bg", RTL_TEXTENCODING_ISO_8859_5 },
728 { "ca", RTL_TEXTENCODING_ISO_8859_1 },
729 { "cs", RTL_TEXTENCODING_ISO_8859_2 },
730 { "da", RTL_TEXTENCODING_ISO_8859_1 },
731 { "de", RTL_TEXTENCODING_ISO_8859_1 },
732 { "el", RTL_TEXTENCODING_ISO_8859_7 },
733 { "en", RTL_TEXTENCODING_ISO_8859_1 },
734 { "es", RTL_TEXTENCODING_ISO_8859_1 },
735 { "et", RTL_TEXTENCODING_ISO_8859_4 },
736 { "eu", RTL_TEXTENCODING_ISO_8859_1 },
737 { "fa", RTL_TEXTENCODING_ISO_8859_6 },
738 { "fi", RTL_TEXTENCODING_ISO_8859_1 },
739 { "fo", RTL_TEXTENCODING_ISO_8859_1 },
740 { "fr", RTL_TEXTENCODING_ISO_8859_1 },
741 { "gr", RTL_TEXTENCODING_ISO_8859_7 },
742 { "he", RTL_TEXTENCODING_ISO_8859_8 },
743 { "hi", RTL_TEXTENCODING_DONTKNOW },
744 { "hr", RTL_TEXTENCODING_ISO_8859_2 },
745 { "hu", RTL_TEXTENCODING_ISO_8859_2 },
746 { "hy", RTL_TEXTENCODING_DONTKNOW },
747 { "id", RTL_TEXTENCODING_ISO_8859_1 },
748 { "is", RTL_TEXTENCODING_ISO_8859_1 },
749 { "it", RTL_TEXTENCODING_ISO_8859_1 },
750 { "iw", RTL_TEXTENCODING_ISO_8859_8 },
751 { "ja", RTL_TEXTENCODING_EUC_JP },
752 { "ka", RTL_TEXTENCODING_DONTKNOW },
753 { "kk", RTL_TEXTENCODING_ISO_8859_5 },
754 { "ko", RTL_TEXTENCODING_EUC_KR },
755 { "lt", RTL_TEXTENCODING_ISO_8859_4 },
756 { "lv", RTL_TEXTENCODING_ISO_8859_4 },
757 { "mk", RTL_TEXTENCODING_ISO_8859_5 },
758 { "mr", RTL_TEXTENCODING_DONTKNOW },
759 { "ms", RTL_TEXTENCODING_ISO_8859_1 },
760 { "nl", RTL_TEXTENCODING_ISO_8859_1 },
761 { "no", RTL_TEXTENCODING_ISO_8859_1 },
762 { "pl", RTL_TEXTENCODING_ISO_8859_2 },
763 { "pt", RTL_TEXTENCODING_ISO_8859_1 },
764 { "ro", RTL_TEXTENCODING_ISO_8859_2 },
765 { "ru", RTL_TEXTENCODING_ISO_8859_5 },
766 { "sa", RTL_TEXTENCODING_DONTKNOW },
767 { "sk", RTL_TEXTENCODING_ISO_8859_2 },
768 { "sl", RTL_TEXTENCODING_ISO_8859_2 },
769 { "sq", RTL_TEXTENCODING_ISO_8859_2 },
770 { "sv", RTL_TEXTENCODING_ISO_8859_1 },
771 { "sw", RTL_TEXTENCODING_ISO_8859_1 },
772 { "ta", RTL_TEXTENCODING_DONTKNOW },
773 { "th", RTL_TEXTENCODING_DONTKNOW },
774 { "tr", RTL_TEXTENCODING_ISO_8859_9 },
775 { "tt", RTL_TEXTENCODING_ISO_8859_5 },
776 { "uk", RTL_TEXTENCODING_ISO_8859_5 },
777 { "ur", RTL_TEXTENCODING_ISO_8859_6 },
778 { "uz", RTL_TEXTENCODING_ISO_8859_9 },
779 { "vi", RTL_TEXTENCODING_DONTKNOW },
780 { "zh", RTL_TEXTENCODING_BIG5 }
783 /*****************************************************************************
784 return the text encoding corresponding to the given locale
785 *****************************************************************************/
787 rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
789 const _pair *language = 0;
790 char locale_buf[64] = "";
791 char *cp;
793 /* default to process locale if pLocale == NULL */
794 if( NULL == pLocale )
795 osl_getProcessLocale( &pLocale );
797 /* convert rtl_Locale to locale string */
798 if( _compose_locale( pLocale, locale_buf, 64 ) )
800 /* check special handling list (EUC) first */
801 const unsigned int members = sizeof( _full_locale_list ) / sizeof( _pair );
802 language = _pair_search( locale_buf, _full_locale_list, members);
804 if( NULL == language )
807 * check if there is a charset qualifier at the end of the given locale string
808 * e.g. de.ISO8859-15 or de.ISO8859-15@euro which strongly indicates what
809 * charset to use
811 cp = strrchr( locale_buf, '.' );
813 if( NULL != cp )
815 const unsigned int members = sizeof( _locale_extension_list ) / sizeof( _pair );
816 language = _pair_search( cp + 1, _locale_extension_list, members);
820 /* use iso language code to determine the charset */
821 if( NULL == language )
823 const unsigned int members = sizeof( _iso_language_list ) / sizeof( _pair );
825 /* iso lang codes have 2 charaters */
826 locale_buf[2] = '\0';
828 language = _pair_search( locale_buf, _iso_language_list, members);
832 /* a matching item in our list provides a mapping from codeset to
833 * rtl-codeset */
834 if ( language != NULL )
835 return language->value;
837 return RTL_TEXTENCODING_DONTKNOW;
840 #ifdef MACOSX
841 #include "system.h"
843 /* OS X locale discovery function */
844 int (*pGetOSXLocale)( char *, sal_uInt32 );
846 /*****************************************************************************
847 return the current process locale
848 *****************************************************************************/
850 int macosx_getLocale(char *locale, sal_uInt32 bufferLen);
852 void _imp_getProcessLocale( rtl_Locale ** ppLocale )
854 static char *locale = NULL;
856 /* basic thread safeness */
857 // pthread_mutex_lock( &aLocalMutex );
859 /* Only fetch the locale once and cache it */
860 if ( NULL == locale )
863 locale = (char *)malloc( 20 );
864 if ( locale )
865 macosx_getLocale( locale, 20 );
866 else
867 fprintf( stderr, "nlsupport.c: locale allocation returned NULL!\n" );
870 /* handle the case where OS specific method of finding locale fails */
871 if ( NULL == locale )
873 /* simulate behavior of setlocale */
874 locale = getenv( "LC_ALL" );
876 if( NULL == locale )
877 locale = getenv( "LC_CTYPE" );
879 if( NULL == locale )
880 locale = getenv( "LANG" );
882 if( NULL == locale )
883 locale = "C";
886 /* return the locale */
887 *ppLocale = _parse_locale( locale );
889 setenv( "LC_ALL", locale, 1);
890 setenv("LC_CTYPE", locale, 1 );
891 setenv("LANG", locale, 1 );
893 #ifdef DEBUG
894 fprintf( stderr, "nlsupport.c: _imp_getProcessLocale() returning %s as current locale.\n", locale );
895 #endif
897 // pthread_mutex_unlock( &aLocalMutex );
900 #else
901 /*****************************************************************************
902 return the current process locale
903 *****************************************************************************/
905 void _imp_getProcessLocale( rtl_Locale ** ppLocale )
907 /* simulate behavior off setlocale */
908 char * locale = getenv( "LC_ALL" );
910 if( NULL == locale )
911 locale = getenv( "LC_CTYPE" );
913 if( NULL == locale )
914 locale = getenv( "LANG" );
916 if( NULL == locale )
917 locale = "C";
919 *ppLocale = _parse_locale( locale );
921 #endif
923 /*****************************************************************************
924 set the current process locale
925 *****************************************************************************/
927 int _imp_setProcessLocale( rtl_Locale * pLocale )
929 #ifdef IRIX
930 char env_buf[80];
931 #endif
932 char locale_buf[64];
934 /* convert rtl_Locale to locale string */
935 if( NULL != _compose_locale( pLocale, locale_buf, 64 ) )
937 /* only change env vars that exist already */
938 if( getenv( "LC_ALL" ) ) {
939 #if defined( IRIX )
940 snprintf(env_buf, sizeof(env_buf), "LC_ALL=%s", locale_buf);
941 env_buf[sizeof(env_buf)] = '\0';
942 putenv(env_buf);
943 #elif defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX )
944 setenv( "LC_ALL", locale_buf, 1);
945 #else
946 setenv( "LC_ALL", locale_buf );
947 #endif
950 if( getenv( "LC_CTYPE" ) ) {
951 #if defined( IRIX )
952 snprintf(env_buf, sizeof(env_buf), "LC_CTYPE=%s", locale_buf);
953 env_buf[sizeof(env_buf)] = '\0';
954 putenv(env_buf);
955 #elif defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX )
956 setenv("LC_CTYPE", locale_buf, 1 );
957 #else
958 setenv( "LC_CTYPE", locale_buf );
959 #endif
962 if( getenv( "LANG" ) ) {
963 #if defined( IRIX )
964 snprintf(env_buf, sizeof(env_buf), "LANG=%s", locale_buf);
965 env_buf[sizeof(env_buf)] = '\0';
966 putenv(env_buf);
967 #elif defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX )
968 setenv("LC_CTYPE", locale_buf, 1 );
969 #else
970 setenv( "LANG", locale_buf );
971 #endif
975 return 0;
978 #endif /* ifdef LINUX || SOLARIS || MACOSX || NETBSD */