1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1996-2013, International Business Machines Corporation
7 * and others. All Rights Reserved.
9 ******************************************************************************
16 * Modification History:
18 * Date Name Description
19 * 2/5/97 aliu Added scanForLocaleInFile. Added
20 * constructor which attempts to read resource bundle
21 * from a specific file, without searching other files.
22 * 2/11/97 aliu Added UErrorCode return values to constructors. Fixed
23 * infinite loops in scanForFile and scanForLocale.
24 * Modified getRawResourceData to not delete storage
25 * in localeData and resourceData which it doesn't own.
26 * Added Mac compatibility #ifdefs for tellp() and
28 * 2/18/97 helena Updated with 100% documentation coverage.
29 * 3/13/97 aliu Rewrote to load in entire resource bundle and store
30 * it as a Hashtable of ResourceBundleData objects.
31 * Added state table to govern parsing of files.
32 * Modified to load locale index out of new file
33 * distinct from default.txt.
34 * 3/25/97 aliu Modified to support 2-d arrays, needed for timezone
35 * data. Added support for custom file suffixes. Again,
36 * needed to support timezone data.
37 * 4/7/97 aliu Cleaned up.
38 * 03/02/99 stephen Removed dependency on FILE*.
39 * 03/29/99 helena Merged Bertrand and Stephen's changes.
40 * 06/11/99 stephen Removed parsing of .txt files.
41 * Reworked to use new binary format.
43 * 06/14/99 stephen Removed methods taking a filename suffix.
44 * 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID
45 ******************************************************************************
51 #include "unicode/utypes.h"
52 #include "unicode/uobject.h"
53 #include "unicode/ures.h"
54 #include "unicode/unistr.h"
55 #include "unicode/locid.h"
59 * \brief C++ API: Resource Bundle
62 #if U_SHOW_CPLUSPLUS_API
66 * A class representing a collection of resource information pertaining to a given
67 * locale. A resource bundle provides a way of accessing locale- specfic information in
68 * a data file. You create a resource bundle that manages the resources for a given
69 * locale and then ask it for individual resources.
71 * Resource bundles in ICU4C are currently defined using text files which conform to the following
72 * <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt">BNF definition</a>.
73 * More on resource bundle concepts and syntax can be found in the
74 * <a href="http://icu-project.org/userguide/ResourceManagement.html">Users Guide</a>.
77 * The ResourceBundle class is not suitable for subclassing.
81 class U_COMMON_API ResourceBundle
: public UObject
{
86 * @param packageName The packageName and locale together point to an ICU udata object,
87 * as defined by <code> udata_open( packageName, "res", locale, err) </code>
88 * or equivalent. Typically, packageName will refer to a (.dat) file, or to
89 * a package registered with udata_setAppData(). Using a full file or directory
90 * pathname for packageName is deprecated.
91 * @param locale This is the locale this resource bundle is for. To get resources
92 * for the French locale, for example, you would create a
93 * ResourceBundle passing Locale::FRENCH for the "locale" parameter,
94 * and all subsequent calls to that resource bundle will return
95 * resources that pertain to the French locale. If the caller doesn't
96 * pass a locale parameter, the default locale for the system (as
97 * returned by Locale::getDefault()) will be used.
98 * @param err The Error Code.
99 * The UErrorCode& err parameter is used to return status information to the user. To
100 * check whether the construction succeeded or not, you should check the value of
101 * U_SUCCESS(err). If you wish more detailed information, you can check for
102 * informational error results which still indicate success. U_USING_FALLBACK_WARNING
103 * indicates that a fall back locale was used. For example, 'de_CH' was requested,
104 * but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that
105 * the default locale data was used; neither the requested locale nor any of its
106 * fall back locales could be found.
109 ResourceBundle(const UnicodeString
& packageName
,
110 const Locale
& locale
,
114 * Construct a resource bundle for the default bundle in the specified package.
116 * @param packageName The packageName and locale together point to an ICU udata object,
117 * as defined by <code> udata_open( packageName, "res", locale, err) </code>
118 * or equivalent. Typically, packageName will refer to a (.dat) file, or to
119 * a package registered with udata_setAppData(). Using a full file or directory
120 * pathname for packageName is deprecated.
121 * @param err A UErrorCode value
124 ResourceBundle(const UnicodeString
& packageName
,
128 * Construct a resource bundle for the ICU default bundle.
130 * @param err A UErrorCode value
133 ResourceBundle(UErrorCode
&err
);
136 * Standard constructor, onstructs a resource bundle for the locale-specific
137 * bundle in the specified package.
139 * @param packageName The packageName and locale together point to an ICU udata object,
140 * as defined by <code> udata_open( packageName, "res", locale, err) </code>
141 * or equivalent. Typically, packageName will refer to a (.dat) file, or to
142 * a package registered with udata_setAppData(). Using a full file or directory
143 * pathname for packageName is deprecated.
144 * NULL is used to refer to ICU data.
145 * @param locale The locale for which to open a resource bundle.
146 * @param err A UErrorCode value
149 ResourceBundle(const char* packageName
,
150 const Locale
& locale
,
156 * @param original The resource bundle to copy.
159 ResourceBundle(const ResourceBundle
&original
);
162 * Constructor from a C UResourceBundle. The resource bundle is
163 * copied and not adopted. ures_close will still need to be used on the
164 * original resource bundle.
166 * @param res A pointer to the C resource bundle.
167 * @param status A UErrorCode value.
170 ResourceBundle(UResourceBundle
*res
,
174 * Assignment operator.
176 * @param other The resource bundle to copy.
180 operator=(const ResourceBundle
& other
);
185 virtual ~ResourceBundle();
189 * Clones can be used concurrently in multiple threads.
190 * If an error occurs, then NULL is returned.
191 * The caller must delete the clone.
193 * @return a clone of this object
195 * @see getDynamicClassID
198 ResourceBundle
*clone() const;
201 * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
202 * the number of child resources.
203 * @warning Integer array is treated as a scalar type. There are no
204 * APIs to access individual members of an integer array. It
205 * is always returned as a whole.
207 * @return number of resources in a given resource.
214 * returns a string from a string resource type
216 * @param status fills in the outgoing error code
217 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
219 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
220 * @return a pointer to a zero-terminated char16_t array which lives in a memory mapped/DLL file.
224 getString(UErrorCode
& status
) const;
227 * returns a binary data from a resource. Can be used at most primitive resource types (binaries,
230 * @param len fills in the length of resulting byte chunk
231 * @param status fills in the outgoing error code
232 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
234 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
235 * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
239 getBinary(int32_t& len
, UErrorCode
& status
) const;
243 * returns an integer vector from a resource.
245 * @param len fills in the length of resulting integer vector
246 * @param status fills in the outgoing error code
247 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
249 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
250 * @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
254 getIntVector(int32_t& len
, UErrorCode
& status
) const;
257 * returns an unsigned integer from a resource.
258 * This integer is originally 28 bits.
260 * @param status fills in the outgoing error code
261 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
263 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
264 * @return an unsigned integer value
268 getUInt(UErrorCode
& status
) const;
271 * returns a signed integer from a resource.
272 * This integer is originally 28 bit and the sign gets propagated.
274 * @param status fills in the outgoing error code
275 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
277 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
278 * @return a signed integer value
282 getInt(UErrorCode
& status
) const;
285 * Checks whether the resource has another element to iterate over.
287 * @return TRUE if there are more elements, FALSE if there is no more elements
294 * Resets the internal context of a resource so that iteration starts from the first element.
302 * Returns the key associated with this resource. Not all the resources have a key - only
303 * those that are members of a table.
305 * @return a key associated to this resource, or NULL if it doesn't have a key
312 * Gets the locale ID of the resource bundle as a string.
313 * Same as getLocale().getName() .
315 * @return the locale ID of the resource bundle as a string
323 * Returns the type of a resource. Available types are defined in enum UResType
325 * @return type of the given resource.
332 * Returns the next resource in a given resource or NULL if there are no more resources
334 * @param status fills in the outgoing error code
335 * @return ResourceBundle object.
339 getNext(UErrorCode
& status
);
342 * Returns the next string in a resource or NULL if there are no more resources
345 * @param status fills in the outgoing error code
346 * @return an UnicodeString object.
350 getNextString(UErrorCode
& status
);
353 * Returns the next string in a resource or NULL if there are no more resources
356 * @param key fill in for key associated with this string
357 * @param status fills in the outgoing error code
358 * @return an UnicodeString object.
362 getNextString(const char ** key
,
366 * Returns the resource in a resource at the specified index.
368 * @param index an index to the wanted resource.
369 * @param status fills in the outgoing error code
370 * @return ResourceBundle object. If there is an error, resource is invalid.
375 UErrorCode
& status
) const;
378 * Returns the string in a given resource at the specified index.
380 * @param index an index to the wanted string.
381 * @param status fills in the outgoing error code
382 * @return an UnicodeString object. If there is an error, string is bogus
386 getStringEx(int32_t index
,
387 UErrorCode
& status
) const;
390 * Returns a resource in a resource that has a given key. This procedure works only with table
393 * @param key a key associated with the wanted resource
394 * @param status fills in the outgoing error code.
395 * @return ResourceBundle object. If there is an error, resource is invalid.
400 UErrorCode
& status
) const;
403 * Returns a string in a resource that has a given key. This procedure works only with table
406 * @param key a key associated with the wanted string
407 * @param status fills in the outgoing error code
408 * @return an UnicodeString object. If there is an error, string is bogus
412 getStringEx(const char* key
,
413 UErrorCode
& status
) const;
415 #ifndef U_HIDE_DEPRECATED_API
417 * Return the version number associated with this ResourceBundle as a string. Please
418 * use getVersion, as this method is going to be deprecated.
420 * @return A version number string as specified in the resource bundle or its parent.
421 * The caller does not own this string.
423 * @deprecated ICU 2.8 Use getVersion instead.
426 getVersionNumber(void) const;
427 #endif /* U_HIDE_DEPRECATED_API */
430 * Return the version number associated with this ResourceBundle as a UVersionInfo array.
432 * @param versionInfo A UVersionInfo array that is filled with the version number
433 * as specified in the resource bundle or its parent.
437 getVersion(UVersionInfo versionInfo
) const;
439 #ifndef U_HIDE_DEPRECATED_API
441 * Return the Locale associated with this ResourceBundle.
443 * @return a Locale object
444 * @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead.
447 getLocale(void) const;
448 #endif /* U_HIDE_DEPRECATED_API */
451 * Return the Locale associated with this ResourceBundle.
452 * @param type You can choose between requested, valid and actual
453 * locale. For description see the definition of
454 * ULocDataLocaleType in uloc.h
455 * @param status just for catching illegal arguments
457 * @return a Locale object
461 getLocale(ULocDataLocaleType type
, UErrorCode
&status
) const;
462 #ifndef U_HIDE_INTERNAL_API
464 * This API implements multilevel fallback
468 getWithFallback(const char* key
, UErrorCode
& status
);
469 #endif /* U_HIDE_INTERNAL_API */
471 * ICU "poor man's RTTI", returns a UClassID for the actual class.
475 virtual UClassID
getDynamicClassID() const;
478 * ICU "poor man's RTTI", returns a UClassID for this class.
482 static UClassID U_EXPORT2
getStaticClassID();
485 ResourceBundle(); // default constructor not implemented
487 UResourceBundle
*fResource
;
488 void constructForLocale(const UnicodeString
& path
, const Locale
& locale
, UErrorCode
& error
);
493 #endif // U_SHOW_CPLUSPLUS_API