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