Removed obsolete INT_Int31Handler.
[wine/multimedia.git] / dlls / kernel / locale.c
blob9b8d223dce02a0364fc9871987c13dc462d4feeb
1 /*
2 * Locale support
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 David Lee Lambert
6 * Copyright 2000 Julio César Gázquez
7 * Copyright 2002 Alexandre Julliard for Codeweavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdlib.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h" /* for RT_STRINGW */
35 #include "winternl.h"
36 #include "wine/unicode.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "thread.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(nls);
44 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
46 extern void CODEPAGE_Init( UINT ansi, UINT oem, UINT mac, LCID lcid );
48 #define NLS_MAX_LANGUAGES 20
49 typedef struct {
50 char lang[128];
51 char country[128];
52 LANGID found_lang_id[NLS_MAX_LANGUAGES];
53 char found_language[NLS_MAX_LANGUAGES][3];
54 char found_country[NLS_MAX_LANGUAGES][3];
55 int n_found;
56 } LANG_FIND_DATA;
59 /***********************************************************************
60 * get_lcid_codepage
62 * Retrieve the ANSI codepage for a given locale.
64 inline static UINT get_lcid_codepage( LCID lcid )
66 UINT ret;
67 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
68 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
69 return ret;
73 /***********************************************************************
74 * create_registry_key
76 * Create the Control Panel\\International registry key.
78 inline static HKEY create_registry_key(void)
80 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
81 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
82 OBJECT_ATTRIBUTES attr;
83 UNICODE_STRING nameW;
84 HKEY hkey;
86 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
88 attr.Length = sizeof(attr);
89 attr.RootDirectory = hkey;
90 attr.ObjectName = &nameW;
91 attr.Attributes = 0;
92 attr.SecurityDescriptor = NULL;
93 attr.SecurityQualityOfService = NULL;
94 RtlInitUnicodeString( &nameW, intlW );
96 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
97 NtClose( attr.RootDirectory );
98 return hkey;
102 /***********************************************************************
103 * update_registry
105 * Update registry contents on startup if the user locale has changed.
106 * This simulates the action of the Windows control panel.
108 inline static void update_registry( LCID lcid )
110 static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
111 UNICODE_STRING nameW;
112 char buffer[20];
113 WCHAR bufferW[80];
114 DWORD count = sizeof(buffer);
115 HKEY hkey;
117 if (!(hkey = create_registry_key()))
118 return; /* don't do anything if we can't create the registry key */
120 RtlInitUnicodeString( &nameW, LocaleW );
121 count = sizeof(bufferW);
122 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
124 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
125 RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &count,
126 (WCHAR *)info->Data, info->DataLength );
127 buffer[count] = 0;
128 if (strtol( buffer, NULL, 16 ) == lcid) /* already set correctly */
130 NtClose( hkey );
131 return;
133 TRACE( "updating registry, locale changed %s -> %08lx\n", buffer, lcid );
135 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
137 sprintf( buffer, "%08lx", lcid );
138 RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, strlen(buffer)+1 );
139 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW)+1) * sizeof(WCHAR) );
140 NtClose( hkey );
142 #define UPDATE_VALUE(lctype) do { \
143 GetLocaleInfoW( lcid, (lctype)|LOCALE_NOUSEROVERRIDE, bufferW, sizeof(bufferW)/sizeof(WCHAR) ); \
144 SetLocaleInfoW( lcid, (lctype), bufferW ); } while (0)
146 UPDATE_VALUE(LOCALE_SLANGUAGE);
147 UPDATE_VALUE(LOCALE_SCOUNTRY);
148 UPDATE_VALUE(LOCALE_ICOUNTRY);
149 UPDATE_VALUE(LOCALE_S1159);
150 UPDATE_VALUE(LOCALE_S2359);
151 UPDATE_VALUE(LOCALE_STIME);
152 UPDATE_VALUE(LOCALE_ITIME);
153 UPDATE_VALUE(LOCALE_ITLZERO);
154 UPDATE_VALUE(LOCALE_SSHORTDATE);
155 UPDATE_VALUE(LOCALE_IDATE);
156 UPDATE_VALUE(LOCALE_SLONGDATE);
157 UPDATE_VALUE(LOCALE_SDATE);
158 UPDATE_VALUE(LOCALE_SCURRENCY);
159 UPDATE_VALUE(LOCALE_ICURRENCY);
160 UPDATE_VALUE(LOCALE_INEGCURR);
161 UPDATE_VALUE(LOCALE_ICURRDIGITS);
162 UPDATE_VALUE(LOCALE_SDECIMAL);
163 UPDATE_VALUE(LOCALE_SLIST);
164 UPDATE_VALUE(LOCALE_STHOUSAND);
165 UPDATE_VALUE(LOCALE_IDIGITS);
166 UPDATE_VALUE(LOCALE_ILZERO);
167 UPDATE_VALUE(LOCALE_IMEASURE);
168 #undef UPDATE_VALUE
172 /***********************************************************************
173 * find_language_id_proc
175 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCSTR type,
176 LPCSTR name, WORD LangID, LPARAM lParam )
178 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
179 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
180 char buf_language[128];
181 char buf_country[128];
182 char buf_en_language[128];
184 TRACE("%04X\n", (UINT)LangID);
185 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
186 return TRUE; /* continue search */
188 buf_language[0] = 0;
189 buf_country[0] = 0;
191 GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
192 buf_language, sizeof(buf_language));
193 TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
195 GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
196 buf_country, sizeof(buf_country));
197 TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
199 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
201 if(l_data->country && strlen(l_data->country) > 0)
203 if(!strcasecmp(l_data->country, buf_country))
205 l_data->found_lang_id[0] = LangID;
206 l_data->n_found = 1;
207 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
208 return FALSE; /* stop enumeration */
211 else /* l_data->country not specified */
213 if(l_data->n_found < NLS_MAX_LANGUAGES)
215 l_data->found_lang_id[l_data->n_found] = LangID;
216 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
217 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
218 l_data->n_found++;
219 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
220 return TRUE; /* continue search */
225 /* Just in case, check LOCALE_SENGLANGUAGE too,
226 * in hope that possible alias name might have that value.
228 buf_en_language[0] = 0;
229 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
230 buf_en_language, sizeof(buf_en_language));
231 TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
233 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
235 l_data->found_lang_id[l_data->n_found] = LangID;
236 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
237 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
238 l_data->n_found++;
239 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
242 return TRUE; /* continue search */
246 /***********************************************************************
247 * get_language_id
249 * INPUT:
250 * Lang: a string whose two first chars are the iso name of a language.
251 * Country: a string whose two first chars are the iso name of country
252 * Charset: a string defining the chosen charset encoding
253 * Dialect: a string defining a variation of the locale
255 * all those values are from the standardized format of locale
256 * name in unix which is: Lang[_Country][.Charset][@Dialect]
258 * RETURNS:
259 * the numeric code of the language used by Windows
261 * FIXME: Charset and Dialect are not handled
263 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
265 LANG_FIND_DATA l_data;
266 char lang_string[256];
268 if(!Lang)
270 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
271 goto END;
274 memset(&l_data, 0, sizeof(LANG_FIND_DATA));
275 strncpy(l_data.lang, Lang, sizeof(l_data.lang));
277 if(Country && strlen(Country) > 0)
278 strncpy(l_data.country, Country, sizeof(l_data.country));
280 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
281 (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LPARAM)&l_data);
283 strcpy(lang_string, l_data.lang);
284 if(l_data.country && strlen(l_data.country) > 0)
286 strcat(lang_string, "_");
287 strcat(lang_string, l_data.country);
290 if(!l_data.n_found)
292 if(l_data.country && strlen(l_data.country) > 0)
294 MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
295 l_data.country[0] = 0;
296 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
297 (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LONG)&l_data);
301 /* Re-evaluate lang_string */
302 strcpy(lang_string, l_data.lang);
303 if(l_data.country && strlen(l_data.country) > 0)
305 strcat(lang_string, "_");
306 strcat(lang_string, l_data.country);
309 if(!l_data.n_found)
311 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
312 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
314 else
316 if(l_data.n_found == 1)
317 TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
318 else /* l_data->n_found > 1 */
320 int i;
321 MESSAGE("For language '%s' several language ids were found:\n", lang_string);
322 for(i = 0; i < l_data.n_found; i++)
323 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
325 MESSAGE("\nInstead of using first in the list, suggest to define\n"
326 "your LANG environment variable like this: LANG=%s_%s\n",
327 l_data.found_language[0], l_data.found_country[0]);
330 END:
331 TRACE("Returning %04X\n", l_data.found_lang_id[0]);
332 return l_data.found_lang_id[0];
336 /***********************************************************************
337 * init_default_lcid
339 static LCID init_default_lcid(void)
341 char buf[256];
342 char *lang,*country,*charset,*dialect,*next;
343 LCID ret = 0;
345 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) ) ||
346 GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) ) ||
347 GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) ) ||
348 GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) ) ||
349 GetEnvironmentVariableA( "LANG", buf, sizeof(buf) ))
351 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C")) goto done;
353 lang=buf;
355 do {
356 next=strchr(lang,':'); if (next) *next++='\0';
357 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
358 charset=strchr(lang,'.'); if (charset) *charset++='\0';
359 country=strchr(lang,'_'); if (country) *country++='\0';
361 ret = get_language_id(lang, country, charset, dialect);
363 lang=next;
364 } while (lang && !ret);
366 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
369 done:
370 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
371 return ret;
375 /******************************************************************************
376 * get_locale_value_name
378 * Gets the registry value name for a given lctype.
380 static const WCHAR *get_locale_value_name( DWORD lctype )
382 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
383 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
384 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
385 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
386 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
387 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
388 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
389 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
390 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
391 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
392 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
393 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
394 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
395 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
396 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
397 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
398 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
399 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
400 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
401 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
402 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
403 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
404 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
405 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
406 static const WCHAR sListW[] = {'s','L','i','s','t',0};
407 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
408 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
409 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
410 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
411 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
412 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
413 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
414 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
415 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
416 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
417 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
419 switch (lctype & ~LOCALE_LOCALEINFOFLAGSMASK)
421 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
422 * the values are stored in the registry, confirmed under Windows.
424 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
425 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
426 case LOCALE_ICURRENCY: return iCurrencyW;
427 case LOCALE_IDIGITS: return iDigitsW;
428 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
429 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
430 case LOCALE_ILZERO: return iLZeroW;
431 case LOCALE_IMEASURE: return iMeasureW;
432 case LOCALE_INEGCURR: return iNegCurrW;
433 case LOCALE_INEGNUMBER: return iNegNumberW;
434 case LOCALE_IPAPERSIZE: return iPaperSizeW;
435 case LOCALE_ITIME: return iTimeW;
436 case LOCALE_S1159: return s1159W;
437 case LOCALE_S2359: return s2359W;
438 case LOCALE_SCURRENCY: return sCurrencyW;
439 case LOCALE_SDATE: return sDateW;
440 case LOCALE_SDECIMAL: return sDecimalW;
441 case LOCALE_SGROUPING: return sGroupingW;
442 case LOCALE_SLIST: return sListW;
443 case LOCALE_SLONGDATE: return sLongDateW;
444 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
445 case LOCALE_SMONGROUPING: return sMonGroupingW;
446 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
447 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
448 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
449 case LOCALE_SSHORTDATE: return sShortDateW;
450 case LOCALE_STHOUSAND: return sThousandW;
451 case LOCALE_STIME: return sTimeW;
452 case LOCALE_STIMEFORMAT: return sTimeFormatW;
453 case LOCALE_SYEARMONTH: return sYearMonthW;
455 /* The following are not listed under MSDN as supported,
456 * but seem to be used and also stored in the registry.
458 case LOCALE_ICOUNTRY: return iCountryW;
459 case LOCALE_IDATE: return iDateW;
460 case LOCALE_ILDATE: return iLDateW;
461 case LOCALE_ITLZERO: return iTLZeroW;
462 case LOCALE_SCOUNTRY: return sCountryW;
463 case LOCALE_SLANGUAGE: return sLanguageW;
465 return NULL;
469 /******************************************************************************
470 * get_registry_locale_info
472 * Retrieve user-modified locale info from the registry.
473 * Return length, 0 on error, -1 if not found.
475 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
477 DWORD size;
478 INT ret;
479 HKEY hkey;
480 NTSTATUS status;
481 UNICODE_STRING nameW;
482 KEY_VALUE_PARTIAL_INFORMATION *info;
483 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
485 if (!(hkey = create_registry_key())) return -1;
487 RtlInitUnicodeString( &nameW, value );
488 size = info_size + len * sizeof(WCHAR);
490 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
492 NtClose( hkey );
493 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
494 return 0;
497 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
498 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
500 if (!status)
502 ret = (size - info_size) / sizeof(WCHAR);
503 /* append terminating null if needed */
504 if (!ret || ((WCHAR *)info->Data)[ret-1])
506 if (ret < len || !buffer) ret++;
507 else
509 SetLastError( ERROR_INSUFFICIENT_BUFFER );
510 ret = 0;
513 if (ret && buffer)
515 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
516 buffer[ret-1] = 0;
519 else
521 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
522 else
524 SetLastError( RtlNtStatusToDosError(status) );
525 ret = 0;
528 NtClose( hkey );
529 HeapFree( GetProcessHeap(), 0, info );
530 return ret;
534 /******************************************************************************
535 * GetLocaleInfoA (KERNEL32.@)
537 * NOTES
538 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
540 * MS online documentation states that the string returned is NULL terminated
541 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
542 * terminated string".
544 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
546 WCHAR *bufferW;
547 INT lenW, ret;
549 if (len < 0 || (len && !buffer))
551 SetLastError( ERROR_INVALID_PARAMETER );
552 return 0;
554 if (!len) buffer = NULL;
556 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
558 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
560 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
561 return 0;
563 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
565 if ((lctype & LOCALE_RETURN_NUMBER) ||
566 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
568 /* it's not an ASCII string, just bytes */
569 ret *= sizeof(WCHAR);
570 if (buffer)
572 if (ret <= len) memcpy( buffer, bufferW, ret );
573 else
575 SetLastError( ERROR_INSUFFICIENT_BUFFER );
576 ret = 0;
580 else
582 UINT codepage = CP_ACP;
583 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
584 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
587 HeapFree( GetProcessHeap(), 0, bufferW );
588 return ret;
592 /******************************************************************************
593 * GetLocaleInfoW (KERNEL32.@)
595 * NOTES
596 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
598 * MS online documentation states that the string returned is NULL terminated
599 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
600 * terminated string".
602 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
604 LANGID lang_id;
605 HRSRC hrsrc;
606 HGLOBAL hmem;
607 HMODULE hModule;
608 INT ret;
609 UINT lcflags;
610 const WCHAR *p;
611 int i;
613 if (len < 0 || (len && !buffer))
615 SetLastError( ERROR_INVALID_PARAMETER );
616 return 0;
618 if (!len) buffer = NULL;
620 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
621 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
623 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
624 lctype &= ~LOCALE_LOCALEINFOFLAGSMASK;
626 /* first check for overrides in the registry */
628 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
630 const WCHAR *value = get_locale_value_name(lctype);
632 if (value && ((ret = get_registry_locale_info( value, buffer, len )) != -1)) return ret;
635 /* now load it from kernel resources */
637 lang_id = LANGIDFROMLCID( lcid );
639 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
640 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
641 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
643 hModule = GetModuleHandleA( "kernel32.dll" );
644 if (!(hrsrc = FindResourceExW( hModule, RT_STRINGW, (LPCWSTR)((lctype >> 4) + 1), lang_id )))
646 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
647 return 0;
649 if (!(hmem = LoadResource( hModule, hrsrc )))
650 return 0;
652 p = LockResource( hmem );
653 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
655 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
656 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
658 if (!buffer) return ret;
660 if (ret > len)
662 SetLastError( ERROR_INSUFFICIENT_BUFFER );
663 return 0;
666 if (lcflags & LOCALE_RETURN_NUMBER)
668 UINT number;
669 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
670 if (!tmp) return 0;
671 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
672 tmp[*p] = 0;
673 number = strtolW( tmp, &end, 10 );
674 if (!*end)
675 memcpy( buffer, &number, sizeof(number) );
676 else /* invalid number */
678 SetLastError( ERROR_INVALID_FLAGS );
679 ret = 0;
681 HeapFree( GetProcessHeap(), 0, tmp );
683 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
684 lcid, lctype, buffer, len, number );
686 else
688 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
689 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
691 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
692 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
694 return ret;
698 /******************************************************************************
699 * SetLocaleInfoA [KERNEL32.@]
701 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
703 UINT codepage = CP_ACP;
704 WCHAR *strW;
705 DWORD len;
706 BOOL ret;
708 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
709 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
711 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
712 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
713 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
715 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
716 return FALSE;
718 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
719 ret = SetLocaleInfoW( lcid, lctype, strW );
720 HeapFree( GetProcessHeap(), 0, strW );
721 return ret;
725 /******************************************************************************
726 * SetLocaleInfoW (KERNEL32.@)
728 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
730 const WCHAR *value;
731 const WCHAR intlW[] = {'i','n','t','l',0 };
732 UNICODE_STRING valueW;
733 NTSTATUS status;
734 HKEY hkey;
736 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
737 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
739 if (!(value = get_locale_value_name( lctype )))
741 SetLastError( ERROR_INVALID_PARAMETER );
742 return FALSE;
744 if (lcid != GetUserDefaultLCID()) return TRUE; /* fake success */
746 TRACE("setting %lx to %s\n", lctype, debugstr_w(data) );
748 /* FIXME: should check that data to set is sane */
750 /* FIXME: profile functions should map to registry */
751 WriteProfileStringW( intlW, value, data );
753 if (!(hkey = create_registry_key())) return FALSE;
754 RtlInitUnicodeString( &valueW, value );
755 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
756 NtClose( hkey );
758 if (status) SetLastError( RtlNtStatusToDosError(status) );
759 return !status;
760 return TRUE;
764 /***********************************************************************
765 * GetThreadLocale (KERNEL32.@)
767 LCID WINAPI GetThreadLocale(void)
769 LCID ret = NtCurrentTeb()->CurrentLocale;
770 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
771 return ret;
775 /**********************************************************************
776 * SetThreadLocale (KERNEL32.@)
778 * FIXME
779 * check if lcid is a valid cp
781 BOOL WINAPI SetThreadLocale( LCID lcid ) /* [in] Locale identifier */
783 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
784 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
786 NtCurrentTeb()->CurrentLocale = lcid;
787 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
788 return TRUE;
793 /******************************************************************************
794 * GetStringTypeW (KERNEL32.@)
796 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
798 if (count == -1) count = strlenW(src) + 1;
799 switch(type)
801 case CT_CTYPE1:
802 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
803 break;
804 case CT_CTYPE2:
805 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
806 break;
807 case CT_CTYPE3:
809 WARN("CT_CTYPE3: semi-stub.\n");
810 while (count--)
812 int c = *src;
813 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
815 type1 = get_char_typeW( *src++ ) & 0xfff;
816 /* try to construct type3 from type1 */
817 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
818 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
819 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
820 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
821 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
822 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
823 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
825 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
826 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
827 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
828 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
829 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
830 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
831 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
832 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
834 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
835 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
836 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
837 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
838 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
839 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
840 *chartype++ = type3;
842 break;
844 default:
845 SetLastError( ERROR_INVALID_PARAMETER );
846 return FALSE;
848 return TRUE;
852 /******************************************************************************
853 * GetStringTypeExW (KERNEL32.@)
855 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
857 /* locale is ignored for Unicode */
858 return GetStringTypeW( type, src, count, chartype );
862 /******************************************************************************
863 * GetStringTypeA (KERNEL32.@)
865 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
867 UINT cp;
868 INT countW;
869 LPWSTR srcW;
870 BOOL ret = FALSE;
872 if(count == -1) count = strlen(src) + 1;
874 if (!(cp = get_lcid_codepage( locale )))
876 FIXME("For locale %04lx using current ANSI code page\n", locale);
877 cp = GetACP();
880 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
881 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
883 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
885 * NOTE: the target buffer has 1 word for each CHARACTER in the source
886 * string, with multibyte characters there maybe be more bytes in count
887 * than character space in the buffer!
889 ret = GetStringTypeW(type, srcW, countW, chartype);
890 HeapFree(GetProcessHeap(), 0, srcW);
892 return ret;
895 /******************************************************************************
896 * GetStringTypeExA (KERNEL32.@)
898 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
900 return GetStringTypeA(locale, type, src, count, chartype);
904 /******************************************************************************
905 * LOCALE_Init
907 void LOCALE_Init(void)
909 UINT ansi = 1252, oem = 437, mac = 10000;
910 LCID lcid = init_default_lcid();
912 GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
913 (LPWSTR)&ansi, sizeof(ansi)/sizeof(WCHAR) );
914 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
915 (LPWSTR)&mac, sizeof(mac)/sizeof(WCHAR) );
916 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
917 (LPWSTR)&oem, sizeof(oem)/sizeof(WCHAR) );
919 CODEPAGE_Init( ansi, oem, mac, lcid );
920 update_registry( lcid );