1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #pragma warning(push,1) /* disable warnings within system headers */
30 #include <osl/mutex.h>
31 #include <osl/nlsupport.h>
32 #include <osl/diagnose.h>
33 #include <osl/process.h>
34 #include <rtl/tencinfo.h>
38 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd373848.aspx
39 * (retrieved 2013-02-13) has some weird description for the LOCALE_SISO*
40 * constants: "The maximum number of characters allowed for this string is
41 * nine, including a terminating null character." NINE?!? In ISO 639 and ISO
44 #define ELP_LANGUAGE_FIELD_LENGTH 4
45 #define ELP_COUNTRY_FIELD_LENGTH 3
47 /** Struct used in EnumLocalesProcA() called via EnumSystemLocalesA() to obtain
50 struct EnumLocalesParams
52 WCHAR Language
[ELP_LANGUAGE_FIELD_LENGTH
];
53 WCHAR Country
[ELP_COUNTRY_FIELD_LENGTH
];
57 static DWORD g_dwTLSLocaleEncId
= (DWORD
) -1;
59 /*****************************************************************************
60 * callback function test
62 * osl_getTextEncodingFromLocale calls EnumSystemLocalesA, so that we don't
63 * need to provide a unicode wrapper for this function under Win9x
64 * that means the callback function has an ansi prototype and receives
65 * the locale strings as ansi strings
66 *****************************************************************************/
68 BOOL CALLBACK
EnumLocalesProcA( LPSTR lpLocaleStringA
)
70 struct EnumLocalesParams
* params
;
75 WCHAR langCode
[ELP_LANGUAGE_FIELD_LENGTH
];
77 /* convert hex-string to LCID */
78 localeId
= strtol( lpLocaleStringA
, &pszEndA
, 16 );
80 /* check params received via TLS */
81 params
= (struct EnumLocalesParams
*) TlsGetValue( g_dwTLSLocaleEncId
);
82 if( NULL
== params
|| '\0' == params
->Language
[0] )
86 get the ISO language code for this locale
88 remember: we call the GetLocaleInfoW function
89 because the ansi version of this function returns
90 an error under WinNT/2000 when called with an
93 if( GetLocaleInfo( localeId
, LOCALE_SISO639LANGNAME
, langCode
, ELP_LANGUAGE_FIELD_LENGTH
) )
95 WCHAR ctryCode
[ELP_COUNTRY_FIELD_LENGTH
];
97 /* continue if language code does not match */
98 if( 0 != wcscmp( langCode
, params
->Language
) )
101 /* check if country code is set and equals the current locale */
102 if( '\0' != params
->Country
[0] && GetLocaleInfo( localeId
,
103 LOCALE_SISO3166CTRYNAME
, ctryCode
, ELP_COUNTRY_FIELD_LENGTH
) )
105 /* save return value in TLS and break if found desired locale */
106 if( 0 == wcscmp( ctryCode
, params
->Country
) )
108 params
->Locale
= localeId
;
114 /* fill with default values for that language */
115 LANGID langId
= LANGIDFROMLCID( localeId
);
117 /* exchange sublanguage with SUBLANG_NEUTRAL */
118 langId
= MAKELANGID( PRIMARYLANGID( langId
), SUBLANG_NEUTRAL
);
120 /* and use default sorting order */
121 params
->Locale
= MAKELCID( langId
, SORT_DEFAULT
);
127 /* retry by going on */
132 /*****************************************************************************
133 * GetTextEncodingFromLCID
134 *****************************************************************************/
136 rtl_TextEncoding
GetTextEncodingFromLCID( LCID localeId
)
138 rtl_TextEncoding Encoding
= RTL_TEXTENCODING_DONTKNOW
;
141 /* query ansi codepage for given locale */
142 if( localeId
&& GetLocaleInfo( localeId
, LOCALE_IDEFAULTANSICODEPAGE
, ansiCP
, 6 ) )
144 /* if GetLocaleInfo returns "0", it is a UNICODE only locale */
145 if( 0 != wcscmp( ansiCP
, L
"0" ) )
150 /* values returned from GetLocaleInfo are decimal based */
151 codepage
= wcstol( ansiCP
, &pwcEnd
, 10 );
153 /* find matching rtl encoding */
154 Encoding
= rtl_getTextEncodingFromWindowsCodePage( codepage
);
157 Encoding
= RTL_TEXTENCODING_UNICODE
;
164 /*****************************************************************************
165 * osl_getTextEncodingFromLocale
166 *****************************************************************************/
168 rtl_TextEncoding SAL_CALL
osl_getTextEncodingFromLocale( rtl_Locale
* pLocale
)
170 struct EnumLocalesParams params
= { L
"", L
"", 0 };
172 /* initialise global TLS id */
173 if( (DWORD
) -1 == g_dwTLSLocaleEncId
)
175 oslMutex globalMutex
= * osl_getGlobalMutex();
177 /* initializing must be thread save */
178 osl_acquireMutex( globalMutex
);
180 if( (DWORD
) -1 == g_dwTLSLocaleEncId
)
181 g_dwTLSLocaleEncId
= TlsAlloc();
183 osl_releaseMutex( globalMutex
);
186 /* if pLocale is NULL, use process locale as default */
187 if( NULL
== pLocale
)
188 osl_getProcessLocale( &pLocale
);
190 /* copy in parameters to structure */
191 if( pLocale
&& pLocale
->Language
&& pLocale
->Language
->length
< ELP_LANGUAGE_FIELD_LENGTH
)
193 wcscpy( params
.Language
, pLocale
->Language
->buffer
);
195 if( pLocale
->Country
&& pLocale
->Country
->length
< ELP_COUNTRY_FIELD_LENGTH
)
196 wcscpy( params
.Country
, pLocale
->Country
->buffer
);
198 /* save pointer to local structure in TLS */
199 TlsSetValue( g_dwTLSLocaleEncId
, ¶ms
);
201 /* enum all locales known to Windows */
202 EnumSystemLocalesA( EnumLocalesProcA
, LCID_SUPPORTED
);
204 /* use the LCID found in iteration */
205 return GetTextEncodingFromLCID( params
.Locale
);
208 return RTL_TEXTENCODING_DONTKNOW
;
211 /*****************************************************************************
212 * imp_getProcessLocale
213 *****************************************************************************/
215 void _imp_getProcessLocale( rtl_Locale
** ppLocale
)
217 WCHAR langCode
[ELP_LANGUAGE_FIELD_LENGTH
];
218 WCHAR ctryCode
[ELP_COUNTRY_FIELD_LENGTH
];
221 OSL_ASSERT( ppLocale
);
223 /* get the LCID to retrieve information from */
224 localeId
= GetUserDefaultLCID();
226 /* call GetLocaleInfo to retrieve the iso codes */
227 if( GetLocaleInfo( localeId
, LOCALE_SISO639LANGNAME
, langCode
, ELP_LANGUAGE_FIELD_LENGTH
) &&
228 GetLocaleInfo( localeId
, LOCALE_SISO3166CTRYNAME
, ctryCode
, ELP_COUNTRY_FIELD_LENGTH
) )
230 *ppLocale
= rtl_locale_register( langCode
, ctryCode
, L
"" );
234 *ppLocale
= rtl_locale_register( L
"C", L
"", L
"" );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */