resbund.h 18 KB

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