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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
36 # include <CoreFoundation/CFLocale.h>
37 # include <CoreFoundation/CFString.h>
41 #define WIN32_NO_STATUS
44 #include "winuser.h" /* for RT_STRINGW */
46 #include "wine/unicode.h"
50 #include "kernel_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(nls);
55 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
57 /* current code pages */
58 static const union cptable *ansi_cptable;
59 static const union cptable *oem_cptable;
60 static const union cptable *mac_cptable;
61 static const union cptable *unix_cptable; /* NULL if UTF8 */
63 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName);
64 static HANDLE NLS_RegOpenSubKey(HANDLE hRootKey, LPCWSTR szKeyName);
66 static const WCHAR szNlsKeyName[] = {
67 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
68 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
69 'C','o','n','t','r','o','l','\\','N','l','s','\0'
72 /* Charset to codepage map, sorted by name. */
73 static const struct charset_entry
75 const char *charset_name;
114 { "ISO88591", 28591 },
115 { "ISO885910", 28600 },
116 { "ISO885913", 28603 },
117 { "ISO885914", 28604 },
118 { "ISO885915", 28605 },
119 { "ISO885916", 28606 },
120 { "ISO88592", 28592 },
121 { "ISO88593", 28593 },
122 { "ISO88594", 28594 },
123 { "ISO88595", 28595 },
124 { "ISO88596", 28596 },
125 { "ISO88597", 28597 },
126 { "ISO88598", 28598 },
127 { "ISO88599", 28599 },
136 WCHAR win_name[128]; /* Windows name ("en-US") */
137 WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
138 WCHAR *country; /* country ("US") */
139 WCHAR *charset; /* charset ("UTF-8") for Unix format only */
140 WCHAR *script; /* script ("Latn") for Windows format only */
141 WCHAR *modifier; /* modifier or sort order */
142 LCID lcid; /* corresponding LCID */
143 int matches; /* number of elements matching LCID (0..4) */
144 UINT codepage; /* codepage corresponding to charset */
147 /* locale ids corresponding to the various Unix locale parameters */
148 static LCID lcid_LC_COLLATE;
149 static LCID lcid_LC_CTYPE;
150 static LCID lcid_LC_MESSAGES;
151 static LCID lcid_LC_MONETARY;
152 static LCID lcid_LC_NUMERIC;
153 static LCID lcid_LC_TIME;
154 static LCID lcid_LC_PAPER;
155 static LCID lcid_LC_MEASUREMENT;
156 static LCID lcid_LC_TELEPHONE;
158 /* Copy Ascii string to Unicode without using codepages */
159 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
161 while (n > 1 && *src)
163 *dst++ = (unsigned char)*src++;
170 /***********************************************************************
173 * Retrieve the ANSI codepage for a given locale.
175 inline static UINT get_lcid_codepage( LCID lcid )
178 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
179 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
184 /***********************************************************************
187 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
189 static const union cptable *get_codepage_table( unsigned int codepage )
191 const union cptable *ret = NULL;
193 assert( ansi_cptable ); /* init must have been done already */
207 if (!(codepage = kernel_get_thread_data()->code_page)) return ansi_cptable;
210 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
211 if (codepage == oem_cptable->info.codepage) return oem_cptable;
212 if (codepage == mac_cptable->info.codepage) return mac_cptable;
213 ret = wine_cp_get_table( codepage );
220 /***********************************************************************
221 * charset_cmp (internal)
223 static int charset_cmp( const void *name, const void *entry )
225 const struct charset_entry *charset = (const struct charset_entry *)entry;
226 return strcasecmp( (const char *)name, charset->charset_name );
229 /***********************************************************************
232 static UINT find_charset( const WCHAR *name )
234 const struct charset_entry *entry;
235 char charset_name[16];
238 /* remove punctuation characters from charset name */
239 for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
240 if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
243 entry = bsearch( charset_name, charset_names,
244 sizeof(charset_names)/sizeof(charset_names[0]),
245 sizeof(charset_names[0]), charset_cmp );
246 if (entry) return entry->codepage;
251 /***********************************************************************
252 * find_locale_id_callback
254 static BOOL CALLBACK find_locale_id_callback( HMODULE hModule, LPCWSTR type,
255 LPCWSTR name, WORD LangID, LPARAM lParam )
257 struct locale_name *data = (struct locale_name *)lParam;
260 LCID lcid = MAKELCID( LangID, SORT_DEFAULT ); /* FIXME: handle sort order */
262 if (PRIMARYLANGID(LangID) == LANG_NEUTRAL) return TRUE; /* continue search */
264 /* first check exact name */
265 if (data->win_name[0] &&
266 GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE,
267 buffer, sizeof(buffer)/sizeof(WCHAR) ))
269 if (!strcmpW( data->win_name, buffer ))
271 matches = 4; /* everything matches */
276 if (!GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME | LOCALE_NOUSEROVERRIDE,
277 buffer, sizeof(buffer)/sizeof(WCHAR) ))
279 if (strcmpW( buffer, data->lang )) return TRUE;
280 matches++; /* language name matched */
284 if (GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
285 buffer, sizeof(buffer)/sizeof(WCHAR) ))
287 if (strcmpW( buffer, data->country )) goto done;
288 matches++; /* country name matched */
291 else /* match default language */
293 if (SUBLANGID(LangID) == SUBLANG_DEFAULT) matches++;
299 if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
300 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ))
302 if (unix_cp == data->codepage) matches++;
306 /* FIXME: check sort order */
309 if (matches > data->matches)
312 data->matches = matches;
314 return (data->matches < 4); /* no need to continue for perfect match */
318 /***********************************************************************
321 * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
322 * Unix format is: lang[_country][.charset][@modifier]
323 * Windows format is: lang[-script][-country][_modifier]
325 static void parse_locale_name( const WCHAR *str, struct locale_name *name )
327 static const WCHAR sepW[] = {'-','_','.','@'};
328 static const WCHAR winsepW[] = {'-','_'};
329 static const WCHAR posixW[] = {'P','O','S','I','X',0};
330 static const WCHAR cW[] = {'C',0};
331 static const WCHAR latinW[] = {'l','a','t','i','n',0};
332 static const WCHAR latnW[] = {'-','L','a','t','n',0};
335 name->country = name->charset = name->script = name->modifier = NULL;
336 name->lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
339 name->win_name[0] = 0;
340 lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
342 if (!(p = strpbrkW( name->lang, sepW )))
344 if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
346 name->matches = 4; /* perfect match for default English lcid */
349 strcpyW( name->win_name, name->lang );
351 else if (*p == '-') /* Windows format */
353 strcpyW( name->win_name, name->lang );
356 if (!(p = strpbrkW( p, winsepW ))) goto done;
360 name->script = name->country;
362 if (!(p = strpbrkW( p, winsepW ))) goto done;
367 else /* Unix format */
373 p = strpbrkW( p, sepW + 2 );
379 name->codepage = find_charset( name->charset );
380 p = strchrW( p, '@' );
388 /* rebuild a Windows name if possible */
390 if (name->charset) goto done; /* can't specify charset in Windows format */
391 if (name->modifier && strcmpW( name->modifier, latinW ))
392 goto done; /* only Latn script supported for now */
393 strcpyW( name->win_name, name->lang );
394 if (name->modifier) strcatW( name->win_name, latnW );
397 p = name->win_name + strlenW(name->win_name);
399 strcpyW( p, name->country );
403 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
404 find_locale_id_callback, (LPARAM)name );
408 /***********************************************************************
409 * convert_default_lcid
411 * Get the default LCID to use for a given lctype in GetLocaleInfo.
413 static LCID convert_default_lcid( LCID lcid, LCTYPE lctype )
415 if (lcid == LOCALE_SYSTEM_DEFAULT ||
416 lcid == LOCALE_USER_DEFAULT ||
417 lcid == LOCALE_NEUTRAL)
421 switch(lctype & 0xffff)
423 case LOCALE_SSORTNAME:
424 default_id = lcid_LC_COLLATE;
427 case LOCALE_FONTSIGNATURE:
428 case LOCALE_IDEFAULTANSICODEPAGE:
429 case LOCALE_IDEFAULTCODEPAGE:
430 case LOCALE_IDEFAULTEBCDICCODEPAGE:
431 case LOCALE_IDEFAULTMACCODEPAGE:
432 case LOCALE_IDEFAULTUNIXCODEPAGE:
433 default_id = lcid_LC_CTYPE;
436 case LOCALE_ICURRDIGITS:
437 case LOCALE_ICURRENCY:
438 case LOCALE_IINTLCURRDIGITS:
439 case LOCALE_INEGCURR:
440 case LOCALE_INEGSEPBYSPACE:
441 case LOCALE_INEGSIGNPOSN:
442 case LOCALE_INEGSYMPRECEDES:
443 case LOCALE_IPOSSEPBYSPACE:
444 case LOCALE_IPOSSIGNPOSN:
445 case LOCALE_IPOSSYMPRECEDES:
446 case LOCALE_SCURRENCY:
447 case LOCALE_SINTLSYMBOL:
448 case LOCALE_SMONDECIMALSEP:
449 case LOCALE_SMONGROUPING:
450 case LOCALE_SMONTHOUSANDSEP:
451 case LOCALE_SNATIVECURRNAME:
452 default_id = lcid_LC_MONETARY;
456 case LOCALE_IDIGITSUBSTITUTION:
458 case LOCALE_INEGNUMBER:
459 case LOCALE_SDECIMAL:
460 case LOCALE_SGROUPING:
462 case LOCALE_SNATIVEDIGITS:
463 case LOCALE_SNEGATIVESIGN:
464 case LOCALE_SNEGINFINITY:
465 case LOCALE_SPOSINFINITY:
466 case LOCALE_SPOSITIVESIGN:
467 case LOCALE_STHOUSAND:
468 default_id = lcid_LC_NUMERIC;
471 case LOCALE_ICALENDARTYPE:
472 case LOCALE_ICENTURY:
474 case LOCALE_IDAYLZERO:
475 case LOCALE_IFIRSTDAYOFWEEK:
476 case LOCALE_IFIRSTWEEKOFYEAR:
478 case LOCALE_IMONLZERO:
479 case LOCALE_IOPTIONALCALENDAR:
481 case LOCALE_ITIMEMARKPOSN:
485 case LOCALE_SABBREVDAYNAME1:
486 case LOCALE_SABBREVDAYNAME2:
487 case LOCALE_SABBREVDAYNAME3:
488 case LOCALE_SABBREVDAYNAME4:
489 case LOCALE_SABBREVDAYNAME5:
490 case LOCALE_SABBREVDAYNAME6:
491 case LOCALE_SABBREVDAYNAME7:
492 case LOCALE_SABBREVMONTHNAME1:
493 case LOCALE_SABBREVMONTHNAME2:
494 case LOCALE_SABBREVMONTHNAME3:
495 case LOCALE_SABBREVMONTHNAME4:
496 case LOCALE_SABBREVMONTHNAME5:
497 case LOCALE_SABBREVMONTHNAME6:
498 case LOCALE_SABBREVMONTHNAME7:
499 case LOCALE_SABBREVMONTHNAME8:
500 case LOCALE_SABBREVMONTHNAME9:
501 case LOCALE_SABBREVMONTHNAME10:
502 case LOCALE_SABBREVMONTHNAME11:
503 case LOCALE_SABBREVMONTHNAME12:
504 case LOCALE_SABBREVMONTHNAME13:
506 case LOCALE_SDAYNAME1:
507 case LOCALE_SDAYNAME2:
508 case LOCALE_SDAYNAME3:
509 case LOCALE_SDAYNAME4:
510 case LOCALE_SDAYNAME5:
511 case LOCALE_SDAYNAME6:
512 case LOCALE_SDAYNAME7:
513 case LOCALE_SDURATION:
514 case LOCALE_SLONGDATE:
515 case LOCALE_SMONTHNAME1:
516 case LOCALE_SMONTHNAME2:
517 case LOCALE_SMONTHNAME3:
518 case LOCALE_SMONTHNAME4:
519 case LOCALE_SMONTHNAME5:
520 case LOCALE_SMONTHNAME6:
521 case LOCALE_SMONTHNAME7:
522 case LOCALE_SMONTHNAME8:
523 case LOCALE_SMONTHNAME9:
524 case LOCALE_SMONTHNAME10:
525 case LOCALE_SMONTHNAME11:
526 case LOCALE_SMONTHNAME12:
527 case LOCALE_SMONTHNAME13:
528 case LOCALE_SSHORTDATE:
529 case LOCALE_SSHORTESTDAYNAME1:
530 case LOCALE_SSHORTESTDAYNAME2:
531 case LOCALE_SSHORTESTDAYNAME3:
532 case LOCALE_SSHORTESTDAYNAME4:
533 case LOCALE_SSHORTESTDAYNAME5:
534 case LOCALE_SSHORTESTDAYNAME6:
535 case LOCALE_SSHORTESTDAYNAME7:
537 case LOCALE_STIMEFORMAT:
538 case LOCALE_SYEARMONTH:
539 default_id = lcid_LC_TIME;
542 case LOCALE_IPAPERSIZE:
543 default_id = lcid_LC_PAPER;
546 case LOCALE_IMEASURE:
547 default_id = lcid_LC_MEASUREMENT;
550 case LOCALE_ICOUNTRY:
551 default_id = lcid_LC_TELEPHONE;
554 if (default_id) lcid = default_id;
556 return ConvertDefaultLocale( lcid );
560 /***********************************************************************
561 * create_registry_key
563 * Create the Control Panel\\International registry key.
565 inline static HANDLE create_registry_key(void)
567 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
568 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
569 OBJECT_ATTRIBUTES attr;
570 UNICODE_STRING nameW;
573 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
575 attr.Length = sizeof(attr);
576 attr.RootDirectory = hkey;
577 attr.ObjectName = &nameW;
579 attr.SecurityDescriptor = NULL;
580 attr.SecurityQualityOfService = NULL;
581 RtlInitUnicodeString( &nameW, intlW );
583 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
584 NtClose( attr.RootDirectory );
589 /* update the registry settings for a given locale parameter */
590 /* return TRUE if an update was needed */
591 static BOOL locale_update_registry( HKEY hkey, const WCHAR *name, LCID lcid,
592 const LCTYPE *values, UINT nb_values )
594 static const WCHAR formatW[] = { '%','0','8','x',0 };
596 UNICODE_STRING nameW;
599 RtlInitUnicodeString( &nameW, name );
600 count = sizeof(bufferW);
601 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
603 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
604 LPCWSTR text = (LPCWSTR)info->Data;
606 if (strtoulW( text, NULL, 16 ) == lcid) return FALSE; /* already set correctly */
607 TRACE( "updating registry, locale %s changed %s -> %08x\n",
608 debugstr_w(name), debugstr_w(text), lcid );
610 else TRACE( "updating registry, locale %s changed none -> %08x\n", debugstr_w(name), lcid );
611 sprintfW( bufferW, formatW, lcid );
612 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR) );
614 for (i = 0; i < nb_values; i++)
616 GetLocaleInfoW( lcid, values[i] | LOCALE_NOUSEROVERRIDE, bufferW,
617 sizeof(bufferW)/sizeof(WCHAR) );
618 SetLocaleInfoW( lcid, values[i], bufferW );
624 /***********************************************************************
625 * LOCALE_InitRegistry
627 * Update registry contents on startup if the user locale has changed.
628 * This simulates the action of the Windows control panel.
630 void LOCALE_InitRegistry(void)
632 static const WCHAR CodepageW[] = {'C','o','d','e','p','a','g','e',0};
633 static const WCHAR acpW[] = {'A','C','P',0};
634 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
635 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
636 static const WCHAR localeW[] = {'L','o','c','a','l','e',0};
637 static const WCHAR lc_ctypeW[] = { 'L','C','_','C','T','Y','P','E',0 };
638 static const WCHAR lc_monetaryW[] = { 'L','C','_','M','O','N','E','T','A','R','Y',0 };
639 static const WCHAR lc_numericW[] = { 'L','C','_','N','U','M','E','R','I','C',0 };
640 static const WCHAR lc_timeW[] = { 'L','C','_','T','I','M','E',0 };
641 static const WCHAR lc_measurementW[] = { 'L','C','_','M','E','A','S','U','R','E','M','E','N','T',0 };
642 static const WCHAR lc_telephoneW[] = { 'L','C','_','T','E','L','E','P','H','O','N','E',0 };
647 } update_cp_values[] = {
648 { acpW, LOCALE_IDEFAULTANSICODEPAGE },
649 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
650 { maccpW, LOCALE_IDEFAULTMACCODEPAGE }
652 static const LCTYPE lc_messages_values[] = {
656 static const LCTYPE lc_monetary_values[] = {
662 static const LCTYPE lc_numeric_values[] = {
666 LOCALE_IDIGITSUBSTITUTION,
667 LOCALE_SNATIVEDIGITS };
668 static const LCTYPE lc_time_values[] = {
677 LOCALE_ITIMEMARKPOSN,
678 LOCALE_ICALENDARTYPE };
679 static const LCTYPE lc_measurement_values[] = { LOCALE_IMEASURE };
680 static const LCTYPE lc_telephone_values[] = { LOCALE_ICOUNTRY };
682 UNICODE_STRING nameW;
686 LCID lcid = GetUserDefaultLCID();
688 if (!(hkey = create_registry_key()))
689 return; /* don't do anything if we can't create the registry key */
691 locale_update_registry( hkey, localeW, lcid_LC_MESSAGES, lc_messages_values,
692 sizeof(lc_messages_values)/sizeof(lc_messages_values[0]) );
693 locale_update_registry( hkey, lc_monetaryW, lcid_LC_MONETARY, lc_monetary_values,
694 sizeof(lc_monetary_values)/sizeof(lc_monetary_values[0]) );
695 locale_update_registry( hkey, lc_numericW, lcid_LC_NUMERIC, lc_numeric_values,
696 sizeof(lc_numeric_values)/sizeof(lc_numeric_values[0]) );
697 locale_update_registry( hkey, lc_timeW, lcid_LC_TIME, lc_time_values,
698 sizeof(lc_time_values)/sizeof(lc_time_values[0]) );
699 locale_update_registry( hkey, lc_measurementW, lcid_LC_MEASUREMENT, lc_measurement_values,
700 sizeof(lc_measurement_values)/sizeof(lc_measurement_values[0]) );
701 locale_update_registry( hkey, lc_telephoneW, lcid_LC_TELEPHONE, lc_telephone_values,
702 sizeof(lc_telephone_values)/sizeof(lc_telephone_values[0]) );
704 if (locale_update_registry( hkey, lc_ctypeW, lcid_LC_CTYPE, NULL, 0 ))
706 HKEY nls_key = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), CodepageW );
708 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
710 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
711 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
712 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
713 NtSetValueKey( nls_key, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
722 /***********************************************************************
725 static UINT setup_unix_locales(void)
727 struct locale_name locale_name;
728 WCHAR buffer[128], ctype_buff[128];
732 if ((locale = setlocale( LC_CTYPE, NULL )))
734 strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
735 parse_locale_name( ctype_buff, &locale_name );
736 lcid_LC_CTYPE = locale_name.lcid;
737 unix_cp = locale_name.codepage;
739 if (!lcid_LC_CTYPE) /* this one needs a default value */
740 lcid_LC_CTYPE = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
742 TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
743 locale_name.lcid, locale_name.matches, debugstr_a(locale) );
745 #define GET_UNIX_LOCALE(cat) do \
746 if ((locale = setlocale( cat, NULL ))) \
748 strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
749 if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
751 parse_locale_name( buffer, &locale_name ); \
752 lcid_##cat = locale_name.lcid; \
753 TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
754 locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
758 GET_UNIX_LOCALE( LC_COLLATE );
759 GET_UNIX_LOCALE( LC_MESSAGES );
760 GET_UNIX_LOCALE( LC_MONETARY );
761 GET_UNIX_LOCALE( LC_NUMERIC );
762 GET_UNIX_LOCALE( LC_TIME );
764 GET_UNIX_LOCALE( LC_PAPER );
766 #ifdef LC_MEASUREMENT
767 GET_UNIX_LOCALE( LC_MEASUREMENT );
770 GET_UNIX_LOCALE( LC_TELEPHONE );
773 #undef GET_UNIX_LOCALE
779 /***********************************************************************
780 * GetUserDefaultLangID (KERNEL32.@)
782 * Get the default language Id for the current user.
788 * The current LANGID of the default language for the current user.
790 LANGID WINAPI GetUserDefaultLangID(void)
792 return LANGIDFROMLCID(GetUserDefaultLCID());
796 /***********************************************************************
797 * GetSystemDefaultLangID (KERNEL32.@)
799 * Get the default language Id for the system.
805 * The current LANGID of the default language for the system.
807 LANGID WINAPI GetSystemDefaultLangID(void)
809 return LANGIDFROMLCID(GetSystemDefaultLCID());
813 /***********************************************************************
814 * GetUserDefaultLCID (KERNEL32.@)
816 * Get the default locale Id for the current user.
822 * The current LCID of the default locale for the current user.
824 LCID WINAPI GetUserDefaultLCID(void)
827 NtQueryDefaultLocale( TRUE, &lcid );
832 /***********************************************************************
833 * GetSystemDefaultLCID (KERNEL32.@)
835 * Get the default locale Id for the system.
841 * The current LCID of the default locale for the system.
843 LCID WINAPI GetSystemDefaultLCID(void)
846 NtQueryDefaultLocale( FALSE, &lcid );
851 /***********************************************************************
852 * GetUserDefaultUILanguage (KERNEL32.@)
854 * Get the default user interface language Id for the current user.
860 * The current LANGID of the default UI language for the current user.
862 LANGID WINAPI GetUserDefaultUILanguage(void)
865 NtQueryDefaultUILanguage( &lang );
870 /***********************************************************************
871 * GetSystemDefaultUILanguage (KERNEL32.@)
873 * Get the default user interface language Id for the system.
879 * The current LANGID of the default UI language for the system. This is
880 * typically the same language used during the installation process.
882 LANGID WINAPI GetSystemDefaultUILanguage(void)
885 NtQueryInstallUILanguage( &lang );
890 /***********************************************************************
891 * LocaleNameToLCID (KERNEL32.@)
893 LCID WINAPI LocaleNameToLCID( LPCWSTR name, DWORD flags )
895 struct locale_name locale_name;
897 if (flags) FIXME( "unsupported flags %x\n", flags );
899 parse_locale_name( name, &locale_name );
901 TRACE( "found lcid %x for %s, matches %d\n",
902 locale_name.lcid, debugstr_w(name), locale_name.matches );
904 if (!locale_name.matches)
905 WARN( "locale %s not recognized, defaulting to English\n", debugstr_w(name) );
906 else if (locale_name.matches == 1)
907 WARN( "locale %s not recognized, defaulting to %s\n",
908 debugstr_w(name), debugstr_w(locale_name.lang) );
910 return locale_name.lcid;
914 /***********************************************************************
915 * LCIDToLocaleName (KERNEL32.@)
917 INT WINAPI LCIDToLocaleName( LCID lcid, LPWSTR name, INT count, DWORD flags )
919 if (flags) FIXME( "unsupported flags %x\n", flags );
921 return GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, name, count );
925 /******************************************************************************
926 * get_locale_value_name
928 * Gets the registry value name for a given lctype.
930 static const WCHAR *get_locale_value_name( DWORD lctype )
932 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
933 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
934 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
935 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
936 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
937 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
938 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
939 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
940 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
941 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
942 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
943 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
944 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
945 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
946 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
947 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
948 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
949 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
950 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
951 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
952 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
953 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
954 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
955 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
956 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
957 static const WCHAR sListW[] = {'s','L','i','s','t',0};
958 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
959 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
960 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
961 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
962 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
963 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
964 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
965 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
966 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
967 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
968 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
969 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
970 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
974 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
975 * the values are stored in the registry, confirmed under Windows.
977 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
978 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
979 case LOCALE_ICURRENCY: return iCurrencyW;
980 case LOCALE_IDIGITS: return iDigitsW;
981 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
982 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
983 case LOCALE_ILZERO: return iLZeroW;
984 case LOCALE_IMEASURE: return iMeasureW;
985 case LOCALE_INEGCURR: return iNegCurrW;
986 case LOCALE_INEGNUMBER: return iNegNumberW;
987 case LOCALE_IPAPERSIZE: return iPaperSizeW;
988 case LOCALE_ITIME: return iTimeW;
989 case LOCALE_S1159: return s1159W;
990 case LOCALE_S2359: return s2359W;
991 case LOCALE_SCURRENCY: return sCurrencyW;
992 case LOCALE_SDATE: return sDateW;
993 case LOCALE_SDECIMAL: return sDecimalW;
994 case LOCALE_SGROUPING: return sGroupingW;
995 case LOCALE_SLIST: return sListW;
996 case LOCALE_SLONGDATE: return sLongDateW;
997 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
998 case LOCALE_SMONGROUPING: return sMonGroupingW;
999 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
1000 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
1001 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
1002 case LOCALE_SSHORTDATE: return sShortDateW;
1003 case LOCALE_STHOUSAND: return sThousandW;
1004 case LOCALE_STIME: return sTimeW;
1005 case LOCALE_STIMEFORMAT: return sTimeFormatW;
1006 case LOCALE_SYEARMONTH: return sYearMonthW;
1008 /* The following are not listed under MSDN as supported,
1009 * but seem to be used and also stored in the registry.
1011 case LOCALE_ICOUNTRY: return iCountryW;
1012 case LOCALE_IDATE: return iDateW;
1013 case LOCALE_ILDATE: return iLDateW;
1014 case LOCALE_ITLZERO: return iTLZeroW;
1015 case LOCALE_SCOUNTRY: return sCountryW;
1016 case LOCALE_SLANGUAGE: return sLanguageW;
1018 /* The following are used in XP and later */
1019 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
1020 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
1021 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
1027 /******************************************************************************
1028 * get_registry_locale_info
1030 * Retrieve user-modified locale info from the registry.
1031 * Return length, 0 on error, -1 if not found.
1033 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
1039 UNICODE_STRING nameW;
1040 KEY_VALUE_PARTIAL_INFORMATION *info;
1041 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
1043 if (!(hkey = create_registry_key())) return -1;
1045 RtlInitUnicodeString( &nameW, value );
1046 size = info_size + len * sizeof(WCHAR);
1048 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
1051 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1055 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
1059 ret = (size - info_size) / sizeof(WCHAR);
1060 /* append terminating null if needed */
1061 if (!ret || ((WCHAR *)info->Data)[ret-1])
1063 if (ret < len || !buffer) ret++;
1066 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1072 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
1076 else if (status == STATUS_BUFFER_OVERFLOW && !buffer)
1078 ret = (size - info_size) / sizeof(WCHAR) + 1;
1080 else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1086 SetLastError( RtlNtStatusToDosError(status) );
1090 HeapFree( GetProcessHeap(), 0, info );
1095 /******************************************************************************
1096 * GetLocaleInfoA (KERNEL32.@)
1098 * Get information about an aspect of a locale.
1101 * lcid [I] LCID of the locale
1102 * lctype [I] LCTYPE_ flags from "winnls.h"
1103 * buffer [O] Destination for the information
1104 * len [I] Length of buffer in characters
1107 * Success: The size of the data requested. If buffer is non-NULL, it is filled
1108 * with the information.
1109 * Failure: 0. Use GetLastError() to determine the cause.
1112 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
1113 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
1114 * which is a bit string.
1116 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
1121 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1123 if (len < 0 || (len && !buffer))
1125 SetLastError( ERROR_INVALID_PARAMETER );
1128 if (!len) buffer = NULL;
1130 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
1132 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1134 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1137 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
1139 if ((lctype & LOCALE_RETURN_NUMBER) ||
1140 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
1142 /* it's not an ASCII string, just bytes */
1143 ret *= sizeof(WCHAR);
1146 if (ret <= len) memcpy( buffer, bufferW, ret );
1149 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1156 UINT codepage = CP_ACP;
1157 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1158 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
1161 HeapFree( GetProcessHeap(), 0, bufferW );
1166 /******************************************************************************
1167 * GetLocaleInfoW (KERNEL32.@)
1169 * See GetLocaleInfoA.
1171 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
1181 if (len < 0 || (len && !buffer))
1183 SetLastError( ERROR_INVALID_PARAMETER );
1186 if (!len) buffer = NULL;
1188 lcid = convert_default_lcid( lcid, lctype );
1190 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
1193 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1195 /* first check for overrides in the registry */
1197 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
1198 lcid == convert_default_lcid( LOCALE_USER_DEFAULT, lctype ))
1200 const WCHAR *value = get_locale_value_name(lctype);
1204 if (lcflags & LOCALE_RETURN_NUMBER)
1207 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
1211 UINT number = strtolW( tmp, &end, 10 );
1212 if (*end) /* invalid number */
1214 SetLastError( ERROR_INVALID_FLAGS );
1217 ret = sizeof(UINT)/sizeof(WCHAR);
1218 if (!buffer) return ret;
1221 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1224 memcpy( buffer, &number, sizeof(number) );
1227 else ret = get_registry_locale_info( value, buffer, len );
1229 if (ret != -1) return ret;
1233 /* now load it from kernel resources */
1235 lang_id = LANGIDFROMLCID( lcid );
1237 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
1238 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
1239 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
1241 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1242 (LPCWSTR)((lctype >> 4) + 1), lang_id )))
1244 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
1247 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
1250 p = LockResource( hmem );
1251 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1253 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1254 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1256 if (!buffer) return ret;
1260 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1264 if (lcflags & LOCALE_RETURN_NUMBER)
1267 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1269 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1271 number = strtolW( tmp, &end, 10 );
1273 memcpy( buffer, &number, sizeof(number) );
1274 else /* invalid number */
1276 SetLastError( ERROR_INVALID_FLAGS );
1279 HeapFree( GetProcessHeap(), 0, tmp );
1281 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
1282 lcid, lctype, buffer, len, number );
1286 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
1287 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1289 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
1290 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1296 /******************************************************************************
1297 * SetLocaleInfoA [KERNEL32.@]
1299 * Set information about an aspect of a locale.
1302 * lcid [I] LCID of the locale
1303 * lctype [I] LCTYPE_ flags from "winnls.h"
1304 * data [I] Information to set
1307 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1308 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1309 * Failure: FALSE. Use GetLastError() to determine the cause.
1312 * - Values are only be set for the current user locale; the system locale
1313 * settings cannot be changed.
1314 * - Any settings changed by this call are lost when the locale is changed by
1315 * the control panel (in Wine, this happens every time you change LANG).
1316 * - The native implementation of this function does not check that lcid matches
1317 * the current user locale, and simply sets the new values. Wine warns you in
1318 * this case, but behaves the same.
1320 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1322 UINT codepage = CP_ACP;
1327 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1331 SetLastError( ERROR_INVALID_PARAMETER );
1334 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1335 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1337 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1340 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1341 ret = SetLocaleInfoW( lcid, lctype, strW );
1342 HeapFree( GetProcessHeap(), 0, strW );
1347 /******************************************************************************
1348 * SetLocaleInfoW (KERNEL32.@)
1350 * See SetLocaleInfoA.
1352 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1355 static const WCHAR intlW[] = {'i','n','t','l',0 };
1356 UNICODE_STRING valueW;
1361 value = get_locale_value_name( lctype );
1363 if (!data || !value)
1365 SetLastError( ERROR_INVALID_PARAMETER );
1369 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1371 SetLastError( ERROR_INVALID_FLAGS );
1375 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1377 /* FIXME: should check that data to set is sane */
1379 /* FIXME: profile functions should map to registry */
1380 WriteProfileStringW( intlW, value, data );
1382 if (!(hkey = create_registry_key())) return FALSE;
1383 RtlInitUnicodeString( &valueW, value );
1384 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1386 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1388 /* Set I-value from S value */
1389 WCHAR *lpD, *lpM, *lpY;
1392 lpD = strrchrW(data, 'd');
1393 lpM = strrchrW(data, 'M');
1394 lpY = strrchrW(data, 'y');
1398 szBuff[0] = '1'; /* D-M-Y */
1403 szBuff[0] = '2'; /* Y-M-D */
1405 szBuff[0] = '0'; /* M-D-Y */
1410 if (lctype == LOCALE_SSHORTDATE)
1411 lctype = LOCALE_IDATE;
1413 lctype = LOCALE_ILDATE;
1415 value = get_locale_value_name( lctype );
1417 WriteProfileStringW( intlW, value, szBuff );
1419 RtlInitUnicodeString( &valueW, value );
1420 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1425 if (status) SetLastError( RtlNtStatusToDosError(status) );
1430 /******************************************************************************
1431 * GetACP (KERNEL32.@)
1433 * Get the current Ansi code page Id for the system.
1439 * The current Ansi code page identifier for the system.
1441 UINT WINAPI GetACP(void)
1443 assert( ansi_cptable );
1444 return ansi_cptable->info.codepage;
1448 /******************************************************************************
1449 * SetCPGlobal (KERNEL32.@)
1451 * Set the current Ansi code page Id for the system.
1454 * acp [I] code page ID to be the new ACP.
1459 UINT WINAPI SetCPGlobal( UINT acp )
1461 UINT ret = GetACP();
1462 const union cptable *new_cptable = wine_cp_get_table( acp );
1464 if (new_cptable) ansi_cptable = new_cptable;
1469 /***********************************************************************
1470 * GetOEMCP (KERNEL32.@)
1472 * Get the current OEM code page Id for the system.
1478 * The current OEM code page identifier for the system.
1480 UINT WINAPI GetOEMCP(void)
1482 assert( oem_cptable );
1483 return oem_cptable->info.codepage;
1487 /***********************************************************************
1488 * IsValidCodePage (KERNEL32.@)
1490 * Determine if a given code page identifier is valid.
1493 * codepage [I] Code page Id to verify.
1496 * TRUE, If codepage is valid and available on the system,
1499 BOOL WINAPI IsValidCodePage( UINT codepage )
1506 return wine_cp_get_table( codepage ) != NULL;
1511 /***********************************************************************
1512 * IsDBCSLeadByteEx (KERNEL32.@)
1514 * Determine if a character is a lead byte in a given code page.
1517 * codepage [I] Code page for the test.
1518 * testchar [I] Character to test
1521 * TRUE, if testchar is a lead byte in codepage,
1524 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1526 const union cptable *table = get_codepage_table( codepage );
1527 return table && wine_is_dbcs_leadbyte( table, testchar );
1531 /***********************************************************************
1532 * IsDBCSLeadByte (KERNEL32.@)
1533 * IsDBCSLeadByte (KERNEL.207)
1535 * Determine if a character is a lead byte.
1538 * testchar [I] Character to test
1541 * TRUE, if testchar is a lead byte in the Ansii code page,
1544 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1546 if (!ansi_cptable) return FALSE;
1547 return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
1551 /***********************************************************************
1552 * GetCPInfo (KERNEL32.@)
1554 * Get information about a code page.
1557 * codepage [I] Code page number
1558 * cpinfo [O] Destination for code page information
1561 * Success: TRUE. cpinfo is updated with the information about codepage.
1562 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1564 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1566 const union cptable *table;
1570 SetLastError( ERROR_INVALID_PARAMETER );
1574 if (!(table = get_codepage_table( codepage )))
1580 cpinfo->DefaultChar[0] = 0x3f;
1581 cpinfo->DefaultChar[1] = 0;
1582 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1583 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
1587 SetLastError( ERROR_INVALID_PARAMETER );
1590 if (table->info.def_char & 0xff00)
1592 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
1593 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1597 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1598 cpinfo->DefaultChar[1] = 0;
1600 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1601 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1603 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1608 /***********************************************************************
1609 * GetCPInfoExA (KERNEL32.@)
1611 * Get extended information about a code page.
1614 * codepage [I] Code page number
1615 * dwFlags [I] Reserved, must to 0.
1616 * cpinfo [O] Destination for code page information
1619 * Success: TRUE. cpinfo is updated with the information about codepage.
1620 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1622 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1626 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
1629 /* the layout is the same except for CodePageName */
1630 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
1631 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
1635 /***********************************************************************
1636 * GetCPInfoExW (KERNEL32.@)
1638 * Unicode version of GetCPInfoExA.
1640 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1642 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1649 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
1651 cpinfo->CodePage = CP_UTF7;
1652 cpinfo->UnicodeDefaultChar = 0x3f;
1653 strcpyW(cpinfo->CodePageName, utf7);
1659 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
1661 cpinfo->CodePage = CP_UTF8;
1662 cpinfo->UnicodeDefaultChar = 0x3f;
1663 strcpyW(cpinfo->CodePageName, utf8);
1669 const union cptable *table = get_codepage_table( codepage );
1671 cpinfo->CodePage = table->info.codepage;
1672 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1673 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1674 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1681 /***********************************************************************
1682 * EnumSystemCodePagesA (KERNEL32.@)
1684 * Call a user defined function for every code page installed on the system.
1687 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1688 * flags [I] Reserved, set to 0.
1691 * TRUE, If all code pages have been enumerated, or
1692 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1694 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1696 const union cptable *table;
1702 if (!(table = wine_cp_enum_table( index++ ))) break;
1703 sprintf( buffer, "%d", table->info.codepage );
1704 if (!lpfnCodePageEnum( buffer )) break;
1710 /***********************************************************************
1711 * EnumSystemCodePagesW (KERNEL32.@)
1713 * See EnumSystemCodePagesA.
1715 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1717 const union cptable *table;
1718 WCHAR buffer[10], *p;
1719 int page, index = 0;
1723 if (!(table = wine_cp_enum_table( index++ ))) break;
1724 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1726 page = table->info.codepage;
1729 *--p = '0' + (page % 10);
1732 if (!lpfnCodePageEnum( p )) break;
1738 /***********************************************************************
1739 * MultiByteToWideChar (KERNEL32.@)
1741 * Convert a multibyte character string into a Unicode string.
1744 * page [I] Codepage character set to convert from
1745 * flags [I] Character mapping flags
1746 * src [I] Source string buffer
1747 * srclen [I] Length of src, or -1 if src is NUL terminated
1748 * dst [O] Destination buffer
1749 * dstlen [I] Length of dst, or 0 to compute the required length
1752 * Success: If dstlen > 0, the number of characters written to dst.
1753 * If dstlen == 0, the number of characters needed to perform the
1754 * conversion. In both cases the count includes the terminating NUL.
1755 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1756 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1757 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1758 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1761 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1762 LPWSTR dst, INT dstlen )
1764 const union cptable *table;
1768 if (!src || (!dst && dstlen))
1770 SetLastError( ERROR_INVALID_PARAMETER );
1774 if (srclen < 0) srclen = strlen(src) + 1;
1776 if (!once && (flags & MB_USEGLYPHCHARS))
1779 FIXME("MB_USEGLYPHCHARS not supported\n");
1787 SetLastError( ERROR_INVALID_PARAMETER );
1790 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1793 FIXME("UTF-7 not supported\n");
1794 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1799 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1804 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1807 if (!(table = get_codepage_table( page )))
1809 SetLastError( ERROR_INVALID_PARAMETER );
1812 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1820 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1821 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1829 /***********************************************************************
1830 * WideCharToMultiByte (KERNEL32.@)
1832 * Convert a Unicode character string into a multibyte string.
1835 * page [I] Code page character set to convert to
1836 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1837 * src [I] Source string buffer
1838 * srclen [I] Length of src, or -1 if src is NUL terminated
1839 * dst [O] Destination buffer
1840 * dstlen [I] Length of dst, or 0 to compute the required length
1841 * defchar [I] Default character to use for conversion if no exact
1842 * conversion can be made
1843 * used [O] Set if default character was used in the conversion
1846 * Success: If dstlen > 0, the number of characters written to dst.
1847 * If dstlen == 0, number of characters needed to perform the
1848 * conversion. In both cases the count includes the terminating NUL.
1849 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1850 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1851 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1852 * parameter was given.
1854 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1855 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1857 const union cptable *table;
1860 if (!src || (!dst && dstlen))
1862 SetLastError( ERROR_INVALID_PARAMETER );
1866 if (srclen < 0) srclen = strlenW(src) + 1;
1871 if( flags || defchar || used)
1873 SetLastError( ERROR_INVALID_PARAMETER );
1876 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1879 FIXME("UTF-7 not supported\n");
1880 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1885 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1886 defchar, used ? &used_tmp : NULL );
1891 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1892 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1895 if (!(table = get_codepage_table( page )))
1897 SetLastError( ERROR_INVALID_PARAMETER );
1900 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1901 defchar, used ? &used_tmp : NULL );
1902 if (used) *used = used_tmp;
1910 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1911 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1915 TRACE("cp %d %s -> %s, ret = %d\n",
1916 page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
1921 /***********************************************************************
1922 * GetThreadLocale (KERNEL32.@)
1924 * Get the current threads locale.
1930 * The LCID currently assocated with the calling thread.
1932 LCID WINAPI GetThreadLocale(void)
1934 LCID ret = NtCurrentTeb()->CurrentLocale;
1935 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1939 /**********************************************************************
1940 * SetThreadLocale (KERNEL32.@)
1942 * Set the current threads locale.
1945 * lcid [I] LCID of the locale to set
1948 * Success: TRUE. The threads locale is set to lcid.
1949 * Failure: FALSE. Use GetLastError() to determine the cause.
1951 BOOL WINAPI SetThreadLocale( LCID lcid )
1953 TRACE("(0x%04X)\n", lcid);
1955 lcid = ConvertDefaultLocale(lcid);
1957 if (lcid != GetThreadLocale())
1959 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1961 SetLastError(ERROR_INVALID_PARAMETER);
1965 NtCurrentTeb()->CurrentLocale = lcid;
1966 kernel_get_thread_data()->code_page = get_lcid_codepage( lcid );
1971 /**********************************************************************
1972 * SetThreadUILanguage (KERNEL32.@)
1974 * Set the current threads UI language.
1977 * langid [I] LANGID of the language to set, or 0 to use
1978 * the available language which is best supported
1979 * for console applications
1982 * Success: The return value is the same as the input value.
1983 * Failure: The return value differs from the input value.
1984 * Use GetLastError() to determine the cause.
1986 LANGID WINAPI SetThreadUILanguage( LANGID langid )
1988 TRACE("(0x%04x) stub - returning success\n", langid);
1992 /******************************************************************************
1993 * ConvertDefaultLocale (KERNEL32.@)
1995 * Convert a default locale identifier into a real identifier.
1998 * lcid [I] LCID identifier of the locale to convert
2001 * lcid unchanged, if not a default locale or its sublanguage is
2002 * not SUBLANG_NEUTRAL.
2003 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2004 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2005 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2007 LCID WINAPI ConvertDefaultLocale( LCID lcid )
2013 case LOCALE_SYSTEM_DEFAULT:
2014 lcid = GetSystemDefaultLCID();
2016 case LOCALE_USER_DEFAULT:
2017 case LOCALE_NEUTRAL:
2018 lcid = GetUserDefaultLCID();
2021 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2022 langid = LANGIDFROMLCID(lcid);
2023 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
2025 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
2026 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2033 /******************************************************************************
2034 * IsValidLocale (KERNEL32.@)
2036 * Determine if a locale is valid.
2039 * lcid [I] LCID of the locale to check
2040 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2043 * TRUE, if lcid is valid,
2047 * Wine does not currently make the distinction between supported and installed. All
2048 * languages supported are installed by default.
2050 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
2052 /* check if language is registered in the kernel32 resources */
2053 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
2054 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
2058 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
2059 LPCSTR name, WORD LangID, LONG_PTR lParam )
2061 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2064 sprintf(buf, "%08x", (UINT)LangID);
2065 return lpfnLocaleEnum( buf );
2068 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
2069 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2071 static const WCHAR formatW[] = {'%','0','8','x',0};
2072 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2074 sprintfW( buf, formatW, (UINT)LangID );
2075 return lpfnLocaleEnum( buf );
2078 /******************************************************************************
2079 * EnumSystemLocalesA (KERNEL32.@)
2081 * Call a users function for each locale available on the system.
2084 * lpfnLocaleEnum [I] Callback function to call for each locale
2085 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2089 * Failure: FALSE. Use GetLastError() to determine the cause.
2091 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
2093 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2094 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
2095 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
2096 (LONG_PTR)lpfnLocaleEnum);
2101 /******************************************************************************
2102 * EnumSystemLocalesW (KERNEL32.@)
2104 * See EnumSystemLocalesA.
2106 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
2108 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2109 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
2110 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
2111 (LONG_PTR)lpfnLocaleEnum);
2116 /***********************************************************************
2117 * VerLanguageNameA (KERNEL32.@)
2119 * Get the name of a language.
2122 * wLang [I] LANGID of the language
2123 * szLang [O] Destination for the language name
2126 * Success: The size of the language name. If szLang is non-NULL, it is filled
2128 * Failure: 0. Use GetLastError() to determine the cause.
2131 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
2133 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2137 /***********************************************************************
2138 * VerLanguageNameW (KERNEL32.@)
2140 * See VerLanguageNameA.
2142 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
2144 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2148 /******************************************************************************
2149 * GetStringTypeW (KERNEL32.@)
2151 * See GetStringTypeA.
2153 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2155 if (count == -1) count = strlenW(src) + 1;
2159 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
2162 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
2166 WARN("CT_CTYPE3: semi-stub.\n");
2170 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
2172 type1 = get_char_typeW( *src++ ) & 0xfff;
2173 /* try to construct type3 from type1 */
2174 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
2175 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
2176 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
2177 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
2178 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
2179 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
2180 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
2182 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
2183 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
2184 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
2185 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
2186 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
2187 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
2188 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
2189 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
2191 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
2192 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
2193 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
2194 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
2195 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
2196 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
2197 *chartype++ = type3;
2202 SetLastError( ERROR_INVALID_PARAMETER );
2209 /******************************************************************************
2210 * GetStringTypeExW (KERNEL32.@)
2212 * See GetStringTypeExA.
2214 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2216 /* locale is ignored for Unicode */
2217 return GetStringTypeW( type, src, count, chartype );
2221 /******************************************************************************
2222 * GetStringTypeA (KERNEL32.@)
2224 * Get characteristics of the characters making up a string.
2227 * locale [I] Locale Id for the string
2228 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2229 * src [I] String to analyse
2230 * count [I] Length of src in chars, or -1 if src is NUL terminated
2231 * chartype [O] Destination for the calculated characteristics
2234 * Success: TRUE. chartype is filled with the requested characteristics of each char
2236 * Failure: FALSE. Use GetLastError() to determine the cause.
2238 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2245 if(count == -1) count = strlen(src) + 1;
2247 if (!(cp = get_lcid_codepage( locale )))
2249 FIXME("For locale %04x using current ANSI code page\n", locale);
2253 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
2254 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
2256 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
2258 * NOTE: the target buffer has 1 word for each CHARACTER in the source
2259 * string, with multibyte characters there maybe be more bytes in count
2260 * than character space in the buffer!
2262 ret = GetStringTypeW(type, srcW, countW, chartype);
2263 HeapFree(GetProcessHeap(), 0, srcW);
2268 /******************************************************************************
2269 * GetStringTypeExA (KERNEL32.@)
2271 * Get characteristics of the characters making up a string.
2274 * locale [I] Locale Id for the string
2275 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2276 * src [I] String to analyse
2277 * count [I] Length of src in chars, or -1 if src is NUL terminated
2278 * chartype [O] Destination for the calculated characteristics
2281 * Success: TRUE. chartype is filled with the requested characteristics of each char
2283 * Failure: FALSE. Use GetLastError() to determine the cause.
2285 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2287 return GetStringTypeA(locale, type, src, count, chartype);
2291 /*************************************************************************
2292 * LCMapStringW (KERNEL32.@)
2296 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2297 LPWSTR dst, INT dstlen)
2301 if (!src || !srclen || dstlen < 0)
2303 SetLastError(ERROR_INVALID_PARAMETER);
2307 /* mutually exclusive flags */
2308 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2309 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2310 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2311 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2313 SetLastError(ERROR_INVALID_FLAGS);
2317 if (!dstlen) dst = NULL;
2319 lcid = ConvertDefaultLocale(lcid);
2321 if (flags & LCMAP_SORTKEY)
2325 SetLastError(ERROR_INVALID_FLAGS);
2329 if (srclen < 0) srclen = strlenW(src);
2331 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2332 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2334 return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2337 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2338 if (flags & SORT_STRINGSORT)
2340 SetLastError(ERROR_INVALID_FLAGS);
2344 if (srclen < 0) srclen = strlenW(src) + 1;
2346 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2347 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2349 if (!dst) /* return required string length */
2353 for (len = 0; srclen; src++, srclen--)
2356 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2357 * and skips white space and punctuation characters for
2358 * NORM_IGNORESYMBOLS.
2360 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2367 if (flags & LCMAP_UPPERCASE)
2369 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2372 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2374 *dst_ptr++ = toupperW(wch);
2378 else if (flags & LCMAP_LOWERCASE)
2380 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2383 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2385 *dst_ptr++ = tolowerW(wch);
2393 SetLastError(ERROR_INVALID_FLAGS);
2396 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2399 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2408 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2412 return dst_ptr - dst;
2415 /*************************************************************************
2416 * LCMapStringA (KERNEL32.@)
2418 * Map characters in a locale sensitive string.
2421 * lcid [I] LCID for the conversion.
2422 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2423 * src [I] String to map
2424 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2425 * dst [O] Destination for mapped string
2426 * dstlen [I] Length of dst in characters
2429 * Success: The length of the mapped string in dst, including the NUL terminator.
2430 * Failure: 0. Use GetLastError() to determine the cause.
2432 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2433 LPSTR dst, INT dstlen)
2435 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2437 INT ret = 0, srclenW, dstlenW;
2438 UINT locale_cp = CP_ACP;
2440 if (!src || !srclen || dstlen < 0)
2442 SetLastError(ERROR_INVALID_PARAMETER);
2446 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2448 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2453 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2454 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2457 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2460 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2463 if (flags & LCMAP_SORTKEY)
2467 SetLastError(ERROR_INVALID_FLAGS);
2468 goto map_string_exit;
2470 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2471 goto map_string_exit;
2474 if (flags & SORT_STRINGSORT)
2476 SetLastError(ERROR_INVALID_FLAGS);
2477 goto map_string_exit;
2480 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2482 goto map_string_exit;
2484 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2487 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2488 goto map_string_exit;
2491 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2492 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2493 HeapFree(GetProcessHeap(), 0, dstW);
2496 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2500 /*************************************************************************
2501 * FoldStringA (KERNEL32.@)
2503 * Map characters in a string.
2506 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2507 * src [I] String to map
2508 * srclen [I] Length of src, or -1 if src is NUL terminated
2509 * dst [O] Destination for mapped string
2510 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2513 * Success: The length of the string written to dst, including the terminating NUL. If
2514 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2515 * and dst may be NULL.
2516 * Failure: 0. Use GetLastError() to determine the cause.
2518 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2519 LPSTR dst, INT dstlen)
2521 INT ret = 0, srclenW = 0;
2522 WCHAR *srcW = NULL, *dstW = NULL;
2524 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2526 SetLastError(ERROR_INVALID_PARAMETER);
2530 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2531 src, srclen, NULL, 0);
2532 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2536 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2537 goto FoldStringA_exit;
2540 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2541 src, srclen, srcW, srclenW);
2543 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2545 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2548 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2552 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2553 goto FoldStringA_exit;
2556 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2557 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2560 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2564 HeapFree(GetProcessHeap(), 0, dstW);
2567 HeapFree(GetProcessHeap(), 0, srcW);
2571 /*************************************************************************
2572 * FoldStringW (KERNEL32.@)
2576 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2577 LPWSTR dst, INT dstlen)
2581 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2586 /* Fall through for dwFlags == 0 */
2587 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2588 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2589 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2590 SetLastError(ERROR_INVALID_FLAGS);
2594 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2596 SetLastError(ERROR_INVALID_PARAMETER);
2600 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2602 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2606 /******************************************************************************
2607 * CompareStringW (KERNEL32.@)
2609 * See CompareStringA.
2611 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2612 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2618 SetLastError(ERROR_INVALID_PARAMETER);
2622 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2623 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
2625 SetLastError(ERROR_INVALID_FLAGS);
2629 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
2630 if (style & 0x10000000)
2631 WARN("Ignoring unknown style 0x10000000\n");
2633 if (len1 < 0) len1 = strlenW(str1);
2634 if (len2 < 0) len2 = strlenW(str2);
2636 ret = wine_compare_string(style, str1, len1, str2, len2);
2638 if (ret) /* need to translate result */
2639 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2643 /******************************************************************************
2644 * CompareStringA (KERNEL32.@)
2646 * Compare two locale sensitive strings.
2649 * lcid [I] LCID for the comparison
2650 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2651 * str1 [I] First string to compare
2652 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2653 * str2 [I] Second string to compare
2654 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2657 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2658 * str2 is less than, equal to or greater than str1 respectively.
2659 * Failure: FALSE. Use GetLastError() to determine the cause.
2661 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2662 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2664 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2665 WCHAR *buf2W = buf1W + 130;
2666 LPWSTR str1W, str2W;
2667 INT len1W, len2W, ret;
2668 UINT locale_cp = CP_ACP;
2672 SetLastError(ERROR_INVALID_PARAMETER);
2675 if (len1 < 0) len1 = strlen(str1);
2676 if (len2 < 0) len2 = strlen(str2);
2678 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2680 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2685 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2686 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2689 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2692 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2694 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2699 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2700 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2703 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2704 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2707 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2710 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2712 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2713 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2717 /*************************************************************************
2718 * lstrcmp (KERNEL32.@)
2719 * lstrcmpA (KERNEL32.@)
2721 * Compare two strings using the current thread locale.
2724 * str1 [I] First string to compare
2725 * str2 [I] Second string to compare
2728 * Success: A number less than, equal to or greater than 0 depending on whether
2729 * str2 is less than, equal to or greater than str1 respectively.
2730 * Failure: FALSE. Use GetLastError() to determine the cause.
2732 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2736 if ((str1 == NULL) && (str2 == NULL)) return 0;
2737 if (str1 == NULL) return -1;
2738 if (str2 == NULL) return 1;
2740 ret = CompareStringA(GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2746 /*************************************************************************
2747 * lstrcmpi (KERNEL32.@)
2748 * lstrcmpiA (KERNEL32.@)
2750 * Compare two strings using the current thread locale, ignoring case.
2753 * str1 [I] First string to compare
2754 * str2 [I] Second string to compare
2757 * Success: A number less than, equal to or greater than 0 depending on whether
2758 * str2 is less than, equal to or greater than str1 respectively.
2759 * Failure: FALSE. Use GetLastError() to determine the cause.
2761 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2765 if ((str1 == NULL) && (str2 == NULL)) return 0;
2766 if (str1 == NULL) return -1;
2767 if (str2 == NULL) return 1;
2769 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2775 /*************************************************************************
2776 * lstrcmpW (KERNEL32.@)
2780 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2784 if ((str1 == NULL) && (str2 == NULL)) return 0;
2785 if (str1 == NULL) return -1;
2786 if (str2 == NULL) return 1;
2788 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2794 /*************************************************************************
2795 * lstrcmpiW (KERNEL32.@)
2799 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2803 if ((str1 == NULL) && (str2 == NULL)) return 0;
2804 if (str1 == NULL) return -1;
2805 if (str2 == NULL) return 1;
2807 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2813 /******************************************************************************
2816 void LOCALE_Init(void)
2818 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2819 const union cptable *unix_cp );
2821 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
2824 /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
2825 char user_locale[50];
2826 CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
2827 CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
2829 CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2830 CFRelease( user_locale_ref );
2831 if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
2832 unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
2833 setenv( "LANG", user_locale, 0 );
2834 TRACE( "setting locale to '%s'\n", user_locale );
2836 setlocale( LC_ALL, "" );
2838 unix_cp = setup_unix_locales();
2839 if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
2840 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
2841 NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
2842 NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
2844 ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
2845 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2846 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2847 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2848 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2850 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2851 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2853 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2854 ansi_cptable = wine_cp_get_table( 1252 );
2855 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2856 oem_cptable = wine_cp_get_table( 437 );
2857 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2858 mac_cptable = wine_cp_get_table( 10000 );
2859 if (unix_cp != CP_UTF8)
2861 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2862 unix_cptable = wine_cp_get_table( 28591 );
2865 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2867 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2868 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2869 mac_cptable->info.codepage, unix_cp );
2871 setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
2874 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
2876 UNICODE_STRING keyName;
2877 OBJECT_ATTRIBUTES attr;
2880 RtlInitUnicodeString( &keyName, szKeyName );
2881 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2883 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2889 static HANDLE NLS_RegOpenSubKey(HANDLE hRootKey, LPCWSTR szKeyName)
2891 HANDLE hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2894 NtClose( hRootKey );
2899 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
2903 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2906 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2907 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2908 info->NameLength > keyNameSize)
2913 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
2915 memcpy( szKeyName, info->Name, info->NameLength);
2916 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2918 TRACE("returning %s\n", debugstr_w(szKeyName));
2922 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
2923 LPWSTR szValueName, ULONG valueNameSize,
2924 LPWSTR szValueData, ULONG valueDataSize)
2927 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2930 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2931 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2932 info->NameLength > valueNameSize ||
2933 info->DataLength > valueDataSize)
2938 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
2940 memcpy( szValueName, info->Name, info->NameLength);
2941 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2942 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2943 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2945 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2949 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
2952 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2953 DWORD dwSize = sizeof(buffer);
2954 UNICODE_STRING valueName;
2956 RtlInitUnicodeString( &valueName, szValueName );
2958 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2959 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2960 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2961 info->DataLength == sizeof(DWORD))
2963 memcpy(lpVal, info->Data, sizeof(DWORD));
2970 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2973 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
2977 /* FIXME: Is it correct to use the system default langid? */
2978 langId = GetSystemDefaultLangID();
2980 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2981 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2983 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
2987 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
2991 ULONG iResourceIndex = lgrpid & 0xf;
2992 LPCWSTR lpResEntry = LockResource( hResDir );
2995 for (i = 0; i < iResourceIndex; i++)
2996 lpResEntry += *lpResEntry + 1;
2998 if (*lpResEntry < nameSize)
3000 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
3001 szName[*lpResEntry] = '\0';
3006 FreeResource( hResource );
3011 /* Registry keys for NLS related information */
3012 static const WCHAR szLangGroupsKeyName[] = {
3013 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
3016 static const WCHAR szCountryListName[] = {
3017 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
3018 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
3019 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3020 'T','e','l','e','p','h','o','n','y','\\',
3021 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
3025 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
3028 LANGUAGEGROUP_ENUMPROCA procA;
3029 LANGUAGEGROUP_ENUMPROCW procW;
3032 } ENUMLANGUAGEGROUP_CALLBACKS;
3034 /* Internal implementation of EnumSystemLanguageGroupsA/W */
3035 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
3037 WCHAR szNumber[10], szValue[4];
3039 BOOL bContinue = TRUE;
3044 SetLastError(ERROR_INVALID_PARAMETER);
3048 switch (lpProcs->dwFlags)
3051 /* Default to LGRPID_INSTALLED */
3052 lpProcs->dwFlags = LGRPID_INSTALLED;
3053 /* Fall through... */
3054 case LGRPID_INSTALLED:
3055 case LGRPID_SUPPORTED:
3058 SetLastError(ERROR_INVALID_FLAGS);
3062 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
3065 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3069 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3070 szValue, sizeof(szValue) ))
3072 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
3073 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
3075 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
3076 bInstalled ? "" : "not ");
3078 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
3080 WCHAR szGrpName[48];
3082 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
3083 szGrpName[0] = '\0';
3086 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
3090 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3091 char szGrpNameA[48];
3093 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
3094 * or whether the language names are ever localised. Assume CP_ACP.
3097 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3098 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
3100 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
3120 /******************************************************************************
3121 * EnumSystemLanguageGroupsA (KERNEL32.@)
3123 * Call a users function for each language group available on the system.
3126 * pLangGrpEnumProc [I] Callback function to call for each language group
3127 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
3128 * lParam [I] User parameter to pass to pLangGrpEnumProc
3132 * Failure: FALSE. Use GetLastError() to determine the cause.
3134 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
3135 DWORD dwFlags, LONG_PTR lParam)
3137 ENUMLANGUAGEGROUP_CALLBACKS procs;
3139 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3141 procs.procA = pLangGrpEnumProc;
3143 procs.dwFlags = dwFlags;
3144 procs.lParam = lParam;
3146 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3149 /******************************************************************************
3150 * EnumSystemLanguageGroupsW (KERNEL32.@)
3152 * See EnumSystemLanguageGroupsA.
3154 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
3155 DWORD dwFlags, LONG_PTR lParam)
3157 ENUMLANGUAGEGROUP_CALLBACKS procs;
3159 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3162 procs.procW = pLangGrpEnumProc;
3163 procs.dwFlags = dwFlags;
3164 procs.lParam = lParam;
3166 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3169 /******************************************************************************
3170 * IsValidLanguageGroup (KERNEL32.@)
3172 * Determine if a language group is supported and/or installed.
3175 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
3176 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
3179 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
3182 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
3184 static const WCHAR szFormat[] = { '%','x','\0' };
3185 WCHAR szValueName[16], szValue[2];
3186 BOOL bSupported = FALSE, bInstalled = FALSE;
3192 case LGRPID_INSTALLED:
3193 case LGRPID_SUPPORTED:
3195 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
3197 sprintfW( szValueName, szFormat, lgrpid );
3199 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
3203 if (szValue[0] == '1')
3213 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
3214 (dwFlags == LGRPID_INSTALLED && bInstalled))
3220 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
3223 LANGGROUPLOCALE_ENUMPROCA procA;
3224 LANGGROUPLOCALE_ENUMPROCW procW;
3228 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
3230 /* Internal implementation of EnumLanguageGrouplocalesA/W */
3231 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
3233 static const WCHAR szLocaleKeyName[] = {
3234 'L','o','c','a','l','e','\0'
3236 static const WCHAR szAlternateSortsKeyName[] = {
3237 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
3239 WCHAR szNumber[10], szValue[4];
3241 BOOL bContinue = TRUE, bAlternate = FALSE;
3243 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
3245 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
3247 SetLastError(ERROR_INVALID_PARAMETER);
3251 if (lpProcs->dwFlags)
3253 SetLastError(ERROR_INVALID_FLAGS);
3257 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
3260 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3264 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3265 szValue, sizeof(szValue) ))
3267 lgrpid = strtoulW( szValue, NULL, 16 );
3269 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
3270 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
3272 if (lgrpid == lpProcs->lgrpid)
3276 lcid = strtoulW( szNumber, NULL, 16 );
3278 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
3279 * '00000437 ;Georgian'
3280 * At present we only pass the LCID string.
3284 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
3287 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3289 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3291 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
3299 /* Finished enumerating this key */
3302 /* Enumerate alternate sorts also */
3303 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
3308 bContinue = FALSE; /* Finished both keys */
3321 /******************************************************************************
3322 * EnumLanguageGroupLocalesA (KERNEL32.@)
3324 * Call a users function for every locale in a language group available on the system.
3327 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3328 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3329 * dwFlags [I] Reserved, set to 0
3330 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3334 * Failure: FALSE. Use GetLastError() to determine the cause.
3336 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3337 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3339 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3341 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3343 callbacks.procA = pLangGrpLcEnumProc;
3344 callbacks.procW = NULL;
3345 callbacks.dwFlags = dwFlags;
3346 callbacks.lgrpid = lgrpid;
3347 callbacks.lParam = lParam;
3349 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3352 /******************************************************************************
3353 * EnumLanguageGroupLocalesW (KERNEL32.@)
3355 * See EnumLanguageGroupLocalesA.
3357 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3358 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3360 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3362 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3364 callbacks.procA = NULL;
3365 callbacks.procW = pLangGrpLcEnumProc;
3366 callbacks.dwFlags = dwFlags;
3367 callbacks.lgrpid = lgrpid;
3368 callbacks.lParam = lParam;
3370 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3373 /******************************************************************************
3374 * EnumSystemGeoID (KERNEL32.@)
3376 * Call a users function for every location available on the system.
3379 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3380 * reserved [I] Reserved, set to 0
3381 * pGeoEnumProc [I] Callback function to call for each location
3385 * Failure: FALSE. Use GetLastError() to determine the cause.
3387 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3389 static const WCHAR szCountryCodeValueName[] = {
3390 'C','o','u','n','t','r','y','C','o','d','e','\0'
3396 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
3398 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3400 SetLastError(ERROR_INVALID_PARAMETER);
3404 hKey = NLS_RegOpenKey( 0, szCountryListName );
3406 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3408 BOOL bContinue = TRUE;
3410 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
3414 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3416 TRACE("Got geoid %d\n", dwGeoId);
3418 if (!pGeoEnumProc( dwGeoId ))
3437 /******************************************************************************
3438 * InvalidateNLSCache (KERNEL32.@)
3440 * Invalidate the cache of NLS values.
3449 BOOL WINAPI InvalidateNLSCache(void)
3455 /******************************************************************************
3456 * GetUserGeoID (KERNEL32.@)
3458 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3460 FIXME("%d\n",GeoClass);
3461 return GEOID_NOT_AVAILABLE;
3464 /******************************************************************************
3465 * SetUserGeoID (KERNEL32.@)
3467 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3469 FIXME("%d\n",GeoID);
3477 UILANGUAGE_ENUMPROCA procA;
3478 UILANGUAGE_ENUMPROCW procW;
3482 } ENUM_UILANG_CALLBACK;
3484 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
3485 LPCSTR name, WORD LangID, LONG_PTR lParam )
3487 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3490 sprintf(buf, "%08x", (UINT)LangID);
3491 return enum_uilang->u.procA( buf, enum_uilang->param );
3494 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
3495 LPCWSTR name, WORD LangID, LONG_PTR lParam )
3497 static const WCHAR formatW[] = {'%','0','8','x',0};
3498 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3501 sprintfW( buf, formatW, (UINT)LangID );
3502 return enum_uilang->u.procW( buf, enum_uilang->param );
3505 /******************************************************************************
3506 * EnumUILanguagesA (KERNEL32.@)
3508 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3510 ENUM_UILANG_CALLBACK enum_uilang;
3512 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3514 if(!pUILangEnumProc) {
3515 SetLastError(ERROR_INVALID_PARAMETER);
3519 SetLastError(ERROR_INVALID_FLAGS);
3523 enum_uilang.u.procA = pUILangEnumProc;
3524 enum_uilang.flags = dwFlags;
3525 enum_uilang.param = lParam;
3527 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
3528 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
3529 (LONG_PTR)&enum_uilang);
3533 /******************************************************************************
3534 * EnumUILanguagesW (KERNEL32.@)
3536 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3538 ENUM_UILANG_CALLBACK enum_uilang;
3540 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3543 if(!pUILangEnumProc) {
3544 SetLastError(ERROR_INVALID_PARAMETER);
3548 SetLastError(ERROR_INVALID_FLAGS);
3552 enum_uilang.u.procW = pUILangEnumProc;
3553 enum_uilang.flags = dwFlags;
3554 enum_uilang.param = lParam;
3556 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
3557 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
3558 (LONG_PTR)&enum_uilang);
3562 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
3563 int cchData, LANGID language)
3565 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3569 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
3570 int cchData, LANGID language)
3572 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);