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