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