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"
37 #include "winuser.h" /* for RT_STRINGW */
40 #include "wine/unicode.h"
45 #include "kernel_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(nls);
50 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
52 /* current code pages */
53 static const union cptable *ansi_cptable;
54 static const union cptable *oem_cptable;
55 static const union cptable *mac_cptable;
56 static const union cptable *unix_cptable; /* NULL if UTF8 */
58 /* Charset to codepage map, sorted by name. */
59 static const struct charset_entry
61 const char *charset_name;
100 { "ISO88591", 28591 },
101 { "ISO885910", 28600 },
102 { "ISO885913", 28603 },
103 { "ISO885914", 28604 },
104 { "ISO885915", 28605 },
105 { "ISO88592", 28592 },
106 { "ISO88593", 28593 },
107 { "ISO88594", 28594 },
108 { "ISO88595", 28595 },
109 { "ISO88596", 28596 },
110 { "ISO88597", 28597 },
111 { "ISO88598", 28598 },
112 { "ISO88599", 28599 },
118 #define NLS_MAX_LANGUAGES 20
122 LANGID found_lang_id[NLS_MAX_LANGUAGES];
123 WCHAR found_language[NLS_MAX_LANGUAGES][3];
124 WCHAR found_country[NLS_MAX_LANGUAGES][3];
129 /* copy Unicode string to Ascii without using codepages */
130 static inline void strcpyWtoA( char *dst, const WCHAR *src )
132 while ((*dst++ = *src++));
135 /* Copy Ascii string to Unicode without using codepages */
136 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
138 while (n > 1 && *src)
140 *dst++ = (unsigned char)*src++;
146 /* return a printable string for a language id */
147 static const char *debugstr_lang( LANGID lang )
149 WCHAR langW[4], countryW[4];
151 LCID lcid = MAKELCID( lang, SORT_DEFAULT );
153 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE, langW, sizeof(langW)/sizeof(WCHAR));
154 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE, countryW, sizeof(countryW)/sizeof(WCHAR));
155 strcpyWtoA( buffer, langW );
156 strcat( buffer, "_" );
157 strcpyWtoA( buffer + strlen(buffer), countryW );
158 return wine_dbg_sprintf( "%s", buffer );
161 /***********************************************************************
164 * Retrieve the ANSI codepage for a given locale.
166 inline static UINT get_lcid_codepage( LCID lcid )
169 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
170 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
175 /***********************************************************************
178 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
180 static const union cptable *get_codepage_table( unsigned int codepage )
182 const union cptable *ret = NULL;
184 assert( ansi_cptable ); /* init must have been done already */
198 if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
201 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
202 if (codepage == oem_cptable->info.codepage) return oem_cptable;
203 if (codepage == mac_cptable->info.codepage) return mac_cptable;
204 ret = wine_cp_get_table( codepage );
210 /***********************************************************************
211 * create_registry_key
213 * Create the Control Panel\\International registry key.
215 inline static HKEY create_registry_key(void)
217 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
218 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
219 OBJECT_ATTRIBUTES attr;
220 UNICODE_STRING nameW;
223 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
225 attr.Length = sizeof(attr);
226 attr.RootDirectory = hkey;
227 attr.ObjectName = &nameW;
229 attr.SecurityDescriptor = NULL;
230 attr.SecurityQualityOfService = NULL;
231 RtlInitUnicodeString( &nameW, intlW );
233 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
234 NtClose( attr.RootDirectory );
239 /***********************************************************************
240 * LOCALE_InitRegistry
242 * Update registry contents on startup if the user locale has changed.
243 * This simulates the action of the Windows control panel.
245 void LOCALE_InitRegistry(void)
247 static const USHORT updateValues[] = {
249 LOCALE_SCOUNTRY, LOCALE_ICOUNTRY,
250 LOCALE_S1159, LOCALE_S2359,
251 LOCALE_STIME, LOCALE_ITIME,
256 LOCALE_SCURRENCY, LOCALE_ICURRENCY,
263 LOCALE_IDIGITSUBSTITUTION,
264 LOCALE_SNATIVEDIGITS,
265 LOCALE_ITIMEMARKPOSN,
266 LOCALE_ICALENDARTYPE,
270 static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
271 UNICODE_STRING nameW;
276 LCID lcid = GetUserDefaultLCID();
278 if (!(hkey = create_registry_key()))
279 return; /* don't do anything if we can't create the registry key */
281 RtlInitUnicodeString( &nameW, LocaleW );
282 count = sizeof(bufferW);
283 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
285 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
286 LPCWSTR szValueText = (LPCWSTR)info->Data;
288 if (strtoulW( szValueText, NULL, 16 ) == lcid) /* already set correctly */
293 TRACE( "updating registry, locale changed %s -> %08lx\n", debugstr_w(szValueText), lcid );
295 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
297 sprintf( buffer, "%08lx", lcid );
298 /* Note: '9' constant below is strlen(buffer) + 1 */
299 RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, 9 );
300 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, 9 * sizeof(WCHAR) );
303 for (i = 0; i < sizeof(updateValues)/sizeof(updateValues[0]); i++)
305 GetLocaleInfoW( lcid, updateValues[i] | LOCALE_NOUSEROVERRIDE, bufferW,
306 sizeof(bufferW)/sizeof(WCHAR) );
307 SetLocaleInfoW( lcid, updateValues[i], bufferW );
312 /***********************************************************************
313 * find_language_id_proc
315 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCWSTR type,
316 LPCWSTR name, WORD LangID, LPARAM lParam )
318 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
319 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
320 WCHAR buf_language[128];
321 WCHAR buf_country[128];
322 WCHAR buf_en_language[128];
324 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
325 return TRUE; /* continue search */
330 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
331 buf_language, sizeof(buf_language)/sizeof(WCHAR));
332 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
333 buf_country, sizeof(buf_country)/sizeof(WCHAR));
335 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_language))
337 if(l_data->country[0])
339 if(!strcmpiW(l_data->country, buf_country))
341 l_data->found_lang_id[0] = LangID;
343 TRACE("Found id %04X for lang %s country %s\n",
344 LangID, debugstr_w(l_data->lang), debugstr_w(l_data->country));
345 return FALSE; /* stop enumeration */
348 else goto found; /* l_data->country not specified */
351 /* Just in case, check LOCALE_SENGLANGUAGE too,
352 * in hope that possible alias name might have that value.
354 buf_en_language[0] = 0;
355 GetLocaleInfoW(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
356 buf_en_language, sizeof(buf_en_language)/sizeof(WCHAR));
358 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_en_language)) goto found;
359 return TRUE; /* not found, continue search */
362 l_data->found_lang_id[l_data->n_found] = LangID;
363 strncpyW(l_data->found_country[l_data->n_found], buf_country, 3);
364 strncpyW(l_data->found_language[l_data->n_found], buf_language, 3);
366 TRACE("Found id %04X for lang %s\n", LangID, debugstr_w(l_data->lang));
367 return (l_data->n_found < NLS_MAX_LANGUAGES); /* continue search, unless we have enough */
371 /***********************************************************************
375 * Lang: a string whose two first chars are the iso name of a language.
376 * Country: a string whose two first chars are the iso name of country
377 * Charset: a string defining the chosen charset encoding
378 * Dialect: a string defining a variation of the locale
380 * all those values are from the standardized format of locale
381 * name in unix which is: Lang[_Country][.Charset][@Dialect]
384 * the numeric code of the language used by Windows
386 * FIXME: Charset and Dialect are not handled
388 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
390 LANG_FIND_DATA l_data;
394 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
399 strcpynAtoW(l_data.lang, Lang, sizeof(l_data.lang));
401 if (Country) strcpynAtoW(l_data.country, Country, sizeof(l_data.country));
402 else l_data.country[0] = 0;
404 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
405 find_language_id_proc, (LPARAM)&l_data);
407 if (l_data.n_found == 1) goto END;
411 if(l_data.country[0])
413 /* retry without country name */
414 l_data.country[0] = 0;
415 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
416 find_language_id_proc, (LONG)&l_data);
419 MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to English.\n",
421 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
423 else MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to '%s'.\n",
424 Lang, Country, debugstr_lang(l_data.found_lang_id[0]) );
428 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English.\n", Lang);
429 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
436 if (Country && Country[0])
437 MESSAGE("For language '%s_%s' several language ids were found:\n", Lang, Country);
439 MESSAGE("For language '%s' several language ids were found:\n", Lang);
441 /* print a list of languages with their description */
442 for (i = 0; i < l_data.n_found; i++)
446 GetLocaleInfoW( MAKELCID( l_data.found_lang_id[i], SORT_DEFAULT ),
447 LOCALE_SLANGUAGE|LOCALE_NOUSEROVERRIDE, buffW, sizeof(buffW)/sizeof(WCHAR));
448 strcpyWtoA( buffA, buffW );
449 MESSAGE( " %s (%04X) - %s\n", debugstr_lang(l_data.found_lang_id[i]),
450 l_data.found_lang_id[i], buffA );
452 MESSAGE("Defaulting to '%s'. You should specify the exact language you want\n"
453 "by defining your LANG environment variable like this: LANG=%s\n",
454 debugstr_lang(l_data.found_lang_id[0]), debugstr_lang(l_data.found_lang_id[0]) );
457 TRACE("Returning %04X (%s)\n", l_data.found_lang_id[0], debugstr_lang(l_data.found_lang_id[0]));
458 return l_data.found_lang_id[0];
462 /***********************************************************************
463 * charset_cmp (internal)
465 static int charset_cmp( const void *name, const void *entry )
467 const struct charset_entry *charset = (const struct charset_entry *)entry;
468 return strcasecmp( (const char *)name, charset->charset_name );
471 /***********************************************************************
474 static LCID init_default_lcid( UINT *unix_cp )
476 char *buf, *lang,*country,*charset,*dialect,*next;
479 if ((lang = getenv( "LC_ALL" )) ||
480 (lang = getenv( "LANGUAGE" )) ||
481 (lang = getenv( "LANG" )))
483 if (!strcmp(lang,"POSIX") || !strcmp(lang,"C")) goto done;
485 buf = RtlAllocateHeap( GetProcessHeap(), 0, strlen(lang) + 1 );
490 next=strchr(lang,':'); if (next) *next++='\0';
491 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
492 charset=strchr(lang,'.'); if (charset) *charset++='\0';
493 country=strchr(lang,'_'); if (country) *country++='\0';
495 ret = get_language_id(lang, country, charset, dialect);
498 const struct charset_entry *entry;
499 char charset_name[16];
502 /* remove punctuation characters from charset name */
503 for (i = j = 0; charset[i] && j < sizeof(charset_name)-1; i++)
504 if (isalnum(charset[i])) charset_name[j++] = charset[i];
507 entry = bsearch( charset_name, charset_names,
508 sizeof(charset_names)/sizeof(charset_names[0]),
509 sizeof(charset_names[0]), charset_cmp );
512 *unix_cp = entry->codepage;
513 TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
516 FIXME("charset %s was not recognized\n", charset);
520 } while (lang && !ret);
522 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
523 RtlFreeHeap( GetProcessHeap(), 0, buf );
527 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
532 /***********************************************************************
533 * GetUserDefaultLangID (KERNEL32.@)
535 * Get the default language Id for the current user.
541 * The current LANGID of the default language for the current user.
543 LANGID WINAPI GetUserDefaultLangID(void)
545 return LANGIDFROMLCID(GetUserDefaultLCID());
549 /***********************************************************************
550 * GetSystemDefaultLangID (KERNEL32.@)
552 * Get the default language Id for the system.
558 * The current LANGID of the default language for the system.
560 LANGID WINAPI GetSystemDefaultLangID(void)
562 return GetUserDefaultLangID();
566 /***********************************************************************
567 * GetUserDefaultLCID (KERNEL32.@)
569 * Get the default locale Id for the current user.
575 * The current LCID of the default locale for the current user.
577 LCID WINAPI GetUserDefaultLCID(void)
580 NtQueryDefaultLocale( TRUE, &lcid );
585 /***********************************************************************
586 * GetSystemDefaultLCID (KERNEL32.@)
588 * Get the default locale Id for the system.
594 * The current LCID of the default locale for the system.
596 LCID WINAPI GetSystemDefaultLCID(void)
599 NtQueryDefaultLocale( FALSE, &lcid );
604 /***********************************************************************
605 * GetUserDefaultUILanguage (KERNEL32.@)
607 * Get the default user interface language Id for the current user.
613 * The current LANGID of the default UI language for the current user.
615 LANGID WINAPI GetUserDefaultUILanguage(void)
617 return GetUserDefaultLangID();
621 /***********************************************************************
622 * GetSystemDefaultUILanguage (KERNEL32.@)
624 * Get the default user interface language Id for the system.
630 * The current LANGID of the default UI language for the system. This is
631 * typically the same language used during the installation process.
633 LANGID WINAPI GetSystemDefaultUILanguage(void)
635 return GetSystemDefaultLangID();
639 /******************************************************************************
640 * get_locale_value_name
642 * Gets the registry value name for a given lctype.
644 static const WCHAR *get_locale_value_name( DWORD lctype )
646 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
647 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
648 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
649 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
650 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
651 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
652 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
653 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
654 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
655 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
656 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
657 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
658 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
659 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
660 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
661 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
662 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
663 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
664 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
665 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
666 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
667 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
668 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
669 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
670 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
671 static const WCHAR sListW[] = {'s','L','i','s','t',0};
672 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
673 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
674 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
675 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
676 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
677 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
678 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
679 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
680 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
681 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
682 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
683 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
684 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
688 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
689 * the values are stored in the registry, confirmed under Windows.
691 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
692 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
693 case LOCALE_ICURRENCY: return iCurrencyW;
694 case LOCALE_IDIGITS: return iDigitsW;
695 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
696 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
697 case LOCALE_ILZERO: return iLZeroW;
698 case LOCALE_IMEASURE: return iMeasureW;
699 case LOCALE_INEGCURR: return iNegCurrW;
700 case LOCALE_INEGNUMBER: return iNegNumberW;
701 case LOCALE_IPAPERSIZE: return iPaperSizeW;
702 case LOCALE_ITIME: return iTimeW;
703 case LOCALE_S1159: return s1159W;
704 case LOCALE_S2359: return s2359W;
705 case LOCALE_SCURRENCY: return sCurrencyW;
706 case LOCALE_SDATE: return sDateW;
707 case LOCALE_SDECIMAL: return sDecimalW;
708 case LOCALE_SGROUPING: return sGroupingW;
709 case LOCALE_SLIST: return sListW;
710 case LOCALE_SLONGDATE: return sLongDateW;
711 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
712 case LOCALE_SMONGROUPING: return sMonGroupingW;
713 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
714 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
715 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
716 case LOCALE_SSHORTDATE: return sShortDateW;
717 case LOCALE_STHOUSAND: return sThousandW;
718 case LOCALE_STIME: return sTimeW;
719 case LOCALE_STIMEFORMAT: return sTimeFormatW;
720 case LOCALE_SYEARMONTH: return sYearMonthW;
722 /* The following are not listed under MSDN as supported,
723 * but seem to be used and also stored in the registry.
725 case LOCALE_ICOUNTRY: return iCountryW;
726 case LOCALE_IDATE: return iDateW;
727 case LOCALE_ILDATE: return iLDateW;
728 case LOCALE_ITLZERO: return iTLZeroW;
729 case LOCALE_SCOUNTRY: return sCountryW;
730 case LOCALE_SLANGUAGE: return sLanguageW;
732 /* The following are used in XP and later */
733 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
734 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
735 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
741 /******************************************************************************
742 * get_registry_locale_info
744 * Retrieve user-modified locale info from the registry.
745 * Return length, 0 on error, -1 if not found.
747 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
753 UNICODE_STRING nameW;
754 KEY_VALUE_PARTIAL_INFORMATION *info;
755 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
757 if (!(hkey = create_registry_key())) return -1;
759 RtlInitUnicodeString( &nameW, value );
760 size = info_size + len * sizeof(WCHAR);
762 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
765 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
769 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
770 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
774 ret = (size - info_size) / sizeof(WCHAR);
775 /* append terminating null if needed */
776 if (!ret || ((WCHAR *)info->Data)[ret-1])
778 if (ret < len || !buffer) ret++;
781 SetLastError( ERROR_INSUFFICIENT_BUFFER );
787 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
793 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
796 SetLastError( RtlNtStatusToDosError(status) );
801 HeapFree( GetProcessHeap(), 0, info );
806 /******************************************************************************
807 * GetLocaleInfoA (KERNEL32.@)
809 * Get information about an aspect of a locale.
812 * lcid [I] LCID of the locale
813 * lctype [I] LCTYPE_ flags from "winnls.h"
814 * buffer [O] Destination for the information
815 * len [I] Length of buffer in characters
818 * Success: The size of the data requested. If buffer is non-NULL, it is filled
819 * with the information.
820 * Failure: 0. Use GetLastError() to determine the cause.
823 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
824 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
825 * which is a bit string.
827 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
832 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d)\n", lcid, lctype, buffer, len );
834 if (len < 0 || (len && !buffer))
836 SetLastError( ERROR_INVALID_PARAMETER );
839 if (!len) buffer = NULL;
841 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
843 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
845 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
848 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
850 if ((lctype & LOCALE_RETURN_NUMBER) ||
851 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
853 /* it's not an ASCII string, just bytes */
854 ret *= sizeof(WCHAR);
857 if (ret <= len) memcpy( buffer, bufferW, ret );
860 SetLastError( ERROR_INSUFFICIENT_BUFFER );
867 UINT codepage = CP_ACP;
868 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
869 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
872 HeapFree( GetProcessHeap(), 0, bufferW );
877 /******************************************************************************
878 * GetLocaleInfoW (KERNEL32.@)
880 * See GetLocaleInfoA.
882 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
892 if (len < 0 || (len && !buffer))
894 SetLastError( ERROR_INVALID_PARAMETER );
897 if (!len) buffer = NULL;
899 lcid = ConvertDefaultLocale(lcid);
901 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
904 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d)\n", lcid, lctype, buffer, len );
906 /* first check for overrides in the registry */
908 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
910 const WCHAR *value = get_locale_value_name(lctype);
914 if (lcflags & LOCALE_RETURN_NUMBER)
917 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
921 UINT number = strtolW( tmp, &end, 10 );
922 if (*end) /* invalid number */
924 SetLastError( ERROR_INVALID_FLAGS );
927 ret = sizeof(UINT)/sizeof(WCHAR);
928 if (!buffer) return ret;
931 SetLastError( ERROR_INSUFFICIENT_BUFFER );
934 memcpy( buffer, &number, sizeof(number) );
937 else ret = get_registry_locale_info( value, buffer, len );
939 if (ret != -1) return ret;
943 /* now load it from kernel resources */
945 lang_id = LANGIDFROMLCID( lcid );
947 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
948 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
949 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
951 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
952 (LPCWSTR)((lctype >> 4) + 1), lang_id )))
954 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
957 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
960 p = LockResource( hmem );
961 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
963 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
964 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
966 if (!buffer) return ret;
970 SetLastError( ERROR_INSUFFICIENT_BUFFER );
974 if (lcflags & LOCALE_RETURN_NUMBER)
977 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
979 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
981 number = strtolW( tmp, &end, 10 );
983 memcpy( buffer, &number, sizeof(number) );
984 else /* invalid number */
986 SetLastError( ERROR_INVALID_FLAGS );
989 HeapFree( GetProcessHeap(), 0, tmp );
991 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
992 lcid, lctype, buffer, len, number );
996 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
997 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
999 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
1000 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1006 /******************************************************************************
1007 * SetLocaleInfoA [KERNEL32.@]
1009 * Set information about an aspect of a locale.
1012 * lcid [I] LCID of the locale
1013 * lctype [I] LCTYPE_ flags from "winnls.h"
1014 * data [I] Information to set
1017 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1018 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1019 * Failure: FALSE. Use GetLastError() to determine the cause.
1022 * - Values are only be set for the current user locale; the system locale
1023 * settings cannot be changed.
1024 * - Any settings changed by this call are lost when the locale is changed by
1025 * the control panel (in Wine, this happens every time you change LANG).
1026 * - The native implementation of this function does not check that lcid matches
1027 * the current user locale, and simply sets the new values. Wine warns you in
1028 * this case, but behaves the same.
1030 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1032 UINT codepage = CP_ACP;
1037 lcid = ConvertDefaultLocale(lcid);
1039 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1043 SetLastError( ERROR_INVALID_PARAMETER );
1046 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1047 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1049 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1052 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1053 ret = SetLocaleInfoW( lcid, lctype, strW );
1054 HeapFree( GetProcessHeap(), 0, strW );
1059 /******************************************************************************
1060 * SetLocaleInfoW (KERNEL32.@)
1062 * See SetLocaleInfoA.
1064 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1067 static const WCHAR intlW[] = {'i','n','t','l',0 };
1068 UNICODE_STRING valueW;
1072 lcid = ConvertDefaultLocale(lcid);
1075 value = get_locale_value_name( lctype );
1077 if (!data || !value)
1079 SetLastError( ERROR_INVALID_PARAMETER );
1083 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1085 SetLastError( ERROR_INVALID_FLAGS );
1089 if (lcid != GetUserDefaultLCID())
1091 /* Windows does not check that the lcid matches the current lcid */
1092 WARN("locale 0x%08lx isn't the current locale (0x%08lx), setting anyway!\n",
1093 lcid, GetUserDefaultLCID());
1096 TRACE("setting %lx (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1098 /* FIXME: should check that data to set is sane */
1100 /* FIXME: profile functions should map to registry */
1101 WriteProfileStringW( intlW, value, data );
1103 if (!(hkey = create_registry_key())) return FALSE;
1104 RtlInitUnicodeString( &valueW, value );
1105 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1107 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1109 /* Set I-value from S value */
1110 WCHAR *lpD, *lpM, *lpY;
1113 lpD = strrchrW(data, 'd');
1114 lpM = strrchrW(data, 'M');
1115 lpY = strrchrW(data, 'y');
1119 szBuff[0] = '1'; /* D-M-Y */
1124 szBuff[0] = '2'; /* Y-M-D */
1126 szBuff[0] = '0'; /* M-D-Y */
1131 if (lctype == LOCALE_SSHORTDATE)
1132 lctype = LOCALE_IDATE;
1134 lctype = LOCALE_ILDATE;
1136 value = get_locale_value_name( lctype );
1138 WriteProfileStringW( intlW, value, szBuff );
1140 RtlInitUnicodeString( &valueW, value );
1141 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1146 if (status) SetLastError( RtlNtStatusToDosError(status) );
1151 /******************************************************************************
1152 * GetACP (KERNEL32.@)
1154 * Get the current Ansi code page Id for the system.
1160 * The current Ansi code page identifier for the system.
1162 UINT WINAPI GetACP(void)
1164 assert( ansi_cptable );
1165 return ansi_cptable->info.codepage;
1169 /***********************************************************************
1170 * GetOEMCP (KERNEL32.@)
1172 * Get the current OEM code page Id for the system.
1178 * The current OEM code page identifier for the system.
1180 UINT WINAPI GetOEMCP(void)
1182 assert( oem_cptable );
1183 return oem_cptable->info.codepage;
1187 /***********************************************************************
1188 * IsValidCodePage (KERNEL32.@)
1190 * Determine if a given code page identifier is valid.
1193 * codepage [I] Code page Id to verify.
1196 * TRUE, If codepage is valid and available on the system,
1199 BOOL WINAPI IsValidCodePage( UINT codepage )
1206 return wine_cp_get_table( codepage ) != NULL;
1211 /***********************************************************************
1212 * IsDBCSLeadByteEx (KERNEL32.@)
1214 * Determine if a character is a lead byte in a given code page.
1217 * codepage [I] Code page for the test.
1218 * testchar [I] Character to test
1221 * TRUE, if testchar is a lead byte in codepage,
1224 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1226 const union cptable *table = get_codepage_table( codepage );
1227 return table && is_dbcs_leadbyte( table, testchar );
1231 /***********************************************************************
1232 * IsDBCSLeadByte (KERNEL32.@)
1233 * IsDBCSLeadByte (KERNEL.207)
1235 * Determine if a character is a lead byte.
1238 * testchar [I] Character to test
1241 * TRUE, if testchar is a lead byte in the Ansii code page,
1244 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1246 if (!ansi_cptable) return FALSE;
1247 return is_dbcs_leadbyte( ansi_cptable, testchar );
1251 /***********************************************************************
1252 * GetCPInfo (KERNEL32.@)
1254 * Get information about a code page.
1257 * codepage [I] Code page number
1258 * cpinfo [O] Destination for code page information
1261 * Success: TRUE. cpinfo is updated with the information about codepage.
1262 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1264 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1266 const union cptable *table = get_codepage_table( codepage );
1270 SetLastError( ERROR_INVALID_PARAMETER );
1273 if (table->info.def_char & 0xff00)
1275 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
1276 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1280 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1281 cpinfo->DefaultChar[1] = 0;
1283 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1284 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1286 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1291 /***********************************************************************
1292 * GetCPInfoExA (KERNEL32.@)
1294 * Get extended information about a code page.
1297 * codepage [I] Code page number
1298 * dwFlags [I] Reserved, must to 0.
1299 * cpinfo [O] Destination for code page information
1302 * Success: TRUE. cpinfo is updated with the information about codepage.
1303 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1305 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1307 const union cptable *table = get_codepage_table( codepage );
1309 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1312 cpinfo->CodePage = table->info.codepage;
1313 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1314 strcpy(cpinfo->CodePageName, table->info.name);
1318 /***********************************************************************
1319 * GetCPInfoExW (KERNEL32.@)
1321 * Unicode version of GetCPInfoExA.
1323 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1325 const union cptable *table = get_codepage_table( codepage );
1327 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1330 cpinfo->CodePage = table->info.codepage;
1331 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1332 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1333 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1337 /***********************************************************************
1338 * EnumSystemCodePagesA (KERNEL32.@)
1340 * Call a user defined function for every code page installed on the system.
1343 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1344 * flags [I] Reserved, set to 0.
1347 * TRUE, If all code pages have been enumerated, or
1348 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1350 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1352 const union cptable *table;
1358 if (!(table = wine_cp_enum_table( index++ ))) break;
1359 sprintf( buffer, "%d", table->info.codepage );
1360 if (!lpfnCodePageEnum( buffer )) break;
1366 /***********************************************************************
1367 * EnumSystemCodePagesW (KERNEL32.@)
1369 * See EnumSystemCodePagesA.
1371 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1373 const union cptable *table;
1374 WCHAR buffer[10], *p;
1375 int page, index = 0;
1379 if (!(table = wine_cp_enum_table( index++ ))) break;
1380 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1382 page = table->info.codepage;
1385 *--p = '0' + (page % 10);
1388 if (!lpfnCodePageEnum( p )) break;
1394 /***********************************************************************
1395 * MultiByteToWideChar (KERNEL32.@)
1397 * Convert a multibyte character string into a Unicode string.
1400 * page [I] Codepage character set to convert from
1401 * flags [I] Character mapping flags
1402 * src [I] Source string buffer
1403 * srclen [I] Length of src, or -1 if src is NUL terminated
1404 * dst [O] Destination buffer
1405 * dstlen [I] Length of dst, or 0 to compute the required length
1408 * Success: If dstlen > 0, the number of characters written to dst.
1409 * If dstlen == 0, the number of characters needed to perform the
1410 * conversion. In both cases the count includes the terminating NUL.
1411 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1412 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1413 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1414 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1417 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1418 LPWSTR dst, INT dstlen )
1420 const union cptable *table;
1423 if (!src || (!dst && dstlen))
1425 SetLastError( ERROR_INVALID_PARAMETER );
1429 if (srclen < 0) srclen = strlen(src) + 1;
1431 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
1438 SetLastError( ERROR_INVALID_PARAMETER );
1441 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1444 FIXME("UTF-7 not supported\n");
1445 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1450 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1455 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1458 if (!(table = get_codepage_table( page )))
1460 SetLastError( ERROR_INVALID_PARAMETER );
1463 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1471 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1472 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1480 /***********************************************************************
1481 * WideCharToMultiByte (KERNEL32.@)
1483 * Convert a Unicode character string into a multibyte string.
1486 * page [I] Code page character set to convert to
1487 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1488 * src [I] Source string buffer
1489 * srclen [I] Length of src, or -1 if src is NUL terminated
1490 * dst [O] Destination buffer
1491 * dstlen [I] Length of dst, or 0 to compute the required length
1492 * defchar [I] Default character to use for conversion if no exact
1493 * conversion can be made
1494 * used [O] Set if default character was used in the conversion
1497 * Success: If dstlen > 0, the number of characters written to dst.
1498 * If dstlen == 0, number of characters needed to perform the
1499 * conversion. In both cases the count includes the terminating NUL.
1500 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1501 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1502 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1503 * parameter was given.
1505 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1506 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1508 const union cptable *table;
1511 if (!src || (!dst && dstlen))
1513 SetLastError( ERROR_INVALID_PARAMETER );
1517 if (srclen < 0) srclen = strlenW(src) + 1;
1522 if( flags || defchar || used)
1524 SetLastError( ERROR_INVALID_PARAMETER );
1527 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1530 FIXME("UTF-7 not supported\n");
1531 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1536 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1537 defchar, used ? &used_tmp : NULL );
1542 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1543 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1546 if (!(table = get_codepage_table( page )))
1548 SetLastError( ERROR_INVALID_PARAMETER );
1551 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1552 defchar, used ? &used_tmp : NULL );
1553 if (used) *used = used_tmp;
1561 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1562 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1566 TRACE("cp %d %s -> %s\n", page, debugstr_w(src), debugstr_a(dst));
1571 /***********************************************************************
1572 * GetThreadLocale (KERNEL32.@)
1574 * Get the current threads locale.
1580 * The LCID currently assocated with the calling thread.
1582 LCID WINAPI GetThreadLocale(void)
1584 LCID ret = NtCurrentTeb()->CurrentLocale;
1585 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1589 /**********************************************************************
1590 * SetThreadLocale (KERNEL32.@)
1592 * Set the current threads locale.
1595 * lcid [I] LCID of the locale to set
1598 * Success: TRUE. The threads locale is set to lcid.
1599 * Failure: FALSE. Use GetLastError() to determine the cause.
1601 BOOL WINAPI SetThreadLocale( LCID lcid )
1603 TRACE("(0x%04lX)\n", lcid);
1605 lcid = ConvertDefaultLocale(lcid);
1607 if (lcid != GetThreadLocale())
1609 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1611 SetLastError(ERROR_INVALID_PARAMETER);
1615 NtCurrentTeb()->CurrentLocale = lcid;
1616 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
1621 /******************************************************************************
1622 * ConvertDefaultLocale (KERNEL32.@)
1624 * Convert a default locale identifier into a real identifier.
1627 * lcid [I] LCID identifier of the locale to convert
1630 * lcid unchanged, if not a default locale or its sublanguage is
1631 * not SUBLANG_NEUTRAL.
1632 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1633 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1634 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1636 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1642 case LOCALE_SYSTEM_DEFAULT:
1643 lcid = GetSystemDefaultLCID();
1645 case LOCALE_USER_DEFAULT:
1646 case LOCALE_NEUTRAL:
1647 lcid = GetUserDefaultLCID();
1650 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1651 langid = LANGIDFROMLCID(lcid);
1652 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1654 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1655 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1662 /******************************************************************************
1663 * IsValidLocale (KERNEL32.@)
1665 * Determine if a locale is valid.
1668 * lcid [I] LCID of the locale to check
1669 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1672 * TRUE, if lcid is valid,
1676 * Wine does not currently make the distinction between supported and installed. All
1677 * languages supported are installed by default.
1679 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1681 /* check if language is registered in the kernel32 resources */
1682 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1683 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1687 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1688 LPCSTR name, WORD LangID, LONG lParam )
1690 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1693 sprintf(buf, "%08x", (UINT)LangID);
1694 return lpfnLocaleEnum( buf );
1697 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1698 LPCWSTR name, WORD LangID, LONG lParam )
1700 static const WCHAR formatW[] = {'%','0','8','x',0};
1701 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1703 sprintfW( buf, formatW, (UINT)LangID );
1704 return lpfnLocaleEnum( buf );
1707 /******************************************************************************
1708 * EnumSystemLocalesA (KERNEL32.@)
1710 * Call a users function for each locale available on the system.
1713 * lpfnLocaleEnum [I] Callback function to call for each locale
1714 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1718 * Failure: FALSE. Use GetLastError() to determine the cause.
1720 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1722 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1723 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
1724 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1725 (LONG)lpfnLocaleEnum);
1730 /******************************************************************************
1731 * EnumSystemLocalesW (KERNEL32.@)
1733 * See EnumSystemLocalesA.
1735 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1737 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1738 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
1739 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1740 (LONG)lpfnLocaleEnum);
1745 /***********************************************************************
1746 * VerLanguageNameA (KERNEL32.@)
1748 * Get the name of a language.
1751 * wLang [I] LANGID of the language
1752 * szLang [O] Destination for the language name
1755 * Success: The size of the language name. If szLang is non-NULL, it is filled
1757 * Failure: 0. Use GetLastError() to determine the cause.
1760 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1762 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1766 /***********************************************************************
1767 * VerLanguageNameW (KERNEL32.@)
1769 * See VerLanguageNameA.
1771 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1773 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1777 /******************************************************************************
1778 * GetStringTypeW (KERNEL32.@)
1780 * See GetStringTypeA.
1782 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1784 if (count == -1) count = strlenW(src) + 1;
1788 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1791 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
1795 WARN("CT_CTYPE3: semi-stub.\n");
1799 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1801 type1 = get_char_typeW( *src++ ) & 0xfff;
1802 /* try to construct type3 from type1 */
1803 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1804 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1805 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1806 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1807 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1808 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1809 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1811 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1812 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1813 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1814 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1815 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1816 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1817 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1818 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1820 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1821 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1822 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1823 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1824 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1825 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1826 *chartype++ = type3;
1831 SetLastError( ERROR_INVALID_PARAMETER );
1838 /******************************************************************************
1839 * GetStringTypeExW (KERNEL32.@)
1841 * See GetStringTypeExA.
1843 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1845 /* locale is ignored for Unicode */
1846 return GetStringTypeW( type, src, count, chartype );
1850 /******************************************************************************
1851 * GetStringTypeA (KERNEL32.@)
1853 * Get characteristics of the characters making up a string.
1856 * locale [I] Locale Id for the string
1857 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1858 * src [I] String to analyse
1859 * count [I] Length of src in chars, or -1 if src is NUL terminated
1860 * chartype [O] Destination for the calculated characteristics
1863 * Success: TRUE. chartype is filled with the requested characteristics of each char
1865 * Failure: FALSE. Use GetLastError() to determine the cause.
1867 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1874 if(count == -1) count = strlen(src) + 1;
1876 if (!(cp = get_lcid_codepage( locale )))
1878 FIXME("For locale %04lx using current ANSI code page\n", locale);
1882 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1883 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1885 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1887 * NOTE: the target buffer has 1 word for each CHARACTER in the source
1888 * string, with multibyte characters there maybe be more bytes in count
1889 * than character space in the buffer!
1891 ret = GetStringTypeW(type, srcW, countW, chartype);
1892 HeapFree(GetProcessHeap(), 0, srcW);
1897 /******************************************************************************
1898 * GetStringTypeExA (KERNEL32.@)
1900 * Get characteristics of the characters making up a string.
1903 * locale [I] Locale Id for the string
1904 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1905 * src [I] String to analyse
1906 * count [I] Length of src in chars, or -1 if src is NUL terminated
1907 * chartype [O] Destination for the calculated characteristics
1910 * Success: TRUE. chartype is filled with the requested characteristics of each char
1912 * Failure: FALSE. Use GetLastError() to determine the cause.
1914 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1916 return GetStringTypeA(locale, type, src, count, chartype);
1920 /*************************************************************************
1921 * LCMapStringW (KERNEL32.@)
1925 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1926 LPWSTR dst, INT dstlen)
1930 if (!src || !srclen || dstlen < 0)
1932 SetLastError(ERROR_INVALID_PARAMETER);
1936 /* mutually exclusive flags */
1937 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1938 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1939 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1940 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1942 SetLastError(ERROR_INVALID_FLAGS);
1946 if (!dstlen) dst = NULL;
1948 lcid = ConvertDefaultLocale(lcid);
1950 if (flags & LCMAP_SORTKEY)
1954 SetLastError(ERROR_INVALID_FLAGS);
1958 if (srclen < 0) srclen = strlenW(src);
1960 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1961 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1963 return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1966 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1967 if (flags & SORT_STRINGSORT)
1969 SetLastError(ERROR_INVALID_FLAGS);
1973 if (srclen < 0) srclen = strlenW(src) + 1;
1975 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1976 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1978 if (!dst) /* return required string length */
1982 for (len = 0; srclen; src++, srclen--)
1985 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1986 * and skips white space and punctuation characters for
1987 * NORM_IGNORESYMBOLS.
1989 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1996 if (flags & LCMAP_UPPERCASE)
1998 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2001 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2003 *dst_ptr++ = toupperW(wch);
2007 else if (flags & LCMAP_LOWERCASE)
2009 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2012 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2014 *dst_ptr++ = tolowerW(wch);
2022 SetLastError(ERROR_INVALID_FLAGS);
2025 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2028 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2037 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2041 return dst_ptr - dst;
2044 /*************************************************************************
2045 * LCMapStringA (KERNEL32.@)
2047 * Map characters in a locale sensitive string.
2050 * lcid [I] LCID for the conversion.
2051 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2052 * src [I] String to map
2053 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2054 * dst [O] Destination for mapped string
2055 * dstlen [I] Length of dst in characters
2058 * Success: The length of the mapped string in dst, including the NUL terminator.
2059 * Failure: 0. Use GetLastError() to determine the cause.
2061 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2062 LPSTR dst, INT dstlen)
2064 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2066 INT ret = 0, srclenW, dstlenW;
2069 if (!src || !srclen || dstlen < 0)
2071 SetLastError(ERROR_INVALID_PARAMETER);
2075 locale_cp = get_lcid_codepage(lcid);
2077 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2082 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2083 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2086 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2089 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2092 if (flags & LCMAP_SORTKEY)
2096 SetLastError(ERROR_INVALID_FLAGS);
2097 goto map_string_exit;
2099 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2100 goto map_string_exit;
2103 if (flags & SORT_STRINGSORT)
2105 SetLastError(ERROR_INVALID_FLAGS);
2106 goto map_string_exit;
2109 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2111 goto map_string_exit;
2113 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2116 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2117 goto map_string_exit;
2120 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2121 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2122 HeapFree(GetProcessHeap(), 0, dstW);
2125 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2129 /*************************************************************************
2130 * FoldStringA (KERNEL32.@)
2132 * Map characters in a string.
2135 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2136 * src [I] String to map
2137 * srclen [I] Length of src, or -1 if src is NUL terminated
2138 * dst [O] Destination for mapped string
2139 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2142 * Success: The length of the string written to dst, including the terminating NUL. If
2143 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2144 * and dst may be NULL.
2145 * Failure: 0. Use GetLastError() to determine the cause.
2147 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2148 LPSTR dst, INT dstlen)
2150 INT ret = 0, srclenW = 0;
2151 WCHAR *srcW = NULL, *dstW = NULL;
2153 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2155 SetLastError(ERROR_INVALID_PARAMETER);
2159 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2160 src, srclen, NULL, 0);
2161 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2165 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2166 goto FoldStringA_exit;
2169 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2170 src, srclen, srcW, srclenW);
2172 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2174 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2177 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2181 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2182 goto FoldStringA_exit;
2185 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2186 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2189 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2194 HeapFree(GetProcessHeap(), 0, dstW);
2198 HeapFree(GetProcessHeap(), 0, srcW);
2202 /*************************************************************************
2203 * FoldStringW (KERNEL32.@)
2207 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2208 LPWSTR dst, INT dstlen)
2212 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2217 /* Fall through for dwFlags == 0 */
2218 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2219 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2220 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2221 SetLastError(ERROR_INVALID_FLAGS);
2225 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2227 SetLastError(ERROR_INVALID_PARAMETER);
2231 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2233 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2237 /******************************************************************************
2238 * CompareStringW (KERNEL32.@)
2240 * See CompareStringA.
2242 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2243 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2249 SetLastError(ERROR_INVALID_PARAMETER);
2253 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2254 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|0x10000000) )
2256 SetLastError(ERROR_INVALID_FLAGS);
2260 if (style & 0x10000000)
2261 FIXME("Ignoring unknown style 0x10000000\n");
2263 if (len1 < 0) len1 = strlenW(str1);
2264 if (len2 < 0) len2 = strlenW(str2);
2266 ret = wine_compare_string(style, str1, len1, str2, len2);
2268 if (ret) /* need to translate result */
2269 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2273 /******************************************************************************
2274 * CompareStringA (KERNEL32.@)
2276 * Compare two locale sensitive strings.
2279 * lcid [I] LCID for the comparison
2280 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2281 * str1 [I] First string to compare
2282 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2283 * str2 [I] Second string to compare
2284 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2287 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2288 * str2 is less than, equal to or greater than str1 respectively.
2289 * Failure: FALSE. Use GetLastError() to determine the cause.
2291 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2292 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2294 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2295 WCHAR *buf2W = buf1W + 130;
2296 LPWSTR str1W, str2W;
2297 INT len1W, len2W, ret;
2302 SetLastError(ERROR_INVALID_PARAMETER);
2305 if (len1 < 0) len1 = strlen(str1);
2306 if (len2 < 0) len2 = strlen(str2);
2308 locale_cp = get_lcid_codepage(lcid);
2310 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2315 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2316 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2319 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2322 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2324 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2329 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2330 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2333 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2334 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2337 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2340 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2342 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2343 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2347 /*************************************************************************
2348 * lstrcmp (KERNEL32.@)
2349 * lstrcmpA (KERNEL32.@)
2351 * Compare two strings using the current thread locale.
2354 * str1 [I] First string to compare
2355 * str2 [I] Second string to compare
2358 * Success: A number less than, equal to or greater than 0 depending on whether
2359 * str2 is less than, equal to or greater than str1 respectively.
2360 * Failure: FALSE. Use GetLastError() to determine the cause.
2362 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2366 if ((str1 == NULL) && (str2 == NULL)) return 0;
2367 if (str1 == NULL) return -1;
2368 if (str2 == NULL) return 1;
2370 ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
2376 /*************************************************************************
2377 * lstrcmpi (KERNEL32.@)
2378 * lstrcmpiA (KERNEL32.@)
2380 * Compare two strings using the current thread locale, ignoring case.
2383 * str1 [I] First string to compare
2384 * str2 [I] Second string to compare
2387 * Success: A number less than, equal to or greater than 0 depending on whether
2388 * str2 is less than, equal to or greater than str1 respectively.
2389 * Failure: FALSE. Use GetLastError() to determine the cause.
2391 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2395 if ((str1 == NULL) && (str2 == NULL)) return 0;
2396 if (str1 == NULL) return -1;
2397 if (str2 == NULL) return 1;
2399 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2405 /*************************************************************************
2406 * lstrcmpW (KERNEL32.@)
2410 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2414 if ((str1 == NULL) && (str2 == NULL)) return 0;
2415 if (str1 == NULL) return -1;
2416 if (str2 == NULL) return 1;
2418 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2424 /*************************************************************************
2425 * lstrcmpiW (KERNEL32.@)
2429 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2433 if ((str1 == NULL) && (str2 == NULL)) return 0;
2434 if (str1 == NULL) return -1;
2435 if (str2 == NULL) return 1;
2437 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2443 /******************************************************************************
2446 void LOCALE_Init(void)
2448 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2449 const union cptable *unix_cp );
2451 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = ~0U;
2452 LCID lcid = init_default_lcid( &unix_cp );
2454 NtSetDefaultLocale( FALSE, lcid );
2455 NtSetDefaultLocale( TRUE, lcid );
2457 ansi_cp = get_lcid_codepage(lcid);
2458 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2459 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2460 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2461 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2463 GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2464 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2466 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2467 ansi_cptable = wine_cp_get_table( 1252 );
2468 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2469 oem_cptable = wine_cp_get_table( 437 );
2470 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2471 mac_cptable = wine_cp_get_table( 10000 );
2472 if (unix_cp != CP_UTF8)
2474 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2475 unix_cptable = wine_cp_get_table( 28591 );
2478 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2480 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2481 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2482 mac_cptable->info.codepage, unix_cp );
2485 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName)
2487 UNICODE_STRING keyName;
2488 OBJECT_ATTRIBUTES attr;
2491 RtlInitUnicodeString( &keyName, szKeyName );
2492 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2494 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2500 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName)
2502 HKEY hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2505 NtClose( hRootKey );
2510 static BOOL NLS_RegEnumSubKey(HKEY hKey, UINT ulIndex, LPWSTR szKeyName,
2514 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2517 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2518 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2519 info->NameLength > keyNameSize)
2524 TRACE("info->Name %s info->NameLength %ld\n", debugstr_w(info->Name), info->NameLength);
2526 memcpy( szKeyName, info->Name, info->NameLength);
2527 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2529 TRACE("returning %s\n", debugstr_w(szKeyName));
2533 static BOOL NLS_RegEnumValue(HKEY hKey, UINT ulIndex,
2534 LPWSTR szValueName, ULONG valueNameSize,
2535 LPWSTR szValueData, ULONG valueDataSize)
2538 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2541 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2542 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2543 info->NameLength > valueNameSize ||
2544 info->DataLength > valueDataSize)
2549 TRACE("info->Name %s info->DataLength %ld\n", debugstr_w(info->Name), info->DataLength);
2551 memcpy( szValueName, info->Name, info->NameLength);
2552 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2553 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2554 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2556 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2560 static BOOL NLS_RegGetDword(HKEY hKey, LPCWSTR szValueName, DWORD *lpVal)
2563 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2564 DWORD dwSize = sizeof(buffer);
2565 UNICODE_STRING valueName;
2567 RtlInitUnicodeString( &valueName, szValueName );
2569 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2570 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2571 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2572 info->DataLength == sizeof(DWORD))
2574 memcpy(lpVal, info->Data, sizeof(DWORD));
2581 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2584 LPCWSTR szResourceName = (LPCWSTR)(((lgrpid + 0x2000) >> 4) + 1);
2588 /* FIXME: Is it correct to use the system default langid? */
2589 langId = GetSystemDefaultLangID();
2591 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2592 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2594 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
2598 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
2602 ULONG iResourceIndex = lgrpid & 0xf;
2603 LPCWSTR lpResEntry = LockResource( hResDir );
2606 for (i = 0; i < iResourceIndex; i++)
2607 lpResEntry += *lpResEntry + 1;
2609 if (*lpResEntry < nameSize)
2611 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
2612 szName[*lpResEntry] = '\0';
2617 FreeResource( hResource );
2622 /* Registry keys for NLS related information */
2623 static const WCHAR szNlsKeyName[] = {
2624 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
2625 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
2626 'C','o','n','t','r','o','l','\\','N','l','s','\0'
2629 static const WCHAR szLangGroupsKeyName[] = {
2630 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
2633 static const WCHAR szCountryListName[] = {
2634 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
2635 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
2636 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2637 'T','e','l','e','p','h','o','n','y','\\',
2638 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
2642 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
2645 LANGUAGEGROUP_ENUMPROCA procA;
2646 LANGUAGEGROUP_ENUMPROCW procW;
2649 } ENUMLANGUAGEGROUP_CALLBACKS;
2651 /* Internal implementation of EnumSystemLanguageGroupsA/W */
2652 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
2654 WCHAR szNumber[10], szValue[4];
2656 BOOL bContinue = TRUE;
2661 SetLastError(ERROR_INVALID_PARAMETER);
2665 switch (lpProcs->dwFlags)
2668 /* Default to LGRPID_INSTALLED */
2669 lpProcs->dwFlags = LGRPID_INSTALLED;
2670 /* Fall through... */
2671 case LGRPID_INSTALLED:
2672 case LGRPID_SUPPORTED:
2675 SetLastError(ERROR_INVALID_FLAGS);
2679 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2682 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2686 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2687 szValue, sizeof(szValue) ))
2689 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2690 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2692 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2693 bInstalled ? "" : "not ");
2695 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2697 WCHAR szGrpName[48];
2699 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2700 szGrpName[0] = '\0';
2703 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2707 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2708 char szGrpNameA[48];
2710 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2711 * or whether the language names are ever localised. Assume CP_ACP.
2714 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2715 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2717 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2737 /******************************************************************************
2738 * EnumSystemLanguageGroupsA (KERNEL32.@)
2740 * Call a users function for each language group available on the system.
2743 * pLangGrpEnumProc [I] Callback function to call for each language group
2744 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2745 * lParam [I] User parameter to pass to pLangGrpEnumProc
2749 * Failure: FALSE. Use GetLastError() to determine the cause.
2751 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2752 DWORD dwFlags, LONG_PTR lParam)
2754 ENUMLANGUAGEGROUP_CALLBACKS procs;
2756 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2758 procs.procA = pLangGrpEnumProc;
2760 procs.dwFlags = dwFlags;
2761 procs.lParam = lParam;
2763 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2766 /******************************************************************************
2767 * EnumSystemLanguageGroupsW (KERNEL32.@)
2769 * See EnumSystemLanguageGroupsA.
2771 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2772 DWORD dwFlags, LONG_PTR lParam)
2774 ENUMLANGUAGEGROUP_CALLBACKS procs;
2776 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2779 procs.procW = pLangGrpEnumProc;
2780 procs.dwFlags = dwFlags;
2781 procs.lParam = lParam;
2783 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2786 /******************************************************************************
2787 * IsValidLanguageGroup (KERNEL32.@)
2789 * Determine if a language group is supported and/or installed.
2792 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
2793 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2796 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2799 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2801 static const WCHAR szFormat[] = { '%','x','\0' };
2802 WCHAR szValueName[16], szValue[2];
2803 BOOL bSupported = FALSE, bInstalled = FALSE;
2809 case LGRPID_INSTALLED:
2810 case LGRPID_SUPPORTED:
2812 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2814 sprintfW( szValueName, szFormat, lgrpid );
2816 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
2820 if (szValue[0] == '1')
2830 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2831 (dwFlags == LGRPID_INSTALLED && bInstalled))
2837 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2840 LANGGROUPLOCALE_ENUMPROCA procA;
2841 LANGGROUPLOCALE_ENUMPROCW procW;
2845 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2847 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2848 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2850 static const WCHAR szLocaleKeyName[] = {
2851 'L','o','c','a','l','e','\0'
2853 static const WCHAR szAlternateSortsKeyName[] = {
2854 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2856 WCHAR szNumber[10], szValue[4];
2858 BOOL bContinue = TRUE, bAlternate = FALSE;
2860 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
2862 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2864 SetLastError(ERROR_INVALID_PARAMETER);
2868 if (lpProcs->dwFlags)
2870 SetLastError(ERROR_INVALID_FLAGS);
2874 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
2877 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2881 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2882 szValue, sizeof(szValue) ))
2884 lgrpid = strtoulW( szValue, NULL, 16 );
2886 TRACE("lcid %s, grpid %ld (%smatched)\n", debugstr_w(szNumber),
2887 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2889 if (lgrpid == lpProcs->lgrpid)
2893 lcid = strtoulW( szNumber, NULL, 16 );
2895 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2896 * '00000437 ;Georgian'
2897 * At present we only pass the LCID string.
2901 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2904 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2906 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2908 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2916 /* Finished enumerating this key */
2919 /* Enumerate alternate sorts also */
2920 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2925 bContinue = FALSE; /* Finished both keys */
2938 /******************************************************************************
2939 * EnumLanguageGroupLocalesA (KERNEL32.@)
2941 * Call a users function for every locale in a language group available on the system.
2944 * pLangGrpLcEnumProc [I] Callback function to call for each locale
2945 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
2946 * dwFlags [I] Reserved, set to 0
2947 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
2951 * Failure: FALSE. Use GetLastError() to determine the cause.
2953 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
2954 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2956 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2958 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2960 callbacks.procA = pLangGrpLcEnumProc;
2961 callbacks.procW = NULL;
2962 callbacks.dwFlags = dwFlags;
2963 callbacks.lgrpid = lgrpid;
2964 callbacks.lParam = lParam;
2966 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2969 /******************************************************************************
2970 * EnumLanguageGroupLocalesW (KERNEL32.@)
2972 * See EnumLanguageGroupLocalesA.
2974 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
2975 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2977 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2979 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2981 callbacks.procA = NULL;
2982 callbacks.procW = pLangGrpLcEnumProc;
2983 callbacks.dwFlags = dwFlags;
2984 callbacks.lgrpid = lgrpid;
2985 callbacks.lParam = lParam;
2987 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2990 /******************************************************************************
2991 * EnumSystemGeoID (KERNEL32.@)
2993 * Call a users function for every location available on the system.
2996 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
2997 * reserved [I] Reserved, set to 0
2998 * pGeoEnumProc [I] Callback function to call for each location
3002 * Failure: FALSE. Use GetLastError() to determine the cause.
3004 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3006 static const WCHAR szCountryCodeValueName[] = {
3007 'C','o','u','n','t','r','y','C','o','d','e','\0'
3013 TRACE("(0x%08lX,0x%08lX,%p)\n", geoclass, reserved, pGeoEnumProc);
3015 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3017 SetLastError(ERROR_INVALID_PARAMETER);
3021 hKey = NLS_RegOpenKey( 0, szCountryListName );
3023 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3025 BOOL bContinue = TRUE;
3027 HKEY hSubKey = NLS_RegOpenKey( hKey, szNumber );
3031 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3033 TRACE("Got geoid %ld\n", dwGeoId);
3035 if (!pGeoEnumProc( dwGeoId ))
3054 /******************************************************************************
3055 * InvalidateNLSCache (KERNEL32.@)
3057 * Invalidate the cache of NLS values.
3066 BOOL WINAPI InvalidateNLSCache(void)
3072 /******************************************************************************
3073 * GetUserGeoID (KERNEL32.@)
3075 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3077 FIXME("%ld\n",GeoClass);
3078 return GEOID_NOT_AVAILABLE;
3081 /******************************************************************************
3082 * SetUserGeoID (KERNEL32.@)
3084 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3086 FIXME("%ld\n",GeoID);