sl@0: /* sl@0: ********************************************************************** sl@0: * Copyright (c) 2002-2004, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: ********************************************************************** sl@0: * Author: Alan Liu sl@0: * Created: October 30 2002 sl@0: * Since: ICU 2.4 sl@0: ********************************************************************** sl@0: */ sl@0: #ifndef PROPNAME_H sl@0: #define PROPNAME_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "unicode/uchar.h" sl@0: #include "udataswp.h" sl@0: #include "uprops.h" sl@0: sl@0: /* sl@0: * This header defines the in-memory layout of the property names data sl@0: * structure representing the UCD data files PropertyAliases.txt and sl@0: * PropertyValueAliases.txt. It is used by: sl@0: * propname.cpp - reads data sl@0: * genpname - creates data sl@0: */ sl@0: sl@0: /* low-level char * property name comparison -------------------------------- */ sl@0: sl@0: U_CDECL_BEGIN sl@0: sl@0: /** sl@0: * \var uprv_comparePropertyNames sl@0: * Unicode property names and property value names are compared "loosely". sl@0: * sl@0: * UCD.html 4.0.1 says: sl@0: * For all property names, property value names, and for property values for sl@0: * Enumerated, Binary, or Catalog properties, use the following sl@0: * loose matching rule: sl@0: * sl@0: * LM3. Ignore case, whitespace, underscore ('_'), and hyphens. sl@0: * sl@0: * This function does just that, for (char *) name strings. sl@0: * It is almost identical to ucnv_compareNames() but also ignores sl@0: * C0 White_Space characters (U+0009..U+000d, and U+0085 on EBCDIC). sl@0: * sl@0: * @internal sl@0: */ sl@0: sl@0: U_CAPI int32_t U_EXPORT2 sl@0: uprv_compareASCIIPropertyNames(const char *name1, const char *name2); sl@0: sl@0: U_CAPI int32_t U_EXPORT2 sl@0: uprv_compareEBCDICPropertyNames(const char *name1, const char *name2); sl@0: sl@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY sl@0: # define uprv_comparePropertyNames uprv_compareASCIIPropertyNames sl@0: #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY sl@0: # define uprv_comparePropertyNames uprv_compareEBCDICPropertyNames sl@0: #else sl@0: # error U_CHARSET_FAMILY is not valid sl@0: #endif sl@0: sl@0: U_CDECL_END sl@0: sl@0: /* UDataMemory structure and signatures ------------------------------------- */ sl@0: sl@0: #define PNAME_DATA_NAME "pnames" sl@0: #define PNAME_DATA_TYPE "icu" sl@0: sl@0: /* Fields in UDataInfo: */ sl@0: sl@0: /* PNAME_SIG[] is encoded as numeric literals for compatibility with the HP compiler */ sl@0: #define PNAME_SIG_0 ((uint8_t)0x70) /* p */ sl@0: #define PNAME_SIG_1 ((uint8_t)0x6E) /* n */ sl@0: #define PNAME_SIG_2 ((uint8_t)0x61) /* a */ sl@0: #define PNAME_SIG_3 ((uint8_t)0x6D) /* m */ sl@0: sl@0: #define PNAME_FORMAT_VERSION ((int8_t)1) /* formatVersion[0] */ sl@0: sl@0: /** sl@0: * Swap pnames.icu. See udataswp.h. sl@0: * @internal sl@0: */ sl@0: U_CAPI int32_t U_EXPORT2 sl@0: upname_swap(const UDataSwapper *ds, sl@0: const void *inData, int32_t length, void *outData, sl@0: UErrorCode *pErrorCode); sl@0: sl@0: sl@0: #ifdef XP_CPLUSPLUS sl@0: sl@0: class Builder; sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /** sl@0: * An offset from the start of the pnames data to a contained entity. sl@0: * This must be a signed value, since negative offsets are used as an sl@0: * end-of-list marker. Offsets to actual objects are non-zero. A sl@0: * zero offset indicates an absent entry; this corresponds to aliases sl@0: * marked "n/a" in the original Unicode data files. sl@0: */ sl@0: typedef int16_t Offset; /* must be signed */ sl@0: sl@0: #define MAX_OFFSET 0x7FFF sl@0: sl@0: /** sl@0: * A generic value for a property or property value. Typically an sl@0: * enum from uchar.h, but sometimes a non-enum value. It must be sl@0: * large enough to accomodate the largest enum value, which as of this sl@0: * writing is the largest general category mask. Need not be signed sl@0: * but may be. Typically it doesn't matter, since the caller will sl@0: * cast it to the proper type before use. Takes the special value sl@0: * UCHAR_INVALID_CODE for invalid input. sl@0: */ sl@0: typedef int32_t EnumValue; sl@0: sl@0: /* ---------------------------------------------------------------------- */ sl@0: /* ValueMap */ sl@0: sl@0: /** sl@0: * For any top-level property that has named values (binary and sl@0: * enumerated properties), there is a ValueMap object. This object sl@0: * maps from enum values to two other maps. One goes from value enums sl@0: * to value names. The other goes from value names to value enums. sl@0: * sl@0: * The value enum values may be contiguous or disjoint. If they are sl@0: * contiguous then the enumToName_offset is nonzero, and the sl@0: * ncEnumToName_offset is zero. Vice versa if the value enums are sl@0: * disjoint. sl@0: * sl@0: * There are n of these objects, where n is the number of binary sl@0: * properties + the number of enumerated properties. sl@0: */ sl@0: struct ValueMap { sl@0: sl@0: /* -- begin pnames data -- */ sl@0: /* Enum=>name EnumToOffset / NonContiguousEnumToOffset objects. */ sl@0: /* Exactly one of these will be nonzero. */ sl@0: Offset enumToName_offset; sl@0: Offset ncEnumToName_offset; sl@0: sl@0: Offset nameToEnum_offset; /* Name=>enum data */ sl@0: /* -- end pnames data -- */ sl@0: }; sl@0: sl@0: /* ---------------------------------------------------------------------- */ sl@0: /* PropertyAliases class */ sl@0: sl@0: /** sl@0: * A class encapsulating access to the memory-mapped data representing sl@0: * property aliases and property value aliases (pnames). The class sl@0: * MUST have no v-table and declares certain methods inline -- small sl@0: * methods and methods that are called from only one point. sl@0: * sl@0: * The data members in this class correspond to the in-memory layout sl@0: * of the header of the pnames data. sl@0: */ sl@0: class PropertyAliases { sl@0: sl@0: /* -- begin pnames data -- */ sl@0: /* Enum=>name EnumToOffset object for binary and enumerated */ sl@0: /* properties */ sl@0: Offset enumToName_offset; sl@0: sl@0: /* Name=>enum data for binary & enumerated properties */ sl@0: Offset nameToEnum_offset; sl@0: sl@0: /* Enum=>offset EnumToOffset object mapping enumerated properties */ sl@0: /* to ValueMap objects */ sl@0: Offset enumToValue_offset; sl@0: sl@0: /* The following are needed by external readers of this data. */ sl@0: /* We don't use them ourselves. */ sl@0: int16_t total_size; /* size in bytes excluding the udata header */ sl@0: Offset valueMap_offset; /* offset to start of array */ sl@0: int16_t valueMap_count; /* number of entries */ sl@0: Offset nameGroupPool_offset; /* offset to start of array */ sl@0: int16_t nameGroupPool_count; /* number of entries (not groups) */ sl@0: Offset stringPool_offset; /* offset to start of pool */ sl@0: int16_t stringPool_count; /* number of strings (not size in bytes) */ sl@0: sl@0: /* -- end pnames data -- */ sl@0: sl@0: friend class ::Builder; sl@0: sl@0: const ValueMap* getValueMap(EnumValue prop) const; sl@0: sl@0: const char* chooseNameInGroup(Offset offset, sl@0: UPropertyNameChoice choice) const; sl@0: sl@0: public: sl@0: sl@0: inline const int8_t* getPointer(Offset o) const { sl@0: return ((const int8_t*) this) + o; sl@0: } sl@0: sl@0: inline const int8_t* getPointerNull(Offset o) const { sl@0: return o ? getPointer(o) : NULL; sl@0: } sl@0: sl@0: inline const char* getPropertyName(EnumValue prop, sl@0: UPropertyNameChoice choice) const; sl@0: sl@0: inline EnumValue getPropertyEnum(const char* alias) const; sl@0: sl@0: inline const char* getPropertyValueName(EnumValue prop, EnumValue value, sl@0: UPropertyNameChoice choice) const; sl@0: sl@0: inline EnumValue getPropertyValueEnum(EnumValue prop, sl@0: const char* alias) const; sl@0: sl@0: static int32_t sl@0: swap(const UDataSwapper *ds, sl@0: const uint8_t *inBytes, int32_t length, uint8_t *outBytes, sl@0: UErrorCode *pErrorCode); sl@0: }; sl@0: sl@0: /* ---------------------------------------------------------------------- */ sl@0: /* EnumToOffset */ sl@0: sl@0: /** sl@0: * A generic map from enum values to Offsets. The enum values must be sl@0: * contiguous, from enumStart to enumLimit. The Offset values may sl@0: * point to anything. sl@0: */ sl@0: class EnumToOffset { sl@0: sl@0: /* -- begin pnames data -- */ sl@0: EnumValue enumStart; sl@0: EnumValue enumLimit; sl@0: Offset _offsetArray; /* [array of enumLimit-enumStart] */ sl@0: /* -- end pnames data -- */ sl@0: sl@0: friend class ::Builder; sl@0: sl@0: Offset* getOffsetArray() { sl@0: return &_offsetArray; sl@0: } sl@0: sl@0: const Offset* getOffsetArray() const { sl@0: return &_offsetArray; sl@0: } sl@0: sl@0: static int32_t getSize(int32_t n) { sl@0: return sizeof(EnumToOffset) + sizeof(Offset) * (n - 1); sl@0: } sl@0: sl@0: int32_t getSize() { sl@0: return getSize(enumLimit - enumStart); sl@0: } sl@0: sl@0: public: sl@0: sl@0: Offset getOffset(EnumValue enumProbe) const { sl@0: if (enumProbe < enumStart || sl@0: enumProbe >= enumLimit) { sl@0: return 0; /* not found */ sl@0: } sl@0: const Offset* p = getOffsetArray(); sl@0: return p[enumProbe - enumStart]; sl@0: } sl@0: sl@0: static int32_t sl@0: swap(const UDataSwapper *ds, sl@0: const uint8_t *inBytes, int32_t length, uint8_t *outBytes, sl@0: uint8_t *temp, int32_t pos, sl@0: UErrorCode *pErrorCode); sl@0: }; sl@0: sl@0: /* ---------------------------------------------------------------------- */ sl@0: /* NonContiguousEnumToOffset */ sl@0: sl@0: /** sl@0: * A generic map from enum values to Offsets. The enum values may be sl@0: * disjoint. If they are contiguous, an EnumToOffset should be used sl@0: * instead. The Offset values may point to anything. sl@0: */ sl@0: class NonContiguousEnumToOffset { sl@0: sl@0: /* -- begin pnames data -- */ sl@0: int32_t count; sl@0: EnumValue _enumArray; /* [array of count] */ sl@0: /* Offset _offsetArray; // [array of count] after enumValue[count-1] */ sl@0: /* -- end pnames data -- */ sl@0: sl@0: friend class ::Builder; sl@0: sl@0: EnumValue* getEnumArray() { sl@0: return &_enumArray; sl@0: } sl@0: sl@0: const EnumValue* getEnumArray() const { sl@0: return &_enumArray; sl@0: } sl@0: sl@0: Offset* getOffsetArray() { sl@0: return (Offset*) (getEnumArray() + count); sl@0: } sl@0: sl@0: const Offset* getOffsetArray() const { sl@0: return (Offset*) (getEnumArray() + count); sl@0: } sl@0: sl@0: static int32_t getSize(int32_t n) { sl@0: return sizeof(int32_t) + (sizeof(EnumValue) + sizeof(Offset)) * n; sl@0: } sl@0: sl@0: int32_t getSize() { sl@0: return getSize(count); sl@0: } sl@0: sl@0: public: sl@0: sl@0: Offset getOffset(EnumValue enumProbe) const { sl@0: const EnumValue* e = getEnumArray(); sl@0: const Offset* p = getOffsetArray(); sl@0: /* linear search; binary later if warranted */ sl@0: /* (binary is not faster for short lists) */ sl@0: for (int32_t i=0; i enumProbe) break; sl@0: return p[i]; sl@0: } sl@0: return 0; /* not found */ sl@0: } sl@0: sl@0: static int32_t sl@0: swap(const UDataSwapper *ds, sl@0: const uint8_t *inBytes, int32_t length, uint8_t *outBytes, sl@0: uint8_t *temp, int32_t pos, sl@0: UErrorCode *pErrorCode); sl@0: }; sl@0: sl@0: /* ---------------------------------------------------------------------- */ sl@0: /* NameToEnum */ sl@0: sl@0: /** sl@0: * A map from names to enum values. sl@0: */ sl@0: class NameToEnum { sl@0: sl@0: /* -- begin pnames data -- */ sl@0: int32_t count; /* number of entries */ sl@0: EnumValue _enumArray; /* [array of count] EnumValues */ sl@0: /* Offset _nameArray; // [array of count] offsets to names */ sl@0: /* -- end pnames data -- */ sl@0: sl@0: friend class ::Builder; sl@0: sl@0: EnumValue* getEnumArray() { sl@0: return &_enumArray; sl@0: } sl@0: sl@0: const EnumValue* getEnumArray() const { sl@0: return &_enumArray; sl@0: } sl@0: sl@0: Offset* getNameArray() { sl@0: return (Offset*) (getEnumArray() + count); sl@0: } sl@0: sl@0: const Offset* getNameArray() const { sl@0: return (Offset*) (getEnumArray() + count); sl@0: } sl@0: sl@0: static int32_t getSize(int32_t n) { sl@0: return sizeof(int32_t) + (sizeof(Offset) + sizeof(EnumValue)) * n; sl@0: } sl@0: sl@0: int32_t getSize() { sl@0: return getSize(count); sl@0: } sl@0: sl@0: public: sl@0: sl@0: EnumValue getEnum(const char* alias, const PropertyAliases& data) const { sl@0: sl@0: const Offset* n = getNameArray(); sl@0: const EnumValue* e = getEnumArray(); sl@0: sl@0: /* linear search; binary later if warranted */ sl@0: /* (binary is not faster for short lists) */ sl@0: for (int32_t i=0; i 0) continue; sl@0: if (c < 0) break; sl@0: return e[i]; sl@0: } sl@0: sl@0: return UCHAR_INVALID_CODE; sl@0: } sl@0: sl@0: static int32_t sl@0: swap(const UDataSwapper *ds, sl@0: const uint8_t *inBytes, int32_t length, uint8_t *outBytes, sl@0: uint8_t *temp, int32_t pos, sl@0: UErrorCode *pErrorCode); sl@0: }; sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * sl@0: * In-memory layout. THIS IS NOT A STANDALONE DOCUMENT. It goes sl@0: * together with above C++ declarations and gives an overview. sl@0: * sl@0: * See above for definitions of Offset and EnumValue. Also, refer to sl@0: * above class declarations for the "bottom line" on data layout. sl@0: * sl@0: * Sizes: sl@0: * '*_offset' is an Offset (see above) sl@0: * 'count' members are typically int32_t (see above declarations) sl@0: * 'enumArray' is an array of EnumValue (see above) sl@0: * 'offsetArray' is an array of Offset (see above) sl@0: * 'nameArray' is an array of Offset (see above) sl@0: * 'enum*' is an EnumValue (see above) sl@0: * '*Array [x n]' means that *Array has n elements sl@0: * sl@0: * References: sl@0: * Instead of pointers, this flat data structure contains offsets. sl@0: * All offsets are relative to the start of 'header'. A notation sl@0: * is used to indicate what structure each offset points to: sl@0: * 'foo (>x)' the offset(s) in foo point to structure x sl@0: * sl@0: * Structures: sl@0: * Each structure is assigned a number, except for the header, sl@0: * which is called 'header'. The numbers are not contiguous sl@0: * for historical reasons. Some structures have sub-parts sl@0: * that are denoted with a letter, e.g., "5a". sl@0: * sl@0: * BEGIN LAYOUT sl@0: * ============ sl@0: * header: sl@0: * enumToName_offset (>0) sl@0: * nameToEnum_offset (>2) sl@0: * enumToValue_offset (>3) sl@0: * (alignment padding build in to header) sl@0: * sl@0: * The header also contains the following, used by "external readers" sl@0: * like ICU4J and icuswap. sl@0: * sl@0: * // The following are needed by external readers of this data. sl@0: * // We don't use them ourselves. sl@0: * int16_t total_size; // size in bytes excluding the udata header sl@0: * Offset valueMap_offset; // offset to start of array sl@0: * int16_t valueMap_count; // number of entries sl@0: * Offset nameGroupPool_offset; // offset to start of array sl@0: * int16_t nameGroupPool_count; // number of entries (not groups) sl@0: * Offset stringPool_offset; // offset to start of pool sl@0: * int16_t stringPool_count; // number of strings (not size in bytes) sl@0: * sl@0: * 0: # NonContiguousEnumToOffset obj for props => name groups sl@0: * count sl@0: * enumArray [x count] sl@0: * offsetArray [x count] (>98) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * (1: omitted -- no longer used) sl@0: * sl@0: * 2: # NameToEnum obj for binary & enumerated props sl@0: * count sl@0: * enumArray [x count] sl@0: * nameArray [x count] (>99) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * 3: # NonContiguousEnumToOffset obj for enumerated props => ValueMaps sl@0: * count sl@0: * enumArray [x count] sl@0: * offsetArray [x count] (>4) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * 4: # ValueMap array [x one for each enumerated prop i] sl@0: * enumToName_offset (>5a +2*i) one of these two is NULL, one is not sl@0: * ncEnumToName_offset (>5b +2*i) sl@0: * nameToEnums_offset (>6 +2*i) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * for each enumerated prop (either 5a or 5b): sl@0: * sl@0: * 5a: # EnumToOffset for enumerated prop's values => name groups sl@0: * enumStart sl@0: * enumLimit sl@0: * offsetArray [x enumLimit - enumStart] (>98) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * 5b: # NonContiguousEnumToOffset for enumerated prop's values => name groups sl@0: * count sl@0: * enumArray [x count] sl@0: * offsetArray [x count] (>98) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * 6: # NameToEnum for enumerated prop's values sl@0: * count sl@0: * enumArray [x count] sl@0: * nameArray [x count] (>99) sl@0: * sl@0: * => pad to next 4-byte boundary sl@0: * sl@0: * 98: # name group pool {NGP} sl@0: * [array of Offset values] (>99) sl@0: * sl@0: * 99: # string pool {SP} sl@0: * [pool of nul-terminated char* strings] sl@0: */ sl@0: U_NAMESPACE_END sl@0: sl@0: #endif /* C++ */ sl@0: sl@0: #endif