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