]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/uresimp.h
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / common / uresimp.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4**********************************************************************
f3c0d7a5 5* Copyright (C) 2000-2016, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7**********************************************************************
8*/
9
10#ifndef URESIMP_H
11#define URESIMP_H
12
13#include "unicode/ures.h"
3d1f044b 14#include "unicode/utypes.h"
b75a7d8f
A
15
16#include "uresdata.h"
17
18#define kRootLocaleName "root"
729e4ab9 19#define kPoolBundleName "pool"
b75a7d8f
A
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
73c04bcf 34#define URES_MAX_BUFFER_SIZE 256
b75a7d8f 35
4388f060
A
36#define EMPTY_SET 0x2205
37
b75a7d8f
A
38struct UResourceDataEntry;
39typedef struct UResourceDataEntry UResourceDataEntry;
40
729e4ab9
A
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 */
b75a7d8f
A
47struct 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 */
b75a7d8f 50 UResourceDataEntry *fParent; /*next resource in fallback chain*/
729e4ab9
A
51 UResourceDataEntry *fAlias;
52 UResourceDataEntry *fPool;
46f4442e
A
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 */
b75a7d8f 56 UErrorCode fBogus;
46f4442e 57 /* int32_t fHashKey;*/ /* for faster access in the hashtable */
b75a7d8f
A
58};
59
374ca955 60#define RES_BUFSIZE 64
b75a7d8f
A
61#define RES_PATH_SEPARATOR '/'
62#define RES_PATH_SEPARATOR_S "/"
63
64struct UResourceBundle {
65 const char *fKey; /*tag*/
66 UResourceDataEntry *fData; /*for low-level access*/
67 char *fVersion;
46f4442e 68 UResourceDataEntry *fTopLevelData; /* for getting the valid locale */
b75a7d8f 69 char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
46f4442e 70 ResourceData fResData;
b75a7d8f
A
71 char fResBuf[RES_BUFSIZE];
72 int32_t fResPathLen;
46f4442e 73 Resource fRes;
b75a7d8f
A
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;
374ca955 80
46f4442e 81 /*const UResourceBundle *fParentRes;*/ /* needed to get the actual locale for a child resource */
b75a7d8f
A
82};
83
374ca955 84U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
b75a7d8f 85
3d1f044b
A
86#ifdef __cplusplus
87
88U_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 */
109class U_COMMON_API StackUResourceBundle {
110public:
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
121 UResourceBundle* getAlias() { return &bundle; }
122
123 UResourceBundle& ref() { return bundle; }
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
132private:
133 UResourceBundle bundle;
134};
135
136U_NAMESPACE_END
137
138#endif /* __cplusplus */
139
b331163b
A
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 */
148U_CAPI UResourceBundle* U_EXPORT2
149ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
150
b75a7d8f
A
151/* Some getters used by the copy constructor */
152U_CFUNC const char* ures_getName(const UResourceBundle* resB);
46f4442e 153#ifdef URES_DEBUG
b75a7d8f 154U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
729e4ab9
A
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 */
159U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
46f4442e 160#endif
73c04bcf 161/*U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd);*/
b75a7d8f 162/*U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd);*/
73c04bcf 163/*U_CFUNC void ures_freeResPath(UResourceBundle *resB);*/
b75a7d8f
A
164
165/* Candidates for export */
166U_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.
374ca955
A
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.
b75a7d8f
A
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
b75a7d8f
A
182 */
183U_CAPI UResourceBundle* U_EXPORT2
184ures_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
b75a7d8f
A
201 */
202U_CAPI UResourceBundle* U_EXPORT2
203ures_findSubResource(const UResourceBundle *resB,
374ca955 204 char* pathToResource,
b75a7d8f
A
205 UResourceBundle *fillIn, UErrorCode *status);
206
374ca955
A
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.
374ca955 222 */
4388f060 223U_CAPI int32_t U_EXPORT2
374ca955
A
224ures_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
374ca955 234 */
4388f060 235U_CAPI UEnumeration* U_EXPORT2
374ca955
A
236ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
237
73c04bcf
A
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
73c04bcf 254 */
4388f060 255U_CAPI UResourceBundle* U_EXPORT2
73c04bcf
A
256ures_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
73c04bcf 275 */
4388f060 276U_CAPI const UChar* U_EXPORT2
73c04bcf
A
277ures_getStringByKeyWithFallback(const UResourceBundle *resB,
278 const char* inKey,
279 int32_t* len,
280 UErrorCode *status);
729e4ab9 281
2ca993e8
A
282#ifdef __cplusplus
283
284U_CAPI void U_EXPORT2
f3c0d7a5
A
285ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
286 icu::ResourceSink &sink, UErrorCode &errorCode);
2ca993e8
A
287
288#endif /* __cplusplus */
289
729e4ab9
A
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
729e4ab9 296 */
4388f060 297U_CAPI void U_EXPORT2
729e4ab9
A
298ures_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
729e4ab9 312 */
4388f060 313U_CAPI const char* U_EXPORT2
729e4ab9
A
314ures_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 *
4388f060
A
323 * This internal version avoids deprecated-warnings in ICU code.
324 *
729e4ab9
A
325 * @param resourceBundle resource bundle in question
326 * @param status just for catching illegal arguments
327 * @return A Locale name
729e4ab9 328 */
4388f060 329U_CAPI const char* U_EXPORT2
729e4ab9
A
330ures_getLocaleInternal(const UResourceBundle* resourceBundle,
331 UErrorCode* status);
332
3d1f044b
A
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 */
350U_CAPI void U_EXPORT2
351ures_openDirectFillIn(UResourceBundle *r,
352 const char *packageName,
353 const char *locale,
354 UErrorCode *status);
355
b75a7d8f 356#endif /*URESIMP_H*/