Home | History | Annotate | Download | only in common
      1 /*
      2 **********************************************************************
      3 *   Copyright (C) 2000-2011, International Business Machines
      4 *   Corporation and others.  All Rights Reserved.
      5 **********************************************************************
      6 */
      7 
      8 #ifndef URESIMP_H
      9 #define URESIMP_H
     10 
     11 #include "unicode/ures.h"
     12 
     13 #include "uresdata.h"
     14 
     15 #define kRootLocaleName         "root"
     16 #define kPoolBundleName         "pool"
     17 
     18 /*
     19  The default minor version and the version separator must be exactly one
     20  character long.
     21 */
     22 
     23 #define kDefaultMinorVersion    "0"
     24 #define kVersionSeparator       "."
     25 #define kVersionTag             "Version"
     26 
     27 #define MAGIC1 19700503
     28 #define MAGIC2 19641227
     29 
     30 #define URES_MAX_ALIAS_LEVEL 256
     31 #define URES_MAX_BUFFER_SIZE 256
     32 
     33 /*
     34 enum UResEntryType {
     35     ENTRY_OK = 0,
     36     ENTRY_GOTO_ROOT = 1,
     37     ENTRY_GOTO_DEFAULT = 2,
     38     ENTRY_INVALID = 3
     39 };
     40 
     41 typedef enum UResEntryType UResEntryType;
     42 */
     43 
     44 struct UResourceDataEntry;
     45 typedef struct UResourceDataEntry UResourceDataEntry;
     46 
     47 /*
     48  * Note: If we wanted to make this structure smaller, then we could try
     49  * to use one UResourceDataEntry pointer for fAlias and fPool, with a separate
     50  * flag to distinguish whether this struct is for a real bundle with a pool,
     51  * or for an alias entry for which we won't use the pool after loading.
     52  */
     53 struct UResourceDataEntry {
     54     char *fName; /* name of the locale for bundle - still to decide whether it is original or fallback */
     55     char *fPath; /* path to bundle - used for distinguishing between resources with the same name */
     56     UResourceDataEntry *fParent; /*next resource in fallback chain*/
     57     UResourceDataEntry *fAlias;
     58     UResourceDataEntry *fPool;
     59     ResourceData fData; /* data for low level access */
     60     char fNameBuffer[3]; /* A small buffer of free space for fName. The free space is due to struct padding. */
     61     uint32_t fCountExisting; /* how much is this resource used */
     62     UErrorCode fBogus;
     63     /* int32_t fHashKey;*/ /* for faster access in the hashtable */
     64 };
     65 
     66 #define RES_BUFSIZE 64
     67 #define RES_PATH_SEPARATOR   '/'
     68 #define RES_PATH_SEPARATOR_S   "/"
     69 
     70 struct UResourceBundle {
     71     const char *fKey; /*tag*/
     72     UResourceDataEntry *fData; /*for low-level access*/
     73     char *fVersion;
     74     UResourceDataEntry *fTopLevelData; /* for getting the valid locale */
     75     char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
     76     ResourceData fResData;
     77     char fResBuf[RES_BUFSIZE];
     78     int32_t fResPathLen;
     79     Resource fRes;
     80     UBool fHasFallback;
     81     UBool fIsTopLevel;
     82     uint32_t fMagic1;   /* For determining if it's a stack object */
     83     uint32_t fMagic2;   /* For determining if it's a stack object */
     84     int32_t fIndex;
     85     int32_t fSize;
     86 
     87     /*const UResourceBundle *fParentRes;*/ /* needed to get the actual locale for a child resource */
     88 };
     89 
     90 U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
     91 
     92 /* Some getters used by the copy constructor */
     93 U_CFUNC const char* ures_getName(const UResourceBundle* resB);
     94 #ifdef URES_DEBUG
     95 U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
     96 /**
     97  * If anything was in the RB cache, dump it to the screen.
     98  * @return TRUE if there was anything into the cache
     99  */
    100 U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
    101 #endif
    102 /*U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd);*/
    103 /*U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd);*/
    104 /*U_CFUNC void ures_freeResPath(UResourceBundle *resB);*/
    105 
    106 /* Candidates for export */
    107 U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
    108 
    109 /**
    110  * Returns a resource that can be located using the pathToResource argument. One needs optional package, locale
    111  * and path inside the locale, for example: "/myData/en/zoneStrings/3". Keys and indexes are supported. Keys
    112  * need to reference data in named structures, while indexes can reference both named and anonymous resources.
    113  * Features a fill-in parameter.
    114  *
    115  * Note, this function does NOT have a syntax for specifying items within a tree.  May want to consider a
    116  * syntax that delineates between package/tree and resource.
    117  *
    118  * @param pathToResource    a path that will lead to the requested resource
    119  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
    120  *                          Alternatively, you can supply a struct to be filled by this function.
    121  * @param status            fills in the outgoing error code.
    122  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
    123  */
    124 U_CAPI UResourceBundle* U_EXPORT2
    125 ures_findResource(const char* pathToResource,
    126                   UResourceBundle *fillIn, UErrorCode *status);
    127 
    128 /**
    129  * Returns a sub resource that can be located using the pathToResource argument. One needs a path inside
    130  * the supplied resource, for example, if you have "en_US" resource bundle opened, you might ask for
    131  * "zoneStrings/3". Keys and indexes are supported. Keys
    132  * need to reference data in named structures, while indexes can reference both
    133  * named and anonymous resources.
    134  * Features a fill-in parameter.
    135  *
    136  * @param resourceBundle    a resource
    137  * @param pathToResource    a path that will lead to the requested resource
    138  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
    139  *                          Alternatively, you can supply a struct to be filled by this function.
    140  * @param status            fills in the outgoing error code.
    141  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
    142  */
    143 U_CAPI UResourceBundle* U_EXPORT2
    144 ures_findSubResource(const UResourceBundle *resB,
    145                      char* pathToResource,
    146                      UResourceBundle *fillIn, UErrorCode *status);
    147 
    148 /**
    149  * Returns a functionally equivalent locale (considering keywords) for the specified keyword.
    150  * @param result fillin for the equivalent locale
    151  * @param resultCapacity capacity of the fillin buffer
    152  * @param path path to the tree, or NULL for ICU data
    153  * @param resName top level resource. Example: "collations"
    154  * @param keyword locale keyword. Example: "collation"
    155  * @param locid The requested locale
    156  * @param isAvailable If non-null, pointer to fillin parameter that indicates whether the
    157  * requested locale was available. The locale is defined as 'available' if it physically
    158  * exists within the specified tree.
    159  * @param omitDefault if TRUE, omit keyword and value if default. 'de_DE\@collation=standard' -> 'de_DE'
    160  * @param status error code
    161  * @return  the actual buffer size needed for the full locale.  If it's greater
    162  * than resultCapacity, the returned full name will be truncated and an error code will be returned.
    163  */
    164 U_CAPI int32_t U_EXPORT2
    165 ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
    166                              const char *path, const char *resName, const char *keyword, const char *locid,
    167                              UBool *isAvailable, UBool omitDefault, UErrorCode *status);
    168 
    169 /**
    170  * Given a tree path and keyword, return a string enumeration of all possible values for that keyword.
    171  * @param path path to the tree, or NULL for ICU data
    172  * @param keyword a particular keyword to consider, must match a top level resource name
    173  * within the tree.
    174  * @param status error code
    175  */
    176 U_CAPI UEnumeration* U_EXPORT2
    177 ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
    178 
    179 
    180 /**
    181  * Get a resource with multi-level fallback. Normally only the top level resources will
    182  * fallback to its parent. This performs fallback on subresources. For example, when a table
    183  * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
    184  * on the sub-resources because the table is defined in the current resource bundle, but this
    185  * function can perform fallback on the sub-resources of the table.
    186  * @param resB              a resource
    187  * @param inKey             a key associated with the requested resource
    188  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
    189  *                          Alternatively, you can supply a struct to be filled by this function.
    190  * @param status: fills in the outgoing error code
    191  *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
    192  *                could be a non-failing error
    193  *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
    194  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
    195  */
    196 U_CAPI UResourceBundle* U_EXPORT2
    197 ures_getByKeyWithFallback(const UResourceBundle *resB,
    198                           const char* inKey,
    199                           UResourceBundle *fillIn,
    200                           UErrorCode *status);
    201 
    202 
    203 /**
    204  * Get a String with multi-level fallback. Normally only the top level resources will
    205  * fallback to its parent. This performs fallback on subresources. For example, when a table
    206  * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
    207  * on the sub-resources because the table is defined in the current resource bundle, but this
    208  * function can perform fallback on the sub-resources of the table.
    209  * @param resB              a resource
    210  * @param inKey             a key associated with the requested resource
    211  * @param status: fills in the outgoing error code
    212  *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
    213  *                could be a non-failing error
    214  *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
    215  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
    216  */
    217 U_CAPI const UChar* U_EXPORT2
    218 ures_getStringByKeyWithFallback(const UResourceBundle *resB,
    219                           const char* inKey,
    220                           int32_t* len,
    221                           UErrorCode *status);
    222 
    223 /**
    224  * Get a version number by key
    225  * @param resB bundle containing version number
    226  * @param key the key for the version number
    227  * @param ver fillin for the version number
    228  * @param status error code
    229  */
    230 U_CAPI void U_EXPORT2
    231 ures_getVersionByKey(const UResourceBundle *resB,
    232                      const char *key,
    233                      UVersionInfo ver,
    234                      UErrorCode *status);
    235 
    236 
    237 /**
    238  * Internal function.
    239  * Return the version number associated with this ResourceBundle as a string.
    240  *
    241  * @param resourceBundle The resource bundle for which the version is checked.
    242  * @return  A version number string as specified in the resource bundle or its parent.
    243  *          The caller does not own this string.
    244  * @see ures_getVersion
    245  */
    246 U_CAPI const char* U_EXPORT2
    247 ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
    248 
    249 /**
    250  * Return the name of the Locale associated with this ResourceBundle. This API allows
    251  * you to query for the real locale of the resource. For example, if you requested
    252  * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned.
    253  * For subresources, the locale where this resource comes from will be returned.
    254  * If fallback has occured, getLocale will reflect this.
    255  *
    256  * This internal version avoids deprecated-warnings in ICU code.
    257  *
    258  * @param resourceBundle resource bundle in question
    259  * @param status just for catching illegal arguments
    260  * @return  A Locale name
    261  */
    262 U_CAPI const char* U_EXPORT2
    263 ures_getLocaleInternal(const UResourceBundle* resourceBundle,
    264                UErrorCode* status);
    265 
    266 #endif /*URESIMP_H*/
    267