• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 *   Copyright (C) 1996-2013, International Business Machines Corporation
7 *   and others.  All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File resbund.h
12 *
13 *   CREATED BY
14 *       Richard Gillam
15 *
16 * Modification History:
17 *
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
27 *                           ios::nocreate.
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.
42 *                           Cleaned up.
43 * 06/14/99      stephen     Removed methods taking a filename suffix.
44 * 11/09/99      weiv        Added getLocale(), fRealLocale, removed fRealLocaleID
45 ******************************************************************************
46 */
47 
48 #ifndef RESBUND_H
49 #define RESBUND_H
50 
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"
56 
57 /**
58  * \file
59  * \brief C++ API: Resource Bundle
60  */
61 
62 U_NAMESPACE_BEGIN
63 
64 /**
65  * A class representing a collection of resource information pertaining to a given
66  * locale. A resource bundle provides a way of accessing locale- specfic information in
67  * a data file. You create a resource bundle that manages the resources for a given
68  * locale and then ask it for individual resources.
69  * <P>
70  * Resource bundles in ICU4C are currently defined using text files which conform to the following
71  * <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt">BNF definition</a>.
72  * More on resource bundle concepts and syntax can be found in the
73  * <a href="http://icu-project.org/userguide/ResourceManagement.html">Users Guide</a>.
74  * <P>
75  *
76  * The ResourceBundle class is not suitable for subclassing.
77  *
78  * @stable ICU 2.0
79  */
80 class U_COMMON_API ResourceBundle : public UObject {
81 public:
82     /**
83      * Constructor
84      *
85      * @param packageName   The packageName and locale together point to an ICU udata object,
86      *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
87      *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
88      *                      a package registered with udata_setAppData(). Using a full file or directory
89      *                      pathname for packageName is deprecated.
90      * @param locale  This is the locale this resource bundle is for. To get resources
91      *                for the French locale, for example, you would create a
92      *                ResourceBundle passing Locale::FRENCH for the "locale" parameter,
93      *                and all subsequent calls to that resource bundle will return
94      *                resources that pertain to the French locale. If the caller doesn't
95      *                pass a locale parameter, the default locale for the system (as
96      *                returned by Locale::getDefault()) will be used.
97      * @param err     The Error Code.
98      * The UErrorCode& err parameter is used to return status information to the user. To
99      * check whether the construction succeeded or not, you should check the value of
100      * U_SUCCESS(err). If you wish more detailed information, you can check for
101      * informational error results which still indicate success. U_USING_FALLBACK_WARNING
102      * indicates that a fall back locale was used. For example, 'de_CH' was requested,
103      * but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that
104      * the default locale data was used; neither the requested locale nor any of its
105      * fall back locales could be found.
106      * @stable ICU 2.0
107      */
108     ResourceBundle(const UnicodeString&    packageName,
109                    const Locale&           locale,
110                    UErrorCode&              err);
111 
112     /**
113      * Construct a resource bundle for the default bundle in the specified package.
114      *
115      * @param packageName   The packageName and locale together point to an ICU udata object,
116      *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
117      *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
118      *                      a package registered with udata_setAppData(). Using a full file or directory
119      *                      pathname for packageName is deprecated.
120      * @param err A UErrorCode value
121      * @stable ICU 2.0
122      */
123     ResourceBundle(const UnicodeString&    packageName,
124                    UErrorCode&              err);
125 
126     /**
127      * Construct a resource bundle for the ICU default bundle.
128      *
129      * @param err A UErrorCode value
130      * @stable ICU 2.0
131      */
132     ResourceBundle(UErrorCode &err);
133 
134     /**
135      * Standard constructor, constructs a resource bundle for the locale-specific
136      * bundle in the specified package.
137      *
138      * @param packageName   The packageName and locale together point to an ICU udata object,
139      *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
140      *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
141      *                      a package registered with udata_setAppData(). Using a full file or directory
142      *                      pathname for packageName is deprecated.
143      *                      NULL is used to refer to ICU data.
144      * @param locale The locale for which to open a resource bundle.
145      * @param err A UErrorCode value
146      * @stable ICU 2.0
147      */
148     ResourceBundle(const char* packageName,
149                    const Locale& locale,
150                    UErrorCode& err);
151 
152     /**
153      * Copy constructor.
154      *
155      * @param original The resource bundle to copy.
156      * @stable ICU 2.0
157      */
158     ResourceBundle(const ResourceBundle &original);
159 
160     /**
161      * Constructor from a C UResourceBundle. The resource bundle is
162      * copied and not adopted. ures_close will still need to be used on the
163      * original resource bundle.
164      *
165      * @param res A pointer to the C resource bundle.
166      * @param status A UErrorCode value.
167      * @stable ICU 2.0
168      */
169     ResourceBundle(UResourceBundle *res,
170                    UErrorCode &status);
171 
172     /**
173      * Assignment operator.
174      *
175      * @param other The resource bundle to copy.
176      * @stable ICU 2.0
177      */
178     ResourceBundle&
179       operator=(const ResourceBundle& other);
180 
181     /** Destructor.
182      * @stable ICU 2.0
183      */
184     virtual ~ResourceBundle();
185 
186     /**
187      * Clone this object.
188      * Clones can be used concurrently in multiple threads.
189      * If an error occurs, then NULL is returned.
190      * The caller must delete the clone.
191      *
192      * @return a clone of this object
193      *
194      * @see getDynamicClassID
195      * @stable ICU 2.8
196      */
197     ResourceBundle *clone() const;
198 
199     /**
200      * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
201      * the number of child resources.
202      * @warning Integer array is treated as a scalar type. There are no
203      *          APIs to access individual members of an integer array. It
204      *          is always returned as a whole.
205      *
206      * @return number of resources in a given resource.
207      * @stable ICU 2.0
208      */
209     int32_t
210       getSize(void) const;
211 
212     /**
213      * returns a string from a string resource type
214      *
215      * @param status  fills in the outgoing error code
216      *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
217      *                could be a warning
218      *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
219      * @return a pointer to a zero-terminated char16_t array which lives in a memory mapped/DLL file.
220      * @stable ICU 2.0
221      */
222     UnicodeString
223       getString(UErrorCode& status) const;
224 
225     /**
226      * returns a binary data from a resource. Can be used at most primitive resource types (binaries,
227      * strings, ints)
228      *
229      * @param len     fills in the length of resulting byte chunk
230      * @param status  fills in the outgoing error code
231      *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
232      *                could be a warning
233      *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
234      * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
235      * @stable ICU 2.0
236      */
237     const uint8_t*
238       getBinary(int32_t& len, UErrorCode& status) const;
239 
240 
241     /**
242      * returns an integer vector from a resource.
243      *
244      * @param len     fills in the length of resulting integer vector
245      * @param status  fills in the outgoing error code
246      *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
247      *                could be a warning
248      *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
249      * @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
250      * @stable ICU 2.0
251      */
252     const int32_t*
253       getIntVector(int32_t& len, UErrorCode& status) const;
254 
255     /**
256      * returns an unsigned integer from a resource.
257      * This integer is originally 28 bits.
258      *
259      * @param status  fills in the outgoing error code
260      *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
261      *                could be a warning
262      *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
263      * @return an unsigned integer value
264      * @stable ICU 2.0
265      */
266     uint32_t
267       getUInt(UErrorCode& status) const;
268 
269     /**
270      * returns a signed integer from a resource.
271      * This integer is originally 28 bit and the sign gets propagated.
272      *
273      * @param status  fills in the outgoing error code
274      *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
275      *                could be a warning
276      *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
277      * @return a signed integer value
278      * @stable ICU 2.0
279      */
280     int32_t
281       getInt(UErrorCode& status) const;
282 
283     /**
284      * Checks whether the resource has another element to iterate over.
285      *
286      * @return TRUE if there are more elements, FALSE if there is no more elements
287      * @stable ICU 2.0
288      */
289     UBool
290       hasNext(void) const;
291 
292     /**
293      * Resets the internal context of a resource so that iteration starts from the first element.
294      *
295      * @stable ICU 2.0
296      */
297     void
298       resetIterator(void);
299 
300     /**
301      * Returns the key associated with this resource. Not all the resources have a key - only
302      * those that are members of a table.
303      *
304      * @return a key associated to this resource, or NULL if it doesn't have a key
305      * @stable ICU 2.0
306      */
307     const char*
308       getKey(void) const;
309 
310     /**
311      * Gets the locale ID of the resource bundle as a string.
312      * Same as getLocale().getName() .
313      *
314      * @return the locale ID of the resource bundle as a string
315      * @stable ICU 2.0
316      */
317     const char*
318       getName(void) const;
319 
320 
321     /**
322      * Returns the type of a resource. Available types are defined in enum UResType
323      *
324      * @return type of the given resource.
325      * @stable ICU 2.0
326      */
327     UResType
328       getType(void) const;
329 
330     /**
331      * Returns the next resource in a given resource or NULL if there are no more resources
332      *
333      * @param status            fills in the outgoing error code
334      * @return                  ResourceBundle object.
335      * @stable ICU 2.0
336      */
337     ResourceBundle
338       getNext(UErrorCode& status);
339 
340     /**
341      * Returns the next string in a resource or NULL if there are no more resources
342      * to iterate over.
343      *
344      * @param status            fills in the outgoing error code
345      * @return an UnicodeString object.
346      * @stable ICU 2.0
347      */
348     UnicodeString
349       getNextString(UErrorCode& status);
350 
351     /**
352      * Returns the next string in a resource or NULL if there are no more resources
353      * to iterate over.
354      *
355      * @param key               fill in for key associated with this string
356      * @param status            fills in the outgoing error code
357      * @return an UnicodeString object.
358      * @stable ICU 2.0
359      */
360     UnicodeString
361       getNextString(const char ** key,
362                     UErrorCode& status);
363 
364     /**
365      * Returns the resource in a resource at the specified index.
366      *
367      * @param index             an index to the wanted resource.
368      * @param status            fills in the outgoing error code
369      * @return                  ResourceBundle object. If there is an error, resource is invalid.
370      * @stable ICU 2.0
371      */
372     ResourceBundle
373       get(int32_t index,
374           UErrorCode& status) const;
375 
376     /**
377      * Returns the string in a given resource at the specified index.
378      *
379      * @param index             an index to the wanted string.
380      * @param status            fills in the outgoing error code
381      * @return                  an UnicodeString object. If there is an error, string is bogus
382      * @stable ICU 2.0
383      */
384     UnicodeString
385       getStringEx(int32_t index,
386                   UErrorCode& status) const;
387 
388     /**
389      * Returns a resource in a resource that has a given key. This procedure works only with table
390      * resources.
391      *
392      * @param key               a key associated with the wanted resource
393      * @param status            fills in the outgoing error code.
394      * @return                  ResourceBundle object. If there is an error, resource is invalid.
395      * @stable ICU 2.0
396      */
397     ResourceBundle
398       get(const char* key,
399           UErrorCode& status) const;
400 
401     /**
402      * Returns a string in a resource that has a given key. This procedure works only with table
403      * resources.
404      *
405      * @param key               a key associated with the wanted string
406      * @param status            fills in the outgoing error code
407      * @return                  an UnicodeString object. If there is an error, string is bogus
408      * @stable ICU 2.0
409      */
410     UnicodeString
411       getStringEx(const char* key,
412                   UErrorCode& status) const;
413 
414 #ifndef U_HIDE_DEPRECATED_API
415     /**
416      * Return the version number associated with this ResourceBundle as a string. Please
417      * use getVersion, as this method is going to be deprecated.
418      *
419      * @return  A version number string as specified in the resource bundle or its parent.
420      *          The caller does not own this string.
421      * @see getVersion
422      * @deprecated ICU 2.8 Use getVersion instead.
423      */
424     const char*
425       getVersionNumber(void) const;
426 #endif  /* U_HIDE_DEPRECATED_API */
427 
428     /**
429      * Return the version number associated with this ResourceBundle as a UVersionInfo array.
430      *
431      * @param versionInfo A UVersionInfo array that is filled with the version number
432      *                    as specified in the resource bundle or its parent.
433      * @stable ICU 2.0
434      */
435     void
436       getVersion(UVersionInfo versionInfo) const;
437 
438 #ifndef U_HIDE_DEPRECATED_API
439     /**
440      * Return the Locale associated with this ResourceBundle.
441      *
442      * @return a Locale object
443      * @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead.
444      */
445     const Locale&
446       getLocale(void) const;
447 #endif  /* U_HIDE_DEPRECATED_API */
448 
449     /**
450      * Return the Locale associated with this ResourceBundle.
451      * @param type You can choose between requested, valid and actual
452      *             locale. For description see the definition of
453      *             ULocDataLocaleType in uloc.h
454      * @param status just for catching illegal arguments
455      *
456      * @return a Locale object
457      * @stable ICU 2.8
458      */
459     const Locale
460       getLocale(ULocDataLocaleType type, UErrorCode &status) const;
461 #ifndef U_HIDE_INTERNAL_API
462     /**
463      * This API implements multilevel fallback
464      * @internal
465      */
466     ResourceBundle
467         getWithFallback(const char* key, UErrorCode& status);
468 #endif  /* U_HIDE_INTERNAL_API */
469     /**
470      * ICU "poor man's RTTI", returns a UClassID for the actual class.
471      *
472      * @stable ICU 2.2
473      */
474     virtual UClassID getDynamicClassID() const;
475 
476     /**
477      * ICU "poor man's RTTI", returns a UClassID for this class.
478      *
479      * @stable ICU 2.2
480      */
481     static UClassID U_EXPORT2 getStaticClassID();
482 
483 private:
484     ResourceBundle(); // default constructor not implemented
485 
486     UResourceBundle *fResource;
487     void constructForLocale(const UnicodeString& path, const Locale& locale, UErrorCode& error);
488     Locale *fLocale;
489 };
490 
491 U_NAMESPACE_END
492 #endif
493