1 // Copyright (C) 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************** 5 * Copyright (C) 2005-2016, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ******************************************************************************** 8 * 9 * File WINUTIL.CPP 10 * 11 ******************************************************************************** 12 */ 13 14 #include "unicode/utypes.h" 15 16 #if U_PLATFORM_HAS_WIN32_API 17 18 #if !UCONFIG_NO_FORMATTING 19 20 #include "cmemory.h" 21 #include "winutil.h" 22 #include "locmap.h" 23 #include "unicode/uloc.h" 24 25 # define WIN32_LEAN_AND_MEAN 26 # define VC_EXTRALEAN 27 # define NOUSER 28 # define NOSERVICE 29 # define NOIME 30 # define NOMCX 31 # include <windows.h> 32 # include <stdio.h> 33 # include <string.h> 34 35 static Win32Utilities::LCIDRecord *lcidRecords = NULL; 36 static int32_t lcidCount = 0; 37 static int32_t lcidMax = 0; 38 39 BOOL CALLBACK EnumLocalesProc(LPSTR lpLocaleString) 40 { 41 char localeID[ULOC_FULLNAME_CAPACITY]; 42 int32_t localeIDLen; 43 UErrorCode status = U_ZERO_ERROR; 44 45 if (lcidCount >= lcidMax) { 46 Win32Utilities::LCIDRecord *newRecords = new Win32Utilities::LCIDRecord[lcidMax + 32]; 47 48 for (int i = 0; i < lcidMax; i += 1) { 49 newRecords[i] = lcidRecords[i]; 50 } 51 52 delete[] lcidRecords; 53 lcidRecords = newRecords; 54 lcidMax += 32; 55 } 56 57 sscanf(lpLocaleString, "%8x", &lcidRecords[lcidCount].lcid); 58 59 localeIDLen = uprv_convertToPosix(lcidRecords[lcidCount].lcid, localeID, UPRV_LENGTHOF(localeID), &status); 60 if (U_SUCCESS(status)) { 61 lcidRecords[lcidCount].localeID = new char[localeIDLen + 1]; 62 memcpy(lcidRecords[lcidCount].localeID, localeID, localeIDLen); 63 lcidRecords[lcidCount].localeID[localeIDLen] = 0; 64 } else { 65 lcidRecords[lcidCount].localeID = NULL; 66 } 67 68 lcidCount += 1; 69 70 return TRUE; 71 } 72 73 Win32Utilities::LCIDRecord *Win32Utilities::getLocales(int32_t &localeCount) 74 { 75 LCIDRecord *result; 76 77 EnumSystemLocalesA(EnumLocalesProc, LCID_INSTALLED); 78 79 localeCount = lcidCount; 80 result = lcidRecords; 81 82 lcidCount = lcidMax = 0; 83 lcidRecords = NULL; 84 85 return result; 86 } 87 88 void Win32Utilities::freeLocales(LCIDRecord *records) 89 { 90 for (int i = 0; i < lcidCount; i++) { 91 delete lcidRecords[i].localeID; 92 } 93 delete[] records; 94 } 95 96 #endif /* #if !UCONFIG_NO_FORMATTING */ 97 98 #endif /* U_PLATFORM_HAS_WIN32_API */ 99