|
|
|||
File indexing completed on 2026-09-22 09:09:31
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ****************************************************************************** 0005 * 0006 * Copyright (C) 1996-2013, International Business Machines Corporation 0007 * and others. All Rights Reserved. 0008 * 0009 ****************************************************************************** 0010 * 0011 * File resbund.h 0012 * 0013 * CREATED BY 0014 * Richard Gillam 0015 * 0016 * Modification History: 0017 * 0018 * Date Name Description 0019 * 2/5/97 aliu Added scanForLocaleInFile. Added 0020 * constructor which attempts to read resource bundle 0021 * from a specific file, without searching other files. 0022 * 2/11/97 aliu Added UErrorCode return values to constructors. Fixed 0023 * infinite loops in scanForFile and scanForLocale. 0024 * Modified getRawResourceData to not delete storage 0025 * in localeData and resourceData which it doesn't own. 0026 * Added Mac compatibility #ifdefs for tellp() and 0027 * ios::nocreate. 0028 * 2/18/97 helena Updated with 100% documentation coverage. 0029 * 3/13/97 aliu Rewrote to load in entire resource bundle and store 0030 * it as a Hashtable of ResourceBundleData objects. 0031 * Added state table to govern parsing of files. 0032 * Modified to load locale index out of new file 0033 * distinct from default.txt. 0034 * 3/25/97 aliu Modified to support 2-d arrays, needed for timezone 0035 * data. Added support for custom file suffixes. Again, 0036 * needed to support timezone data. 0037 * 4/7/97 aliu Cleaned up. 0038 * 03/02/99 stephen Removed dependency on FILE*. 0039 * 03/29/99 helena Merged Bertrand and Stephen's changes. 0040 * 06/11/99 stephen Removed parsing of .txt files. 0041 * Reworked to use new binary format. 0042 * Cleaned up. 0043 * 06/14/99 stephen Removed methods taking a filename suffix. 0044 * 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID 0045 ****************************************************************************** 0046 */ 0047 0048 #ifndef RESBUND_H 0049 #define RESBUND_H 0050 0051 #include "unicode/utypes.h" 0052 0053 #if U_SHOW_CPLUSPLUS_API 0054 0055 #include "unicode/uobject.h" 0056 #include "unicode/ures.h" 0057 #include "unicode/unistr.h" 0058 #include "unicode/locid.h" 0059 0060 /** 0061 * \file 0062 * \brief C++ API: Resource Bundle 0063 */ 0064 0065 U_NAMESPACE_BEGIN 0066 0067 /** 0068 * A class representing a collection of resource information pertaining to a given 0069 * locale. A resource bundle provides a way of accessing locale- specific information in 0070 * a data file. You create a resource bundle that manages the resources for a given 0071 * locale and then ask it for individual resources. 0072 * <P> 0073 * Resource bundles in ICU4C are currently defined using text files which conform to the following 0074 * <a href="https://github.com/unicode-org/icu-docs/blob/main/design/bnf_rb.txt">BNF definition</a>. 0075 * More on resource bundle concepts and syntax can be found in the 0076 * <a href="https://unicode-org.github.io/icu/userguide/locale/resources">Users Guide</a>. 0077 * <P> 0078 * 0079 * The ResourceBundle class is not suitable for subclassing. 0080 * 0081 * @stable ICU 2.0 0082 */ 0083 class U_COMMON_API ResourceBundle : public UObject { 0084 public: 0085 /** 0086 * Constructor 0087 * 0088 * @param packageName The packageName and locale together point to an ICU udata object, 0089 * as defined by <code> udata_open( packageName, "res", locale, err) </code> 0090 * or equivalent. Typically, packageName will refer to a (.dat) file, or to 0091 * a package registered with udata_setAppData(). Using a full file or directory 0092 * pathname for packageName is deprecated. 0093 * @param locale This is the locale this resource bundle is for. To get resources 0094 * for the French locale, for example, you would create a 0095 * ResourceBundle passing Locale::FRENCH for the "locale" parameter, 0096 * and all subsequent calls to that resource bundle will return 0097 * resources that pertain to the French locale. If the caller doesn't 0098 * pass a locale parameter, the default locale for the system (as 0099 * returned by Locale::getDefault()) will be used. 0100 * @param err The Error Code. 0101 * The UErrorCode& err parameter is used to return status information to the user. To 0102 * check whether the construction succeeded or not, you should check the value of 0103 * U_SUCCESS(err). If you wish more detailed information, you can check for 0104 * informational error results which still indicate success. U_USING_FALLBACK_WARNING 0105 * indicates that a fall back locale was used. For example, 'de_CH' was requested, 0106 * but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that 0107 * the default locale data was used; neither the requested locale nor any of its 0108 * fall back locales could be found. 0109 * @stable ICU 2.0 0110 */ 0111 ResourceBundle(const UnicodeString& packageName, 0112 const Locale& locale, 0113 UErrorCode& err); 0114 0115 /** 0116 * Construct a resource bundle for the default bundle in the specified package. 0117 * 0118 * @param packageName The packageName and locale together point to an ICU udata object, 0119 * as defined by <code> udata_open( packageName, "res", locale, err) </code> 0120 * or equivalent. Typically, packageName will refer to a (.dat) file, or to 0121 * a package registered with udata_setAppData(). Using a full file or directory 0122 * pathname for packageName is deprecated. 0123 * @param err A UErrorCode value 0124 * @stable ICU 2.0 0125 */ 0126 ResourceBundle(const UnicodeString& packageName, 0127 UErrorCode& err); 0128 0129 /** 0130 * Construct a resource bundle for the ICU default bundle. 0131 * 0132 * @param err A UErrorCode value 0133 * @stable ICU 2.0 0134 */ 0135 ResourceBundle(UErrorCode &err); 0136 0137 /** 0138 * Standard constructor, constructs a resource bundle for the locale-specific 0139 * bundle in the specified package. 0140 * 0141 * @param packageName The packageName and locale together point to an ICU udata object, 0142 * as defined by <code> udata_open( packageName, "res", locale, err) </code> 0143 * or equivalent. Typically, packageName will refer to a (.dat) file, or to 0144 * a package registered with udata_setAppData(). Using a full file or directory 0145 * pathname for packageName is deprecated. 0146 * nullptr is used to refer to ICU data. 0147 * @param locale The locale for which to open a resource bundle. 0148 * @param err A UErrorCode value 0149 * @stable ICU 2.0 0150 */ 0151 ResourceBundle(const char* packageName, 0152 const Locale& locale, 0153 UErrorCode& err); 0154 0155 /** 0156 * Copy constructor. 0157 * 0158 * @param original The resource bundle to copy. 0159 * @stable ICU 2.0 0160 */ 0161 ResourceBundle(const ResourceBundle &original); 0162 0163 /** 0164 * Constructor from a C UResourceBundle. The resource bundle is 0165 * copied and not adopted. ures_close will still need to be used on the 0166 * original resource bundle. 0167 * 0168 * @param res A pointer to the C resource bundle. 0169 * @param status A UErrorCode value. 0170 * @stable ICU 2.0 0171 */ 0172 ResourceBundle(UResourceBundle *res, 0173 UErrorCode &status); 0174 0175 /** 0176 * Assignment operator. 0177 * 0178 * @param other The resource bundle to copy. 0179 * @stable ICU 2.0 0180 */ 0181 ResourceBundle& 0182 operator=(const ResourceBundle& other); 0183 0184 /** Destructor. 0185 * @stable ICU 2.0 0186 */ 0187 virtual ~ResourceBundle(); 0188 0189 /** 0190 * Clone this object. 0191 * Clones can be used concurrently in multiple threads. 0192 * If an error occurs, then nullptr is returned. 0193 * The caller must delete the clone. 0194 * 0195 * @return a clone of this object 0196 * 0197 * @see getDynamicClassID 0198 * @stable ICU 2.8 0199 */ 0200 ResourceBundle *clone() const; 0201 0202 /** 0203 * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is 0204 * the number of child resources. 0205 * @warning Integer array is treated as a scalar type. There are no 0206 * APIs to access individual members of an integer array. It 0207 * is always returned as a whole. 0208 * 0209 * @return number of resources in a given resource. 0210 * @stable ICU 2.0 0211 */ 0212 int32_t getSize() const; 0213 0214 /** 0215 * returns a string from a string resource type 0216 * 0217 * @param status fills in the outgoing error code 0218 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found 0219 * could be a warning 0220 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT> 0221 * @return a pointer to a zero-terminated char16_t array which lives in a memory mapped/DLL file. 0222 * @stable ICU 2.0 0223 */ 0224 UnicodeString 0225 getString(UErrorCode& status) const; 0226 0227 /** 0228 * returns a binary data from a resource. Can be used at most primitive resource types (binaries, 0229 * strings, ints) 0230 * 0231 * @param len fills in the length of resulting byte chunk 0232 * @param status fills in the outgoing error code 0233 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found 0234 * could be a warning 0235 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT> 0236 * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file. 0237 * @stable ICU 2.0 0238 */ 0239 const uint8_t* 0240 getBinary(int32_t& len, UErrorCode& status) const; 0241 0242 0243 /** 0244 * returns an integer vector from a resource. 0245 * 0246 * @param len fills in the length of resulting integer vector 0247 * @param status fills in the outgoing error code 0248 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found 0249 * could be a warning 0250 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT> 0251 * @return a pointer to a vector of integers that lives in a memory mapped/DLL file. 0252 * @stable ICU 2.0 0253 */ 0254 const int32_t* 0255 getIntVector(int32_t& len, UErrorCode& status) const; 0256 0257 /** 0258 * returns an unsigned integer from a resource. 0259 * This integer is originally 28 bits. 0260 * 0261 * @param status fills in the outgoing error code 0262 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found 0263 * could be a warning 0264 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT> 0265 * @return an unsigned integer value 0266 * @stable ICU 2.0 0267 */ 0268 uint32_t 0269 getUInt(UErrorCode& status) const; 0270 0271 /** 0272 * returns a signed integer from a resource. 0273 * This integer is originally 28 bit and the sign gets propagated. 0274 * 0275 * @param status fills in the outgoing error code 0276 * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found 0277 * could be a warning 0278 * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT> 0279 * @return a signed integer value 0280 * @stable ICU 2.0 0281 */ 0282 int32_t 0283 getInt(UErrorCode& status) const; 0284 0285 /** 0286 * Checks whether the resource has another element to iterate over. 0287 * 0288 * @return true if there are more elements, false if there is no more elements 0289 * @stable ICU 2.0 0290 */ 0291 UBool hasNext() const; 0292 0293 /** 0294 * Resets the internal context of a resource so that iteration starts from the first element. 0295 * 0296 * @stable ICU 2.0 0297 */ 0298 void resetIterator(); 0299 0300 /** 0301 * Returns the key associated with this resource. Not all the resources have a key - only 0302 * those that are members of a table. 0303 * 0304 * @return a key associated to this resource, or nullptr if it doesn't have a key 0305 * @stable ICU 2.0 0306 */ 0307 const char* getKey() const; 0308 0309 /** 0310 * Gets the locale ID of the resource bundle as a string. 0311 * Same as getLocale().getName() . 0312 * 0313 * @return the locale ID of the resource bundle as a string 0314 * @stable ICU 2.0 0315 */ 0316 const char* getName() const; 0317 0318 /** 0319 * Returns the type of a resource. Available types are defined in enum UResType 0320 * 0321 * @return type of the given resource. 0322 * @stable ICU 2.0 0323 */ 0324 UResType getType() const; 0325 0326 /** 0327 * Returns the next resource in a given resource or nullptr if there are no more resources 0328 * 0329 * @param status fills in the outgoing error code 0330 * @return ResourceBundle object. 0331 * @stable ICU 2.0 0332 */ 0333 ResourceBundle 0334 getNext(UErrorCode& status); 0335 0336 /** 0337 * Returns the next string in a resource or nullptr if there are no more resources 0338 * to iterate over. 0339 * 0340 * @param status fills in the outgoing error code 0341 * @return an UnicodeString object. 0342 * @stable ICU 2.0 0343 */ 0344 UnicodeString 0345 getNextString(UErrorCode& status); 0346 0347 /** 0348 * Returns the next string in a resource or nullptr if there are no more resources 0349 * to iterate over. 0350 * 0351 * @param key fill in for key associated with this string 0352 * @param status fills in the outgoing error code 0353 * @return an UnicodeString object. 0354 * @stable ICU 2.0 0355 */ 0356 UnicodeString 0357 getNextString(const char ** key, 0358 UErrorCode& status); 0359 0360 /** 0361 * Returns the resource in a resource at the specified index. 0362 * 0363 * @param index an index to the wanted resource. 0364 * @param status fills in the outgoing error code 0365 * @return ResourceBundle object. If there is an error, resource is invalid. 0366 * @stable ICU 2.0 0367 */ 0368 ResourceBundle 0369 get(int32_t index, 0370 UErrorCode& status) const; 0371 0372 /** 0373 * Returns the string in a given resource at the specified index. 0374 * 0375 * @param index an index to the wanted string. 0376 * @param status fills in the outgoing error code 0377 * @return an UnicodeString object. If there is an error, string is bogus 0378 * @stable ICU 2.0 0379 */ 0380 UnicodeString 0381 getStringEx(int32_t index, 0382 UErrorCode& status) const; 0383 0384 /** 0385 * Returns a resource in a resource that has a given key. This procedure works only with table 0386 * resources. 0387 * 0388 * @param key a key associated with the wanted resource 0389 * @param status fills in the outgoing error code. 0390 * @return ResourceBundle object. If there is an error, resource is invalid. 0391 * @stable ICU 2.0 0392 */ 0393 ResourceBundle 0394 get(const char* key, 0395 UErrorCode& status) const; 0396 0397 /** 0398 * Returns a string in a resource that has a given key. This procedure works only with table 0399 * resources. 0400 * 0401 * @param key a key associated with the wanted string 0402 * @param status fills in the outgoing error code 0403 * @return an UnicodeString object. If there is an error, string is bogus 0404 * @stable ICU 2.0 0405 */ 0406 UnicodeString 0407 getStringEx(const char* key, 0408 UErrorCode& status) const; 0409 0410 #ifndef U_HIDE_DEPRECATED_API 0411 /** 0412 * Return the version number associated with this ResourceBundle as a string. Please 0413 * use getVersion, as this method is going to be deprecated. 0414 * 0415 * @return A version number string as specified in the resource bundle or its parent. 0416 * The caller does not own this string. 0417 * @see getVersion 0418 * @deprecated ICU 2.8 Use getVersion instead. 0419 */ 0420 const char* getVersionNumber() const; 0421 #endif /* U_HIDE_DEPRECATED_API */ 0422 0423 /** 0424 * Return the version number associated with this ResourceBundle as a UVersionInfo array. 0425 * 0426 * @param versionInfo A UVersionInfo array that is filled with the version number 0427 * as specified in the resource bundle or its parent. 0428 * @stable ICU 2.0 0429 */ 0430 void 0431 getVersion(UVersionInfo versionInfo) const; 0432 0433 #ifndef U_HIDE_DEPRECATED_API 0434 /** 0435 * Return the Locale associated with this ResourceBundle. 0436 * 0437 * @return a Locale object 0438 * @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead. 0439 */ 0440 const Locale& getLocale() const; 0441 #endif /* U_HIDE_DEPRECATED_API */ 0442 0443 /** 0444 * Return the Locale associated with this ResourceBundle. 0445 * @param type You can choose between requested, valid and actual 0446 * locale. For description see the definition of 0447 * ULocDataLocaleType in uloc.h 0448 * @param status just for catching illegal arguments 0449 * 0450 * @return a Locale object 0451 * @stable ICU 2.8 0452 */ 0453 const Locale 0454 getLocale(ULocDataLocaleType type, UErrorCode &status) const; 0455 #ifndef U_HIDE_INTERNAL_API 0456 /** 0457 * This API implements multilevel fallback 0458 * @internal 0459 */ 0460 ResourceBundle 0461 getWithFallback(const char* key, UErrorCode& status); 0462 #endif /* U_HIDE_INTERNAL_API */ 0463 /** 0464 * ICU "poor man's RTTI", returns a UClassID for the actual class. 0465 * 0466 * @stable ICU 2.2 0467 */ 0468 virtual UClassID getDynamicClassID() const override; 0469 0470 /** 0471 * ICU "poor man's RTTI", returns a UClassID for this class. 0472 * 0473 * @stable ICU 2.2 0474 */ 0475 static UClassID U_EXPORT2 getStaticClassID(); 0476 0477 private: 0478 ResourceBundle() = delete; // default constructor not implemented 0479 0480 UResourceBundle *fResource; 0481 void constructForLocale(const UnicodeString& path, const Locale& locale, UErrorCode& error); 0482 Locale *fLocale; 0483 }; 0484 0485 U_NAMESPACE_END 0486 0487 #endif /* U_SHOW_CPLUSPLUS_API */ 0488 0489 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|