2 *******************************************************************************
4 * Copyright (C) 1997-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: locavailable.cpp
10 * tab size: 8 (not used)
13 * created on: 2010feb25
14 * created by: Markus W. Scherer
16 * Code for available locales, separated out from other .cpp files
17 * that then do not depend on resource bundle code and res_index bundles.
20 #include "unicode/utypes.h"
21 #include "unicode/locid.h"
22 #include "unicode/uloc.h"
23 #include "unicode/ures.h"
29 // C++ API ----------------------------------------------------------------- ***
31 static icu::Locale
* availableLocaleList
= NULL
;
32 static int32_t availableLocaleListCount
;
36 static UBool U_CALLCONV
locale_available_cleanup(void)
40 if (availableLocaleList
) {
41 delete []availableLocaleList
;
42 availableLocaleList
= NULL
;
44 availableLocaleListCount
= 0;
53 const Locale
* U_EXPORT2
54 Locale::getAvailableLocales(int32_t& count
)
56 // for now, there is a hardcoded list, so just walk through that list and set it up.
58 UMTX_CHECK(NULL
, availableLocaleList
== NULL
, needInit
);
61 int32_t locCount
= uloc_countAvailable();
62 Locale
*newLocaleList
= 0;
64 newLocaleList
= new Locale
[locCount
];
66 if (newLocaleList
== NULL
) {
73 while(--locCount
>= 0) {
74 newLocaleList
[locCount
].setFromPOSIXID(uloc_getAvailable(locCount
));
78 if(availableLocaleList
== 0) {
79 availableLocaleListCount
= count
;
80 availableLocaleList
= newLocaleList
;
82 ucln_common_registerCleanup(UCLN_COMMON_LOCALE_AVAILABLE
, locale_available_cleanup
);
85 delete []newLocaleList
;
87 count
= availableLocaleListCount
;
88 return availableLocaleList
;
94 // C API ------------------------------------------------------------------- ***
98 /* ### Constants **************************************************/
100 /* These strings describe the resources we attempt to load from
101 the locale ResourceBundle data file.*/
102 static const char _kIndexLocaleName
[] = "res_index";
103 static const char _kIndexTag
[] = "InstalledLocales";
105 static char** _installedLocales
= NULL
;
106 static int32_t _installedLocalesCount
= 0;
108 /* ### Get available **************************************************/
110 static UBool U_CALLCONV
uloc_cleanup(void) {
113 if (_installedLocales
) {
114 temp
= _installedLocales
;
115 _installedLocales
= NULL
;
117 _installedLocalesCount
= 0;
124 static void _load_installedLocales()
128 UMTX_CHECK(NULL
, _installedLocales
!= NULL
, localesLoaded
);
130 if (localesLoaded
== FALSE
) {
131 UResourceBundle
*indexLocale
= NULL
;
132 UResourceBundle installed
;
133 UErrorCode status
= U_ZERO_ERROR
;
138 ures_initStackObject(&installed
);
139 indexLocale
= ures_openDirect(NULL
, _kIndexLocaleName
, &status
);
140 ures_getByKey(indexLocale
, _kIndexTag
, &installed
, &status
);
142 if(U_SUCCESS(status
)) {
143 localeCount
= ures_getSize(&installed
);
144 temp
= (char **) uprv_malloc(sizeof(char*) * (localeCount
+1));
145 /* Check for null pointer */
147 ures_resetIterator(&installed
);
148 while(ures_hasNext(&installed
)) {
149 ures_getNextString(&installed
, NULL
, (const char **)&temp
[i
++], &status
);
154 if (_installedLocales
== NULL
)
156 _installedLocalesCount
= localeCount
;
157 _installedLocales
= temp
;
159 ucln_common_registerCleanup(UCLN_COMMON_ULOC
, uloc_cleanup
);
166 ures_close(&installed
);
167 ures_close(indexLocale
);
171 U_CAPI
const char* U_EXPORT2
172 uloc_getAvailable(int32_t offset
)
175 _load_installedLocales();
177 if (offset
> _installedLocalesCount
)
179 return _installedLocales
[offset
];
182 U_CAPI
int32_t U_EXPORT2
183 uloc_countAvailable()
185 _load_installedLocales();
186 return _installedLocalesCount
;