Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1122 lines
38 KiB

  1. /***
  2. *getqloc.c - get qualified locale
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines __get_qualified_locale - get complete locale information
  8. *
  9. *Revision History:
  10. * 12-11-92 CFW initial version
  11. * 01-08-93 CFW cleaned up file
  12. * 02-02-93 CFW Added test for NULL input string fields
  13. * 02-08-93 CFW Casts to remove warnings.
  14. * 02-18-93 CFW Removed debugging support routines, changed copyright.
  15. * 02-18-93 CFW Removed debugging support routines, changed copyright.
  16. * 03-01-93 CFW Test code page validity, use ANSI comments.
  17. * 03-02-93 CFW Add ISO 3166 3-letter country codes, verify country table.
  18. * 03-04-93 CFW Call IsValidCodePage to test code page vailidity.
  19. * 03-10-93 CFW Protect table testing code.
  20. * 03-17-93 CFW Add __ to lang & ctry info tables, move defs to setlocal.h.
  21. * 03-23-93 CFW Make internal functions static, add _ to GetQualifiedLocale.
  22. * 03-24-93 CFW Change to _get_qualified_locale, support ".codepage".
  23. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  24. * 04-08-93 SKS Replace stricmp() with ANSI-conforming _stricmp()
  25. * 04-20-93 CFW Enable all strange countries.
  26. * 05-20-93 GJF Include windows.h, not individual win*.h files
  27. * 05-24-93 CFW Clean up file (brief is evil).
  28. * 09-15-93 CFW Use ANSI conformant "__" names.
  29. * 09-22-93 CFW Use __crtxxx internal NLS API wrapper.
  30. * 11-09-93 CFW Add code page for __crtxxx().
  31. * 11-11-93 CFW Verify ALL code pages.
  32. * 02-04-94 CFW Remove unused param, clean up, new languages,
  33. * default is ANSI, allow .ACP/.OCP for code page.
  34. * 02-07-94 CFW Back to OEM, NT 3.1 doesn't handle ANSI properly.
  35. * 02-24-94 CFW Back to ANSI, we'll use our own table.
  36. * 04-04-94 CFW Update NT-supported countries/languages.
  37. * 04-25-94 CFW Update countries to new ISO 3166 (1993) standard.
  38. * 02-02-95 BWT Update _POSIX_ support
  39. * 04-07-95 CFW Remove NT 3.1 hacks, reduce string space.
  40. * 02-14-97 RDK Complete rewrite to dynamically use the installed
  41. * system locales to determine the best match for the
  42. * language and/or country specified.
  43. * 02-19-97 RDK Do not use iPrimaryLen if zero.
  44. * 02-24-97 RDK For Win95, simulate nonfunctional GetLocaleInfoA
  45. * calls with hard-coded values.
  46. * 07-07-97 GJF Made arrays of data global and selectany so linker
  47. * can eliminate them when possible.
  48. * 10-02-98 GJF Replaced IsThisWindowsNT with test of _osplatform.
  49. * 11-10-99 PML Try untranslated language string first (vs7#61130).
  50. * 05-17-00 GB Translating LCID 0814 to Norwegian-Nynorsk as special
  51. * case
  52. * 09-06-00 PML Use proper geopolitical terminology (vs7#81673). Also
  53. * move data tables to .rdata.
  54. *
  55. *******************************************************************************/
  56. #include <cruntime.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <windows.h>
  60. #include <setlocal.h>
  61. #include <awint.h>
  62. #if defined(_POSIX_)
  63. BOOL __cdecl __get_qualified_locale(const LPLC_STRINGS lpInStr, LPLC_ID lpOutId,
  64. LPLC_STRINGS lpOutStr)
  65. {
  66. return FALSE;
  67. }
  68. #else //if defined(_POSIX_)
  69. // local defines
  70. #define __LCID_DEFAULT 0x1 // default language locale for country
  71. #define __LCID_PRIMARY 0x2 // primary language locale for country
  72. #define __LCID_FULL 0x4 // fully matched language locale for country
  73. #define __LCID_LANGUAGE 0x100 // language default seen
  74. #define __LCID_EXISTS 0x200 // language is installed
  75. // local structure definitions
  76. typedef struct tagLOCALETAB
  77. {
  78. CHAR * szName;
  79. CHAR chAbbrev[4];
  80. } LOCALETAB;
  81. typedef struct tagRGLOCINFO
  82. {
  83. LCID lcid;
  84. char chILanguage[8];
  85. char * pchSEngLanguage;
  86. char chSAbbrevLangName[4];
  87. char * pchSEngCountry;
  88. char chSAbbrevCtryName[4];
  89. char chIDefaultCodepage[8];
  90. char chIDefaultAnsiCodepage[8];
  91. } RGLOCINFO;
  92. // function prototypes
  93. BOOL __cdecl __get_qualified_locale(const LPLC_STRINGS, LPLC_ID, LPLC_STRINGS);
  94. static BOOL TranslateName(const LOCALETAB *, int, const char **);
  95. static void GetLcidFromLangCountry(void);
  96. static BOOL CALLBACK LangCountryEnumProc(LPSTR);
  97. static void GetLcidFromLanguage(void);
  98. static BOOL CALLBACK LanguageEnumProc(LPSTR);
  99. static void GetLcidFromCountry(void);
  100. static BOOL CALLBACK CountryEnumProc(LPSTR);
  101. static void GetLcidFromDefault(void);
  102. static int ProcessCodePage(LPSTR);
  103. static BOOL TestDefaultCountry(LCID);
  104. static BOOL TestDefaultLanguage(LCID, BOOL);
  105. static int __stdcall crtGetLocaleInfoA(LCID, LCTYPE, LPSTR, int);
  106. static LCID LcidFromHexString(LPSTR);
  107. static int GetPrimaryLen(LPSTR);
  108. // non-NLS language string table
  109. __declspec(selectany) const LOCALETAB __rg_language[] =
  110. {
  111. {"american", "ENU"},
  112. {"american english", "ENU"},
  113. {"american-english", "ENU"},
  114. {"australian", "ENA"},
  115. {"belgian", "NLB"},
  116. {"canadian", "ENC"},
  117. {"chh", "ZHH"},
  118. {"chi", "ZHI"},
  119. {"chinese", "CHS"},
  120. {"chinese-hongkong", "ZHH"},
  121. {"chinese-simplified", "CHS"},
  122. {"chinese-singapore", "ZHI"},
  123. {"chinese-traditional", "CHT"},
  124. {"dutch-belgian", "NLB"},
  125. {"english-american", "ENU"},
  126. {"english-aus", "ENA"},
  127. {"english-belize", "ENL"},
  128. {"english-can", "ENC"},
  129. {"english-caribbean", "ENB"},
  130. {"english-ire", "ENI"},
  131. {"english-jamaica", "ENJ"},
  132. {"english-nz", "ENZ"},
  133. {"english-south africa", "ENS"},
  134. {"english-trinidad y tobago", "ENT"},
  135. {"english-uk", "ENG"},
  136. {"english-us", "ENU"},
  137. {"english-usa", "ENU"},
  138. {"french-belgian", "FRB"},
  139. {"french-canadian", "FRC"},
  140. {"french-luxembourg", "FRL"},
  141. {"french-swiss", "FRS"},
  142. {"german-austrian", "DEA"},
  143. {"german-lichtenstein", "DEC"},
  144. {"german-luxembourg", "DEL"},
  145. {"german-swiss", "DES"},
  146. {"irish-english", "ENI"},
  147. {"italian-swiss", "ITS"},
  148. {"norwegian", "NOR"},
  149. {"norwegian-bokmal", "NOR"},
  150. {"norwegian-nynorsk", "NON"},
  151. {"portuguese-brazilian", "PTB"},
  152. {"spanish-argentina", "ESS"},
  153. {"spanish-bolivia", "ESB"},
  154. {"spanish-chile", "ESL"},
  155. {"spanish-colombia", "ESO"},
  156. {"spanish-costa rica", "ESC"},
  157. {"spanish-dominican republic", "ESD"},
  158. {"spanish-ecuador", "ESF"},
  159. {"spanish-el salvador", "ESE"},
  160. {"spanish-guatemala", "ESG"},
  161. {"spanish-honduras", "ESH"},
  162. {"spanish-mexican", "ESM"},
  163. {"spanish-modern", "ESN"},
  164. {"spanish-nicaragua", "ESI"},
  165. {"spanish-panama", "ESA"},
  166. {"spanish-paraguay", "ESZ"},
  167. {"spanish-peru", "ESR"},
  168. {"spanish-puerto rico", "ESU"},
  169. {"spanish-uruguay", "ESY"},
  170. {"spanish-venezuela", "ESV"},
  171. {"swedish-finland", "SVF"},
  172. {"swiss", "DES"},
  173. {"uk", "ENG"},
  174. {"us", "ENU"},
  175. {"usa", "ENU"}
  176. };
  177. // non-NLS country/region string table
  178. __declspec( selectany ) const LOCALETAB __rg_country[] =
  179. {
  180. {"america", "USA"},
  181. {"britain", "GBR"},
  182. {"china", "CHN"},
  183. {"czech", "CZE"},
  184. {"england", "GBR"},
  185. {"great britain", "GBR"},
  186. {"holland", "NLD"},
  187. {"hong-kong", "HKG"},
  188. {"new-zealand", "NZL"},
  189. {"nz", "NZL"},
  190. {"pr china", "CHN"},
  191. {"pr-china", "CHN"},
  192. {"puerto-rico", "PRI"},
  193. {"slovak", "SVK"},
  194. {"south africa", "ZAF"},
  195. {"south korea", "KOR"},
  196. {"south-africa", "ZAF"},
  197. {"south-korea", "KOR"},
  198. {"trinidad & tobago", "TTO"},
  199. {"uk", "GBR"},
  200. {"united-kingdom", "GBR"},
  201. {"united-states", "USA"},
  202. {"us", "USA"},
  203. };
  204. // LANGID's of locales of nondefault languages
  205. __declspec( selectany ) const LANGID __rglangidNotDefault[] =
  206. {
  207. MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_CANADIAN),
  208. MAKELANGID(LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC),
  209. MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG),
  210. MAKELANGID(LANG_AFRIKAANS, SUBLANG_DEFAULT),
  211. MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_BELGIAN),
  212. MAKELANGID(LANG_BASQUE, SUBLANG_DEFAULT),
  213. MAKELANGID(LANG_CATALAN, SUBLANG_DEFAULT),
  214. MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_SWISS),
  215. MAKELANGID(LANG_ITALIAN, SUBLANG_ITALIAN_SWISS),
  216. MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND)
  217. };
  218. // locale information not supported in Win95
  219. __declspec( selectany ) const RGLOCINFO __rgLocInfo[] =
  220. {
  221. { 0x040a, "040a", "Spanish - Traditional Sort", "ESP", "Spain",
  222. "ESP", "850", "1252" },
  223. { 0x040b, "040b", "Finnish", "FIN", "Finland", "FIN", "850", "1252" },
  224. { 0x040c, "040c", "French", "FRA", "France", "FRA", "850", "1252" },
  225. { 0x040f, "040f", "Icelandic", "ISL", "Iceland", "ISL", "850", "1252" },
  226. { 0x041d, "041d", "Swedish", "SVE", "Sweden", "SWE", "850", "1252" },
  227. { 0x042d, "042d", "Basque", "EUQ", "Spain", "ESP", "850", "1252" },
  228. { 0x080a, "080a", "Spanish", "ESM", "Mexico", "MEX", "850", "1252" },
  229. { 0x080c, "080c", "French", "FRB", "Belgium", "BEL", "850", "1252" },
  230. { 0x0c07, "0c07", "German", "DEA", "Austria", "AUT", "850", "1252" },
  231. { 0x0c09, "0c09", "English", "ENA", "Australia", "AUS", "850", "1252" },
  232. { 0x0c0a, "0c0a", "Spanish - Modern Sort", "ESN", "Spain",
  233. "ESP", "850", "1252" },
  234. { 0x0c0c, "0c0c", "French", "FRC", "Canada", "CAN", "850", "1252" },
  235. { 0x100a, "100a", "Spanish", "ESG", "Guatemala", "GTM", "850", "1252" },
  236. { 0x100c, "100c", "French", "FRS", "Switzerland", "CHE", "850", "1252" },
  237. { 0x140a, "140a", "Spanish", "ESC", "Costa Rica", "CRI", "850", "1252" },
  238. { 0x140c, "140c", "French", "FRL", "Luxembourg", "LUX", "850", "1252" },
  239. { 0x180a, "180a", "Spanish", "ESA", "Panama", "PAN", "850", "1252" },
  240. { 0x1c09, "1c09", "English", "ENS", "South Africa", "ZAF", "437", "1252" },
  241. { 0x1c0a, "1c0a", "Spanish", "ESD", "Dominican Republic",
  242. "DOM", "850", "1252" },
  243. { 0x200a, "200a", "Spanish", "ESV", "Venezuela", "VEN", "850", "1252" },
  244. { 0x240a, "240a", "Spanish", "ESO", "Colombia", "COL", "850", "1252" },
  245. { 0x280a, "280a", "Spanish", "ESR", "Peru", "PER", "850", "1252" },
  246. { 0x2c0a, "2c0a", "Spanish", "ESS", "Argentina", "ARG", "850", "1252" },
  247. { 0x300a, "300a", "Spanish", "ESF", "Ecuador", "ECU", "850", "1252" },
  248. { 0x340a, "340a", "Spanish", "ESL", "Chile", "CHL", "850", "1252" },
  249. { 0x380a, "380a", "Spanish", "ESY", "Uruguay", "URY", "850", "1252" },
  250. { 0x3c0a, "3c0a", "Spanish", "ESZ", "Paraguay", "PRY", "850", "1252" }
  251. };
  252. // static variable to point to GetLocaleInfoA for Windows NT and
  253. // crtGetLocaleInfoA for Win95
  254. typedef int (__stdcall * PFNGETLOCALEINFOA)(LCID, LCTYPE, LPSTR, int);
  255. static PFNGETLOCALEINFOA pfnGetLocaleInfoA = NULL;
  256. // static variables used in locale enumeration callback routines
  257. static char * pchLanguage;
  258. static char * pchCountry;
  259. static int iLcidState;
  260. static int iPrimaryLen;
  261. static BOOL bAbbrevLanguage;
  262. static BOOL bAbbrevCountry;
  263. static LCID lcidLanguage;
  264. static LCID lcidCountry;
  265. /***
  266. *BOOL __get_qualified_locale - return fully qualified locale
  267. *
  268. *Purpose:
  269. * get default locale, qualify partially complete locales
  270. *
  271. *Entry:
  272. * lpInStr - input strings to be qualified
  273. * lpOutId - pointer to numeric LCIDs and codepage output
  274. * lpOutStr - pointer to string LCIDs and codepage output
  275. *
  276. *Exit:
  277. * TRUE if success, qualified locale is valid
  278. * FALSE if failure
  279. *
  280. *Exceptions:
  281. *
  282. *******************************************************************************/
  283. BOOL __cdecl __get_qualified_locale(const LPLC_STRINGS lpInStr, LPLC_ID lpOutId,
  284. LPLC_STRINGS lpOutStr)
  285. {
  286. int iCodePage;
  287. // initialize pointer to call locale info routine based on operating system
  288. if (!pfnGetLocaleInfoA)
  289. {
  290. pfnGetLocaleInfoA = (_osplatform == VER_PLATFORM_WIN32_NT) ?
  291. GetLocaleInfoA : crtGetLocaleInfoA;
  292. }
  293. if (!lpInStr)
  294. {
  295. // if no input defined, just use default LCID
  296. GetLcidFromDefault();
  297. }
  298. else
  299. {
  300. pchLanguage = lpInStr->szLanguage;
  301. // convert non-NLS country strings to three-letter abbreviations
  302. pchCountry = lpInStr->szCountry;
  303. if (pchCountry && *pchCountry)
  304. TranslateName(__rg_country,
  305. sizeof(__rg_country) / sizeof(LOCALETAB) - 1,
  306. &pchCountry);
  307. iLcidState = 0;
  308. if (pchLanguage && *pchLanguage)
  309. {
  310. if (pchCountry && *pchCountry)
  311. {
  312. // both language and country strings defined
  313. GetLcidFromLangCountry();
  314. }
  315. else
  316. {
  317. // language string defined, but country string undefined
  318. GetLcidFromLanguage();
  319. }
  320. if (!iLcidState) {
  321. // first attempt failed, try substituting the language name
  322. // convert non-NLS language strings to three-letter abbrevs
  323. if (TranslateName(__rg_language,
  324. sizeof(__rg_language) / sizeof(LOCALETAB) - 1,
  325. &pchLanguage))
  326. {
  327. if (pchCountry && *pchCountry)
  328. {
  329. GetLcidFromLangCountry();
  330. }
  331. else
  332. {
  333. GetLcidFromLanguage();
  334. }
  335. }
  336. }
  337. }
  338. else
  339. {
  340. if (pchCountry && *pchCountry)
  341. {
  342. // country string defined, but language string undefined
  343. GetLcidFromCountry();
  344. }
  345. else
  346. {
  347. // both language and country strings undefined
  348. GetLcidFromDefault();
  349. }
  350. }
  351. }
  352. // test for error in LCID processing
  353. if (!iLcidState)
  354. return FALSE;
  355. // process codepage value
  356. iCodePage = ProcessCodePage(lpInStr->szCodePage);
  357. // verify codepage validity
  358. if (!iCodePage || !IsValidCodePage((WORD)iCodePage))
  359. return FALSE;
  360. // verify locale is installed
  361. if (!IsValidLocale(lcidLanguage, LCID_INSTALLED))
  362. return FALSE;
  363. // set numeric LCID and codepage results
  364. if (lpOutId)
  365. {
  366. lpOutId->wLanguage = LANGIDFROMLCID(lcidLanguage);
  367. lpOutId->wCountry = LANGIDFROMLCID(lcidCountry);
  368. lpOutId->wCodePage = (WORD)iCodePage;
  369. }
  370. // set string language, country, and codepage results
  371. if (lpOutStr)
  372. {
  373. // Norwegian-Nynorsk is special case because Langauge and country pair
  374. // for Norwegian-Nynorsk and Norwegian is same ie. Norwegian_Norway
  375. if ( lpOutId->wLanguage == 0x0814)
  376. strcpy(lpOutStr->szLanguage, "Norwegian-Nynorsk");
  377. else if ((*pfnGetLocaleInfoA)(lcidLanguage, LOCALE_SENGLANGUAGE,
  378. lpOutStr->szLanguage, MAX_LANG_LEN) == 0)
  379. return FALSE;
  380. if ((*pfnGetLocaleInfoA)(lcidCountry, LOCALE_SENGCOUNTRY,
  381. lpOutStr->szCountry, MAX_CTRY_LEN) == 0)
  382. return FALSE;
  383. _itoa((int)iCodePage, (char *)lpOutStr->szCodePage, 10);
  384. }
  385. return TRUE;
  386. }
  387. /***
  388. *BOOL TranslateName - convert known non-NLS string to NLS equivalent
  389. *
  390. *Purpose:
  391. * Provide compatibility with existing code for non-NLS strings
  392. *
  393. *Entry:
  394. * lpTable - pointer to LOCALETAB used for translation
  395. * high - maximum index of table (size - 1)
  396. * ppchName - pointer to pointer of string to translate
  397. *
  398. *Exit:
  399. * ppchName - pointer to pointer of string possibly translated
  400. * TRUE if string translated, FALSE if unchanged
  401. *
  402. *Exceptions:
  403. *
  404. *******************************************************************************/
  405. static BOOL TranslateName (
  406. const LOCALETAB * lpTable,
  407. int high,
  408. const char ** ppchName)
  409. {
  410. int i;
  411. int cmp = 1;
  412. int low = 0;
  413. // typical binary search - do until no more to search or match
  414. while (low <= high && cmp != 0)
  415. {
  416. i = (low + high) / 2;
  417. cmp = _stricmp(*ppchName, (const char *)(*(lpTable + i)).szName);
  418. if (cmp == 0)
  419. *ppchName = (*(lpTable + i)).chAbbrev;
  420. else if (cmp < 0)
  421. high = i - 1;
  422. else
  423. low = i + 1;
  424. }
  425. return !cmp;
  426. }
  427. /***
  428. *void GetLcidFromLangCountry - get LCIDs from language and country strings
  429. *
  430. *Purpose:
  431. * Match the best LCIDs to the language and country string given.
  432. * After global variables are initialized, the LangCountryEnumProc
  433. * routine is registered as an EnumSystemLocalesA callback to actually
  434. * perform the matching as the LCIDs are enumerated.
  435. *
  436. *Entry:
  437. * pchLanguage - language string
  438. * bAbbrevLanguage - language string is a three-letter abbreviation
  439. * pchCountry - country string
  440. * bAbbrevCountry - country string ia a three-letter abbreviation
  441. * iPrimaryLen - length of language string with primary name
  442. *
  443. *Exit:
  444. * lcidLanguage - LCID of language string
  445. * lcidCountry - LCID of country string
  446. *
  447. *Exceptions:
  448. *
  449. *******************************************************************************/
  450. static void GetLcidFromLangCountry (void)
  451. {
  452. // initialize static variables for callback use
  453. bAbbrevLanguage = strlen(pchLanguage) == 3;
  454. bAbbrevCountry = strlen(pchCountry) == 3;
  455. lcidLanguage = 0;
  456. iPrimaryLen = bAbbrevLanguage ? 2 : GetPrimaryLen(pchLanguage);
  457. EnumSystemLocalesA(LangCountryEnumProc, LCID_INSTALLED);
  458. // locale value is invalid if the language was not installed or the language
  459. // was not available for the country specified
  460. if (!(iLcidState & __LCID_LANGUAGE) || !(iLcidState & __LCID_EXISTS) ||
  461. !(iLcidState & (__LCID_FULL | __LCID_PRIMARY | __LCID_DEFAULT)))
  462. iLcidState = 0;
  463. }
  464. /***
  465. *BOOL CALLBACK LangCountryEnumProc - callback routine for GetLcidFromLangCountry
  466. *
  467. *Purpose:
  468. * Determine if LCID given matches the language in pchLanguage
  469. * and country in pchCountry.
  470. *
  471. *Entry:
  472. * lpLcidString - pointer to string with decimal LCID
  473. * pchCountry - pointer to country name
  474. * bAbbrevCountry - set if country is three-letter abbreviation
  475. *
  476. *Exit:
  477. * iLcidState - status of match
  478. * __LCID_FULL - both language and country match (best match)
  479. * __LCID_PRIMARY - primary language and country match (better)
  480. * __LCID_DEFAULT - default language and country match (good)
  481. * __LCID_LANGUAGE - default primary language exists
  482. * __LCID_EXISTS - full match of language string exists
  483. * (Overall match occurs for the best of FULL/PRIMARY/DEFAULT
  484. * and LANGUAGE/EXISTS both set.)
  485. * lcidLanguage - LCID matched
  486. * lcidCountry - LCID matched
  487. * FALSE if match occurred to terminate enumeration, else TRUE.
  488. *
  489. *Exceptions:
  490. *
  491. *******************************************************************************/
  492. static BOOL CALLBACK LangCountryEnumProc (LPSTR lpLcidString)
  493. {
  494. LCID lcid = LcidFromHexString(lpLcidString);
  495. char rgcInfo[120];
  496. // test locale country against input value
  497. if ((*pfnGetLocaleInfoA)(lcid, bAbbrevCountry ? LOCALE_SABBREVCTRYNAME
  498. : LOCALE_SENGCOUNTRY,
  499. rgcInfo, sizeof(rgcInfo)) == 0)
  500. {
  501. // set error condition and exit
  502. iLcidState = 0;
  503. return TRUE;
  504. }
  505. if (!_stricmp(pchCountry, rgcInfo))
  506. {
  507. // country matched - test for language match
  508. if ((*pfnGetLocaleInfoA)(lcid, bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
  509. : LOCALE_SENGLANGUAGE,
  510. rgcInfo, sizeof(rgcInfo)) == 0)
  511. {
  512. // set error condition and exit
  513. iLcidState = 0;
  514. return TRUE;
  515. }
  516. if (!_stricmp(pchLanguage, rgcInfo))
  517. {
  518. // language matched also - set state and value
  519. iLcidState |= (__LCID_FULL | __LCID_LANGUAGE | __LCID_EXISTS);
  520. lcidLanguage = lcidCountry = lcid;
  521. }
  522. // test if match already for primary langauage
  523. else if (!(iLcidState & __LCID_PRIMARY))
  524. {
  525. // if not, use iPrimaryLen to partial match language string
  526. if (iPrimaryLen && !_strnicmp(pchLanguage, rgcInfo, iPrimaryLen))
  527. {
  528. // primary language matched - set state and country LCID
  529. iLcidState |= __LCID_PRIMARY;
  530. lcidCountry = lcid;
  531. // if language is primary only (no subtype), set language LCID
  532. if ((int)strlen(pchLanguage) == iPrimaryLen)
  533. lcidLanguage = lcid;
  534. }
  535. // test if default language already defined
  536. else if (!(iLcidState & __LCID_DEFAULT))
  537. {
  538. // if not, test if locale language is default for country
  539. if (TestDefaultCountry(lcid))
  540. {
  541. // default language for country - set state, value
  542. iLcidState |= __LCID_DEFAULT;
  543. lcidCountry = lcid;
  544. }
  545. }
  546. }
  547. }
  548. // test if input language both exists and default primary language defined
  549. if ((iLcidState & (__LCID_LANGUAGE | __LCID_EXISTS)) !=
  550. (__LCID_LANGUAGE | __LCID_EXISTS))
  551. {
  552. // test language match to determine whether it is installed
  553. if ((*pfnGetLocaleInfoA)(lcid, bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
  554. : LOCALE_SENGLANGUAGE,
  555. rgcInfo, sizeof(rgcInfo)) == 0)
  556. {
  557. // set error condition and exit
  558. iLcidState = 0;
  559. return TRUE;
  560. }
  561. if (!_stricmp(pchLanguage, rgcInfo))
  562. {
  563. // language matched - set bit for existance
  564. iLcidState |= __LCID_EXISTS;
  565. if (bAbbrevLanguage)
  566. {
  567. // abbreviation - set state
  568. // also set language LCID if not set already
  569. iLcidState |= __LCID_LANGUAGE;
  570. if (!lcidLanguage)
  571. lcidLanguage = lcid;
  572. }
  573. // test if language is primary only (no sublanguage)
  574. else if (iPrimaryLen && ((int)strlen(pchLanguage) == iPrimaryLen))
  575. {
  576. // primary language only - test if default LCID
  577. if (TestDefaultLanguage(lcid, TRUE))
  578. {
  579. // default primary language - set state
  580. // also set LCID if not set already
  581. iLcidState |= __LCID_LANGUAGE;
  582. if (!lcidLanguage)
  583. lcidLanguage = lcid;
  584. }
  585. }
  586. else
  587. {
  588. // language with sublanguage - set state
  589. // also set LCID if not set already
  590. iLcidState |= __LCID_LANGUAGE;
  591. if (!lcidLanguage)
  592. lcidLanguage = lcid;
  593. }
  594. }
  595. else if (!bAbbrevLanguage && iPrimaryLen
  596. && !_strnicmp(pchLanguage, rgcInfo, iPrimaryLen))
  597. {
  598. // primary language match - test for default language only
  599. if (TestDefaultLanguage(lcid, FALSE))
  600. {
  601. // default primary language - set state
  602. // also set LCID if not set already
  603. iLcidState |= __LCID_LANGUAGE;
  604. if (!lcidLanguage)
  605. lcidLanguage = lcid;
  606. }
  607. }
  608. }
  609. // if LOCALE_FULL set, return FALSE to stop enumeration,
  610. // else return TRUE to continue
  611. return (iLcidState & __LCID_FULL) == 0;
  612. }
  613. /***
  614. *void GetLcidFromLanguage - get LCIDs from language string
  615. *
  616. *Purpose:
  617. * Match the best LCIDs to the language string given. After global
  618. * variables are initialized, the LanguageEnumProc routine is
  619. * registered as an EnumSystemLocalesA callback to actually perform
  620. * the matching as the LCIDs are enumerated.
  621. *
  622. *Entry:
  623. * pchLanguage - language string
  624. * bAbbrevLanguage - language string is a three-letter abbreviation
  625. * iPrimaryLen - length of language string with primary name
  626. *
  627. *Exit:
  628. * lcidLanguage - lcidCountry - LCID of language with default
  629. * country
  630. *
  631. *Exceptions:
  632. *
  633. *******************************************************************************/
  634. static void GetLcidFromLanguage (void)
  635. {
  636. // initialize static variables for callback use
  637. bAbbrevLanguage = strlen(pchLanguage) == 3;
  638. iPrimaryLen = bAbbrevLanguage ? 2 : GetPrimaryLen(pchLanguage);
  639. EnumSystemLocalesA(LanguageEnumProc, LCID_INSTALLED);
  640. // locale value is invalid if the language was not installed
  641. // or the language was not available for the country specified
  642. if (!(iLcidState & __LCID_FULL))
  643. iLcidState = 0;
  644. }
  645. /***
  646. *BOOL CALLBACK LanguageEnumProc - callback routine for GetLcidFromLanguage
  647. *
  648. *Purpose:
  649. * Determine if LCID given matches the default country for the
  650. * language in pchLanguage.
  651. *
  652. *Entry:
  653. * lpLcidString - pointer to string with decimal LCID
  654. * pchLanguage - pointer to language name
  655. * bAbbrevLanguage - set if language is three-letter abbreviation
  656. *
  657. *Exit:
  658. * lcidLanguage - lcidCountry - LCID matched
  659. * FALSE if match occurred to terminate enumeration, else TRUE.
  660. *
  661. *Exceptions:
  662. *
  663. *******************************************************************************/
  664. static BOOL CALLBACK LanguageEnumProc (LPSTR lpLcidString)
  665. {
  666. LCID lcid = LcidFromHexString(lpLcidString);
  667. char rgcInfo[120];
  668. // test locale for language specified
  669. if ((*pfnGetLocaleInfoA)(lcid, bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
  670. : LOCALE_SENGLANGUAGE,
  671. rgcInfo, sizeof(rgcInfo)) == 0)
  672. {
  673. // set error condition and exit
  674. iLcidState = 0;
  675. return TRUE;
  676. }
  677. if (!_stricmp(pchLanguage, rgcInfo))
  678. {
  679. // language matched - test if locale country is default
  680. // or if locale is implied in the language string
  681. if (bAbbrevLanguage || TestDefaultLanguage(lcid, TRUE))
  682. {
  683. // this locale has the default country
  684. lcidLanguage = lcidCountry = lcid;
  685. iLcidState |= __LCID_FULL;
  686. }
  687. }
  688. else if (!bAbbrevLanguage && iPrimaryLen
  689. && !_strnicmp(pchLanguage, rgcInfo, iPrimaryLen))
  690. {
  691. // primary language matched - test if locale country is default
  692. if (TestDefaultLanguage(lcid, FALSE))
  693. {
  694. // this is the default country
  695. lcidLanguage = lcidCountry = lcid;
  696. iLcidState |= __LCID_FULL;
  697. }
  698. }
  699. return (iLcidState & __LCID_FULL) == 0;
  700. }
  701. /***
  702. *void GetLcidFromCountry - get LCIDs from country string
  703. *
  704. *Purpose:
  705. * Match the best LCIDs to the country string given. After global
  706. * variables are initialized, the CountryEnumProc routine is
  707. * registered as an EnumSystemLocalesA callback to actually perform
  708. * the matching as the LCIDs are enumerated.
  709. *
  710. *Entry:
  711. * pchCountry - country string
  712. * bAbbrevCountry - country string is a three-letter abbreviation
  713. *
  714. *Exit:
  715. * lcidLanguage - lcidCountry - LCID of country with default
  716. * language
  717. *
  718. *Exceptions:
  719. *
  720. *******************************************************************************/
  721. static void GetLcidFromCountry (void)
  722. {
  723. bAbbrevCountry = strlen(pchCountry) == 3;
  724. EnumSystemLocalesA(CountryEnumProc, LCID_INSTALLED);
  725. // locale value is invalid if the country was not defined or
  726. // no default language was found
  727. if (!(iLcidState & __LCID_FULL))
  728. iLcidState = 0;
  729. }
  730. /***
  731. *BOOL CALLBACK CountryEnumProc - callback routine for GetLcidFromCountry
  732. *
  733. *Purpose:
  734. * Determine if LCID given matches the default language for the
  735. * country in pchCountry.
  736. *
  737. *Entry:
  738. * lpLcidString - pointer to string with decimal LCID
  739. * pchCountry - pointer to country name
  740. * bAbbrevCountry - set if country is three-letter abbreviation
  741. *
  742. *Exit:
  743. * lcidLanguage - lcidCountry - LCID matched
  744. * FALSE if match occurred to terminate enumeration, else TRUE.
  745. *
  746. *Exceptions:
  747. *
  748. *******************************************************************************/
  749. static BOOL CALLBACK CountryEnumProc (LPSTR lpLcidString)
  750. {
  751. LCID lcid = LcidFromHexString(lpLcidString);
  752. char rgcInfo[120];
  753. // test locale for country specified
  754. if ((*pfnGetLocaleInfoA)(lcid, bAbbrevCountry ? LOCALE_SABBREVCTRYNAME
  755. : LOCALE_SENGCOUNTRY,
  756. rgcInfo, sizeof(rgcInfo)) == 0)
  757. {
  758. // set error condition and exit
  759. iLcidState = 0;
  760. return TRUE;
  761. }
  762. if (!_stricmp(pchCountry, rgcInfo))
  763. {
  764. // language matched - test if locale country is default
  765. if (TestDefaultCountry(lcid))
  766. {
  767. // this locale has the default language
  768. lcidLanguage = lcidCountry = lcid;
  769. iLcidState |= __LCID_FULL;
  770. }
  771. }
  772. return (iLcidState & __LCID_FULL) == 0;
  773. }
  774. /***
  775. *void GetLcidFromDefault - get default LCIDs
  776. *
  777. *Purpose:
  778. * Set both language and country LCIDs to the system default.
  779. *
  780. *Entry:
  781. * None.
  782. *
  783. *Exit:
  784. * lcidLanguage - set to system LCID
  785. * lcidCountry - set to system LCID
  786. *
  787. *Exceptions:
  788. *
  789. *******************************************************************************/
  790. static void GetLcidFromDefault (void)
  791. {
  792. iLcidState |= (__LCID_FULL | __LCID_LANGUAGE);
  793. lcidLanguage = lcidCountry = GetUserDefaultLCID();
  794. }
  795. /***
  796. *int ProcessCodePage - convert codepage string to numeric value
  797. *
  798. *Purpose:
  799. * Process codepage string consisting of a decimal string, or the
  800. * special case strings "ACP" and "OCP", for ANSI and OEM codepages,
  801. * respectively. Null pointer or string returns the ANSI codepage.
  802. *
  803. *Entry:
  804. * lpCodePageStr - pointer to codepage string
  805. *
  806. *Exit:
  807. * Returns numeric value of codepage.
  808. *
  809. *Exceptions:
  810. *
  811. *******************************************************************************/
  812. static int ProcessCodePage (LPSTR lpCodePageStr)
  813. {
  814. char chCodePage[8];
  815. if (!lpCodePageStr || !*lpCodePageStr || !strcmp(lpCodePageStr, "ACP"))
  816. {
  817. // get ANSI codepage for the country LCID
  818. if ((*pfnGetLocaleInfoA)(lcidCountry, LOCALE_IDEFAULTANSICODEPAGE,
  819. chCodePage, sizeof(chCodePage)) == 0)
  820. return 0;
  821. lpCodePageStr = chCodePage;
  822. }
  823. else if (!strcmp(lpCodePageStr, "OCP"))
  824. {
  825. // get OEM codepage for the country LCID
  826. if ((*pfnGetLocaleInfoA)(lcidCountry, LOCALE_IDEFAULTCODEPAGE,
  827. chCodePage, sizeof(chCodePage)) == 0)
  828. return 0;
  829. lpCodePageStr = chCodePage;
  830. }
  831. // convert decimal string to numeric value
  832. return (int)atol(lpCodePageStr);
  833. }
  834. /***
  835. *BOOL TestDefaultCountry - determine if default locale for country
  836. *
  837. *Purpose:
  838. * Using a hardcoded list, determine if the locale of the given LCID
  839. * has the default sublanguage for the locale primary language. The
  840. * list contains the locales NOT having the default sublanguage.
  841. *
  842. *Entry:
  843. * lcid - LCID of locale to test
  844. *
  845. *Exit:
  846. * Returns TRUE if default sublanguage, else FALSE.
  847. *
  848. *Exceptions:
  849. *
  850. *******************************************************************************/
  851. static BOOL TestDefaultCountry (LCID lcid)
  852. {
  853. LANGID langid = LANGIDFROMLCID(lcid);
  854. int i;
  855. for (i = 0; i < sizeof(__rglangidNotDefault) / sizeof(LANGID); i++)
  856. {
  857. if (langid == __rglangidNotDefault[i])
  858. return FALSE;
  859. }
  860. return TRUE;
  861. }
  862. /***
  863. *BOOL TestDefaultLanguage - determine if default locale for language
  864. *
  865. *Purpose:
  866. * Determines if the given LCID has the default sublanguage.
  867. * If bTestPrimary is set, also allow TRUE when string contains an
  868. * implicit sublanguage.
  869. *
  870. *Entry:
  871. * LCID - lcid of locale to test
  872. * bTestPrimary - set if testing if language is primary
  873. *
  874. *Exit:
  875. * Returns TRUE if sublanguage is default for locale tested.
  876. * If bTestPrimary set, TRUE is language has implied sublanguge.
  877. *
  878. *Exceptions:
  879. *
  880. *******************************************************************************/
  881. static BOOL TestDefaultLanguage (LCID lcid, BOOL bTestPrimary)
  882. {
  883. char rgcInfo[120];
  884. LCID lcidDefault = MAKELCID(MAKELANGID(PRIMARYLANGID(LANGIDFROMLCID(lcid)),
  885. SUBLANG_DEFAULT), SORT_DEFAULT);
  886. if ((*pfnGetLocaleInfoA)(lcidDefault, LOCALE_ILANGUAGE, rgcInfo,
  887. sizeof(rgcInfo)) == 0)
  888. return FALSE;
  889. if (lcid != LcidFromHexString(rgcInfo))
  890. {
  891. // test if string contains an implicit sublanguage by
  892. // having a character other than upper/lowercase letters.
  893. if (bTestPrimary && GetPrimaryLen(pchLanguage) == (int)strlen(pchLanguage))
  894. return FALSE;
  895. }
  896. return TRUE;
  897. }
  898. /***
  899. *int crtGetLocalInfoA - get locale information for Win95
  900. *
  901. *Purpose:
  902. * For Win95, some calls to GetLocaleInfoA return incorrect results.
  903. * Simulate these calls with values looked up in a hard-coded table.
  904. *
  905. *Entry:
  906. * lcid - LCID of locale to get information from
  907. * lctype - index of information selection
  908. * lpdata - pointer to output string
  909. * cchdata - size of output string (including null)
  910. *
  911. *Exit:
  912. * lpdata - return string of locale information
  913. * returns TRUE if successful, else FALSE
  914. *
  915. *Exceptions:
  916. *
  917. *******************************************************************************/
  918. static int __stdcall crtGetLocaleInfoA (LCID lcid, LCTYPE lctype, LPSTR lpdata,
  919. int cchdata)
  920. {
  921. int i;
  922. int low = 0;
  923. int high = sizeof(__rgLocInfo) / sizeof(RGLOCINFO) - 1;
  924. const char * pchResult = NULL;
  925. // typical binary search - do until no more to search
  926. while (low <= high)
  927. {
  928. i = (low + high) / 2;
  929. if (lcid == __rgLocInfo[i].lcid)
  930. {
  931. // LCID matched - test for valid LCTYPE to simulate call
  932. switch (lctype)
  933. {
  934. case LOCALE_ILANGUAGE:
  935. pchResult = __rgLocInfo[i].chILanguage;
  936. break;
  937. case LOCALE_SENGLANGUAGE:
  938. pchResult = __rgLocInfo[i].pchSEngLanguage;
  939. break;
  940. case LOCALE_SABBREVLANGNAME:
  941. pchResult = __rgLocInfo[i].chSAbbrevLangName;
  942. break;
  943. case LOCALE_SENGCOUNTRY:
  944. pchResult = __rgLocInfo[i].pchSEngCountry;
  945. break;
  946. case LOCALE_SABBREVCTRYNAME:
  947. pchResult = __rgLocInfo[i].chSAbbrevCtryName;
  948. break;
  949. case LOCALE_IDEFAULTCODEPAGE:
  950. pchResult = __rgLocInfo[i].chIDefaultCodepage;
  951. break;
  952. case LOCALE_IDEFAULTANSICODEPAGE:
  953. pchResult = __rgLocInfo[i].chIDefaultAnsiCodepage;
  954. default:
  955. break;
  956. }
  957. if (!pchResult || cchdata < 1)
  958. // if LCTYPE did not match, break to use normal routine
  959. break;
  960. else
  961. {
  962. // copy data as much as possible to result and null-terminate
  963. strncpy(lpdata, pchResult, cchdata - 1);
  964. *(lpdata + cchdata - 1) = '\0';
  965. return 1;
  966. }
  967. }
  968. else if (lcid < __rgLocInfo[i].lcid)
  969. high = i - 1;
  970. else
  971. low = i + 1;
  972. }
  973. // LCID not found or LCTYPE not simulated
  974. return GetLocaleInfoA(lcid,lctype, lpdata, cchdata);
  975. }
  976. /***
  977. *LCID LcidFromHexString - convert hex string to value for LCID
  978. *
  979. *Purpose:
  980. * LCID values returned in hex ANSI strings - straight conversion
  981. *
  982. *Entry:
  983. * lpHexString - pointer to hex string to convert
  984. *
  985. *Exit:
  986. * Returns LCID computed.
  987. *
  988. *Exceptions:
  989. *
  990. *******************************************************************************/
  991. static LCID LcidFromHexString (LPSTR lpHexString)
  992. {
  993. char ch;
  994. DWORD lcid = 0;
  995. while (ch = *lpHexString++)
  996. {
  997. if (ch >= 'a' && ch <= 'f')
  998. ch += '9' + 1 - 'a';
  999. else if (ch >= 'A' && ch <= 'F')
  1000. ch += '9' + 1 - 'A';
  1001. lcid = lcid * 0x10 + ch - '0';
  1002. }
  1003. return (LCID)lcid;
  1004. }
  1005. /***
  1006. *int GetPrimaryLen - get length of primary language name
  1007. *
  1008. *Purpose:
  1009. * Determine primary language string length by scanning until
  1010. * first non-alphabetic character.
  1011. *
  1012. *Entry:
  1013. * pchLanguage - string to scan
  1014. *
  1015. *Exit:
  1016. * Returns length of primary language string.
  1017. *
  1018. *Exceptions:
  1019. *
  1020. *******************************************************************************/
  1021. static int GetPrimaryLen (LPSTR pchLanguage)
  1022. {
  1023. int len = 0;
  1024. char ch;
  1025. ch = *pchLanguage++;
  1026. while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
  1027. {
  1028. len++;
  1029. ch = *pchLanguage++;
  1030. }
  1031. return len;
  1032. }
  1033. #endif //if defined(_POSIX_)