resbund.h 18 KB

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