2 *******************************************************************************
3 * Copyright (C) 2011-2014, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #ifndef __TZNAMES_IMPL_H__
9 #define __TZNAMES_IMPL_H__
14 * \brief C++ API: TimeZoneNames object
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/tznames.h"
22 #include "unicode/ures.h"
23 #include "unicode/locid.h"
31 * ZNStringPool Pool of (UChar *) strings. Provides for sharing of repeated
34 struct ZNStringPoolChunk
;
35 class U_I18N_API ZNStringPool
: public UMemory
{
37 ZNStringPool(UErrorCode
&status
);
40 /* Get the pooled string that is equal to the supplied string s.
41 * Copy the string into the pool if it is not already present.
43 * Life time of the returned string is that of the pool.
45 const UChar
*get(const UChar
*s
, UErrorCode
&status
);
47 /* Get the pooled string that is equal to the supplied string s.
48 * Copy the string into the pool if it is not already present.
50 const UChar
*get(const UnicodeString
&s
, UErrorCode
&status
);
52 /* Adopt a string into the pool, without copying it.
53 * Used for strings from resource bundles, which will persist without copying.
55 const UChar
*adopt(const UChar
*s
, UErrorCode
&status
);
57 /* Freeze the string pool. Discards the hash table that is used
58 * for looking up a string. All pointers to pooled strings remain valid.
63 ZNStringPoolChunk
*fChunks
;
68 * Character node used by TextTrieMap
70 struct CharacterNode
{
71 // No constructor or destructor.
72 // We malloc and free an uninitalized array of CharacterNode objects
73 // and clear and delete them ourselves.
76 void deleteValues(UObjectDeleter
*valueDeleter
);
78 void addValue(void *value
, UObjectDeleter
*valueDeleter
, UErrorCode
&status
);
79 inline UBool
hasValues() const;
80 inline int32_t countValues() const;
81 inline const void *getValue(int32_t index
) const;
83 void *fValues
; // Union of one single value vs. UVector of values.
84 UChar fCharacter
; // UTF-16 code unit.
85 uint16_t fFirstChild
; // 0 if no children.
86 uint16_t fNextSibling
; // 0 terminates the list.
87 UBool fHasValuesVector
;
90 // No value: fValues == NULL and fHasValuesVector == FALSE
91 // One value: fValues == value and fHasValuesVector == FALSE
92 // >=2 values: fValues == UVector of values and fHasValuesVector == TRUE
95 inline UBool
CharacterNode::hasValues() const {
96 return (UBool
)(fValues
!= NULL
);
99 inline int32_t CharacterNode::countValues() const {
101 fValues
== NULL
? 0 :
102 !fHasValuesVector
? 1 :
103 ((const UVector
*)fValues
)->size();
106 inline const void *CharacterNode::getValue(int32_t index
) const {
107 if (!fHasValuesVector
) {
108 return fValues
; // Assume index == 0.
110 return ((const UVector
*)fValues
)->elementAt(index
);
115 * Search result handler callback interface used by TextTrieMap search.
117 class TextTrieMapSearchResultHandler
: public UMemory
{
119 virtual UBool
handleMatch(int32_t matchLength
,
120 const CharacterNode
*node
, UErrorCode
& status
) = 0;
121 virtual ~TextTrieMapSearchResultHandler(); //added to avoid warning
125 * TextTrieMap is a trie implementation for supporting
126 * fast prefix match for the string key.
128 class U_I18N_API TextTrieMap
: public UMemory
{
130 TextTrieMap(UBool ignoreCase
, UObjectDeleter
*valeDeleter
);
131 virtual ~TextTrieMap();
133 void put(const UnicodeString
&key
, void *value
, ZNStringPool
&sp
, UErrorCode
&status
);
134 void put(const UChar
*, void *value
, UErrorCode
&status
);
135 void search(const UnicodeString
&text
, int32_t start
,
136 TextTrieMapSearchResultHandler
*handler
, UErrorCode
& status
) const;
137 int32_t isEmpty() const;
141 CharacterNode
*fNodes
;
142 int32_t fNodesCapacity
;
145 UVector
*fLazyContents
;
147 UObjectDeleter
*fValueDeleter
;
150 CharacterNode
* addChildNode(CharacterNode
*parent
, UChar c
, UErrorCode
&status
);
151 CharacterNode
* getChildNode(CharacterNode
*parent
, UChar c
) const;
153 void putImpl(const UnicodeString
&key
, void *value
, UErrorCode
&status
);
154 void buildTrie(UErrorCode
&status
);
155 void search(CharacterNode
*node
, const UnicodeString
&text
, int32_t start
,
156 int32_t index
, TextTrieMapSearchResultHandler
*handler
, UErrorCode
&status
) const;
165 class TimeZoneNamesImpl
: public TimeZoneNames
{
167 TimeZoneNamesImpl(const Locale
& locale
, UErrorCode
& status
);
169 virtual ~TimeZoneNamesImpl();
171 virtual UBool
operator==(const TimeZoneNames
& other
) const;
172 virtual TimeZoneNames
* clone() const;
174 StringEnumeration
* getAvailableMetaZoneIDs(UErrorCode
& status
) const;
175 StringEnumeration
* getAvailableMetaZoneIDs(const UnicodeString
& tzID
, UErrorCode
& status
) const;
177 UnicodeString
& getMetaZoneID(const UnicodeString
& tzID
, UDate date
, UnicodeString
& mzID
) const;
178 UnicodeString
& getReferenceZoneID(const UnicodeString
& mzID
, const char* region
, UnicodeString
& tzID
) const;
180 UnicodeString
& getMetaZoneDisplayName(const UnicodeString
& mzID
, UTimeZoneNameType type
, UnicodeString
& name
) const;
181 UnicodeString
& getTimeZoneDisplayName(const UnicodeString
& tzID
, UTimeZoneNameType type
, UnicodeString
& name
) const;
183 UnicodeString
& getExemplarLocationName(const UnicodeString
& tzID
, UnicodeString
& name
) const;
185 TimeZoneNames::MatchInfoCollection
* find(const UnicodeString
& text
, int32_t start
, uint32_t types
, UErrorCode
& status
) const;
187 static UnicodeString
& getDefaultExemplarLocationName(const UnicodeString
& tzID
, UnicodeString
& name
);
189 static StringEnumeration
* _getAvailableMetaZoneIDs(UErrorCode
& status
);
190 static StringEnumeration
* _getAvailableMetaZoneIDs(const UnicodeString
& tzID
, UErrorCode
& status
);
191 static UnicodeString
& _getMetaZoneID(const UnicodeString
& tzID
, UDate date
, UnicodeString
& mzID
);
192 static UnicodeString
& _getReferenceZoneID(const UnicodeString
& mzID
, const char* region
, UnicodeString
& tzID
);
198 UResourceBundle
* fZoneStrings
;
200 UHashtable
* fTZNamesMap
;
201 UHashtable
* fMZNamesMap
;
203 UBool fNamesTrieFullyLoaded
;
204 TextTrieMap fNamesTrie
;
206 void initialize(const Locale
& locale
, UErrorCode
& status
);
209 void loadStrings(const UnicodeString
& tzCanonicalID
);
211 ZNames
* loadMetaZoneNames(const UnicodeString
& mzId
);
212 TZNames
* loadTimeZoneNames(const UnicodeString
& mzId
);
217 class TZDBTimeZoneNames
: public TimeZoneNames
{
219 TZDBTimeZoneNames(const Locale
& locale
);
220 virtual ~TZDBTimeZoneNames();
222 virtual UBool
operator==(const TimeZoneNames
& other
) const;
223 virtual TimeZoneNames
* clone() const;
225 StringEnumeration
* getAvailableMetaZoneIDs(UErrorCode
& status
) const;
226 StringEnumeration
* getAvailableMetaZoneIDs(const UnicodeString
& tzID
, UErrorCode
& status
) const;
228 UnicodeString
& getMetaZoneID(const UnicodeString
& tzID
, UDate date
, UnicodeString
& mzID
) const;
229 UnicodeString
& getReferenceZoneID(const UnicodeString
& mzID
, const char* region
, UnicodeString
& tzID
) const;
231 UnicodeString
& getMetaZoneDisplayName(const UnicodeString
& mzID
, UTimeZoneNameType type
, UnicodeString
& name
) const;
232 UnicodeString
& getTimeZoneDisplayName(const UnicodeString
& tzID
, UTimeZoneNameType type
, UnicodeString
& name
) const;
234 TimeZoneNames::MatchInfoCollection
* find(const UnicodeString
& text
, int32_t start
, uint32_t types
, UErrorCode
& status
) const;
236 static const TZDBNames
* getMetaZoneNames(const UnicodeString
& mzId
, UErrorCode
& status
);
240 char fRegion
[ULOC_COUNTRY_CAPACITY
];
245 #endif /* #if !UCONFIG_NO_FORMATTING */
247 #endif // __TZNAMES_IMPL_H__