1 /******************************************************************************* 2 3 copyright: Copyright (c) 2005 John Chapman. All rights reserved 4 5 license: BSD style: $(LICENSE) 6 7 version: Initial release: 2005 8 9 author: John Chapman 10 11 ******************************************************************************/ 12 13 module tango.text.locale.Win32; 14 15 version (Windows) 16 { 17 alias tango.text.locale.Win32 nativeMethods; 18 19 extern (Windows) 20 private { 21 uint GetUserDefaultLCID(); 22 uint GetThreadLocale(); 23 bool SetThreadLocale(uint Locale); 24 int MultiByteToWideChar(uint CodePage, uint dwFlags, const(char)* lpMultiByteStr, int cbMultiByte, wchar* lpWideCharStr, int cchWideChar); 25 int CompareStringW(uint Locale, uint dwCmpFlags, const(wchar)* lpString1, int cchCount1, const(wchar)* lpString2, int cchCount2); 26 27 } 28 29 int getUserCulture() { 30 return GetUserDefaultLCID(); 31 } 32 33 void setUserCulture(int lcid) { 34 SetThreadLocale(lcid); 35 } 36 37 int compareString(int lcid, const(char)[] stringA, uint offsetA, uint lengthA, const(char)[] stringB, uint offsetB, uint lengthB, bool ignoreCase) { 38 39 wchar[] toUnicode(const(char)[] string, uint offset, uint length, out int translated) { 40 const(char)* chars = string.ptr + offset; 41 int required = MultiByteToWideChar(0, 0, chars, length, null, 0); 42 wchar[] result = new wchar[required]; 43 translated = MultiByteToWideChar(0, 0, chars, length, result.ptr, required); 44 return result; 45 } 46 47 int sortId = (lcid >> 16) & 0xF; 48 sortId = (sortId == 0) ? lcid : (lcid | (sortId << 16)); 49 50 int len1, len2; 51 wchar[] string1 = toUnicode(stringA, offsetA, lengthA, len1); 52 wchar[] string2 = toUnicode(stringB, offsetB, lengthB, len2); 53 54 return CompareStringW(sortId, ignoreCase ? 0x1 : 0x0, string1.ptr, len1, string2.ptr, len2) - 2; 55 } 56 57 }