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
25 #include "wine/port.h"
34 #include "winuser.h" /* for RT_STRINGW */
36 #include "wine/unicode.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
52 LANGID found_lang_id[NLS_MAX_LANGUAGES];
53 char found_language[NLS_MAX_LANGUAGES][3];
54 char found_country[NLS_MAX_LANGUAGES][3];
59 /***********************************************************************
62 * Retrieve the ANSI codepage for a given locale.
64 inline static UINT get_lcid_codepage( LCID lcid )
67 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
68 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
73 /***********************************************************************
76 * Update registry contents on startup if the user locale has changed.
77 * This simulates the action of the Windows control panel.
79 inline static void update_registry( LCID lcid )
83 DWORD count = sizeof(buffer);
86 if (RegCreateKeyExA( HKEY_CURRENT_USER, "Control Panel\\International", 0, NULL,
87 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
88 return; /* don't do anything if we can't create the registry key */
90 if (!RegQueryValueExA( hkey, "Locale", NULL, NULL, (LPBYTE)buffer, &count ))
92 if (strtol( buffer, NULL, 16 ) == lcid) /* already set correctly */
97 TRACE( "updating registry, locale changed %s -> %08lx\n", buffer, lcid );
99 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
101 sprintf( buffer, "%08lx", lcid );
102 RegSetValueExA( hkey, "Locale", 0, REG_SZ, (LPBYTE)buffer, strlen(buffer)+1 );
105 #define UPDATE_VALUE(lctype) do { \
106 GetLocaleInfoW( lcid, (lctype)|LOCALE_NOUSEROVERRIDE, bufferW, sizeof(bufferW)/sizeof(WCHAR) ); \
107 SetLocaleInfoW( lcid, (lctype), bufferW ); } while (0)
109 UPDATE_VALUE(LOCALE_SLANGUAGE);
110 UPDATE_VALUE(LOCALE_SCOUNTRY);
111 UPDATE_VALUE(LOCALE_ICOUNTRY);
112 UPDATE_VALUE(LOCALE_S1159);
113 UPDATE_VALUE(LOCALE_S2359);
114 UPDATE_VALUE(LOCALE_STIME);
115 UPDATE_VALUE(LOCALE_ITIME);
116 UPDATE_VALUE(LOCALE_ITLZERO);
117 UPDATE_VALUE(LOCALE_SSHORTDATE);
118 UPDATE_VALUE(LOCALE_IDATE);
119 UPDATE_VALUE(LOCALE_SLONGDATE);
120 UPDATE_VALUE(LOCALE_SDATE);
121 UPDATE_VALUE(LOCALE_SCURRENCY);
122 UPDATE_VALUE(LOCALE_ICURRENCY);
123 UPDATE_VALUE(LOCALE_INEGCURR);
124 UPDATE_VALUE(LOCALE_ICURRDIGITS);
125 UPDATE_VALUE(LOCALE_SDECIMAL);
126 UPDATE_VALUE(LOCALE_SLIST);
127 UPDATE_VALUE(LOCALE_STHOUSAND);
128 UPDATE_VALUE(LOCALE_IDIGITS);
129 UPDATE_VALUE(LOCALE_ILZERO);
130 UPDATE_VALUE(LOCALE_IMEASURE);
135 /***********************************************************************
136 * find_language_id_proc
138 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCSTR type,
139 LPCSTR name, WORD LangID, LPARAM lParam )
141 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
142 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
143 char buf_language[128];
144 char buf_country[128];
145 char buf_en_language[128];
147 TRACE("%04X\n", (UINT)LangID);
148 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
149 return TRUE; /* continue search */
154 GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
155 buf_language, sizeof(buf_language));
156 TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
158 GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
159 buf_country, sizeof(buf_country));
160 TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
162 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
164 if(l_data->country && strlen(l_data->country) > 0)
166 if(!strcasecmp(l_data->country, buf_country))
168 l_data->found_lang_id[0] = LangID;
170 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
171 return FALSE; /* stop enumeration */
174 else /* l_data->country not specified */
176 if(l_data->n_found < NLS_MAX_LANGUAGES)
178 l_data->found_lang_id[l_data->n_found] = LangID;
179 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
180 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
182 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
183 return TRUE; /* continue search */
188 /* Just in case, check LOCALE_SENGLANGUAGE too,
189 * in hope that possible alias name might have that value.
191 buf_en_language[0] = 0;
192 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
193 buf_en_language, sizeof(buf_en_language));
194 TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
196 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
198 l_data->found_lang_id[l_data->n_found] = LangID;
199 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
200 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
202 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
205 return TRUE; /* continue search */
209 /***********************************************************************
213 * Lang: a string whose two first chars are the iso name of a language.
214 * Country: a string whose two first chars are the iso name of country
215 * Charset: a string defining the chosen charset encoding
216 * Dialect: a string defining a variation of the locale
218 * all those values are from the standardized format of locale
219 * name in unix which is: Lang[_Country][.Charset][@Dialect]
222 * the numeric code of the language used by Windows
224 * FIXME: Charset and Dialect are not handled
226 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
228 LANG_FIND_DATA l_data;
229 char lang_string[256];
233 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
237 memset(&l_data, 0, sizeof(LANG_FIND_DATA));
238 strncpy(l_data.lang, Lang, sizeof(l_data.lang));
240 if(Country && strlen(Country) > 0)
241 strncpy(l_data.country, Country, sizeof(l_data.country));
243 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
244 (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LPARAM)&l_data);
246 strcpy(lang_string, l_data.lang);
247 if(l_data.country && strlen(l_data.country) > 0)
249 strcat(lang_string, "_");
250 strcat(lang_string, l_data.country);
255 if(l_data.country && strlen(l_data.country) > 0)
257 MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
258 l_data.country[0] = 0;
259 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
260 (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LONG)&l_data);
264 /* Re-evaluate lang_string */
265 strcpy(lang_string, l_data.lang);
266 if(l_data.country && strlen(l_data.country) > 0)
268 strcat(lang_string, "_");
269 strcat(lang_string, l_data.country);
274 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
275 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
279 if(l_data.n_found == 1)
280 TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
281 else /* l_data->n_found > 1 */
284 MESSAGE("For language '%s' several language ids were found:\n", lang_string);
285 for(i = 0; i < l_data.n_found; i++)
286 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
288 MESSAGE("\nInstead of using first in the list, suggest to define\n"
289 "your LANG environment variable like this: LANG=%s_%s\n",
290 l_data.found_language[0], l_data.found_country[0]);
294 TRACE("Returning %04X\n", l_data.found_lang_id[0]);
295 return l_data.found_lang_id[0];
299 /***********************************************************************
302 static LCID init_default_lcid(void)
305 char *lang,*country,*charset,*dialect,*next;
308 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) ) ||
309 GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) ) ||
310 GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) ) ||
311 GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) ) ||
312 GetEnvironmentVariableA( "LANG", buf, sizeof(buf) ))
314 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C")) goto done;
319 next=strchr(lang,':'); if (next) *next++='\0';
320 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
321 charset=strchr(lang,'.'); if (charset) *charset++='\0';
322 country=strchr(lang,'_'); if (country) *country++='\0';
324 ret = get_language_id(lang, country, charset, dialect);
327 } while (lang && !ret);
329 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
333 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
338 /******************************************************************************
339 * get_locale_value_name
341 * Gets the registry value name for a given lctype.
343 static const WCHAR *get_locale_value_name( DWORD lctype )
345 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
346 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
347 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
348 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
349 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
350 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
351 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
352 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
353 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
354 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
355 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
356 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
357 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
358 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
359 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
360 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
361 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
362 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
363 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
364 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
365 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
366 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
367 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
368 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
369 static const WCHAR sListW[] = {'s','L','i','s','t',0};
370 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
371 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
372 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
373 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
374 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
375 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
376 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
377 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
378 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
379 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
380 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
382 switch (lctype & ~LOCALE_LOCALEINFOFLAGSMASK)
384 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
385 * the values are stored in the registry, confirmed under Windows.
387 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
388 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
389 case LOCALE_ICURRENCY: return iCurrencyW;
390 case LOCALE_IDIGITS: return iDigitsW;
391 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
392 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
393 case LOCALE_ILZERO: return iLZeroW;
394 case LOCALE_IMEASURE: return iMeasureW;
395 case LOCALE_INEGCURR: return iNegCurrW;
396 case LOCALE_INEGNUMBER: return iNegNumberW;
397 case LOCALE_IPAPERSIZE: return iPaperSizeW;
398 case LOCALE_ITIME: return iTimeW;
399 case LOCALE_S1159: return s1159W;
400 case LOCALE_S2359: return s2359W;
401 case LOCALE_SCURRENCY: return sCurrencyW;
402 case LOCALE_SDATE: return sDateW;
403 case LOCALE_SDECIMAL: return sDecimalW;
404 case LOCALE_SGROUPING: return sGroupingW;
405 case LOCALE_SLIST: return sListW;
406 case LOCALE_SLONGDATE: return sLongDateW;
407 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
408 case LOCALE_SMONGROUPING: return sMonGroupingW;
409 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
410 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
411 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
412 case LOCALE_SSHORTDATE: return sShortDateW;
413 case LOCALE_STHOUSAND: return sThousandW;
414 case LOCALE_STIME: return sTimeW;
415 case LOCALE_STIMEFORMAT: return sTimeFormatW;
416 case LOCALE_SYEARMONTH: return sYearMonthW;
418 /* The following are not listed under MSDN as supported,
419 * but seem to be used and also stored in the registry.
421 case LOCALE_ICOUNTRY: return iCountryW;
422 case LOCALE_IDATE: return iDateW;
423 case LOCALE_ILDATE: return iLDateW;
424 case LOCALE_ITLZERO: return iTLZeroW;
425 case LOCALE_SCOUNTRY: return sCountryW;
426 case LOCALE_SLANGUAGE: return sLanguageW;
432 /******************************************************************************
433 * get_registry_locale_info
435 * Retrieve user-modified locale info from the registry.
436 * Return length, 0 on error, -1 if not found.
438 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
444 UNICODE_STRING nameW;
445 KEY_VALUE_PARTIAL_INFORMATION *info;
446 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
448 if (RegOpenKeyExA( HKEY_CURRENT_USER, "Control Panel\\International", 0, KEY_READ, &hkey))
451 RtlInitUnicodeString( &nameW, value );
452 size = info_size + len * sizeof(WCHAR);
454 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
457 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
461 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
462 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
466 ret = (size - info_size) / sizeof(WCHAR);
467 /* append terminating null if needed */
468 if (!ret || ((WCHAR *)info->Data)[ret-1])
470 if (ret < len || !buffer) ret++;
473 SetLastError( ERROR_INSUFFICIENT_BUFFER );
479 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
485 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
488 SetLastError( RtlNtStatusToDosError(status) );
493 HeapFree( GetProcessHeap(), 0, info );
498 /******************************************************************************
499 * GetLocaleInfoA (KERNEL32.@)
502 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
504 * MS online documentation states that the string returned is NULL terminated
505 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
506 * terminated string".
508 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
513 if (len < 0 || (len && !buffer))
515 SetLastError( ERROR_INVALID_PARAMETER );
518 if (!len) buffer = NULL;
520 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
522 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
524 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
527 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
529 if ((lctype & LOCALE_RETURN_NUMBER) ||
530 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
532 /* it's not an ASCII string, just bytes */
533 ret *= sizeof(WCHAR);
536 if (ret <= len) memcpy( buffer, bufferW, ret );
539 SetLastError( ERROR_INSUFFICIENT_BUFFER );
546 UINT codepage = CP_ACP;
547 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
548 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
551 HeapFree( GetProcessHeap(), 0, bufferW );
556 /******************************************************************************
557 * GetLocaleInfoW (KERNEL32.@)
560 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
562 * MS online documentation states that the string returned is NULL terminated
563 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
564 * terminated string".
566 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
577 if (len < 0 || (len && !buffer))
579 SetLastError( ERROR_INVALID_PARAMETER );
582 if (!len) buffer = NULL;
584 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
585 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
587 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
588 lctype &= ~LOCALE_LOCALEINFOFLAGSMASK;
590 /* first check for overrides in the registry */
592 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
594 const WCHAR *value = get_locale_value_name(lctype);
596 if (value && ((ret = get_registry_locale_info( value, buffer, len )) != -1)) return ret;
599 /* now load it from kernel resources */
601 lang_id = LANGIDFROMLCID( lcid );
603 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
604 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
605 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
607 hModule = GetModuleHandleA( "kernel32.dll" );
608 if (!(hrsrc = FindResourceExW( hModule, RT_STRINGW, (LPCWSTR)((lctype >> 4) + 1), lang_id )))
610 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
613 if (!(hmem = LoadResource( hModule, hrsrc )))
616 p = LockResource( hmem );
617 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
619 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
620 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
622 if (!buffer) return ret;
626 SetLastError( ERROR_INSUFFICIENT_BUFFER );
630 if (lcflags & LOCALE_RETURN_NUMBER)
633 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
635 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
637 number = strtolW( tmp, &end, 10 );
639 memcpy( buffer, &number, sizeof(number) );
640 else /* invalid number */
642 SetLastError( ERROR_INVALID_FLAGS );
645 HeapFree( GetProcessHeap(), 0, tmp );
647 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
648 lcid, lctype, buffer, len, number );
652 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
653 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
655 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
656 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
662 /******************************************************************************
663 * SetLocaleInfoA [KERNEL32.@]
665 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
667 UINT codepage = CP_ACP;
672 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
673 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
675 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
676 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
677 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
679 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
682 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
683 ret = SetLocaleInfoW( lcid, lctype, strW );
684 HeapFree( GetProcessHeap(), 0, strW );
689 /******************************************************************************
690 * SetLocaleInfoW (KERNEL32.@)
692 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
695 const WCHAR intlW[] = {'i','n','t','l',0 };
696 UNICODE_STRING valueW;
700 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
701 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
703 if (!(value = get_locale_value_name( lctype )))
705 SetLastError( ERROR_INVALID_PARAMETER );
708 if (lcid != GetUserDefaultLCID()) return TRUE; /* fake success */
710 TRACE("setting %lx to %s\n", lctype, debugstr_w(data) );
712 /* FIXME: should check that data to set is sane */
714 /* FIXME: profile functions should map to registry */
715 WriteProfileStringW( intlW, value, data );
717 if (RegCreateKeyExA( HKEY_CURRENT_USER, "Control Panel\\International", 0, NULL,
718 0, KEY_ALL_ACCESS, NULL, &hkey, NULL )) return FALSE;
719 RtlInitUnicodeString( &valueW, value );
720 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
723 if (status) SetLastError( RtlNtStatusToDosError(status) );
729 /***********************************************************************
730 * GetThreadLocale (KERNEL32.@)
732 LCID WINAPI GetThreadLocale(void)
734 LCID ret = NtCurrentTeb()->CurrentLocale;
735 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
740 /**********************************************************************
741 * SetThreadLocale (KERNEL32.@)
744 * check if lcid is a valid cp
746 BOOL WINAPI SetThreadLocale( LCID lcid ) /* [in] Locale identifier */
748 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
749 else if (lcid == LANG_USER_DEFAULT) lcid = GetUserDefaultLCID();
751 NtCurrentTeb()->CurrentLocale = lcid;
752 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
758 /******************************************************************************
759 * GetStringTypeW (KERNEL32.@)
761 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
763 if (count == -1) count = strlenW(src) + 1;
767 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
770 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
774 WARN("CT_CTYPE3: semi-stub.\n");
778 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
780 type1 = get_char_typeW( *src++ ) & 0xfff;
781 /* try to construct type3 from type1 */
782 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
783 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
784 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
785 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
786 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
787 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
788 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
790 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
791 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
792 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
793 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
794 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
795 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
796 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
797 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
799 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
800 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
801 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
802 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
803 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
804 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
810 SetLastError( ERROR_INVALID_PARAMETER );
817 /******************************************************************************
818 * GetStringTypeExW (KERNEL32.@)
820 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
822 /* locale is ignored for Unicode */
823 return GetStringTypeW( type, src, count, chartype );
827 /******************************************************************************
828 * GetStringTypeA (KERNEL32.@)
830 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
837 if(count == -1) count = strlen(src) + 1;
839 if (!(cp = get_lcid_codepage( locale )))
841 FIXME("For locale %04lx using current ANSI code page\n", locale);
845 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
846 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
848 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
850 * NOTE: the target buffer has 1 word for each CHARACTER in the source
851 * string, with multibyte characters there maybe be more bytes in count
852 * than character space in the buffer!
854 ret = GetStringTypeW(type, srcW, countW, chartype);
855 HeapFree(GetProcessHeap(), 0, srcW);
860 /******************************************************************************
861 * GetStringTypeExA (KERNEL32.@)
863 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
865 return GetStringTypeA(locale, type, src, count, chartype);
869 /******************************************************************************
872 void LOCALE_Init(void)
874 UINT ansi = 1252, oem = 437, mac = 10000;
875 LCID lcid = init_default_lcid();
877 GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
878 (LPWSTR)&ansi, sizeof(ansi)/sizeof(WCHAR) );
879 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
880 (LPWSTR)&mac, sizeof(mac)/sizeof(WCHAR) );
881 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
882 (LPWSTR)&oem, sizeof(oem)/sizeof(WCHAR) );
884 CODEPAGE_Init( ansi, oem, mac, lcid );
885 update_registry( lcid );