PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/p7zip/CPP/myWindows/wine_GetXXXDefaultLangID.cpp

https://bitbucket.org/dbingham/cleanarchiver
C++ | 731 lines | 590 code | 87 blank | 54 comment | 89 complexity | ebd720c7b966f047de15ff220013829b MD5 | raw file
Possible License(s): LGPL-2.1, Unlicense
  1. #include "StdAfx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <wchar.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. typedef DWORD LCID;
  9. typedef void * ULONG_PTR; /* typedef unsigned long ULONG_PTR; */
  10. #define SORT_DEFAULT 0x0
  11. #define LANG_NEUTRAL 0x00
  12. #define LANG_ENGLISH 0x09
  13. #define SUBLANG_DEFAULT 0x01 /* user default */
  14. #define MAKELCID(l, s) ( (l & 0xFFFF) | ((s & 0xFFFF)<<16))
  15. #define MAKELANGID(p, s) ((((WORD)(s))<<10) | (WORD)(p))
  16. #define LANGIDFROMLCID(lcid) ((WORD)(lcid))
  17. static LCID lcid_LC_MESSAGES = 0;
  18. static LCID lcid_LC_CTYPE = 0;
  19. struct locale_name
  20. {
  21. WCHAR win_name[128]; /* Windows name ("en-US") */
  22. WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
  23. WCHAR *country; /* country ("US") */
  24. WCHAR *charset; /* charset ("UTF-8") for Unix format only */
  25. WCHAR *script; /* script ("Latn") for Windows format only */
  26. WCHAR *modifier; /* modifier or sort order */
  27. LCID lcid; /* corresponding LCID */
  28. int matches; /* number of elements matching LCID (0..4) */
  29. UINT codepage; /* codepage corresponding to charset */
  30. };
  31. #define WINE_UNICODE_INLINE static
  32. /***********************************************************/
  33. typedef struct {
  34. const WCHAR * LOCALE_SNAME;
  35. const WCHAR * LOCALE_SISO639LANGNAME;
  36. const WCHAR * LOCALE_SISO3166CTRYNAME;
  37. unsigned int LOCALE_IDEFAULTUNIXCODEPAGE;
  38. unsigned int LOCALE_ILANGUAGE;
  39. } t_info;
  40. static t_info g_langInfo[] = {
  41. { L"af-ZA" , L"af" , L"ZA" , 28591 , 0x0436 }, /* afk.nls */
  42. { L"ar-SA" , L"ar" , L"SA" , 28596 , 0x0401 }, /* ara.nls */
  43. { L"ar-LB" , L"ar" , L"LB" , 28596 , 0x3001 }, /* arb.nls */
  44. { L"ar-EG" , L"ar" , L"EG" , 28596 , 0x0c01 }, /* are.nls */
  45. { L"ar-DZ" , L"ar" , L"DZ" , 28596 , 0x1401 }, /* arg.nls */
  46. { L"ar-BH" , L"ar" , L"BH" , 28596 , 0x3c01 }, /* arh.nls */
  47. { L"ar-IQ" , L"ar" , L"IQ" , 28596 , 0x0801 }, /* ari.nls */
  48. { L"ar-JO" , L"ar" , L"JO" , 28596 , 0x2c01 }, /* arj.nls */
  49. { L"ar-KW" , L"ar" , L"KW" , 28596 , 0x3401 }, /* ark.nls */
  50. { L"ar-LY" , L"ar" , L"LY" , 28596 , 0x1001 }, /* arl.nls */
  51. { L"ar-MA" , L"ar" , L"MA" , 28596 , 0x1801 }, /* arm.nls */
  52. { L"ar-OM" , L"ar" , L"OM" , 28596 , 0x2001 }, /* aro.nls */
  53. { L"ar-QA" , L"ar" , L"QA" , 28596 , 0x4001 }, /* arq.nls */
  54. { L"ar-SY" , L"ar" , L"SY" , 28596 , 0x2801 }, /* ars.nls */
  55. { L"ar-TN" , L"ar" , L"TN" , 28596 , 0x1c01 }, /* art.nls */
  56. { L"ar-AE" , L"ar" , L"AE" , 28596 , 0x3801 }, /* aru.nls */
  57. { L"ar-YE" , L"ar" , L"YE" , 28596 , 0x2401 }, /* ary.nls */
  58. { L"az-AZ" , L"az" , L"AZ" , 28595 , 0x082c }, /* aze.nls */
  59. { L"az-Latn-AZ" , L"az" , L"AZ" , 28599 , 0x042c }, /* azl.nls */
  60. { L"be-BY" , L"be" , L"BY" , 1251 , 0x0423 }, /* bel.nls */
  61. { L"bg-BG" , L"bg" , L"BG" , 1251 , 0x0402 }, /* bgr.nls */
  62. { L"br-FR" , L"br" , L"FR" , 28605 , 0x0493 }, /* brf.nls */
  63. { L"ca-ES" , L"ca" , L"ES" , 28605 , 0x0403 }, /* cat.nls */
  64. { L"zh-CN" , L"zh" , L"CN" , 936 , 0x0804 }, /* chs.nls */
  65. { L"zh-TW" , L"zh" , L"TW" , 950 , 0x0404 }, /* cht.nls */
  66. { L"kw-GB" , L"kw" , L"GB" , 28605 , 0x04891 }, /* cor.nls */
  67. { L"cs-CZ" , L"cs" , L"CZ" , 28592 , 0x0405 }, /* csy.nls */
  68. { L"cy-GB" , L"cy" , L"GB" , 28604 , 0x0492 }, /* cym.nls */
  69. { L"da-DK" , L"da" , L"DK" , 28605 , 0x0406 }, /* dan.nls */
  70. { L"de-AT" , L"de" , L"AT" , 28605 , 0x0c07 }, /* dea.nls */
  71. { L"de-LI" , L"de" , L"LI" , 28605 , 0x1407 }, /* dec.nls */
  72. { L"de-LU" , L"de" , L"LU" , 28605 , 0x1007 }, /* del.nls */
  73. { L"de-CH" , L"de" , L"CH" , 28605 , 0x0807 }, /* des.nls */
  74. { L"de-DE" , L"de" , L"DE" , 28605 , 0x0407 }, /* deu.nls */
  75. { L"dv-MV" , L"dv" , L"MV" , 65001 , 0x0465 }, /* div.nls */
  76. { L"el-GR" , L"el" , L"GR" , 28597 , 0x0408 }, /* ell.nls */
  77. { L"en-AU" , L"en" , L"AU" , 28591 , 0x0c09 }, /* ena.nls */
  78. { L"en-CB" , L"en" , L"CB" , 28591 , 0x2409 }, /* enb.nls */
  79. { L"en-CA" , L"en" , L"CA" , 28591 , 0x1009 }, /* enc.nls */
  80. { L"en-GB" , L"en" , L"GB" , 28605 , 0x0809 }, /* eng.nls */
  81. { L"en-IE" , L"en" , L"IE" , 28605 , 0x1809 }, /* eni.nls */
  82. { L"en-JM" , L"en" , L"JM" , 28591 , 0x2009 }, /* enj.nls */
  83. { L"en-BZ" , L"en" , L"BZ" , 28591 , 0x2809 }, /* enl.nls */
  84. { L"en-PH" , L"en" , L"PH" , 28591 , 0x3409 }, /* enp.nls */
  85. { L"en-ZA" , L"en" , L"ZA" , 28591 , 0x1c09 }, /* ens.nls */
  86. { L"en-TT" , L"en" , L"TT" , 28591 , 0x2c09 }, /* ent.nls */
  87. { L"en-US" , L"en" , L"US" , 28591 , 0x0409 }, /* enu.nls */
  88. { L"en-ZW" , L"en" , L"ZW" , 28591 , 0x3009 }, /* enw.nls */
  89. { L"en-NZ" , L"en" , L"NZ" , 28591 , 0x1409 }, /* enz.nls */
  90. { L"eo" , L"eo" , L"" , 65001 , 0x048f }, /* eox.nls */
  91. { L"es-PA" , L"es" , L"PA" , 28591 , 0x180a }, /* esa.nls */
  92. { L"es-BO" , L"es" , L"BO" , 28591 , 0x400a }, /* esb.nls */
  93. { L"es-CR" , L"es" , L"CR" , 28591 , 0x140a }, /* esc.nls */
  94. { L"es-DO" , L"es" , L"DO" , 28591 , 0x1c0a }, /* esd.nls */
  95. { L"es-SV" , L"es" , L"SV" , 28591 , 0x440a }, /* ese.nls */
  96. { L"es-EC" , L"es" , L"EC" , 28591 , 0x300a }, /* esf.nls */
  97. { L"es-GT" , L"es" , L"GT" , 28591 , 0x100a }, /* esg.nls */
  98. { L"es-HN" , L"es" , L"HN" , 28591 , 0x480a }, /* esh.nls */
  99. { L"es-NI" , L"es" , L"NI" , 28591 , 0x4c0a }, /* esi.nls */
  100. { L"es-C" , L"es" , L"C" , 28591 , 0x340a }, /* esl.nls */
  101. { L"es-MX" , L"es" , L"MX" , 28591 , 0x080a }, /* esm.nls */
  102. { L"es-ES_modern" , L"es" , L"ES" , 28605 , 0x0c0a }, /* esn.nls */
  103. { L"es-CO" , L"es" , L"CO" , 28591 , 0x240a }, /* eso.nls */
  104. { L"es-ES" , L"es" , L"ES" , 28605 , 0x040a }, /* esp.nls */
  105. { L"es-PE" , L"es" , L"PE" , 28591 , 0x280a }, /* esr.nls */
  106. { L"es-AR" , L"es" , L"AR" , 28591 , 0x2c0a }, /* ess.nls */
  107. { L"es-PR" , L"es" , L"PR" , 28591 , 0x500a }, /* esu.nls */
  108. { L"es-VE" , L"es" , L"VE" , 28591 , 0x200a }, /* esv.nls */
  109. { L"es-UY" , L"es" , L"UY" , 28591 , 0x380a }, /* esy.nls */
  110. { L"es-PY" , L"es" , L"PY" , 28591 , 0x3c0a }, /* esz.nls */
  111. { L"et-EE" , L"et" , L"EE" , 28605 , 0x0425 }, /* eti.nls */
  112. { L"eu-ES" , L"eu" , L"ES" , 28605 , 0x042d }, /* euq.nls */
  113. { L"fa-IR" , L"fa" , L"IR" , 65001 , 0x0429 }, /* far.nls */
  114. { L"fi-FI" , L"fi" , L"FI" , 28605 , 0x040b }, /* fin.nls */
  115. { L"fo-FO" , L"fo" , L"FO" , 28605 , 0x0438 }, /* fos.nls */
  116. { L"fr-FR" , L"fr" , L"FR" , 28605 , 0x040c }, /* fra.nls */
  117. { L"fr-BE" , L"fr" , L"BE" , 28605 , 0x080c }, /* frb.nls */
  118. { L"fr-CA" , L"fr" , L"CA" , 28591 , 0x0c0c }, /* frc.nls */
  119. { L"fr-LU" , L"fr" , L"LU" , 28605 , 0x140c }, /* frl.nls */
  120. { L"fr-MC" , L"fr" , L"MC" , 28605 , 0x180c }, /* frm.nls */
  121. { L"fr-CH" , L"fr" , L"CH" , 28605 , 0x100c }, /* frs.nls */
  122. { L"ga-IE" , L"ga" , L"IE" , 28605 , 0x043c }, /* gae.nls */
  123. { L"gd-GB" , L"gd" , L"GB" , 28605 , 0x083c }, /* gdh.nls */
  124. { L"gv-GB" , L"gv" , L"GB" , 28605 , 0x0c3c }, /* gdv.nls */
  125. { L"gl-ES" , L"gl" , L"ES" , 28605 , 0x0456 }, /* glc.nls */
  126. { L"gu-IN" , L"gu" , L"IN" , 65001 , 0x0447 }, /* guj.nls */
  127. { L"he-I" , L"he" , L"I" , 28598 , 0x040d }, /* heb.nls */
  128. { L"hi-IN" , L"hi" , L"IN" , 65001 , 0x0439 }, /* hin.nls */
  129. { L"hr-HR" , L"hr" , L"HR" , 28592 , 0x041a }, /* hrv.nls */
  130. { L"hu-HU" , L"hu" , L"HU" , 28592 , 0x040e }, /* hun.nls */
  131. { L"hy-AM" , L"hy" , L"AM" , 65001 , 0x042b }, /* hye.nls */
  132. { L"id-ID" , L"id" , L"ID" , 28591 , 0x0421 }, /* ind.nls */
  133. { L"is-IS" , L"is" , L"IS" , 28605 , 0x040f }, /* isl.nls */
  134. { L"it-IT" , L"it" , L"IT" , 28605 , 0x0410 }, /* ita.nls */
  135. { L"it-CH" , L"it" , L"CH" , 28605 , 0x0810 }, /* its.nls */
  136. { L"ja-JP" , L"ja" , L"JP" , 20932 , 0x0411 }, /* jpn.nls */
  137. { L"kn-IN" , L"kn" , L"IN" , 65001 , 0x044b }, /* kan.nls */
  138. { L"ka-GE" , L"ka" , L"GE" , 65001 , 0x0437 }, /* kat.nls */
  139. { L"kk-KZ" , L"kk" , L"KZ" , 28595 , 0x043f }, /* kkz.nls */
  140. { L"kok-IN" , L"kok" , L"IN" , 65001 , 0x0457 }, /* knk.nls */
  141. { L"ko-KR" , L"ko" , L"KR" , 949 , 0x0412 }, /* kor.nls */
  142. { L"ky-KG" , L"ky" , L"KG" , 28595 , 0x0440 }, /* kyr.nls */
  143. { L"lt-LT" , L"lt" , L"LT" , 28603 , 0x0427 }, /* lth.nls */
  144. { L"lv-LV" , L"lv" , L"LV" , 28603 , 0x0426 }, /* lvi.nls */
  145. { L"mr-IN" , L"mr" , L"IN" , 65001 , 0x044e }, /* mar.nls */
  146. { L"mk-MK" , L"mk" , L"MK" , 28595 , 0x042f }, /* mki.nls */
  147. { L"mn-MN" , L"mn" , L"MN" , 28595 , 0x0450 }, /* mon.nls */
  148. { L"ms-BN" , L"ms" , L"BN" , 28591 , 0x083e }, /* msb.nls */
  149. { L"ms-MY" , L"ms" , L"MY" , 28591 , 0x043e }, /* msl.nls */
  150. { L"nl-BE" , L"nl" , L"BE" , 28605 , 0x0813 }, /* nlb.nls */
  151. { L"nl-N" , L"nl" , L"N" , 28605 , 0x0413 }, /* nld.nls */
  152. { L"nl-SR" , L"nl" , L"SR" , 28605 , 0x0c13 }, /* nls.nls */
  153. { L"nn-NO" , L"nn" , L"NO" , 28605 , 0x0814 }, /* non.nls */
  154. { L"nb-NO" , L"nb" , L"NO" , 28605 , 0x0414 }, /* nor.nls */
  155. { L"pa-IN" , L"pa" , L"IN" , 65001 , 0x0446 }, /* pan.nls */
  156. { L"pl-P" , L"pl" , L"P" , 28592 , 0x0415 }, /* plk.nls */
  157. { L"pt-BR" , L"pt" , L"BR" , 28591 , 0x0416 }, /* ptb.nls */
  158. { L"pt-PT" , L"pt" , L"PT" , 28605 , 0x0816 }, /* ptg.nls */
  159. { L"rm-CH" , L"rm" , L"CH" , 28605 , 0x0417 }, /* rmc.nls */
  160. { L"ro-RO" , L"ro" , L"RO" , 28592 , 0x0418 }, /* rom.nls */
  161. { L"ru-RU" , L"ru" , L"RU" , 20866 , 0x0419 }, /* rus.nls */
  162. { L"sa-IN" , L"sa" , L"IN" , 65001 , 0x044f }, /* san.nls */
  163. { L"sk-SK" , L"sk" , L"SK" , 28592 , 0x041b }, /* sky.nls */
  164. { L"sl-SI" , L"sl" , L"SI" , 28592 , 0x0424 }, /* slv.nls */
  165. { L"sq-A" , L"sq" , L"A" , 28592 , 0x041c }, /* sqi.nls */
  166. { L"sr-SP" , L"sr" , L"SP" , 28595 , 0x0c1a }, /* srb.nls */
  167. { L"sr-Latn-SP" , L"sr" , L"SP" , 28592 , 0x081a }, /* srl.nls */
  168. { L"sv-SE" , L"sv" , L"SE" , 28605 , 0x041d }, /* sve.nls */
  169. { L"sv-FI" , L"sv" , L"FI" , 28605 , 0x081d }, /* svf.nls */
  170. { L"sw-KE" , L"sw" , L"KE" , 28591 , 0x0441 }, /* swk.nls */
  171. { L"syr-SY" , L"syr" , L"SY" , 65001 , 0x045a }, /* syr.nls */
  172. { L"ta-IN" , L"ta" , L"IN" , 65001 , 0x0449 }, /* tam.nls */
  173. { L"te-IN" , L"te" , L"IN" , 65001 , 0x044a }, /* tel.nls */
  174. { L"th-TH" , L"th" , L"TH" , 874 , 0x041e }, /* tha.nls */
  175. { L"tr-TR" , L"tr" , L"TR" , 28599 , 0x041f }, /* trk.nls */
  176. { L"tt-TA" , L"tt" , L"TA" , 28595 , 0x0444 }, /* ttt.nls */
  177. { L"uk-UA" , L"uk" , L"UA" , 21866 , 0x0422 }, /* ukr.nls */
  178. { L"ur-PK" , L"ur" , L"PK" , 1256 , 0x0420 }, /* urd.nls */
  179. { L"uz-UZ" , L"uz" , L"UZ" , 28595 , 0x0843 }, /* uzb.nls */
  180. { L"uz-Latn-UZ" , L"uz" , L"UZ" , 28605 , 0x0443 }, /* uzl.nls */
  181. { L"vi-VN" , L"vi" , L"VN" , 1258 , 0x042a }, /* vit.nls */
  182. { L"wa-BE" , L"wa" , L"BE" , 28605 , 0x0490 }, /* wal.nls */
  183. { L"zh-HK" , L"zh" , L"HK" , 950 , 0x0c04 }, /* zhh.nls */
  184. { L"zh-SG" , L"zh" , L"SG" , 936 , 0x1004 }, /* zhi.nls */
  185. { L"zh-MO" , L"zh" , L"MO" , 950 , 0x1404 }, /* zhm.nls */
  186. { 0 , 0 , 0 , 0, 0 }
  187. };
  188. /***********************************************************/
  189. WINE_UNICODE_INLINE WCHAR *strchrW( const WCHAR *str, WCHAR ch )
  190. {
  191. do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
  192. return NULL;
  193. }
  194. WINE_UNICODE_INLINE WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
  195. {
  196. for ( ; *str; str++) if (strchrW( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
  197. return NULL;
  198. }
  199. /***********************************************************/
  200. WINE_UNICODE_INLINE unsigned int strlenW( const WCHAR *str )
  201. {
  202. const WCHAR *s = str;
  203. while (*s) s++;
  204. return s - str;
  205. }
  206. WINE_UNICODE_INLINE WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
  207. {
  208. WCHAR *p = dst;
  209. while ((*p++ = *src++));
  210. return dst;
  211. }
  212. WINE_UNICODE_INLINE WCHAR *strcatW( WCHAR *dst, const WCHAR *src )
  213. {
  214. strcpyW( dst + strlenW(dst), src );
  215. return dst;
  216. }
  217. WINE_UNICODE_INLINE int strcmpW( const WCHAR *str1, const WCHAR *str2 )
  218. {
  219. while (*str1 && (*str1 == *str2)) { str1++; str2++; }
  220. return *str1 - *str2;
  221. }
  222. WINE_UNICODE_INLINE LPWSTR lstrcpynW( LPWSTR dst, LPCWSTR src, int n )
  223. {
  224. {
  225. LPWSTR d = dst;
  226. LPCWSTR s = src;
  227. UINT count = n;
  228. while ((count > 1) && *s)
  229. {
  230. count--;
  231. *d++ = *s++;
  232. }
  233. if (count) *d = 0;
  234. }
  235. return dst;
  236. }
  237. /* Copy Ascii string to Unicode without using codepages */
  238. static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
  239. {
  240. while (n > 1 && *src)
  241. {
  242. *dst++ = (unsigned char)*src++;
  243. n--;
  244. }
  245. if (n) *dst = 0;
  246. }
  247. /*******************************************************/
  248. /* Charset to codepage map, sorted by name. */
  249. static const struct charset_entry
  250. {
  251. const char *charset_name;
  252. UINT codepage;
  253. } charset_names[] =
  254. {
  255. { "BIG5", 950 },
  256. { "CP1250", 1250 },
  257. { "CP1251", 1251 },
  258. { "CP1252", 1252 },
  259. { "CP1253", 1253 },
  260. { "CP1254", 1254 },
  261. { "CP1255", 1255 },
  262. { "CP1256", 1256 },
  263. { "CP1257", 1257 },
  264. { "CP1258", 1258 },
  265. { "CP932", 932 },
  266. { "CP936", 936 },
  267. { "CP949", 949 },
  268. { "CP950", 950 },
  269. { "EUCJP", 20932 },
  270. { "GB2312", 936 },
  271. { "IBM037", 37 },
  272. { "IBM1026", 1026 },
  273. { "IBM424", 424 },
  274. { "IBM437", 437 },
  275. { "IBM500", 500 },
  276. { "IBM850", 850 },
  277. { "IBM852", 852 },
  278. { "IBM855", 855 },
  279. { "IBM857", 857 },
  280. { "IBM860", 860 },
  281. { "IBM861", 861 },
  282. { "IBM862", 862 },
  283. { "IBM863", 863 },
  284. { "IBM864", 864 },
  285. { "IBM865", 865 },
  286. { "IBM866", 866 },
  287. { "IBM869", 869 },
  288. { "IBM874", 874 },
  289. { "IBM875", 875 },
  290. { "ISO88591", 28591 },
  291. { "ISO885910", 28600 },
  292. { "ISO885913", 28603 },
  293. { "ISO885914", 28604 },
  294. { "ISO885915", 28605 },
  295. { "ISO885916", 28606 },
  296. { "ISO88592", 28592 },
  297. { "ISO88593", 28593 },
  298. { "ISO88594", 28594 },
  299. { "ISO88595", 28595 },
  300. { "ISO88596", 28596 },
  301. { "ISO88597", 28597 },
  302. { "ISO88598", 28598 },
  303. { "ISO88599", 28599 },
  304. { "KOI8R", 20866 },
  305. { "KOI8U", 21866 },
  306. { "UTF8", CP_UTF8 }
  307. };
  308. static int charset_cmp( const void *name, const void *entry )
  309. {
  310. const struct charset_entry *charset = (const struct charset_entry *)entry;
  311. return strcasecmp( (const char *)name, charset->charset_name );
  312. }
  313. static UINT find_charset( const WCHAR *name )
  314. {
  315. const struct charset_entry *entry;
  316. char charset_name[16];
  317. size_t i, j;
  318. /* remove punctuation characters from charset name */
  319. for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
  320. if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
  321. charset_name[j] = 0;
  322. entry = (const struct charset_entry *)bsearch( charset_name, charset_names,
  323. sizeof(charset_names)/sizeof(charset_names[0]),
  324. sizeof(charset_names[0]), charset_cmp );
  325. if (entry) return entry->codepage;
  326. return 0;
  327. }
  328. /*******************************************************/
  329. static BOOL find_locale_id_callback(/* LPCWSTR name, ? */ const t_info * tab, struct locale_name *data)
  330. {
  331. // WCHAR buffer[128];
  332. int matches = 0;
  333. WORD LangID = tab->LOCALE_ILANGUAGE & 0xFFFF; /* FIXME */
  334. LCID lcid = MAKELCID( LangID, SORT_DEFAULT ); /* FIXME: handle sort order */
  335. if (PRIMARYLANGID(LangID) == LANG_NEUTRAL) return TRUE; /* continue search */
  336. /* first check exact name */
  337. if (data->win_name[0] && tab->LOCALE_SNAME[0])
  338. /* GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE,
  339. buffer, sizeof(buffer)/sizeof(WCHAR) )) */
  340. {
  341. if (!strcmpW( data->win_name, tab->LOCALE_SNAME ))
  342. {
  343. matches = 4; /* everything matches */
  344. goto done;
  345. }
  346. }
  347. /*if (!GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME | LOCALE_NOUSEROVERRIDE,
  348. buffer, sizeof(buffer)/sizeof(WCHAR) )) */
  349. if (tab->LOCALE_SISO639LANGNAME[0] == 0)
  350. return TRUE;
  351. if (strcmpW( tab->LOCALE_SISO639LANGNAME , data->lang )) return TRUE;
  352. matches++; /* language name matched */
  353. if (data->country)
  354. {
  355. /* if (GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
  356. buffer, sizeof(buffer)/sizeof(WCHAR) )) */
  357. if (tab->LOCALE_SISO3166CTRYNAME[0])
  358. {
  359. if (strcmpW(tab->LOCALE_SISO3166CTRYNAME , data->country )) goto done;
  360. matches++; /* country name matched */
  361. }
  362. }
  363. else /* match default language */
  364. {
  365. if (SUBLANGID(LangID) == SUBLANG_DEFAULT) matches++;
  366. }
  367. if (data->codepage)
  368. {
  369. UINT unix_cp;
  370. /* if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
  371. (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) )) */
  372. unix_cp = tab->LOCALE_IDEFAULTUNIXCODEPAGE;
  373. {
  374. if (unix_cp == data->codepage) matches++;
  375. }
  376. }
  377. /* FIXME: check sort order */
  378. done:
  379. if (matches > data->matches)
  380. {
  381. data->lcid = lcid;
  382. data->matches = matches;
  383. }
  384. return (data->matches < 4); /* no need to continue for perfect match */
  385. }
  386. /***********************************************************************
  387. * parse_locale_name
  388. *
  389. * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
  390. * Unix format is: lang[_country][.charset][@modifier]
  391. * Windows format is: lang[-script][-country][_modifier]
  392. */
  393. static void parse_locale_name( const WCHAR *str, struct locale_name *name )
  394. {
  395. static const WCHAR sepW[] = {'-','_','.','@',0};
  396. static const WCHAR winsepW[] = {'-','_',0};
  397. static const WCHAR posixW[] = {'P','O','S','I','X',0};
  398. static const WCHAR cW[] = {'C',0};
  399. static const WCHAR latinW[] = {'l','a','t','i','n',0};
  400. static const WCHAR latnW[] = {'-','L','a','t','n',0};
  401. WCHAR *p;
  402. int ind;
  403. // TRACE("%s\n", debugstr_w(str));
  404. name->country = name->charset = name->script = name->modifier = NULL;
  405. name->lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
  406. name->matches = 0;
  407. name->codepage = 0;
  408. name->win_name[0] = 0;
  409. lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
  410. if (!(p = strpbrkW( name->lang, sepW )))
  411. {
  412. if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
  413. {
  414. name->matches = 4; /* perfect match for default English lcid */
  415. return;
  416. }
  417. strcpyW( name->win_name, name->lang );
  418. }
  419. else if (*p == '-') /* Windows format */
  420. {
  421. strcpyW( name->win_name, name->lang );
  422. *p++ = 0;
  423. name->country = p;
  424. if (!(p = strpbrkW( p, winsepW ))) goto done;
  425. if (*p == '-')
  426. {
  427. *p++ = 0;
  428. name->script = name->country;
  429. name->country = p;
  430. if (!(p = strpbrkW( p, winsepW ))) goto done;
  431. }
  432. *p++ = 0;
  433. name->modifier = p;
  434. }
  435. else /* Unix format */
  436. {
  437. if (*p == '_')
  438. {
  439. *p++ = 0;
  440. name->country = p;
  441. p = strpbrkW( p, sepW + 2 );
  442. }
  443. if (p && *p == '.')
  444. {
  445. *p++ = 0;
  446. name->charset = p;
  447. p = strchrW( p, '@' );
  448. }
  449. if (p)
  450. {
  451. *p++ = 0;
  452. name->modifier = p;
  453. }
  454. if (name->charset)
  455. name->codepage = find_charset( name->charset );
  456. /* rebuild a Windows name if possible */
  457. if (name->charset) goto done; /* can't specify charset in Windows format */
  458. if (name->modifier && strcmpW( name->modifier, latinW ))
  459. goto done; /* only Latn script supported for now */
  460. strcpyW( name->win_name, name->lang );
  461. if (name->modifier) strcatW( name->win_name, latnW );
  462. if (name->country)
  463. {
  464. p = name->win_name + strlenW(name->win_name);
  465. *p++ = '-';
  466. strcpyW( p, name->country );
  467. }
  468. }
  469. done:
  470. ;
  471. /* DEBUG
  472. printf("EnumResourceLanguagesW(...):\n");
  473. printf(" name->win_name=%ls\n", name->win_name);
  474. printf(" name->lang=%ls\n", name->lang);
  475. printf(" name->country=%ls\n", name->country);
  476. printf(" name->codepage=%d\n", name->codepage);
  477. */
  478. // EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
  479. // find_locale_id_callback, (LPARAM)name );
  480. ind = 0;
  481. while (g_langInfo[ind].LOCALE_SNAME)
  482. {
  483. BOOL ret = find_locale_id_callback(&g_langInfo[ind],name);
  484. if (ret == FALSE)
  485. break;
  486. ind++;
  487. }
  488. }
  489. /********************************/
  490. static UINT setup_unix_locales(void)
  491. {
  492. struct locale_name locale_name;
  493. // WCHAR buffer[128];
  494. WCHAR ctype_buff[128];
  495. char *locale;
  496. UINT unix_cp = 0;
  497. if ((locale = setlocale( LC_CTYPE, NULL )))
  498. {
  499. strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
  500. parse_locale_name( ctype_buff, &locale_name );
  501. lcid_LC_CTYPE = locale_name.lcid;
  502. unix_cp = locale_name.codepage;
  503. }
  504. if (!lcid_LC_CTYPE) /* this one needs a default value */
  505. lcid_LC_CTYPE = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
  506. #if 0
  507. TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
  508. locale_name.lcid, locale_name.matches, debugstr_a(locale) );
  509. #define GET_UNIX_LOCALE(cat) do \
  510. if ((locale = setlocale( cat, NULL ))) \
  511. { \
  512. strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
  513. if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
  514. else { \
  515. parse_locale_name( buffer, &locale_name ); \
  516. lcid_##cat = locale_name.lcid; \
  517. TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
  518. locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
  519. } \
  520. } while (0)
  521. GET_UNIX_LOCALE( LC_COLLATE );
  522. GET_UNIX_LOCALE( LC_MESSAGES );
  523. GET_UNIX_LOCALE( LC_MONETARY );
  524. GET_UNIX_LOCALE( LC_NUMERIC );
  525. GET_UNIX_LOCALE( LC_TIME );
  526. #ifdef LC_PAPER
  527. GET_UNIX_LOCALE( LC_PAPER );
  528. #endif
  529. #ifdef LC_MEASUREMENT
  530. GET_UNIX_LOCALE( LC_MEASUREMENT );
  531. #endif
  532. #ifdef LC_TELEPHONE
  533. GET_UNIX_LOCALE( LC_TELEPHONE );
  534. #endif
  535. #undef GET_UNIX_LOCALE
  536. #endif // #if 0
  537. return unix_cp;
  538. }
  539. /********************************/
  540. static void LOCALE_Init(void)
  541. {
  542. /*
  543. extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
  544. const union cptable *unix_cp );
  545. */
  546. // UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
  547. UINT unix_cp = 0;
  548. #ifdef __APPLE__
  549. /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
  550. CFArrayRef preferred_locales, all_locales;
  551. CFStringRef user_language_string_ref = NULL;
  552. char user_locale[50];
  553. CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
  554. CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
  555. CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
  556. CFRelease( user_locale_ref );
  557. if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
  558. unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
  559. setenv( "LANG", user_locale, 0 );
  560. // TRACE( "setting locale to '%s'\n", user_locale );
  561. /* We still want to set the retrieve the preferred language as chosen in
  562. System Preferences.app, because it can differ from CFLocaleCopyCurrent().
  563. */
  564. all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
  565. preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
  566. if (preferred_locales && CFArrayGetCount( preferred_locales ))
  567. user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
  568. CFRelease( all_locales );
  569. #endif /* __APPLE__ */
  570. // FIXME setlocale( LC_ALL, "" );
  571. unix_cp = setup_unix_locales();
  572. if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
  573. #if 0
  574. #ifdef __APPLE__
  575. /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */
  576. if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref)
  577. {
  578. struct locale_name locale_name;
  579. WCHAR buffer[128];
  580. CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
  581. strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) );
  582. parse_locale_name( buffer, &locale_name );
  583. lcid_LC_MESSAGES = locale_name.lcid;
  584. TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale );
  585. }
  586. if (preferred_locales)
  587. CFRelease( preferred_locales );
  588. #endif
  589. NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
  590. NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
  591. NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
  592. ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
  593. GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
  594. (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
  595. GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
  596. (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
  597. if (!unix_cp)
  598. GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
  599. (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
  600. if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
  601. ansi_cptable = wine_cp_get_table( 1252 );
  602. if (!(oem_cptable = wine_cp_get_table( oem_cp )))
  603. oem_cptable = wine_cp_get_table( 437 );
  604. if (!(mac_cptable = wine_cp_get_table( mac_cp )))
  605. mac_cptable = wine_cp_get_table( 10000 );
  606. if (unix_cp != CP_UTF8)
  607. {
  608. if (!(unix_cptable = wine_cp_get_table( unix_cp )))
  609. unix_cptable = wine_cp_get_table( 28591 );
  610. }
  611. __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
  612. TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
  613. ansi_cptable->info.codepage, oem_cptable->info.codepage,
  614. mac_cptable->info.codepage, unix_cp );
  615. setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
  616. #endif
  617. }
  618. LANGID GetUserDefaultLangID(void)
  619. {
  620. // return LANGIDFROMLCID(GetUserDefaultLCID());
  621. if (lcid_LC_MESSAGES == 0) LOCALE_Init();
  622. return LANGIDFROMLCID(lcid_LC_MESSAGES);
  623. }
  624. LANGID GetSystemDefaultLangID(void)
  625. {
  626. // return LANGIDFROMLCID(GetSystemDefaultLCID());
  627. if (lcid_LC_MESSAGES == 0) LOCALE_Init();
  628. return LANGIDFROMLCID(lcid_LC_MESSAGES);
  629. }
  630. #ifdef TEST
  631. int main()
  632. {
  633. LANGID langID;
  634. WORD primLang;
  635. WORD subLang;
  636. setlocale( LC_ALL, "" );
  637. langID = GetUserDefaultLangID();
  638. printf("langID=0x%x\n",langID);
  639. primLang = (WORD)(PRIMARYLANGID(langID));
  640. subLang = (WORD)(SUBLANGID(langID));
  641. printf("primLang=%d subLang=%d\n",(unsigned)primLang,(unsigned)subLang);
  642. return 0;
  643. }
  644. #endif