1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/tznames_impl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 1.4 +/* 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2011-2013, International Business Machines Corporation and * 1.7 + * others. All Rights Reserved. * 1.8 + ******************************************************************************* 1.9 + */ 1.10 + 1.11 +#ifndef __TZNAMES_IMPL_H__ 1.12 +#define __TZNAMES_IMPL_H__ 1.13 + 1.14 + 1.15 +/** 1.16 + * \file 1.17 + * \brief C++ API: TimeZoneNames object 1.18 + */ 1.19 + 1.20 +#include "unicode/utypes.h" 1.21 + 1.22 +#if !UCONFIG_NO_FORMATTING 1.23 + 1.24 +#include "unicode/tznames.h" 1.25 +#include "unicode/ures.h" 1.26 +#include "unicode/locid.h" 1.27 +#include "uhash.h" 1.28 +#include "uvector.h" 1.29 +#include "umutex.h" 1.30 + 1.31 +U_NAMESPACE_BEGIN 1.32 + 1.33 +/* 1.34 + * ZNStringPool Pool of (UChar *) strings. Provides for sharing of repeated 1.35 + * zone strings. 1.36 + */ 1.37 +struct ZNStringPoolChunk; 1.38 +class U_I18N_API ZNStringPool: public UMemory { 1.39 + public: 1.40 + ZNStringPool(UErrorCode &status); 1.41 + ~ZNStringPool(); 1.42 + 1.43 + /* Get the pooled string that is equal to the supplied string s. 1.44 + * Copy the string into the pool if it is not already present. 1.45 + * 1.46 + * Life time of the returned string is that of the pool. 1.47 + */ 1.48 + const UChar *get(const UChar *s, UErrorCode &status); 1.49 + 1.50 + /* Get the pooled string that is equal to the supplied string s. 1.51 + * Copy the string into the pool if it is not already present. 1.52 + */ 1.53 + const UChar *get(const UnicodeString &s, UErrorCode &status); 1.54 + 1.55 + /* Adopt a string into the pool, without copying it. 1.56 + * Used for strings from resource bundles, which will persist without copying. 1.57 + */ 1.58 + const UChar *adopt(const UChar *s, UErrorCode &status); 1.59 + 1.60 + /* Freeze the string pool. Discards the hash table that is used 1.61 + * for looking up a string. All pointers to pooled strings remain valid. 1.62 + */ 1.63 + void freeze(); 1.64 + 1.65 + private: 1.66 + ZNStringPoolChunk *fChunks; 1.67 + UHashtable *fHash; 1.68 +}; 1.69 + 1.70 +/* 1.71 + * Character node used by TextTrieMap 1.72 + */ 1.73 +struct CharacterNode { 1.74 + // No constructor or destructor. 1.75 + // We malloc and free an uninitalized array of CharacterNode objects 1.76 + // and clear and delete them ourselves. 1.77 + 1.78 + void clear(); 1.79 + void deleteValues(UObjectDeleter *valueDeleter); 1.80 + 1.81 + void addValue(void *value, UObjectDeleter *valueDeleter, UErrorCode &status); 1.82 + inline UBool hasValues() const; 1.83 + inline int32_t countValues() const; 1.84 + inline const void *getValue(int32_t index) const; 1.85 + 1.86 + void *fValues; // Union of one single value vs. UVector of values. 1.87 + UChar fCharacter; // UTF-16 code unit. 1.88 + uint16_t fFirstChild; // 0 if no children. 1.89 + uint16_t fNextSibling; // 0 terminates the list. 1.90 + UBool fHasValuesVector; 1.91 + UBool fPadding; 1.92 + 1.93 + // No value: fValues == NULL and fHasValuesVector == FALSE 1.94 + // One value: fValues == value and fHasValuesVector == FALSE 1.95 + // >=2 values: fValues == UVector of values and fHasValuesVector == TRUE 1.96 +}; 1.97 + 1.98 +inline UBool CharacterNode::hasValues() const { 1.99 + return (UBool)(fValues != NULL); 1.100 +} 1.101 + 1.102 +inline int32_t CharacterNode::countValues() const { 1.103 + return 1.104 + fValues == NULL ? 0 : 1.105 + !fHasValuesVector ? 1 : 1.106 + ((const UVector *)fValues)->size(); 1.107 +} 1.108 + 1.109 +inline const void *CharacterNode::getValue(int32_t index) const { 1.110 + if (!fHasValuesVector) { 1.111 + return fValues; // Assume index == 0. 1.112 + } else { 1.113 + return ((const UVector *)fValues)->elementAt(index); 1.114 + } 1.115 +} 1.116 + 1.117 +/* 1.118 + * Search result handler callback interface used by TextTrieMap search. 1.119 + */ 1.120 +class TextTrieMapSearchResultHandler : public UMemory { 1.121 +public: 1.122 + virtual UBool handleMatch(int32_t matchLength, 1.123 + const CharacterNode *node, UErrorCode& status) = 0; 1.124 + virtual ~TextTrieMapSearchResultHandler(); //added to avoid warning 1.125 +}; 1.126 + 1.127 +/** 1.128 + * TextTrieMap is a trie implementation for supporting 1.129 + * fast prefix match for the string key. 1.130 + */ 1.131 +class U_I18N_API TextTrieMap : public UMemory { 1.132 +public: 1.133 + TextTrieMap(UBool ignoreCase, UObjectDeleter *valeDeleter); 1.134 + virtual ~TextTrieMap(); 1.135 + 1.136 + void put(const UnicodeString &key, void *value, ZNStringPool &sp, UErrorCode &status); 1.137 + void put(const UChar*, void *value, UErrorCode &status); 1.138 + void search(const UnicodeString &text, int32_t start, 1.139 + TextTrieMapSearchResultHandler *handler, UErrorCode& status) const; 1.140 + int32_t isEmpty() const; 1.141 + 1.142 +private: 1.143 + UBool fIgnoreCase; 1.144 + CharacterNode *fNodes; 1.145 + int32_t fNodesCapacity; 1.146 + int32_t fNodesCount; 1.147 + 1.148 + UVector *fLazyContents; 1.149 + UBool fIsEmpty; 1.150 + UObjectDeleter *fValueDeleter; 1.151 + 1.152 + UBool growNodes(); 1.153 + CharacterNode* addChildNode(CharacterNode *parent, UChar c, UErrorCode &status); 1.154 + CharacterNode* getChildNode(CharacterNode *parent, UChar c) const; 1.155 + 1.156 + void putImpl(const UnicodeString &key, void *value, UErrorCode &status); 1.157 + void buildTrie(UErrorCode &status); 1.158 + void search(CharacterNode *node, const UnicodeString &text, int32_t start, 1.159 + int32_t index, TextTrieMapSearchResultHandler *handler, UErrorCode &status) const; 1.160 +}; 1.161 + 1.162 + 1.163 + 1.164 +class ZNames; 1.165 +class TZNames; 1.166 +class TextTrieMap; 1.167 + 1.168 +class TimeZoneNamesImpl : public TimeZoneNames { 1.169 +public: 1.170 + TimeZoneNamesImpl(const Locale& locale, UErrorCode& status); 1.171 + 1.172 + virtual ~TimeZoneNamesImpl(); 1.173 + 1.174 + virtual UBool operator==(const TimeZoneNames& other) const; 1.175 + virtual TimeZoneNames* clone() const; 1.176 + 1.177 + StringEnumeration* getAvailableMetaZoneIDs(UErrorCode& status) const; 1.178 + StringEnumeration* getAvailableMetaZoneIDs(const UnicodeString& tzID, UErrorCode& status) const; 1.179 + 1.180 + UnicodeString& getMetaZoneID(const UnicodeString& tzID, UDate date, UnicodeString& mzID) const; 1.181 + UnicodeString& getReferenceZoneID(const UnicodeString& mzID, const char* region, UnicodeString& tzID) const; 1.182 + 1.183 + UnicodeString& getMetaZoneDisplayName(const UnicodeString& mzID, UTimeZoneNameType type, UnicodeString& name) const; 1.184 + UnicodeString& getTimeZoneDisplayName(const UnicodeString& tzID, UTimeZoneNameType type, UnicodeString& name) const; 1.185 + 1.186 + UnicodeString& getExemplarLocationName(const UnicodeString& tzID, UnicodeString& name) const; 1.187 + 1.188 + TimeZoneNames::MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const; 1.189 + 1.190 + static UnicodeString& getDefaultExemplarLocationName(const UnicodeString& tzID, UnicodeString& name); 1.191 + 1.192 +private: 1.193 + 1.194 + Locale fLocale; 1.195 + 1.196 + UResourceBundle* fZoneStrings; 1.197 + 1.198 + UHashtable* fTZNamesMap; 1.199 + UHashtable* fMZNamesMap; 1.200 + 1.201 + UBool fNamesTrieFullyLoaded; 1.202 + TextTrieMap fNamesTrie; 1.203 + 1.204 + void initialize(const Locale& locale, UErrorCode& status); 1.205 + void cleanup(); 1.206 + 1.207 + void loadStrings(const UnicodeString& tzCanonicalID); 1.208 + 1.209 + ZNames* loadMetaZoneNames(const UnicodeString& mzId); 1.210 + TZNames* loadTimeZoneNames(const UnicodeString& mzId); 1.211 +}; 1.212 + 1.213 +U_NAMESPACE_END 1.214 + 1.215 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.216 + 1.217 +#endif // __TZNAMES_IMPL_H__ 1.218 +//eof 1.219 +//