• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 *   Copyright (C) 2000-2016, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 */
9 
10 #ifndef URESIMP_H
11 #define URESIMP_H
12 
13 #include "unicode/ures.h"
14 #include "unicode/utypes.h"
15 
16 #include "uresdata.h"
17 
18 #define kRootLocaleName         "root"
19 #define kPoolBundleName         "pool"
20 
21 /*
22  The default minor version and the version separator must be exactly one
23  character long.
24 */
25 
26 #define kDefaultMinorVersion    "0"
27 #define kVersionSeparator       "."
28 #define kVersionTag             "Version"
29 
30 #define MAGIC1 19700503
31 #define MAGIC2 19641227
32 
33 #define URES_MAX_ALIAS_LEVEL 256
34 
35 #define EMPTY_SET 0x2205
36 
37 struct UResourceDataEntry;
38 typedef struct UResourceDataEntry UResourceDataEntry;
39 
40 /*
41  * Note: If we wanted to make this structure smaller, then we could try
42  * to use one UResourceDataEntry pointer for fAlias and fPool, with a separate
43  * flag to distinguish whether this struct is for a real bundle with a pool,
44  * or for an alias entry for which we won't use the pool after loading.
45  */
46 struct UResourceDataEntry {
47     char *fName; /* name of the locale for bundle - still to decide whether it is original or fallback */
48     char *fPath; /* path to bundle - used for distinguishing between resources with the same name */
49     UResourceDataEntry *fParent; /*next resource in fallback chain*/
50     UResourceDataEntry *fAlias;
51     UResourceDataEntry *fPool;
52     ResourceData fData; /* data for low level access */
53     char fNameBuffer[3]; /* A small buffer of free space for fName. The free space is due to struct padding. */
54     uint32_t fCountExisting; /* how much is this resource used */
55     UErrorCode fBogus;
56     /* int32_t fHashKey;*/ /* for faster access in the hashtable */
57 };
58 
59 #define RES_BUFSIZE 64
60 #define RES_PATH_SEPARATOR   '/'
61 #define RES_PATH_SEPARATOR_S   "/"
62 
63 U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
64 
65 #ifdef __cplusplus
66 
67 struct UResourceBundle {
68     const char *fKey; /*tag*/
69     /**
70      * The dataEntry for the actual locale in which this item lives.
71      * Used for accessing the item's data.
72      * Non-const pointer for reference counting via entryIncrease().
73      */
74     UResourceDataEntry *fData; /*for low-level access*/
75     char *fVersion;
76     /**
77      * The dataEntry for the valid locale.
78      * Used for /LOCALE/path alias resolution that starts back from the valid locale,
79      * rather than from the actual locale of this item which might live in
80      * an ancestor bundle.
81      */
82     UResourceDataEntry *fValidLocaleDataEntry;
83     char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
84     char fResBuf[RES_BUFSIZE];
85     int32_t fResPathLen;
86     Resource fRes;
87     UBool fHasFallback;
88     UBool fIsTopLevel;
89     uint32_t fMagic1;   /* For determining if it's a stack object */
90     uint32_t fMagic2;   /* For determining if it's a stack object */
91     int32_t fIndex;
92     int32_t fSize;
93 
getResDataUResourceBundle94     inline const ResourceData &getResData() const { return fData->fData; }
95 };
96 
97 U_NAMESPACE_BEGIN
98 
99 /**
100  * \class StackUResourceBundle
101  * "Smart pointer" like class, closes a UResourceBundle via ures_close().
102  *
103  * This code:
104  *
105  *   StackUResourceBundle bundle;
106  *   foo(bundle.getAlias());
107  *
108  * Is equivalent to this code:
109  *
110  *   UResourceBundle bundle;
111  *   ures_initStackObject(&bundle);
112  *   foo(&bundle);
113  *   ures_close(&bundle);
114  *
115  * @see LocalUResourceBundlePointer
116  * @internal
117  */
118 class U_COMMON_API StackUResourceBundle {
119 public:
120     // No heap allocation. Use only on the stack.
121     static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
122     static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
123 #if U_HAVE_PLACEMENT_NEW
124     static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
125 #endif
126 
127     StackUResourceBundle();
128     ~StackUResourceBundle();
129 
getAlias()130     UResourceBundle* getAlias() { return &bundle; }
131 
ref()132     UResourceBundle& ref() { return bundle; }
ref()133     const UResourceBundle& ref() const { return bundle; }
134 
135     StackUResourceBundle(const StackUResourceBundle&) = delete;
136     StackUResourceBundle& operator=(const StackUResourceBundle&) = delete;
137 
138     StackUResourceBundle(StackUResourceBundle&&) = delete;
139     StackUResourceBundle& operator=(StackUResourceBundle&&) = delete;
140 
141 private:
142     UResourceBundle bundle;
143 };
144 
145 U_NAMESPACE_END
146 
147 #endif  /* __cplusplus */
148 
149 /**
150  * Opens a resource bundle for the locale;
151  * if there is not even a base language bundle, then loads the root bundle;
152  * never falls back to the default locale.
153  *
154  * This is used for algorithms that have good pan-Unicode default behavior,
155  * such as case mappings, collation, and segmentation (BreakIterator).
156  */
157 U_CAPI UResourceBundle* U_EXPORT2
158 ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
159 
160 /* Some getters used by the copy constructor */
161 U_CFUNC const char* ures_getName(const UResourceBundle* resB);
162 #ifdef URES_DEBUG
163 U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
164 /**
165  * If anything was in the RB cache, dump it to the screen.
166  * @return true if there was anything into the cache
167  */
168 U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
169 #endif
170 
171 /* Candidates for export */
172 U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
173 
174 /**
175  * Returns a resource that can be located using the pathToResource argument. One needs optional package, locale
176  * and path inside the locale, for example: "/myData/en/zoneStrings/3". Keys and indexes are supported. Keys
177  * need to reference data in named structures, while indexes can reference both named and anonymous resources.
178  * Features a fill-in parameter.
179  *
180  * Note, this function does NOT have a syntax for specifying items within a tree.  May want to consider a
181  * syntax that delineates between package/tree and resource.
182  *
183  * @param pathToResource    a path that will lead to the requested resource
184  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
185  *                          Alternatively, you can supply a struct to be filled by this function.
186  * @param status            fills in the outgoing error code.
187  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
188  */
189 U_CAPI UResourceBundle* U_EXPORT2
190 ures_findResource(const char* pathToResource,
191                   UResourceBundle *fillIn, UErrorCode *status);
192 
193 /**
194  * Returns a sub resource that can be located using the pathToResource argument. One needs a path inside
195  * the supplied resource, for example, if you have "en_US" resource bundle opened, you might ask for
196  * "zoneStrings/3". Keys and indexes are supported. Keys
197  * need to reference data in named structures, while indexes can reference both
198  * named and anonymous resources.
199  * Features a fill-in parameter.
200  *
201  * @param resourceBundle    a resource
202  * @param pathToResource    a path that will lead to the requested resource
203  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
204  *                          Alternatively, you can supply a struct to be filled by this function.
205  * @param status            fills in the outgoing error code.
206  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
207  */
208 U_CAPI UResourceBundle* U_EXPORT2
209 ures_findSubResource(const UResourceBundle *resB,
210                      char* pathToResource,
211                      UResourceBundle *fillIn, UErrorCode *status);
212 
213 /**
214  * Returns a functionally equivalent locale (considering keywords) for the specified keyword.
215  * @param result fillin for the equivalent locale
216  * @param resultCapacity capacity of the fillin buffer
217  * @param path path to the tree, or NULL for ICU data
218  * @param resName top level resource. Example: "collations"
219  * @param keyword locale keyword. Example: "collation"
220  * @param locid The requested locale
221  * @param isAvailable If non-null, pointer to fillin parameter that indicates whether the
222  * requested locale was available. The locale is defined as 'available' if it physically
223  * exists within the specified tree.
224  * @param omitDefault if true, omit keyword and value if default. 'de_DE\@collation=standard' -> 'de_DE'
225  * @param status error code
226  * @return  the actual buffer size needed for the full locale.  If it's greater
227  * than resultCapacity, the returned full name will be truncated and an error code will be returned.
228  */
229 U_CAPI int32_t U_EXPORT2
230 ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
231                              const char *path, const char *resName, const char *keyword, const char *locid,
232                              UBool *isAvailable, UBool omitDefault, UErrorCode *status);
233 
234 /**
235  * Given a tree path and keyword, return a string enumeration of all possible values for that keyword.
236  * @param path path to the tree, or NULL for ICU data
237  * @param keyword a particular keyword to consider, must match a top level resource name
238  * within the tree.
239  * @param status error code
240  */
241 U_CAPI UEnumeration* U_EXPORT2
242 ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
243 
244 
245 /**
246  * Get a resource with multi-level fallback. Normally only the top level resources will
247  * fallback to its parent. This performs fallback on subresources. For example, when a table
248  * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
249  * on the sub-resources because the table is defined in the current resource bundle, but this
250  * function can perform fallback on the sub-resources of the table.
251  * @param resB              a resource
252  * @param inKey             a key associated with the requested resource
253  * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
254  *                          Alternatively, you can supply a struct to be filled by this function.
255  * @param status: fills in the outgoing error code
256  *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
257  *                could be a non-failing error
258  *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
259  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
260  */
261 U_CAPI UResourceBundle* U_EXPORT2
262 ures_getByKeyWithFallback(const UResourceBundle *resB,
263                           const char* inKey,
264                           UResourceBundle *fillIn,
265                           UErrorCode *status);
266 
267 /**
268  * Get a String with multi-level fallback. Normally only the top level resources will
269  * fallback to its parent. This performs fallback on subresources. For example, when a table
270  * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
271  * on the sub-resources because the table is defined in the current resource bundle, but this
272  * function can perform fallback on the sub-resources of the table.
273  * @param resB              a resource
274  * @param inKey             a key associated with the requested resource
275  * @param len               if not NULL, used to return the length of the string
276  * @param status: fills in the outgoing error code
277  *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
278  *                could be a non-failing error
279  *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
280  * @return returns a pointer to a zero-terminated UChar array which lives in a
281  *         memory mapped/DLL file.
282  */
283 U_CAPI const UChar* U_EXPORT2
284 ures_getStringByKeyWithFallback(const UResourceBundle *resB,
285                           const char* inKey,
286                           int32_t* len,
287                           UErrorCode *status);
288 
289 #ifdef __cplusplus
290 
291 U_CAPI void U_EXPORT2
292 ures_getValueWithFallback(const UResourceBundle *bundle, const char *path,
293                           UResourceBundle *tempFillIn,
294                           icu::ResourceDataValue &value, UErrorCode &errorCode);
295 
296 /**
297  * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
298  * necessary fallback and following any aliases) and calls the specified `sink`'s `put()` method with that
299  * resource.  Then walks the bundle's parent chain, calling `put()` on the sink for each item in the
300  * parent chain.
301  * @param bundle The bundle to search
302  * @param path The path of the desired resource
303  * @param sink A `ResourceSink` that gets called for each resource in the parent chain
304  * @param errorCode The error code
305  */
306 U_CAPI void U_EXPORT2
307 ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
308                              icu::ResourceSink &sink, UErrorCode &errorCode);
309 
310 /**
311  * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
312  * necessary fallback and following any aliases) and, if the resource is a table resource, iterates over its
313  * immediate child resources (again, following any aliases to get the individual resource values), and calls the specified
314  * `sink`'s `put()` method for each child resource (passing it that resource's key and either its actual value or,
315  * if that value is an alias, the value you get by following the alias).  Then walks back over the bundle's
316  * parent chain, similarly iterating over each parent table resource's child resources.
317  * Does not descend beyond one level of table children.
318  * @param bundle The bundle to search
319  * @param path The path of the desired resource
320  * @param sink A `ResourceSink` that gets called for each child resource of the specified resource (and each child
321  * of the resources in its parent chain).
322  * @param errorCode The error code.  This will be U_RESOURCE_TYPE_MISMATCH if the resource the caller
323  * is asking for isn't a table resource.
324  */
325 U_CAPI void U_EXPORT2
326 ures_getAllChildrenWithFallback(const UResourceBundle *bundle, const char *path,
327                                 icu::ResourceSink &sink, UErrorCode &errorCode);
328 
329 #endif  /* __cplusplus */
330 
331 /**
332  * Get a version number by key
333  * @param resB bundle containing version number
334  * @param key the key for the version number
335  * @param ver fillin for the version number
336  * @param status error code
337  */
338 U_CAPI void U_EXPORT2
339 ures_getVersionByKey(const UResourceBundle *resB,
340                      const char *key,
341                      UVersionInfo ver,
342                      UErrorCode *status);
343 
344 
345 /**
346  * Internal function.
347  * Return the version number associated with this ResourceBundle as a string.
348  *
349  * @param resourceBundle The resource bundle for which the version is checked.
350  * @return  A version number string as specified in the resource bundle or its parent.
351  *          The caller does not own this string.
352  * @see ures_getVersion
353  */
354 U_CAPI const char* U_EXPORT2
355 ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
356 
357 /**
358  * Return the name of the Locale associated with this ResourceBundle. This API allows
359  * you to query for the real locale of the resource. For example, if you requested
360  * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned.
361  * For subresources, the locale where this resource comes from will be returned.
362  * If fallback has occurred, getLocale will reflect this.
363  *
364  * This internal version avoids deprecated-warnings in ICU code.
365  *
366  * @param resourceBundle resource bundle in question
367  * @param status just for catching illegal arguments
368  * @return  A Locale name
369  */
370 U_CAPI const char* U_EXPORT2
371 ures_getLocaleInternal(const UResourceBundle* resourceBundle,
372                UErrorCode* status);
373 
374 /**
375  * Same as ures_openDirect() but uses the fill-in parameter instead of allocating a new bundle.
376  *
377  * @param r The existing UResourceBundle to fill in. If NULL then status will be
378  *          set to U_ILLEGAL_ARGUMENT_ERROR.
379  * @param packageName   The packageName and locale together point to an ICU udata object,
380  *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
381  *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
382  *                      a package registered with udata_setAppData(). Using a full file or directory
383  *                      pathname for packageName is deprecated. If NULL, ICU data will be used.
384  * @param locale  specifies the locale for which we want to open the resource
385  *                if NULL, the default locale will be used. If strlen(locale) == 0
386  *                root locale will be used.
387  * @param status The error code.
388  * @see ures_openDirect
389  * @internal
390  */
391 U_CAPI void U_EXPORT2
392 ures_openDirectFillIn(UResourceBundle *r,
393                       const char *packageName,
394                       const char *locale,
395                       UErrorCode *status);
396 
397 #endif /*URESIMP_H*/
398