michael@0: /** michael@0: ******************************************************************************* michael@0: * Copyright (C) 2001-2004, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: * michael@0: ******************************************************************************* michael@0: */ michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_SERVICE michael@0: michael@0: #include "unicode/resbund.h" michael@0: #include "uresimp.h" michael@0: #include "cmemory.h" michael@0: #include "servloc.h" michael@0: #include "ustrfmt.h" michael@0: #include "uhash.h" michael@0: #include "charstr.h" michael@0: #include "ucln_cmn.h" michael@0: #include "uassert.h" michael@0: michael@0: #define UNDERSCORE_CHAR ((UChar)0x005f) michael@0: #define AT_SIGN_CHAR ((UChar)64) michael@0: #define PERIOD_CHAR ((UChar)46) michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: LocaleKey* michael@0: LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID, michael@0: const UnicodeString* canonicalFallbackID, michael@0: UErrorCode& status) michael@0: { michael@0: return LocaleKey::createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY, status); michael@0: } michael@0: michael@0: LocaleKey* michael@0: LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID, michael@0: const UnicodeString* canonicalFallbackID, michael@0: int32_t kind, michael@0: UErrorCode& status) michael@0: { michael@0: if (primaryID == NULL || U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: UnicodeString canonicalPrimaryID; michael@0: LocaleUtility::canonicalLocaleString(primaryID, canonicalPrimaryID); michael@0: return new LocaleKey(*primaryID, canonicalPrimaryID, canonicalFallbackID, kind); michael@0: } michael@0: michael@0: LocaleKey::LocaleKey(const UnicodeString& primaryID, michael@0: const UnicodeString& canonicalPrimaryID, michael@0: const UnicodeString* canonicalFallbackID, michael@0: int32_t kind) michael@0: : ICUServiceKey(primaryID) michael@0: , _kind(kind) michael@0: , _primaryID(canonicalPrimaryID) michael@0: , _fallbackID() michael@0: , _currentID() michael@0: { michael@0: _fallbackID.setToBogus(); michael@0: if (_primaryID.length() != 0) { michael@0: if (canonicalFallbackID != NULL && _primaryID != *canonicalFallbackID) { michael@0: _fallbackID = *canonicalFallbackID; michael@0: } michael@0: } michael@0: michael@0: _currentID = _primaryID; michael@0: } michael@0: michael@0: LocaleKey::~LocaleKey() {} michael@0: michael@0: UnicodeString& michael@0: LocaleKey::prefix(UnicodeString& result) const { michael@0: if (_kind != KIND_ANY) { michael@0: UChar buffer[64]; michael@0: uprv_itou(buffer, 64, _kind, 10, 0); michael@0: UnicodeString temp(buffer); michael@0: result.append(temp); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: int32_t michael@0: LocaleKey::kind() const { michael@0: return _kind; michael@0: } michael@0: michael@0: UnicodeString& michael@0: LocaleKey::canonicalID(UnicodeString& result) const { michael@0: return result.append(_primaryID); michael@0: } michael@0: michael@0: UnicodeString& michael@0: LocaleKey::currentID(UnicodeString& result) const { michael@0: if (!_currentID.isBogus()) { michael@0: result.append(_currentID); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: LocaleKey::currentDescriptor(UnicodeString& result) const { michael@0: if (!_currentID.isBogus()) { michael@0: prefix(result).append(PREFIX_DELIMITER).append(_currentID); michael@0: } else { michael@0: result.setToBogus(); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: Locale& michael@0: LocaleKey::canonicalLocale(Locale& result) const { michael@0: return LocaleUtility::initLocaleFromName(_primaryID, result); michael@0: } michael@0: michael@0: Locale& michael@0: LocaleKey::currentLocale(Locale& result) const { michael@0: return LocaleUtility::initLocaleFromName(_currentID, result); michael@0: } michael@0: michael@0: UBool michael@0: LocaleKey::fallback() { michael@0: if (!_currentID.isBogus()) { michael@0: int x = _currentID.lastIndexOf(UNDERSCORE_CHAR); michael@0: if (x != -1) { michael@0: _currentID.remove(x); // truncate current or fallback, whichever we're pointing to michael@0: return TRUE; michael@0: } michael@0: michael@0: if (!_fallbackID.isBogus()) { michael@0: _currentID = _fallbackID; michael@0: _fallbackID.setToBogus(); michael@0: return TRUE; michael@0: } michael@0: michael@0: if (_currentID.length() > 0) { michael@0: _currentID.remove(0); // completely truncate michael@0: return TRUE; michael@0: } michael@0: michael@0: _currentID.setToBogus(); michael@0: } michael@0: michael@0: return FALSE; michael@0: } michael@0: michael@0: UBool michael@0: LocaleKey::isFallbackOf(const UnicodeString& id) const { michael@0: UnicodeString temp(id); michael@0: parseSuffix(temp); michael@0: return temp.indexOf(_primaryID) == 0 && michael@0: (temp.length() == _primaryID.length() || michael@0: temp.charAt(_primaryID.length()) == UNDERSCORE_CHAR); michael@0: } michael@0: michael@0: #ifdef SERVICE_DEBUG michael@0: UnicodeString& michael@0: LocaleKey::debug(UnicodeString& result) const michael@0: { michael@0: ICUServiceKey::debug(result); michael@0: result.append(" kind: "); michael@0: result.append(_kind); michael@0: result.append(" primaryID: "); michael@0: result.append(_primaryID); michael@0: result.append(" fallbackID: "); michael@0: result.append(_fallbackID); michael@0: result.append(" currentID: "); michael@0: result.append(_currentID); michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: LocaleKey::debugClass(UnicodeString& result) const michael@0: { michael@0: return result.append("LocaleKey "); michael@0: } michael@0: #endif michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* !UCONFIG_NO_SERVICE */ michael@0: #endif michael@0: michael@0: