1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/servlk.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 1.4 +/** 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2001-2004, International Business Machines Corporation and * 1.7 + * others. All Rights Reserved. * 1.8 + ******************************************************************************* 1.9 + * 1.10 + ******************************************************************************* 1.11 + */ 1.12 +#include "unicode/utypes.h" 1.13 + 1.14 +#if !UCONFIG_NO_SERVICE 1.15 + 1.16 +#include "unicode/resbund.h" 1.17 +#include "uresimp.h" 1.18 +#include "cmemory.h" 1.19 +#include "servloc.h" 1.20 +#include "ustrfmt.h" 1.21 +#include "uhash.h" 1.22 +#include "charstr.h" 1.23 +#include "ucln_cmn.h" 1.24 +#include "uassert.h" 1.25 + 1.26 +#define UNDERSCORE_CHAR ((UChar)0x005f) 1.27 +#define AT_SIGN_CHAR ((UChar)64) 1.28 +#define PERIOD_CHAR ((UChar)46) 1.29 + 1.30 +U_NAMESPACE_BEGIN 1.31 + 1.32 +LocaleKey* 1.33 +LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID, 1.34 + const UnicodeString* canonicalFallbackID, 1.35 + UErrorCode& status) 1.36 +{ 1.37 + return LocaleKey::createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY, status); 1.38 +} 1.39 + 1.40 +LocaleKey* 1.41 +LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID, 1.42 + const UnicodeString* canonicalFallbackID, 1.43 + int32_t kind, 1.44 + UErrorCode& status) 1.45 +{ 1.46 + if (primaryID == NULL || U_FAILURE(status)) { 1.47 + return NULL; 1.48 + } 1.49 + UnicodeString canonicalPrimaryID; 1.50 + LocaleUtility::canonicalLocaleString(primaryID, canonicalPrimaryID); 1.51 + return new LocaleKey(*primaryID, canonicalPrimaryID, canonicalFallbackID, kind); 1.52 +} 1.53 + 1.54 +LocaleKey::LocaleKey(const UnicodeString& primaryID, 1.55 + const UnicodeString& canonicalPrimaryID, 1.56 + const UnicodeString* canonicalFallbackID, 1.57 + int32_t kind) 1.58 + : ICUServiceKey(primaryID) 1.59 + , _kind(kind) 1.60 + , _primaryID(canonicalPrimaryID) 1.61 + , _fallbackID() 1.62 + , _currentID() 1.63 +{ 1.64 + _fallbackID.setToBogus(); 1.65 + if (_primaryID.length() != 0) { 1.66 + if (canonicalFallbackID != NULL && _primaryID != *canonicalFallbackID) { 1.67 + _fallbackID = *canonicalFallbackID; 1.68 + } 1.69 + } 1.70 + 1.71 + _currentID = _primaryID; 1.72 +} 1.73 + 1.74 +LocaleKey::~LocaleKey() {} 1.75 + 1.76 +UnicodeString& 1.77 +LocaleKey::prefix(UnicodeString& result) const { 1.78 + if (_kind != KIND_ANY) { 1.79 + UChar buffer[64]; 1.80 + uprv_itou(buffer, 64, _kind, 10, 0); 1.81 + UnicodeString temp(buffer); 1.82 + result.append(temp); 1.83 + } 1.84 + return result; 1.85 +} 1.86 + 1.87 +int32_t 1.88 +LocaleKey::kind() const { 1.89 + return _kind; 1.90 +} 1.91 + 1.92 +UnicodeString& 1.93 +LocaleKey::canonicalID(UnicodeString& result) const { 1.94 + return result.append(_primaryID); 1.95 +} 1.96 + 1.97 +UnicodeString& 1.98 +LocaleKey::currentID(UnicodeString& result) const { 1.99 + if (!_currentID.isBogus()) { 1.100 + result.append(_currentID); 1.101 + } 1.102 + return result; 1.103 +} 1.104 + 1.105 +UnicodeString& 1.106 +LocaleKey::currentDescriptor(UnicodeString& result) const { 1.107 + if (!_currentID.isBogus()) { 1.108 + prefix(result).append(PREFIX_DELIMITER).append(_currentID); 1.109 + } else { 1.110 + result.setToBogus(); 1.111 + } 1.112 + return result; 1.113 +} 1.114 + 1.115 +Locale& 1.116 +LocaleKey::canonicalLocale(Locale& result) const { 1.117 + return LocaleUtility::initLocaleFromName(_primaryID, result); 1.118 +} 1.119 + 1.120 +Locale& 1.121 +LocaleKey::currentLocale(Locale& result) const { 1.122 + return LocaleUtility::initLocaleFromName(_currentID, result); 1.123 +} 1.124 + 1.125 +UBool 1.126 +LocaleKey::fallback() { 1.127 + if (!_currentID.isBogus()) { 1.128 + int x = _currentID.lastIndexOf(UNDERSCORE_CHAR); 1.129 + if (x != -1) { 1.130 + _currentID.remove(x); // truncate current or fallback, whichever we're pointing to 1.131 + return TRUE; 1.132 + } 1.133 + 1.134 + if (!_fallbackID.isBogus()) { 1.135 + _currentID = _fallbackID; 1.136 + _fallbackID.setToBogus(); 1.137 + return TRUE; 1.138 + } 1.139 + 1.140 + if (_currentID.length() > 0) { 1.141 + _currentID.remove(0); // completely truncate 1.142 + return TRUE; 1.143 + } 1.144 + 1.145 + _currentID.setToBogus(); 1.146 + } 1.147 + 1.148 + return FALSE; 1.149 +} 1.150 + 1.151 +UBool 1.152 +LocaleKey::isFallbackOf(const UnicodeString& id) const { 1.153 + UnicodeString temp(id); 1.154 + parseSuffix(temp); 1.155 + return temp.indexOf(_primaryID) == 0 && 1.156 + (temp.length() == _primaryID.length() || 1.157 + temp.charAt(_primaryID.length()) == UNDERSCORE_CHAR); 1.158 +} 1.159 + 1.160 +#ifdef SERVICE_DEBUG 1.161 +UnicodeString& 1.162 +LocaleKey::debug(UnicodeString& result) const 1.163 +{ 1.164 + ICUServiceKey::debug(result); 1.165 + result.append(" kind: "); 1.166 + result.append(_kind); 1.167 + result.append(" primaryID: "); 1.168 + result.append(_primaryID); 1.169 + result.append(" fallbackID: "); 1.170 + result.append(_fallbackID); 1.171 + result.append(" currentID: "); 1.172 + result.append(_currentID); 1.173 + return result; 1.174 +} 1.175 + 1.176 +UnicodeString& 1.177 +LocaleKey::debugClass(UnicodeString& result) const 1.178 +{ 1.179 + return result.append("LocaleKey "); 1.180 +} 1.181 +#endif 1.182 + 1.183 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey) 1.184 + 1.185 +U_NAMESPACE_END 1.186 + 1.187 +/* !UCONFIG_NO_SERVICE */ 1.188 +#endif 1.189 + 1.190 +