2 ******************************************************************************
4 * Copyright (C) 1996-2003, 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.
150 * @param res A pointer to the C resource bundle.
151 * @param status A UErrorCode value.
154 ResourceBundle(UResourceBundle
*res
,
158 * Assignment operator.
160 * @param other The resource bundle to copy.
164 operator=(const ResourceBundle
& other
);
172 * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
173 * the number of child resources.
175 * @return number of resources in a given resource.
182 * returns a string from a string resource type
184 * @param status fills in the outgoing error code
185 * could be <TT>U_MISSING_RESOURCE_ERROR</T> if the key is not found
187 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
188 * @return a pointer to a zero-terminated UChar array which lives in a memory mapped/DLL file.
192 getString(UErrorCode
& status
) const;
195 * returns a binary data from a resource. Can be used at most primitive resource types (binaries,
198 * @param len fills in the length of resulting byte chunk
199 * @param status fills in the outgoing error code
200 * could be <TT>U_MISSING_RESOURCE_ERROR</T> if the key is not found
202 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
203 * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
207 getBinary(int32_t& len
, UErrorCode
& status
) const;
211 * returns an integer vector from a resource.
213 * @param len fills in the length of resulting integer vector
214 * @param status fills in the outgoing error code
215 * could be <TT>U_MISSING_RESOURCE_ERROR</T> if the key is not found
217 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
218 * @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
222 getIntVector(int32_t& len
, UErrorCode
& status
) const;
225 * returns an unsigned integer from a resource.
226 * This integer is originally 28 bits.
228 * @param status fills in the outgoing error code
229 * could be <TT>U_MISSING_RESOURCE_ERROR</T> if the key is not found
231 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
232 * @return an unsigned integer value
236 getUInt(UErrorCode
& status
) const;
239 * returns a signed integer from a resource.
240 * This integer is originally 28 bit and the sign gets propagated.
242 * @param status fills in the outgoing error code
243 * could be <TT>U_MISSING_RESOURCE_ERROR</T> if the key is not found
245 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
246 * @return a signed integer value
250 getInt(UErrorCode
& status
) const;
253 * Checks whether the resource has another element to iterate over.
255 * @return TRUE if there are more elements, FALSE if there is no more elements
262 * Resets the internal context of a resource so that iteration starts from the first element.
270 * Returns the key associated with this resource. Not all the resources have a key - only
271 * those that are members of a table.
273 * @return a key associated to this resource, or NULL if it doesn't have a key
280 * Gets the locale ID of the resource bundle as a string.
281 * Same as getLocale().getName() .
283 * @return the locale ID of the resource bundle as a string
291 * Returns the type of a resource. Available types are defined in enum UResType
293 * @return type of the given resource.
300 * Returns the next resource in a given resource or NULL if there are no more resources
302 * @param status fills in the outgoing error code
303 * @return ResourceBundle object.
307 getNext(UErrorCode
& status
);
310 * Returns the next string in a resource or NULL if there are no more resources
313 * @param status fills in the outgoing error code
314 * @return an UnicodeString object.
318 getNextString(UErrorCode
& status
);
321 * Returns the next string in a resource or NULL if there are no more resources
324 * @param key fill in for key associated with this string
325 * @param status fills in the outgoing error code
326 * @return an UnicodeString object.
330 getNextString(const char ** key
,
334 * Returns the resource in a resource at the specified index.
336 * @param index an index to the wanted resource.
337 * @param status fills in the outgoing error code
338 * @return ResourceBundle object. If there is an error, resource is invalid.
343 UErrorCode
& status
) const;
346 * Returns the string in a given resource at the specified index.
348 * @param index an index to the wanted string.
349 * @param status fills in the outgoing error code
350 * @return an UnicodeString object. If there is an error, string is bogus
354 getStringEx(int32_t index
,
355 UErrorCode
& status
) const;
358 * Returns a resource in a resource that has a given key. This procedure works only with table
361 * @param key a key associated with the wanted resource
362 * @param status fills in the outgoing error code.
363 * @return ResourceBundle object. If there is an error, resource is invalid.
368 UErrorCode
& status
) const;
371 * Returns a string in a resource that has a given key. This procedure works only with table
374 * @param key a key associated with the wanted string
375 * @param status fills in the outgoing error code
376 * @return an UnicodeString object. If there is an error, string is bogus
380 getStringEx(const char* key
,
381 UErrorCode
& status
) const;
384 * Return the version number associated with this ResourceBundle as a string. Please
385 * use getVersion, as this method is going to be deprecated.
387 * @return A version number string as specified in the resource bundle or its parent.
388 * The caller does not own this string.
393 getVersionNumber(void) const;
396 * Return the version number associated with this ResourceBundle as a UVersionInfo array.
398 * @param versionInfo A UVersionInfo array that is filled with the version number
399 * as specified in the resource bundle or its parent.
403 getVersion(UVersionInfo versionInfo
) const;
406 * Return the Locale associated with this ResourceBundle.
408 * @return a Locale object
412 getLocale(void) const;
415 * ICU "poor man's RTTI", returns a UClassID for the actual class.
419 virtual inline UClassID
getDynamicClassID() const;
422 * ICU "poor man's RTTI", returns a UClassID for this class.
426 static inline UClassID
getStaticClassID();
429 ResourceBundle(); // default constructor not implemented
431 UResourceBundle
*resource
;
432 void constructForLocale(const UnicodeString
& path
, const Locale
& locale
, UErrorCode
& error
);
436 * The address of this static class variable serves as this class's ID
437 * for ICU "poor man's RTTI".
439 static const char fgClassID
;
443 ResourceBundle::getStaticClassID()
444 { return (UClassID
)&fgClassID
; }
447 ResourceBundle::getDynamicClassID() const
448 { return ResourceBundle::getStaticClassID(); }