xref: /AOO41X/main/sal/osl/os2/nlsupport.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #define INCL_WIN
29*cdf0e10cSrcweir #include "svpm.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/nlsupport.h>
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include <osl/process.h>
34*cdf0e10cSrcweir #include <rtl/memory.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <string.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /*****************************************************************************
39*cdf0e10cSrcweir  typedefs
40*cdf0e10cSrcweir  *****************************************************************************/
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir typedef struct {
44*cdf0e10cSrcweir     const char              *key;
45*cdf0e10cSrcweir     const rtl_TextEncoding   value;
46*cdf0e10cSrcweir } _pair;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /*****************************************************************************
50*cdf0e10cSrcweir  compare function for binary search
51*cdf0e10cSrcweir  *****************************************************************************/
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir static int
54*cdf0e10cSrcweir _pair_compare (const char *key, const _pair *pair)
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     int result = rtl_str_compareIgnoreAsciiCase( key, pair->key );
57*cdf0e10cSrcweir     return result;
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir /*****************************************************************************
61*cdf0e10cSrcweir  binary search on encoding tables
62*cdf0e10cSrcweir  *****************************************************************************/
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir static const _pair*
65*cdf0e10cSrcweir _pair_search (const char *key, const _pair *base, unsigned int member )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     unsigned int lower = 0;
68*cdf0e10cSrcweir     unsigned int upper = member;
69*cdf0e10cSrcweir     unsigned int current;
70*cdf0e10cSrcweir     int comparison;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /* check for validity of input */
73*cdf0e10cSrcweir     if ( (key == NULL) || (base == NULL) || (member == 0) )
74*cdf0e10cSrcweir         return NULL;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     /* binary search */
77*cdf0e10cSrcweir     while ( lower < upper )
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         current = (lower + upper) / 2;
80*cdf0e10cSrcweir         comparison = _pair_compare( key, base + current );
81*cdf0e10cSrcweir         if (comparison < 0)
82*cdf0e10cSrcweir             upper = current;
83*cdf0e10cSrcweir         else if (comparison > 0)
84*cdf0e10cSrcweir             lower = current + 1;
85*cdf0e10cSrcweir         else
86*cdf0e10cSrcweir             return base + current;
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     return NULL;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir /*****************************************************************************
94*cdf0e10cSrcweir  convert rtl_Locale to locale string
95*cdf0e10cSrcweir  *****************************************************************************/
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir static char * _compose_locale( rtl_Locale * pLocale, char * buffer, size_t n )
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     /* check if a valid locale is specified */
100*cdf0e10cSrcweir     if( pLocale && pLocale->Language && (pLocale->Language->length == 2) )
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         size_t offset = 0;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         /* convert language code to ascii */
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             rtl_String *pLanguage = NULL;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             rtl_uString2String( &pLanguage,
109*cdf0e10cSrcweir                 pLocale->Language->buffer, pLocale->Language->length,
110*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir             if( pLanguage->length < n )
113*cdf0e10cSrcweir             {
114*cdf0e10cSrcweir                 strcpy( buffer, pLanguage->buffer );
115*cdf0e10cSrcweir                 offset = pLanguage->length;
116*cdf0e10cSrcweir             }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             rtl_string_release( pLanguage );
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         /* convert country code to ascii */
122*cdf0e10cSrcweir         if( pLocale->Country && (pLocale->Country->length == 2) )
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             rtl_String *pCountry = NULL;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             rtl_uString2String( &pCountry,
127*cdf0e10cSrcweir                 pLocale->Country->buffer, pLocale->Country->length,
128*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             if( offset + pCountry->length + 1 < n )
131*cdf0e10cSrcweir             {
132*cdf0e10cSrcweir                 strcpy( buffer + offset++, "_" );
133*cdf0e10cSrcweir                 strcpy( buffer + offset, pCountry->buffer );
134*cdf0e10cSrcweir                 offset += pCountry->length;
135*cdf0e10cSrcweir             }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             rtl_string_release( pCountry );
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         /* convert variant to ascii - check if there is enough space for the variant string */
141*cdf0e10cSrcweir         if( pLocale->Variant && pLocale->Variant->length &&
142*cdf0e10cSrcweir             ( pLocale->Variant->length < n - 6 ) )
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             rtl_String *pVariant = NULL;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir             rtl_uString2String( &pVariant,
147*cdf0e10cSrcweir                 pLocale->Variant->buffer, pLocale->Variant->length,
148*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir             if( offset + pVariant->length + 1 < n )
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 strcpy( buffer + offset, pVariant->buffer );
153*cdf0e10cSrcweir                 offset += pVariant->length;
154*cdf0e10cSrcweir             }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir             rtl_string_release( pVariant );
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         return buffer;
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     return NULL;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir /*****************************************************************************
166*cdf0e10cSrcweir  convert locale string to rtl_Locale
167*cdf0e10cSrcweir  *****************************************************************************/
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir static rtl_Locale * _parse_locale( const char * locale )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     static sal_Unicode c_locale[2] = { (sal_Unicode) 'C', 0 };
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     /* check if locale contains a valid string */
174*cdf0e10cSrcweir     if( locale )
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         size_t len = strlen( locale );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         if( len >= 2 )
179*cdf0e10cSrcweir         {
180*cdf0e10cSrcweir             rtl_uString * pLanguage = NULL;
181*cdf0e10cSrcweir             rtl_uString * pCountry  = NULL;
182*cdf0e10cSrcweir             rtl_uString * pVariant  = NULL;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir             size_t offset = 2;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir             /* convert language code to unicode */
187*cdf0e10cSrcweir             rtl_string2UString( &pLanguage, locale, 2, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
188*cdf0e10cSrcweir             OSL_ASSERT(pLanguage != NULL);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             /* convert country code to unicode */
191*cdf0e10cSrcweir             if( len >= 5 && '_' == locale[2] )
192*cdf0e10cSrcweir             {
193*cdf0e10cSrcweir                 rtl_string2UString( &pCountry, locale + 3, 2, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
194*cdf0e10cSrcweir                 OSL_ASSERT(pCountry != NULL);
195*cdf0e10cSrcweir                 offset = 5;
196*cdf0e10cSrcweir             }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir             /* convert variant code to unicode - do not rely on "." as delimiter */
199*cdf0e10cSrcweir             if( len > offset ) {
200*cdf0e10cSrcweir                 rtl_string2UString( &pVariant, locale + offset, len - offset, RTL_TEXTENCODING_ASCII_US, OSTRING_TO_OUSTRING_CVTFLAGS );
201*cdf0e10cSrcweir                 OSL_ASSERT(pVariant != NULL);
202*cdf0e10cSrcweir             }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir             rtl_Locale * ret =  rtl_locale_register( pLanguage->buffer, pCountry ? pCountry->buffer : c_locale + 1, pVariant ? pVariant->buffer : c_locale + 1 );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             if (pVariant) rtl_uString_release(pVariant);
207*cdf0e10cSrcweir             if (pCountry) rtl_uString_release(pCountry);
208*cdf0e10cSrcweir             if (pLanguage) rtl_uString_release(pLanguage);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 			return ret;
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir         else
213*cdf0e10cSrcweir             return rtl_locale_register( c_locale, c_locale + 1, c_locale + 1 );
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     return NULL;
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir /*
220*cdf0e10cSrcweir  * This implementation of osl_getTextEncodingFromLocale maps
221*cdf0e10cSrcweir  * from the ISO language codes.
222*cdf0e10cSrcweir  */
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir const _pair _full_locale_list[] = {
225*cdf0e10cSrcweir     { "ja_JP.eucJP",  RTL_TEXTENCODING_EUC_JP      },
226*cdf0e10cSrcweir     { "ja_JP.EUC",    RTL_TEXTENCODING_EUC_JP      },
227*cdf0e10cSrcweir     { "ko_KR.EUC",    RTL_TEXTENCODING_EUC_KR      },
228*cdf0e10cSrcweir     { "zh_CN.EUC",    RTL_TEXTENCODING_EUC_CN      },
229*cdf0e10cSrcweir     { "zh_TW.EUC",    RTL_TEXTENCODING_EUC_TW      }
230*cdf0e10cSrcweir };
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir const _pair _locale_extension_list[] = {
233*cdf0e10cSrcweir     { "big5",         RTL_TEXTENCODING_BIG5        },
234*cdf0e10cSrcweir     { "big5hk",       RTL_TEXTENCODING_BIG5_HKSCS  },
235*cdf0e10cSrcweir     { "gb18030",      RTL_TEXTENCODING_GB_18030    },
236*cdf0e10cSrcweir     { "euc",          RTL_TEXTENCODING_EUC_JP      },
237*cdf0e10cSrcweir     { "iso8859-1",    RTL_TEXTENCODING_ISO_8859_1  },
238*cdf0e10cSrcweir     { "iso8859-10",   RTL_TEXTENCODING_ISO_8859_10 },
239*cdf0e10cSrcweir     { "iso8859-13",   RTL_TEXTENCODING_ISO_8859_13 },
240*cdf0e10cSrcweir     { "iso8859-14",   RTL_TEXTENCODING_ISO_8859_14 },
241*cdf0e10cSrcweir     { "iso8859-15",   RTL_TEXTENCODING_ISO_8859_15 },
242*cdf0e10cSrcweir     { "iso8859-2",    RTL_TEXTENCODING_ISO_8859_2  },
243*cdf0e10cSrcweir     { "iso8859-3",    RTL_TEXTENCODING_ISO_8859_3  },
244*cdf0e10cSrcweir     { "iso8859-4",    RTL_TEXTENCODING_ISO_8859_4  },
245*cdf0e10cSrcweir     { "iso8859-5",    RTL_TEXTENCODING_ISO_8859_5  },
246*cdf0e10cSrcweir     { "iso8859-6",    RTL_TEXTENCODING_ISO_8859_6  },
247*cdf0e10cSrcweir     { "iso8859-7",    RTL_TEXTENCODING_ISO_8859_7  },
248*cdf0e10cSrcweir     { "iso8859-8",    RTL_TEXTENCODING_ISO_8859_8  },
249*cdf0e10cSrcweir     { "iso8859-9",    RTL_TEXTENCODING_ISO_8859_9  },
250*cdf0e10cSrcweir     { "koi8-r",       RTL_TEXTENCODING_KOI8_R      },
251*cdf0e10cSrcweir     { "koi8-u",       RTL_TEXTENCODING_KOI8_U      },
252*cdf0e10cSrcweir     { "pck",          RTL_TEXTENCODING_MS_932      },
253*cdf0e10cSrcweir #if (0)
254*cdf0e10cSrcweir     { "sun_eu_greek", RTL_TEXTENCODING_DONTKNOW    },
255*cdf0e10cSrcweir #endif
256*cdf0e10cSrcweir     { "utf-16",       RTL_TEXTENCODING_UNICODE     },
257*cdf0e10cSrcweir     { "utf-7",        RTL_TEXTENCODING_UTF7        },
258*cdf0e10cSrcweir     { "utf-8",        RTL_TEXTENCODING_UTF8        }
259*cdf0e10cSrcweir };
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir const _pair _iso_language_list[] = {
262*cdf0e10cSrcweir     { "af",  RTL_TEXTENCODING_ISO_8859_1 },
263*cdf0e10cSrcweir     { "ar",  RTL_TEXTENCODING_ISO_8859_6 },
264*cdf0e10cSrcweir     { "az",  RTL_TEXTENCODING_ISO_8859_9 },
265*cdf0e10cSrcweir     { "be",  RTL_TEXTENCODING_ISO_8859_5 },
266*cdf0e10cSrcweir     { "bg",  RTL_TEXTENCODING_ISO_8859_5 },
267*cdf0e10cSrcweir     { "ca",  RTL_TEXTENCODING_ISO_8859_1 },
268*cdf0e10cSrcweir     { "cs",  RTL_TEXTENCODING_ISO_8859_2 },
269*cdf0e10cSrcweir     { "da",  RTL_TEXTENCODING_ISO_8859_1 },
270*cdf0e10cSrcweir     { "de",  RTL_TEXTENCODING_ISO_8859_1 },
271*cdf0e10cSrcweir     { "el",  RTL_TEXTENCODING_ISO_8859_7 },
272*cdf0e10cSrcweir     { "en",  RTL_TEXTENCODING_ISO_8859_1 },
273*cdf0e10cSrcweir     { "es",  RTL_TEXTENCODING_ISO_8859_1 },
274*cdf0e10cSrcweir     { "et",  RTL_TEXTENCODING_ISO_8859_4 },
275*cdf0e10cSrcweir     { "eu",  RTL_TEXTENCODING_ISO_8859_1 },
276*cdf0e10cSrcweir     { "fa",  RTL_TEXTENCODING_ISO_8859_6 },
277*cdf0e10cSrcweir     { "fi",  RTL_TEXTENCODING_ISO_8859_1 },
278*cdf0e10cSrcweir     { "fo",  RTL_TEXTENCODING_ISO_8859_1 },
279*cdf0e10cSrcweir     { "fr",  RTL_TEXTENCODING_ISO_8859_1 },
280*cdf0e10cSrcweir     { "gr",  RTL_TEXTENCODING_ISO_8859_7 },
281*cdf0e10cSrcweir     { "he",  RTL_TEXTENCODING_ISO_8859_8 },
282*cdf0e10cSrcweir     { "hi",  RTL_TEXTENCODING_DONTKNOW },
283*cdf0e10cSrcweir     { "hr",  RTL_TEXTENCODING_ISO_8859_2 },
284*cdf0e10cSrcweir     { "hu",  RTL_TEXTENCODING_ISO_8859_2 },
285*cdf0e10cSrcweir     { "hy",  RTL_TEXTENCODING_DONTKNOW },
286*cdf0e10cSrcweir     { "id",  RTL_TEXTENCODING_ISO_8859_1 },
287*cdf0e10cSrcweir     { "is",  RTL_TEXTENCODING_ISO_8859_1 },
288*cdf0e10cSrcweir     { "it",  RTL_TEXTENCODING_ISO_8859_1 },
289*cdf0e10cSrcweir     { "iw",  RTL_TEXTENCODING_ISO_8859_8 },
290*cdf0e10cSrcweir     { "ja",  RTL_TEXTENCODING_EUC_JP },
291*cdf0e10cSrcweir     { "ka",  RTL_TEXTENCODING_DONTKNOW },
292*cdf0e10cSrcweir     { "kk",  RTL_TEXTENCODING_ISO_8859_5 },
293*cdf0e10cSrcweir     { "ko",  RTL_TEXTENCODING_EUC_KR },
294*cdf0e10cSrcweir     { "lt",  RTL_TEXTENCODING_ISO_8859_4 },
295*cdf0e10cSrcweir     { "lv",  RTL_TEXTENCODING_ISO_8859_4 },
296*cdf0e10cSrcweir     { "mk",  RTL_TEXTENCODING_ISO_8859_5 },
297*cdf0e10cSrcweir     { "mr",  RTL_TEXTENCODING_DONTKNOW },
298*cdf0e10cSrcweir     { "ms",  RTL_TEXTENCODING_ISO_8859_1 },
299*cdf0e10cSrcweir     { "nl",  RTL_TEXTENCODING_ISO_8859_1 },
300*cdf0e10cSrcweir     { "no",  RTL_TEXTENCODING_ISO_8859_1 },
301*cdf0e10cSrcweir     { "pl",  RTL_TEXTENCODING_ISO_8859_2 },
302*cdf0e10cSrcweir     { "pt",  RTL_TEXTENCODING_ISO_8859_1 },
303*cdf0e10cSrcweir     { "ro",  RTL_TEXTENCODING_ISO_8859_2 },
304*cdf0e10cSrcweir     { "ru",  RTL_TEXTENCODING_ISO_8859_5 },
305*cdf0e10cSrcweir     { "sa",  RTL_TEXTENCODING_DONTKNOW },
306*cdf0e10cSrcweir     { "sk",  RTL_TEXTENCODING_ISO_8859_2 },
307*cdf0e10cSrcweir     { "sl",  RTL_TEXTENCODING_ISO_8859_2 },
308*cdf0e10cSrcweir     { "sq",  RTL_TEXTENCODING_ISO_8859_2 },
309*cdf0e10cSrcweir     { "sv",  RTL_TEXTENCODING_ISO_8859_1 },
310*cdf0e10cSrcweir     { "sw",  RTL_TEXTENCODING_ISO_8859_1 },
311*cdf0e10cSrcweir     { "ta",  RTL_TEXTENCODING_DONTKNOW },
312*cdf0e10cSrcweir     { "th",  RTL_TEXTENCODING_DONTKNOW },
313*cdf0e10cSrcweir     { "tr",  RTL_TEXTENCODING_ISO_8859_9 },
314*cdf0e10cSrcweir     { "tt",  RTL_TEXTENCODING_ISO_8859_5 },
315*cdf0e10cSrcweir     { "uk",  RTL_TEXTENCODING_ISO_8859_5 },
316*cdf0e10cSrcweir     { "ur",  RTL_TEXTENCODING_ISO_8859_6 },
317*cdf0e10cSrcweir     { "uz",  RTL_TEXTENCODING_ISO_8859_9 },
318*cdf0e10cSrcweir     { "vi",  RTL_TEXTENCODING_DONTKNOW },
319*cdf0e10cSrcweir     { "zh",  RTL_TEXTENCODING_BIG5 }
320*cdf0e10cSrcweir };
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir /*****************************************************************************
323*cdf0e10cSrcweir  return the text encoding corresponding to the given locale
324*cdf0e10cSrcweir  *****************************************************************************/
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
327*cdf0e10cSrcweir {
328*cdf0e10cSrcweir     const _pair *language = 0;
329*cdf0e10cSrcweir     char locale_buf[64] = "";
330*cdf0e10cSrcweir     char *cp;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
333*cdf0e10cSrcweir 		"Please contact technical support and report above informations.\n\n",
334*cdf0e10cSrcweir 		"Critical error: osl_getTextEncodingFromLocale",
335*cdf0e10cSrcweir 		0, MB_ERROR | MB_OK | MB_MOVEABLE);
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir     /* default to process locale if pLocale == NULL */
338*cdf0e10cSrcweir     if( NULL == pLocale )
339*cdf0e10cSrcweir         osl_getProcessLocale( &pLocale );
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir     /* convert rtl_Locale to locale string */
342*cdf0e10cSrcweir     if( _compose_locale( pLocale, locale_buf, 64 ) )
343*cdf0e10cSrcweir     {
344*cdf0e10cSrcweir         /* check special handling list (EUC) first */
345*cdf0e10cSrcweir         const unsigned int members = sizeof( _full_locale_list ) / sizeof( _pair );
346*cdf0e10cSrcweir         language = _pair_search( locale_buf, _full_locale_list, members);
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir         if( NULL == language )
349*cdf0e10cSrcweir         {
350*cdf0e10cSrcweir             /*
351*cdf0e10cSrcweir              *  check if there is a charset qualifier at the end of the given locale string
352*cdf0e10cSrcweir              *  e.g. de.ISO8859-15 or de.ISO8859-15@euro which strongly indicates what
353*cdf0e10cSrcweir              *  charset to use
354*cdf0e10cSrcweir              */
355*cdf0e10cSrcweir 		    cp = strrchr( locale_buf, '.' );
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir             if( NULL != cp )
358*cdf0e10cSrcweir             {
359*cdf0e10cSrcweir                 const unsigned int members = sizeof( _locale_extension_list ) / sizeof( _pair );
360*cdf0e10cSrcweir                 language = _pair_search( cp + 1, _locale_extension_list, members);
361*cdf0e10cSrcweir             }
362*cdf0e10cSrcweir         }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir         /* use iso language code to determine the charset */
365*cdf0e10cSrcweir         if( NULL == language )
366*cdf0e10cSrcweir         {
367*cdf0e10cSrcweir             const unsigned int members = sizeof( _iso_language_list ) / sizeof( _pair );
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir             /* iso lang codes have 2 charaters */
370*cdf0e10cSrcweir             locale_buf[2] = '\0';
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir             language = _pair_search( locale_buf, _iso_language_list, members);
373*cdf0e10cSrcweir         }
374*cdf0e10cSrcweir     }
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir     /* a matching item in our list provides a mapping from codeset to
377*cdf0e10cSrcweir      * rtl-codeset */
378*cdf0e10cSrcweir     if ( language != NULL )
379*cdf0e10cSrcweir         return language->value;
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     return RTL_TEXTENCODING_DONTKNOW;
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir /*****************************************************************************
385*cdf0e10cSrcweir  return the current process locale
386*cdf0e10cSrcweir  *****************************************************************************/
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir void _imp_getProcessLocale( rtl_Locale ** ppLocale )
389*cdf0e10cSrcweir {
390*cdf0e10cSrcweir     /* simulate behavior off setlocale */
391*cdf0e10cSrcweir     char * locale = getenv( "LC_ALL" );
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir     if( NULL == locale )
394*cdf0e10cSrcweir         locale = getenv( "LC_CTYPE" );
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir     if( NULL == locale )
397*cdf0e10cSrcweir         locale = getenv( "LANG" );
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir     if( NULL == locale )
400*cdf0e10cSrcweir         locale = "C";
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     *ppLocale = _parse_locale( locale );
403*cdf0e10cSrcweir }
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir /*****************************************************************************
406*cdf0e10cSrcweir  set the current process locale
407*cdf0e10cSrcweir  *****************************************************************************/
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir int _imp_setProcessLocale( rtl_Locale * pLocale )
410*cdf0e10cSrcweir {
411*cdf0e10cSrcweir     char locale_buf[64];
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir     /* convert rtl_Locale to locale string */
414*cdf0e10cSrcweir     if( NULL != _compose_locale( pLocale, locale_buf, 64 ) )
415*cdf0e10cSrcweir     {
416*cdf0e10cSrcweir         /* only change env vars that exist already */
417*cdf0e10cSrcweir         if( getenv( "LC_ALL" ) ) {
418*cdf0e10cSrcweir #if defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX ) || defined( __EMX__ )
419*cdf0e10cSrcweir             setenv( "LC_ALL", locale_buf, 1);
420*cdf0e10cSrcweir #else
421*cdf0e10cSrcweir             setenv( "LC_ALL", locale_buf );
422*cdf0e10cSrcweir #endif
423*cdf0e10cSrcweir         }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir         if( getenv( "LC_CTYPE" ) ) {
426*cdf0e10cSrcweir #if defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX ) || defined( __EMX__ )
427*cdf0e10cSrcweir             setenv("LC_CTYPE", locale_buf, 1 );
428*cdf0e10cSrcweir #else
429*cdf0e10cSrcweir             setenv( "LC_CTYPE", locale_buf );
430*cdf0e10cSrcweir #endif
431*cdf0e10cSrcweir         }
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir         if( getenv( "LANG" ) ) {
434*cdf0e10cSrcweir #if defined( FREEBSD ) || defined( NETBSD ) || defined( MACOSX ) || defined( __EMX__ )
435*cdf0e10cSrcweir             setenv("LC_CTYPE", locale_buf, 1 );
436*cdf0e10cSrcweir #else
437*cdf0e10cSrcweir             setenv( "LANG", locale_buf );
438*cdf0e10cSrcweir #endif
439*cdf0e10cSrcweir         }
440*cdf0e10cSrcweir     }
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir     return 0;
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir 
446