2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2010-02-25 22:32:39 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1997-2013, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2010-02-25 22:32:39 +00:00
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: locavailable.cpp
|
2017-02-03 18:57:23 +00:00
|
|
|
* encoding: UTF-8
|
2010-02-25 22:32:39 +00:00
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2010feb25
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* Code for available locales, separated out from other .cpp files
|
|
|
|
* that then do not depend on resource bundle code and res_index bundles.
|
|
|
|
*/
|
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
#include "unicode/errorcode.h"
|
2010-02-25 22:32:39 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/locid.h"
|
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "cmemory.h"
|
2019-08-15 01:56:22 +00:00
|
|
|
#include "cstring.h"
|
2010-02-25 22:32:39 +00:00
|
|
|
#include "ucln_cmn.h"
|
2013-06-01 03:37:16 +00:00
|
|
|
#include "uassert.h"
|
2010-02-25 22:32:39 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
#include "uresimp.h"
|
|
|
|
|
|
|
|
// C++ API ----------------------------------------------------------------- ***
|
|
|
|
|
2013-09-10 00:13:41 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2011-07-06 04:03:35 +00:00
|
|
|
static icu::Locale* availableLocaleList = NULL;
|
2010-02-25 22:32:39 +00:00
|
|
|
static int32_t availableLocaleListCount;
|
2017-08-11 21:38:54 +00:00
|
|
|
static icu::UInitOnce gInitOnceLocale = U_INITONCE_INITIALIZER;
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2013-09-10 00:13:41 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2010-02-25 22:32:39 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
2013-10-20 21:15:16 +00:00
|
|
|
static UBool U_CALLCONV locale_available_cleanup(void)
|
2010-02-25 22:32:39 +00:00
|
|
|
{
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
|
|
|
if (availableLocaleList) {
|
|
|
|
delete []availableLocaleList;
|
|
|
|
availableLocaleList = NULL;
|
|
|
|
}
|
|
|
|
availableLocaleListCount = 0;
|
2017-08-11 21:38:54 +00:00
|
|
|
gInitOnceLocale.reset();
|
2010-02-25 22:32:39 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CDECL_END
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2013-06-01 03:37:16 +00:00
|
|
|
void U_CALLCONV locale_available_init() {
|
|
|
|
// This function is a friend of class Locale.
|
|
|
|
// This function is only invoked via umtx_initOnce().
|
|
|
|
|
|
|
|
// for now, there is a hardcoded list, so just walk through that list and set it up.
|
|
|
|
// Note: this function is a friend of class Locale.
|
|
|
|
availableLocaleListCount = uloc_countAvailable();
|
|
|
|
if(availableLocaleListCount) {
|
|
|
|
availableLocaleList = new Locale[availableLocaleListCount];
|
|
|
|
}
|
|
|
|
if (availableLocaleList == NULL) {
|
|
|
|
availableLocaleListCount= 0;
|
|
|
|
}
|
|
|
|
for (int32_t locCount=availableLocaleListCount-1; locCount>=0; --locCount) {
|
|
|
|
availableLocaleList[locCount].setFromPOSIXID(uloc_getAvailable(locCount));
|
|
|
|
}
|
|
|
|
ucln_common_registerCleanup(UCLN_COMMON_LOCALE_AVAILABLE, locale_available_cleanup);
|
|
|
|
}
|
|
|
|
|
2010-02-25 22:32:39 +00:00
|
|
|
const Locale* U_EXPORT2
|
|
|
|
Locale::getAvailableLocales(int32_t& count)
|
|
|
|
{
|
2017-08-11 21:38:54 +00:00
|
|
|
umtx_initOnce(gInitOnceLocale, &locale_available_init);
|
2010-02-25 22:32:39 +00:00
|
|
|
count = availableLocaleListCount;
|
|
|
|
return availableLocaleList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
// C API ------------------------------------------------------------------- ***
|
|
|
|
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
|
|
|
/* ### Constants **************************************************/
|
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
namespace {
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
// Enough capacity for the two lists in the res_index.res file
|
|
|
|
const char** gAvailableLocaleNames[2] = {};
|
|
|
|
int32_t gAvailableLocaleCounts[2] = {};
|
|
|
|
icu::UInitOnce ginstalledLocalesInitOnce = U_INITONCE_INITIALIZER;
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
class AvailableLocalesSink : public ResourceSink {
|
|
|
|
public:
|
|
|
|
void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE {
|
|
|
|
ResourceTable resIndexTable = value.getTable(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; resIndexTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
ULocAvailableType type;
|
|
|
|
if (uprv_strcmp(key, "InstalledLocales") == 0) {
|
|
|
|
type = ULOC_AVAILABLE_DEFAULT;
|
|
|
|
} else if (uprv_strcmp(key, "AliasLocales") == 0) {
|
|
|
|
type = ULOC_AVAILABLE_ONLY_LEGACY_ALIASES;
|
|
|
|
} else {
|
|
|
|
// CLDRVersion, etc.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ResourceTable availableLocalesTable = value.getTable(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gAvailableLocaleCounts[type] = availableLocalesTable.getSize();
|
|
|
|
gAvailableLocaleNames[type] = static_cast<const char**>(
|
|
|
|
uprv_malloc(gAvailableLocaleCounts[type] * sizeof(const char*)));
|
|
|
|
if (gAvailableLocaleNames[type] == nullptr) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int32_t j = 0; availableLocalesTable.getKeyAndValue(j, key, value); ++j) {
|
|
|
|
gAvailableLocaleNames[type][j] = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
class AvailableLocalesStringEnumeration : public StringEnumeration {
|
|
|
|
public:
|
|
|
|
AvailableLocalesStringEnumeration(ULocAvailableType type) : fType(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* next(int32_t *resultLength, UErrorCode&) override {
|
|
|
|
ULocAvailableType actualType = fType;
|
|
|
|
int32_t actualIndex = fIndex++;
|
|
|
|
|
|
|
|
// If the "combined" list was requested, resolve that now
|
|
|
|
if (fType == ULOC_AVAILABLE_WITH_LEGACY_ALIASES) {
|
|
|
|
int32_t defaultLocalesCount = gAvailableLocaleCounts[ULOC_AVAILABLE_DEFAULT];
|
|
|
|
if (actualIndex < defaultLocalesCount) {
|
|
|
|
actualType = ULOC_AVAILABLE_DEFAULT;
|
|
|
|
} else {
|
|
|
|
actualIndex -= defaultLocalesCount;
|
|
|
|
actualType = ULOC_AVAILABLE_ONLY_LEGACY_ALIASES;
|
|
|
|
}
|
|
|
|
}
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
// Return the requested string
|
|
|
|
int32_t count = gAvailableLocaleCounts[actualType];
|
|
|
|
const char* result;
|
|
|
|
if (actualIndex < count) {
|
|
|
|
result = gAvailableLocaleNames[actualType][actualIndex];
|
|
|
|
if (resultLength != nullptr) {
|
|
|
|
*resultLength = static_cast<int32_t>(uprv_strlen(result));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = nullptr;
|
|
|
|
if (resultLength != nullptr) {
|
|
|
|
*resultLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
void reset(UErrorCode&) override {
|
|
|
|
fIndex = 0;
|
|
|
|
}
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
int32_t count(UErrorCode&) const override {
|
|
|
|
if (fType == ULOC_AVAILABLE_WITH_LEGACY_ALIASES) {
|
|
|
|
return gAvailableLocaleCounts[ULOC_AVAILABLE_DEFAULT]
|
|
|
|
+ gAvailableLocaleCounts[ULOC_AVAILABLE_ONLY_LEGACY_ALIASES];
|
|
|
|
} else {
|
|
|
|
return gAvailableLocaleCounts[fType];
|
|
|
|
}
|
2010-02-25 22:32:39 +00:00
|
|
|
}
|
2019-08-15 01:56:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ULocAvailableType fType;
|
|
|
|
int32_t fIndex = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ### Get available **************************************************/
|
|
|
|
|
|
|
|
static UBool U_CALLCONV uloc_cleanup(void) {
|
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(gAvailableLocaleNames); i++) {
|
|
|
|
uprv_free(gAvailableLocaleNames[i]);
|
|
|
|
gAvailableLocaleNames[i] = nullptr;
|
|
|
|
gAvailableLocaleCounts[i] = 0;
|
|
|
|
}
|
|
|
|
ginstalledLocalesInitOnce.reset();
|
2010-02-25 22:32:39 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-06-01 03:37:16 +00:00
|
|
|
// Load Installed Locales. This function will be called exactly once
|
|
|
|
// via the initOnce mechanism.
|
2010-02-25 22:32:39 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
static void U_CALLCONV loadInstalledLocales(UErrorCode& status) {
|
|
|
|
ucln_common_registerCleanup(UCLN_COMMON_ULOC, uloc_cleanup);
|
2018-10-31 14:07:05 +00:00
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
icu::LocalUResourceBundlePointer rb(ures_openDirect(NULL, "res_index", &status));
|
|
|
|
AvailableLocalesSink sink;
|
|
|
|
ures_getAllItemsWithFallback(rb.getAlias(), "", sink, status);
|
2013-06-01 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
void _load_installedLocales(UErrorCode& status) {
|
|
|
|
umtx_initOnce(ginstalledLocalesInitOnce, &loadInstalledLocales, status);
|
2010-02-25 22:32:39 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:22 +00:00
|
|
|
} // namespace
|
|
|
|
|
2010-02-25 22:32:39 +00:00
|
|
|
U_CAPI const char* U_EXPORT2
|
2019-08-15 01:56:22 +00:00
|
|
|
uloc_getAvailable(int32_t offset) {
|
|
|
|
icu::ErrorCode status;
|
|
|
|
_load_installedLocales(status);
|
|
|
|
if (status.isFailure()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (offset > gAvailableLocaleCounts[0]) {
|
|
|
|
// *status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return gAvailableLocaleNames[0][offset];
|
2010-02-25 22:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2019-08-15 01:56:22 +00:00
|
|
|
uloc_countAvailable() {
|
|
|
|
icu::ErrorCode status;
|
|
|
|
_load_installedLocales(status);
|
|
|
|
if (status.isFailure()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return gAvailableLocaleCounts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UEnumeration* U_EXPORT2
|
|
|
|
uloc_openAvailableByType(ULocAvailableType type, UErrorCode* status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (type < 0 || type >= ULOC_AVAILABLE_COUNT) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
_load_installedLocales(*status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
LocalPointer<AvailableLocalesStringEnumeration> result(
|
|
|
|
new AvailableLocalesStringEnumeration(type), *status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return uenum_openFromStringEnumeration(result.orphan(), status);
|
2010-02-25 22:32:39 +00:00
|
|
|
}
|
2013-09-10 00:13:41 +00:00
|
|
|
|