2 ******************************************************************************
4 * Copyright (C) 1996-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
14 * Modification History:
16 * Date Name Description
17 * 2/5/97 aliu Added scanForLocaleInFile. Added
18 * constructor which attempts to read resource bundle
19 * from a specific file, without searching other files.
20 * 2/11/97 aliu Added UErrorCode return values to constructors. Fixed
21 * infinite loops in scanForFile and scanForLocale.
22 * Modified getRawResourceData to not delete storage
23 * in localeData and resourceData which it doesn't own.
24 * Added Mac compatibility #ifdefs for tellp() and
26 * 2/18/97 helena Updated with 100% documentation coverage.
27 * 3/13/97 aliu Rewrote to load in entire resource bundle and store
28 * it as a Hashtable of ResourceBundleData objects.
29 * Added state table to govern parsing of files.
30 * Modified to load locale index out of new file
31 * distinct from default.txt.
32 * 3/25/97 aliu Modified to support 2-d arrays, needed for timezone
33 * data. Added support for custom file suffixes. Again,
34 * needed to support timezone data.
35 * 4/7/97 aliu Cleaned up.
36 * 03/02/99 stephen Removed dependency on FILE*.
37 * 03/29/99 helena Merged Bertrand and Stephen's changes.
38 * 06/11/99 stephen Removed parsing of .txt files.
39 * Reworked to use new binary format.
41 * 06/14/99 stephen Removed methods taking a filename suffix.
42 * 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID
43 ******************************************************************************
49 #include "unicode/utypes.h"
50 #include "unicode/uobject.h"
51 #include "unicode/ures.h"
52 #include "unicode/unistr.h"
53 #include "unicode/locid.h"
58 * A class representing a collection of resource information pertaining to a given
59 * locale. A resource bundle provides a way of accessing locale- specfic information in
60 * a data file. You create a resource bundle that manages the resources for a given
61 * locale and then ask it for individual resources.
63 * Resource bundles in ICU4C are currently defined using text files which conform to the following
64 * <a href="http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/bnf_rb.txt">BNF definition</a>.
65 * More on resource bundle concepts and syntax can be found in the
66 * <a href="http://oss.software.ibm.com/icu/userguide/ResourceManagement.html">Users Guide</a>.
69 * The ResourceBundle class is not suitable for subclassing.
73 class U_COMMON_API ResourceBundle
: public UObject
{
78 * @param path This is a full pathname in the platform-specific format for the
79 * directory containing the resource data files we want to load
80 * resources from. We use locale IDs to generate filenames, and the
81 * filenames have this string prepended to them before being passed
82 * to the C++ I/O functions. Therefore, this string must always end
83 * with a directory delimiter (whatever that is for the target OS)
84 * for this class to work correctly.
85 * @param locale This is the locale this resource bundle is for. To get resources
86 * for the French locale, for example, you would create a
87 * ResourceBundle passing Locale::FRENCH for the "locale" parameter,
88 * and all subsequent calls to that resource bundle will return
89 * resources that pertain to the French locale. If the caller doesn't
90 * pass a locale parameter, the default locale for the system (as
91 * returned by Locale::getDefault()) will be used.
92 * @param err The Error Code.
93 * The UErrorCode& err parameter is used to return status information to the user. To
94 * check whether the construction succeeded or not, you should check the value of
95 * U_SUCCESS(err). If you wish more detailed information, you can check for
96 * informational error results which still indicate success. U_USING_FALLBACK_WARNING
97 * indicates that a fall back locale was used. For example, 'de_CH' was requested,
98 * but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that
99 * the default locale data was used; neither the requested locale nor any of its
100 * fall back locales could be found.
103 ResourceBundle(const UnicodeString
& path
,
104 const Locale
& locale
,
108 * Construct a resource bundle for the root bundle in the specified path.
110 * @param path A path/basename for the data file(s) containing the bundle.
111 * @param err A UErrorCode value
114 ResourceBundle(const UnicodeString
& path
,
118 * Construct a resource bundle for the ICU root bundle.
120 * @param err A UErrorCode value
123 ResourceBundle(UErrorCode
&err
);
126 * Standard constructor, onstructs a resource bundle for the locale-specific
127 * bundle in the specified path.
129 * @param path A path/basename for the data file(s) containing the bundle.
130 * NULL is used for ICU data.
131 * @param locale The locale for which to open a resource bundle.
132 * @param err A UErrorCode value
135 ResourceBundle(const char* path
,
136 const Locale
& locale
,
142 * @param original The resource bundle to copy.
145 ResourceBundle(const ResourceBundle
&original
);
148 * Constructor from a C UResourceBundle. The resource bundle is
149 * copied and not adopted. ures_close will still need to be used on the
150 * original resource bundle.
152 * @param res A pointer to the C resource bundle.
153 * @param status A UErrorCode value.
156 ResourceBundle(UResourceBundle
*res
,
160 * Assignment operator.
162 * @param other The resource bundle to copy.
166 operator=(const ResourceBundle
& other
);
171 virtual ~ResourceBundle();
175 * Clones can be used concurrently in multiple threads.
176 * If an error occurs, then NULL is returned.
177 * The caller must delete the clone.
179 * @return a clone of this object
181 * @see getDynamicClassID
184 ResourceBundle
*clone() const;
187 * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
188 * the number of child resources.
189 * @warning Integer array is treated as a scalar type. There are no
190 * APIs to access individual members of an integer array. It
191 * is always returned as a whole.
193 * @return number of resources in a given resource.
200 * returns a string from a string resource type
202 * @param status fills in the outgoing error code
203 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
205 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
206 * @return a pointer to a zero-terminated UChar array which lives in a memory mapped/DLL file.
210 getString(UErrorCode
& status
) const;
213 * returns a binary data from a resource. Can be used at most primitive resource types (binaries,
216 * @param len fills in the length of resulting byte chunk
217 * @param status fills in the outgoing error code
218 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
220 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
221 * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
225 getBinary(int32_t& len
, UErrorCode
& status
) const;
229 * returns an integer vector from a resource.
231 * @param len fills in the length of resulting integer vector
232 * @param status fills in the outgoing error code
233 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
235 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
236 * @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
240 getIntVector(int32_t& len
, UErrorCode
& status
) const;
243 * returns an unsigned integer from a resource.
244 * This integer is originally 28 bits.
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 an unsigned integer value
254 getUInt(UErrorCode
& status
) const;
257 * returns a signed integer from a resource.
258 * This integer is originally 28 bit and the sign gets propagated.
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 a signed integer value
268 getInt(UErrorCode
& status
) const;
271 * Checks whether the resource has another element to iterate over.
273 * @return TRUE if there are more elements, FALSE if there is no more elements
280 * Resets the internal context of a resource so that iteration starts from the first element.
288 * Returns the key associated with this resource. Not all the resources have a key - only
289 * those that are members of a table.
291 * @return a key associated to this resource, or NULL if it doesn't have a key
298 * Gets the locale ID of the resource bundle as a string.
299 * Same as getLocale().getName() .
301 * @return the locale ID of the resource bundle as a string
309 * Returns the type of a resource. Available types are defined in enum UResType
311 * @return type of the given resource.
318 * Returns the next resource in a given resource or NULL if there are no more resources
320 * @param status fills in the outgoing error code
321 * @return ResourceBundle object.
325 getNext(UErrorCode
& status
);
328 * Returns the next string in a resource or NULL if there are no more resources
331 * @param status fills in the outgoing error code
332 * @return an UnicodeString object.
336 getNextString(UErrorCode
& status
);
339 * Returns the next string in a resource or NULL if there are no more resources
342 * @param key fill in for key associated with this string
343 * @param status fills in the outgoing error code
344 * @return an UnicodeString object.
348 getNextString(const char ** key
,
352 * Returns the resource in a resource at the specified index.
354 * @param index an index to the wanted resource.
355 * @param status fills in the outgoing error code
356 * @return ResourceBundle object. If there is an error, resource is invalid.
361 UErrorCode
& status
) const;
364 * Returns the string in a given resource at the specified index.
366 * @param index an index to the wanted string.
367 * @param status fills in the outgoing error code
368 * @return an UnicodeString object. If there is an error, string is bogus
372 getStringEx(int32_t index
,
373 UErrorCode
& status
) const;
376 * Returns a resource in a resource that has a given key. This procedure works only with table
379 * @param key a key associated with the wanted resource
380 * @param status fills in the outgoing error code.
381 * @return ResourceBundle object. If there is an error, resource is invalid.
386 UErrorCode
& status
) const;
389 * Returns a string in a resource that has a given key. This procedure works only with table
392 * @param key a key associated with the wanted string
393 * @param status fills in the outgoing error code
394 * @return an UnicodeString object. If there is an error, string is bogus
398 getStringEx(const char* key
,
399 UErrorCode
& status
) const;
402 * Return the version number associated with this ResourceBundle as a string. Please
403 * use getVersion, as this method is going to be deprecated.
405 * @return A version number string as specified in the resource bundle or its parent.
406 * The caller does not own this string.
408 * @deprecated ICU 2.8 Use getVersion instead.
411 getVersionNumber(void) const;
414 * Return the version number associated with this ResourceBundle as a UVersionInfo array.
416 * @param versionInfo A UVersionInfo array that is filled with the version number
417 * as specified in the resource bundle or its parent.
421 getVersion(UVersionInfo versionInfo
) const;
424 * Return the Locale associated with this ResourceBundle.
426 * @return a Locale object
427 * @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead.
430 getLocale(void) const;
433 * Return the Locale associated with this ResourceBundle.
434 * @param type You can choose between requested, valid and actual
435 * locale. For description see the definition of
436 * ULocDataLocaleType in uloc.h
437 * @param status just for catching illegal arguments
439 * @return a Locale object
443 getLocale(ULocDataLocaleType type
, UErrorCode
&status
) const;
445 * This API implements multilevel fallback
449 getWithFallback(const char* key
, UErrorCode
& status
);
451 * ICU "poor man's RTTI", returns a UClassID for the actual class.
455 virtual UClassID
getDynamicClassID() const;
458 * ICU "poor man's RTTI", returns a UClassID for this class.
462 static UClassID U_EXPORT2
getStaticClassID();
465 ResourceBundle(); // default constructor not implemented
467 UResourceBundle
*fResource
;
468 void constructForLocale(const UnicodeString
& path
, const Locale
& locale
, UErrorCode
& error
);