Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1997-2013, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: locavailable.cpp |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2010feb25 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | * |
michael@0 | 16 | * Code for available locales, separated out from other .cpp files |
michael@0 | 17 | * that then do not depend on resource bundle code and res_index bundles. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/utypes.h" |
michael@0 | 21 | #include "unicode/locid.h" |
michael@0 | 22 | #include "unicode/uloc.h" |
michael@0 | 23 | #include "unicode/ures.h" |
michael@0 | 24 | #include "cmemory.h" |
michael@0 | 25 | #include "ucln_cmn.h" |
michael@0 | 26 | #include "uassert.h" |
michael@0 | 27 | #include "umutex.h" |
michael@0 | 28 | #include "uresimp.h" |
michael@0 | 29 | |
michael@0 | 30 | // C++ API ----------------------------------------------------------------- *** |
michael@0 | 31 | |
michael@0 | 32 | U_NAMESPACE_BEGIN |
michael@0 | 33 | |
michael@0 | 34 | static icu::Locale* availableLocaleList = NULL; |
michael@0 | 35 | static int32_t availableLocaleListCount; |
michael@0 | 36 | static icu::UInitOnce gInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 37 | |
michael@0 | 38 | U_NAMESPACE_END |
michael@0 | 39 | |
michael@0 | 40 | U_CDECL_BEGIN |
michael@0 | 41 | |
michael@0 | 42 | static UBool U_CALLCONV locale_available_cleanup(void) |
michael@0 | 43 | { |
michael@0 | 44 | U_NAMESPACE_USE |
michael@0 | 45 | |
michael@0 | 46 | if (availableLocaleList) { |
michael@0 | 47 | delete []availableLocaleList; |
michael@0 | 48 | availableLocaleList = NULL; |
michael@0 | 49 | } |
michael@0 | 50 | availableLocaleListCount = 0; |
michael@0 | 51 | gInitOnce.reset(); |
michael@0 | 52 | |
michael@0 | 53 | return TRUE; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | U_CDECL_END |
michael@0 | 57 | |
michael@0 | 58 | U_NAMESPACE_BEGIN |
michael@0 | 59 | |
michael@0 | 60 | void U_CALLCONV locale_available_init() { |
michael@0 | 61 | // This function is a friend of class Locale. |
michael@0 | 62 | // This function is only invoked via umtx_initOnce(). |
michael@0 | 63 | |
michael@0 | 64 | // for now, there is a hardcoded list, so just walk through that list and set it up. |
michael@0 | 65 | // Note: this function is a friend of class Locale. |
michael@0 | 66 | availableLocaleListCount = uloc_countAvailable(); |
michael@0 | 67 | if(availableLocaleListCount) { |
michael@0 | 68 | availableLocaleList = new Locale[availableLocaleListCount]; |
michael@0 | 69 | } |
michael@0 | 70 | if (availableLocaleList == NULL) { |
michael@0 | 71 | availableLocaleListCount= 0; |
michael@0 | 72 | } |
michael@0 | 73 | for (int32_t locCount=availableLocaleListCount-1; locCount>=0; --locCount) { |
michael@0 | 74 | availableLocaleList[locCount].setFromPOSIXID(uloc_getAvailable(locCount)); |
michael@0 | 75 | } |
michael@0 | 76 | ucln_common_registerCleanup(UCLN_COMMON_LOCALE_AVAILABLE, locale_available_cleanup); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | const Locale* U_EXPORT2 |
michael@0 | 80 | Locale::getAvailableLocales(int32_t& count) |
michael@0 | 81 | { |
michael@0 | 82 | umtx_initOnce(gInitOnce, &locale_available_init); |
michael@0 | 83 | count = availableLocaleListCount; |
michael@0 | 84 | return availableLocaleList; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | U_NAMESPACE_END |
michael@0 | 89 | |
michael@0 | 90 | // C API ------------------------------------------------------------------- *** |
michael@0 | 91 | |
michael@0 | 92 | U_NAMESPACE_USE |
michael@0 | 93 | |
michael@0 | 94 | /* ### Constants **************************************************/ |
michael@0 | 95 | |
michael@0 | 96 | /* These strings describe the resources we attempt to load from |
michael@0 | 97 | the locale ResourceBundle data file.*/ |
michael@0 | 98 | static const char _kIndexLocaleName[] = "res_index"; |
michael@0 | 99 | static const char _kIndexTag[] = "InstalledLocales"; |
michael@0 | 100 | |
michael@0 | 101 | static char** _installedLocales = NULL; |
michael@0 | 102 | static int32_t _installedLocalesCount = 0; |
michael@0 | 103 | static icu::UInitOnce _installedLocalesInitOnce; |
michael@0 | 104 | |
michael@0 | 105 | /* ### Get available **************************************************/ |
michael@0 | 106 | |
michael@0 | 107 | static UBool U_CALLCONV uloc_cleanup(void) { |
michael@0 | 108 | char ** temp; |
michael@0 | 109 | |
michael@0 | 110 | if (_installedLocales) { |
michael@0 | 111 | temp = _installedLocales; |
michael@0 | 112 | _installedLocales = NULL; |
michael@0 | 113 | |
michael@0 | 114 | _installedLocalesCount = 0; |
michael@0 | 115 | _installedLocalesInitOnce.reset(); |
michael@0 | 116 | |
michael@0 | 117 | uprv_free(temp); |
michael@0 | 118 | } |
michael@0 | 119 | return TRUE; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // Load Installed Locales. This function will be called exactly once |
michael@0 | 123 | // via the initOnce mechanism. |
michael@0 | 124 | |
michael@0 | 125 | static void U_CALLCONV loadInstalledLocales() { |
michael@0 | 126 | UResourceBundle *indexLocale = NULL; |
michael@0 | 127 | UResourceBundle installed; |
michael@0 | 128 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 129 | int32_t i = 0; |
michael@0 | 130 | int32_t localeCount; |
michael@0 | 131 | |
michael@0 | 132 | U_ASSERT(_installedLocales == NULL); |
michael@0 | 133 | U_ASSERT(_installedLocalesCount == 0); |
michael@0 | 134 | |
michael@0 | 135 | _installedLocalesCount = 0; |
michael@0 | 136 | ures_initStackObject(&installed); |
michael@0 | 137 | indexLocale = ures_openDirect(NULL, _kIndexLocaleName, &status); |
michael@0 | 138 | ures_getByKey(indexLocale, _kIndexTag, &installed, &status); |
michael@0 | 139 | |
michael@0 | 140 | if(U_SUCCESS(status)) { |
michael@0 | 141 | localeCount = ures_getSize(&installed); |
michael@0 | 142 | _installedLocales = (char **) uprv_malloc(sizeof(char*) * (localeCount+1)); |
michael@0 | 143 | if (_installedLocales != NULL) { |
michael@0 | 144 | ures_resetIterator(&installed); |
michael@0 | 145 | while(ures_hasNext(&installed)) { |
michael@0 | 146 | ures_getNextString(&installed, NULL, (const char **)&_installedLocales[i++], &status); |
michael@0 | 147 | } |
michael@0 | 148 | _installedLocales[i] = NULL; |
michael@0 | 149 | _installedLocalesCount = localeCount; |
michael@0 | 150 | ucln_common_registerCleanup(UCLN_COMMON_ULOC, uloc_cleanup); |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | ures_close(&installed); |
michael@0 | 154 | ures_close(indexLocale); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | static void _load_installedLocales() |
michael@0 | 158 | { |
michael@0 | 159 | umtx_initOnce(_installedLocalesInitOnce, &loadInstalledLocales); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | U_CAPI const char* U_EXPORT2 |
michael@0 | 163 | uloc_getAvailable(int32_t offset) |
michael@0 | 164 | { |
michael@0 | 165 | |
michael@0 | 166 | _load_installedLocales(); |
michael@0 | 167 | |
michael@0 | 168 | if (offset > _installedLocalesCount) |
michael@0 | 169 | return NULL; |
michael@0 | 170 | return _installedLocales[offset]; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 174 | uloc_countAvailable() |
michael@0 | 175 | { |
michael@0 | 176 | _load_installedLocales(); |
michael@0 | 177 | return _installedLocalesCount; |
michael@0 | 178 | } |
michael@0 | 179 |