Coverage Report

Created: 2026-02-05 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/uresimp.h
Line
Count
Source
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
282k
#define kRootLocaleName         "root"
19
10.5k
#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
0
#define kVersionTag             "Version"
29
30
41.8M
#define MAGIC1 19700503
31
20.1M
#define MAGIC2 19641227
32
33
218k
#define URES_MAX_ALIAS_LEVEL 256
34
35
2.27M
#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
41.7M
#define RES_BUFSIZE 64
60
128M
#define RES_PATH_SEPARATOR   '/'
61
18.3M
#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
94
67.4M
    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) noexcept = delete;
122
    static void* U_EXPORT2 operator new[](size_t) noexcept = delete;
123
    static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete;
124
125
    StackUResourceBundle();
126
    ~StackUResourceBundle();
127
128
4.16M
    UResourceBundle* getAlias() { return &bundle; }
129
130
685k
    UResourceBundle& ref() { return bundle; }
131
0
    const UResourceBundle& ref() const { return bundle; }
132
133
    StackUResourceBundle(const StackUResourceBundle&) = delete;
134
    StackUResourceBundle& operator=(const StackUResourceBundle&) = delete;
135
136
    StackUResourceBundle(StackUResourceBundle&&) = delete;
137
    StackUResourceBundle& operator=(StackUResourceBundle&&) = delete;
138
139
private:
140
    UResourceBundle bundle;
141
};
142
143
U_NAMESPACE_END
144
145
#endif  /* __cplusplus */
146
147
/**
148
 * Opens a resource bundle for the locale;
149
 * if there is not even a base language bundle, then loads the root bundle;
150
 * never falls back to the default locale.
151
 *
152
 * This is used for algorithms that have good pan-Unicode default behavior,
153
 * such as case mappings, collation, and segmentation (BreakIterator).
154
 */
155
U_CAPI UResourceBundle* U_EXPORT2
156
ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
157
158
/* Some getters used by the copy constructor */
159
U_CFUNC const char* ures_getName(const UResourceBundle* resB);
160
#ifdef URES_DEBUG
161
U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
162
/**
163
 * If anything was in the RB cache, dump it to the screen.
164
 * @return true if there was anything into the cache
165
 */
166
U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
167
#endif
168
169
/* Candidates for export */
170
U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
171
172
/**
173
 * Returns a resource that can be located using the pathToResource argument. One needs optional package, locale
174
 * and path inside the locale, for example: "/myData/en/zoneStrings/3". Keys and indexes are supported. Keys
175
 * need to reference data in named structures, while indexes can reference both named and anonymous resources.
176
 * Features a fill-in parameter. 
177
 * 
178
 * Note, this function does NOT have a syntax for specifying items within a tree.  May want to consider a
179
 * syntax that delineates between package/tree and resource.  
180
 *
181
 * @param pathToResource    a path that will lead to the requested resource
182
 * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
183
 *                          Alternatively, you can supply a struct to be filled by this function.
184
 * @param status            fills in the outgoing error code.
185
 * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
186
 */
187
U_CAPI UResourceBundle* U_EXPORT2
188
ures_findResource(const char* pathToResource, 
189
                  UResourceBundle *fillIn, UErrorCode *status); 
190
191
/**
192
 * Returns a sub resource that can be located using the pathToResource argument. One needs a path inside 
193
 * the supplied resource, for example, if you have "en_US" resource bundle opened, you might ask for
194
 * "zoneStrings/3". Keys and indexes are supported. Keys
195
 * need to reference data in named structures, while indexes can reference both 
196
 * named and anonymous resources.
197
 * Features a fill-in parameter. 
198
 *
199
 * @param resourceBundle    a resource
200
 * @param pathToResource    a path that will lead to the requested resource
201
 * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
202
 *                          Alternatively, you can supply a struct to be filled by this function.
203
 * @param status            fills in the outgoing error code.
204
 * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
205
 */
206
U_CAPI UResourceBundle* U_EXPORT2
207
ures_findSubResource(const UResourceBundle *resB, 
208
                     char* pathToResource, 
209
                     UResourceBundle *fillIn, UErrorCode *status);
210
211
/**
212
 * Returns a functionally equivalent locale (considering keywords) for the specified keyword.
213
 * @param result fillin for the equivalent locale
214
 * @param resultCapacity capacity of the fillin buffer
215
 * @param path path to the tree, or NULL for ICU data
216
 * @param resName top level resource. Example: "collations"
217
 * @param keyword locale keyword. Example: "collation"
218
 * @param locid The requested locale
219
 * @param isAvailable If non-null, pointer to fillin parameter that indicates whether the 
220
 * requested locale was available. The locale is defined as 'available' if it physically 
221
 * exists within the specified tree.
222
 * @param omitDefault if true, omit keyword and value if default. 'de_DE\@collation=standard' -> 'de_DE'
223
 * @param status error code
224
 * @return  the actual buffer size needed for the full locale.  If it's greater 
225
 * than resultCapacity, the returned full name will be truncated and an error code will be returned.
226
 */
227
U_CAPI int32_t U_EXPORT2
228
ures_getFunctionalEquivalent(char *result, int32_t resultCapacity, 
229
                             const char *path, const char *resName, const char *keyword, const char *locid,
230
                             UBool *isAvailable, UBool omitDefault, UErrorCode *status);
231
232
/**
233
 * Given a tree path and keyword, return a string enumeration of all possible values for that keyword.
234
 * @param path path to the tree, or NULL for ICU data
235
 * @param keyword a particular keyword to consider, must match a top level resource name 
236
 * within the tree.
237
 * @param status error code
238
 */
239
U_CAPI UEnumeration* U_EXPORT2
240
ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
241
242
243
/**
244
 * Get a resource with multi-level fallback. Normally only the top level resources will
245
 * fallback to its parent. This performs fallback on subresources. For example, when a table
246
 * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
247
 * on the sub-resources because the table is defined in the current resource bundle, but this
248
 * function can perform fallback on the sub-resources of the table.
249
 * @param resB              a resource
250
 * @param inKey             a key associated with the requested resource
251
 * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
252
 *                          Alternatively, you can supply a struct to be filled by this function.
253
 * @param status: fills in the outgoing error code
254
 *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
255
 *                could be a non-failing error 
256
 *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
257
 * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
258
 */
259
U_CAPI UResourceBundle* U_EXPORT2 
260
ures_getByKeyWithFallback(const UResourceBundle *resB, 
261
                          const char* inKey, 
262
                          UResourceBundle *fillIn, 
263
                          UErrorCode *status);
264
265
/**
266
 * Get a String with multi-level fallback. Normally only the top level resources will
267
 * fallback to its parent. This performs fallback on subresources. For example, when a table
268
 * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
269
 * on the sub-resources because the table is defined in the current resource bundle, but this
270
 * function can perform fallback on the sub-resources of the table.
271
 * @param resB              a resource
272
 * @param inKey             a key associated with the requested resource
273
 * @param len               if not NULL, used to return the length of the string
274
 * @param status: fills in the outgoing error code
275
 *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
276
 *                could be a non-failing error 
277
 *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
278
 * @return returns a pointer to a zero-terminated UChar array which lives in a
279
 *         memory mapped/DLL file.
280
 */
281
U_CAPI const UChar* U_EXPORT2 
282
ures_getStringByKeyWithFallback(const UResourceBundle *resB, 
283
                          const char* inKey,  
284
                          int32_t* len,
285
                          UErrorCode *status);
286
287
#ifdef __cplusplus
288
289
U_CAPI void U_EXPORT2
290
ures_getValueWithFallback(const UResourceBundle *bundle, const char *path,
291
                          UResourceBundle *tempFillIn,
292
                          icu::ResourceDataValue &value, UErrorCode &errorCode);
293
294
/**
295
 * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
296
 * necessary fallback and following any aliases) and calls the specified `sink`'s `put()` method with that
297
 * resource.  Then walks the bundle's parent chain, calling `put()` on the sink for each item in the
298
 * parent chain.
299
 * @param bundle The bundle to search
300
 * @param path The path of the desired resource
301
 * @param sink A `ResourceSink` that gets called for each resource in the parent chain
302
 * @param errorCode The error code
303
 */
304
U_CAPI void U_EXPORT2
305
ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
306
                             icu::ResourceSink &sink, UErrorCode &errorCode);
307
308
/**
309
 * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
310
 * necessary fallback and following any aliases) and, if the resource is a table resource, iterates over its
311
 * immediate child resources (again, following any aliases to get the individual resource values), and calls the specified
312
 * `sink`'s `put()` method for each child resource (passing it that resource's key and either its actual value or,
313
 * if that value is an alias, the value you get by following the alias).  Then walks back over the bundle's
314
 * parent chain, similarly iterating over each parent table resource's child resources.
315
 * Does not descend beyond one level of table children.
316
 * @param bundle The bundle to search
317
 * @param path The path of the desired resource
318
 * @param sink A `ResourceSink` that gets called for each child resource of the specified resource (and each child
319
 * of the resources in its parent chain).
320
 * @param errorCode The error code.  This will be U_RESOURCE_TYPE_MISMATCH if the resource the caller
321
 * is asking for isn't a table resource.
322
 */
323
U_CAPI void U_EXPORT2
324
ures_getAllChildrenWithFallback(const UResourceBundle *bundle, const char *path,
325
                                icu::ResourceSink &sink, UErrorCode &errorCode);
326
327
#endif  /* __cplusplus */
328
329
/**
330
 * Get a version number by key
331
 * @param resB bundle containing version number
332
 * @param key the key for the version number
333
 * @param ver fillin for the version number
334
 * @param status error code
335
 */
336
U_CAPI void U_EXPORT2
337
ures_getVersionByKey(const UResourceBundle *resB,
338
                     const char *key,
339
                     UVersionInfo ver,
340
                     UErrorCode *status);
341
342
343
/**
344
 * Internal function.
345
 * Return the version number associated with this ResourceBundle as a string.
346
 *
347
 * @param resourceBundle The resource bundle for which the version is checked.
348
 * @return  A version number string as specified in the resource bundle or its parent.
349
 *          The caller does not own this string.
350
 * @see ures_getVersion
351
 */
352
U_CAPI const char* U_EXPORT2 
353
ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
354
355
/**
356
 * Return the name of the Locale associated with this ResourceBundle. This API allows
357
 * you to query for the real locale of the resource. For example, if you requested 
358
 * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned. 
359
 * For subresources, the locale where this resource comes from will be returned.
360
 * If fallback has occurred, getLocale will reflect this.
361
 *
362
 * This internal version avoids deprecated-warnings in ICU code.
363
 *
364
 * @param resourceBundle resource bundle in question
365
 * @param status just for catching illegal arguments
366
 * @return  A Locale name
367
 */
368
U_CAPI const char* U_EXPORT2 
369
ures_getLocaleInternal(const UResourceBundle* resourceBundle, 
370
               UErrorCode* status);
371
372
/**
373
 * Same as ures_openDirect() but uses the fill-in parameter instead of allocating a new bundle.
374
 * 
375
 * @param r The existing UResourceBundle to fill in. If NULL then status will be
376
 *          set to U_ILLEGAL_ARGUMENT_ERROR.
377
 * @param packageName   The packageName and locale together point to an ICU udata object,
378
 *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
379
 *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
380
 *                      a package registered with udata_setAppData(). Using a full file or directory
381
 *                      pathname for packageName is deprecated. If NULL, ICU data will be used.
382
 * @param locale  specifies the locale for which we want to open the resource
383
 *                if NULL, the default locale will be used. If strlen(locale) == 0
384
 *                root locale will be used.
385
 * @param status The error code.
386
 * @see ures_openDirect
387
 * @internal
388
 */
389
U_CAPI void U_EXPORT2
390
ures_openDirectFillIn(UResourceBundle *r,
391
                      const char *packageName,
392
                      const char *locale,
393
                      UErrorCode *status);
394
395
#endif /*URESIMP_H*/