michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011-2013, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef __TZNAMES_IMPL_H__ michael@0: #define __TZNAMES_IMPL_H__ michael@0: michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: TimeZoneNames object michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/tznames.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/locid.h" michael@0: #include "uhash.h" michael@0: #include "uvector.h" michael@0: #include "umutex.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* michael@0: * ZNStringPool Pool of (UChar *) strings. Provides for sharing of repeated michael@0: * zone strings. michael@0: */ michael@0: struct ZNStringPoolChunk; michael@0: class U_I18N_API ZNStringPool: public UMemory { michael@0: public: michael@0: ZNStringPool(UErrorCode &status); michael@0: ~ZNStringPool(); michael@0: michael@0: /* Get the pooled string that is equal to the supplied string s. michael@0: * Copy the string into the pool if it is not already present. michael@0: * michael@0: * Life time of the returned string is that of the pool. michael@0: */ michael@0: const UChar *get(const UChar *s, UErrorCode &status); michael@0: michael@0: /* Get the pooled string that is equal to the supplied string s. michael@0: * Copy the string into the pool if it is not already present. michael@0: */ michael@0: const UChar *get(const UnicodeString &s, UErrorCode &status); michael@0: michael@0: /* Adopt a string into the pool, without copying it. michael@0: * Used for strings from resource bundles, which will persist without copying. michael@0: */ michael@0: const UChar *adopt(const UChar *s, UErrorCode &status); michael@0: michael@0: /* Freeze the string pool. Discards the hash table that is used michael@0: * for looking up a string. All pointers to pooled strings remain valid. michael@0: */ michael@0: void freeze(); michael@0: michael@0: private: michael@0: ZNStringPoolChunk *fChunks; michael@0: UHashtable *fHash; michael@0: }; michael@0: michael@0: /* michael@0: * Character node used by TextTrieMap michael@0: */ michael@0: struct CharacterNode { michael@0: // No constructor or destructor. michael@0: // We malloc and free an uninitalized array of CharacterNode objects michael@0: // and clear and delete them ourselves. michael@0: michael@0: void clear(); michael@0: void deleteValues(UObjectDeleter *valueDeleter); michael@0: michael@0: void addValue(void *value, UObjectDeleter *valueDeleter, UErrorCode &status); michael@0: inline UBool hasValues() const; michael@0: inline int32_t countValues() const; michael@0: inline const void *getValue(int32_t index) const; michael@0: michael@0: void *fValues; // Union of one single value vs. UVector of values. michael@0: UChar fCharacter; // UTF-16 code unit. michael@0: uint16_t fFirstChild; // 0 if no children. michael@0: uint16_t fNextSibling; // 0 terminates the list. michael@0: UBool fHasValuesVector; michael@0: UBool fPadding; michael@0: michael@0: // No value: fValues == NULL and fHasValuesVector == FALSE michael@0: // One value: fValues == value and fHasValuesVector == FALSE michael@0: // >=2 values: fValues == UVector of values and fHasValuesVector == TRUE michael@0: }; michael@0: michael@0: inline UBool CharacterNode::hasValues() const { michael@0: return (UBool)(fValues != NULL); michael@0: } michael@0: michael@0: inline int32_t CharacterNode::countValues() const { michael@0: return michael@0: fValues == NULL ? 0 : michael@0: !fHasValuesVector ? 1 : michael@0: ((const UVector *)fValues)->size(); michael@0: } michael@0: michael@0: inline const void *CharacterNode::getValue(int32_t index) const { michael@0: if (!fHasValuesVector) { michael@0: return fValues; // Assume index == 0. michael@0: } else { michael@0: return ((const UVector *)fValues)->elementAt(index); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Search result handler callback interface used by TextTrieMap search. michael@0: */ michael@0: class TextTrieMapSearchResultHandler : public UMemory { michael@0: public: michael@0: virtual UBool handleMatch(int32_t matchLength, michael@0: const CharacterNode *node, UErrorCode& status) = 0; michael@0: virtual ~TextTrieMapSearchResultHandler(); //added to avoid warning michael@0: }; michael@0: michael@0: /** michael@0: * TextTrieMap is a trie implementation for supporting michael@0: * fast prefix match for the string key. michael@0: */ michael@0: class U_I18N_API TextTrieMap : public UMemory { michael@0: public: michael@0: TextTrieMap(UBool ignoreCase, UObjectDeleter *valeDeleter); michael@0: virtual ~TextTrieMap(); michael@0: michael@0: void put(const UnicodeString &key, void *value, ZNStringPool &sp, UErrorCode &status); michael@0: void put(const UChar*, void *value, UErrorCode &status); michael@0: void search(const UnicodeString &text, int32_t start, michael@0: TextTrieMapSearchResultHandler *handler, UErrorCode& status) const; michael@0: int32_t isEmpty() const; michael@0: michael@0: private: michael@0: UBool fIgnoreCase; michael@0: CharacterNode *fNodes; michael@0: int32_t fNodesCapacity; michael@0: int32_t fNodesCount; michael@0: michael@0: UVector *fLazyContents; michael@0: UBool fIsEmpty; michael@0: UObjectDeleter *fValueDeleter; michael@0: michael@0: UBool growNodes(); michael@0: CharacterNode* addChildNode(CharacterNode *parent, UChar c, UErrorCode &status); michael@0: CharacterNode* getChildNode(CharacterNode *parent, UChar c) const; michael@0: michael@0: void putImpl(const UnicodeString &key, void *value, UErrorCode &status); michael@0: void buildTrie(UErrorCode &status); michael@0: void search(CharacterNode *node, const UnicodeString &text, int32_t start, michael@0: int32_t index, TextTrieMapSearchResultHandler *handler, UErrorCode &status) const; michael@0: }; michael@0: michael@0: michael@0: michael@0: class ZNames; michael@0: class TZNames; michael@0: class TextTrieMap; michael@0: michael@0: class TimeZoneNamesImpl : public TimeZoneNames { michael@0: public: michael@0: TimeZoneNamesImpl(const Locale& locale, UErrorCode& status); michael@0: michael@0: virtual ~TimeZoneNamesImpl(); michael@0: michael@0: virtual UBool operator==(const TimeZoneNames& other) const; michael@0: virtual TimeZoneNames* clone() const; michael@0: michael@0: StringEnumeration* getAvailableMetaZoneIDs(UErrorCode& status) const; michael@0: StringEnumeration* getAvailableMetaZoneIDs(const UnicodeString& tzID, UErrorCode& status) const; michael@0: michael@0: UnicodeString& getMetaZoneID(const UnicodeString& tzID, UDate date, UnicodeString& mzID) const; michael@0: UnicodeString& getReferenceZoneID(const UnicodeString& mzID, const char* region, UnicodeString& tzID) const; michael@0: michael@0: UnicodeString& getMetaZoneDisplayName(const UnicodeString& mzID, UTimeZoneNameType type, UnicodeString& name) const; michael@0: UnicodeString& getTimeZoneDisplayName(const UnicodeString& tzID, UTimeZoneNameType type, UnicodeString& name) const; michael@0: michael@0: UnicodeString& getExemplarLocationName(const UnicodeString& tzID, UnicodeString& name) const; michael@0: michael@0: TimeZoneNames::MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const; michael@0: michael@0: static UnicodeString& getDefaultExemplarLocationName(const UnicodeString& tzID, UnicodeString& name); michael@0: michael@0: private: michael@0: michael@0: Locale fLocale; michael@0: michael@0: UResourceBundle* fZoneStrings; michael@0: michael@0: UHashtable* fTZNamesMap; michael@0: UHashtable* fMZNamesMap; michael@0: michael@0: UBool fNamesTrieFullyLoaded; michael@0: TextTrieMap fNamesTrie; michael@0: michael@0: void initialize(const Locale& locale, UErrorCode& status); michael@0: void cleanup(); michael@0: michael@0: void loadStrings(const UnicodeString& tzCanonicalID); michael@0: michael@0: ZNames* loadMetaZoneNames(const UnicodeString& mzId); michael@0: TZNames* loadTimeZoneNames(const UnicodeString& mzId); michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // __TZNAMES_IMPL_H__ michael@0: //eof michael@0: //