resbund.h 18 KB

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