uresimp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2000-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. */
  9. #ifndef URESIMP_H
  10. #define URESIMP_H
  11. #include "unicode/ures.h"
  12. #include "unicode/utypes.h"
  13. #include "uresdata.h"
  14. #define kRootLocaleName "root"
  15. #define kPoolBundleName "pool"
  16. /*
  17. The default minor version and the version separator must be exactly one
  18. character long.
  19. */
  20. #define kDefaultMinorVersion "0"
  21. #define kVersionSeparator "."
  22. #define kVersionTag "Version"
  23. #define MAGIC1 19700503
  24. #define MAGIC2 19641227
  25. #define URES_MAX_ALIAS_LEVEL 256
  26. #define EMPTY_SET 0x2205
  27. struct UResourceDataEntry;
  28. typedef struct UResourceDataEntry UResourceDataEntry;
  29. /*
  30. * Note: If we wanted to make this structure smaller, then we could try
  31. * to use one UResourceDataEntry pointer for fAlias and fPool, with a separate
  32. * flag to distinguish whether this struct is for a real bundle with a pool,
  33. * or for an alias entry for which we won't use the pool after loading.
  34. */
  35. struct UResourceDataEntry {
  36. char *fName; /* name of the locale for bundle - still to decide whether it is original or fallback */
  37. char *fPath; /* path to bundle - used for distinguishing between resources with the same name */
  38. UResourceDataEntry *fParent; /*next resource in fallback chain*/
  39. UResourceDataEntry *fAlias;
  40. UResourceDataEntry *fPool;
  41. ResourceData fData; /* data for low level access */
  42. char fNameBuffer[3]; /* A small buffer of free space for fName. The free space is due to struct padding. */
  43. uint32_t fCountExisting; /* how much is this resource used */
  44. UErrorCode fBogus;
  45. /* int32_t fHashKey;*/ /* for faster access in the hashtable */
  46. };
  47. #define RES_BUFSIZE 64
  48. #define RES_PATH_SEPARATOR '/'
  49. #define RES_PATH_SEPARATOR_S "/"
  50. U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
  51. #ifdef __cplusplus
  52. struct UResourceBundle {
  53. const char *fKey; /*tag*/
  54. /**
  55. * The dataEntry for the actual locale in which this item lives.
  56. * Used for accessing the item's data.
  57. * Non-const pointer for reference counting via entryIncrease().
  58. */
  59. UResourceDataEntry *fData; /*for low-level access*/
  60. char *fVersion;
  61. /**
  62. * The dataEntry for the valid locale.
  63. * Used for /LOCALE/path alias resolution that starts back from the valid locale,
  64. * rather than from the actual locale of this item which might live in
  65. * an ancestor bundle.
  66. */
  67. UResourceDataEntry *fValidLocaleDataEntry;
  68. char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
  69. char fResBuf[RES_BUFSIZE];
  70. int32_t fResPathLen;
  71. Resource fRes;
  72. UBool fHasFallback;
  73. UBool fIsTopLevel;
  74. uint32_t fMagic1; /* For determining if it's a stack object */
  75. uint32_t fMagic2; /* For determining if it's a stack object */
  76. int32_t fIndex;
  77. int32_t fSize;
  78. inline const ResourceData &getResData() const { return fData->fData; }
  79. };
  80. U_NAMESPACE_BEGIN
  81. /**
  82. * \class StackUResourceBundle
  83. * "Smart pointer" like class, closes a UResourceBundle via ures_close().
  84. *
  85. * This code:
  86. *
  87. * StackUResourceBundle bundle;
  88. * foo(bundle.getAlias());
  89. *
  90. * Is equivalent to this code:
  91. *
  92. * UResourceBundle bundle;
  93. * ures_initStackObject(&bundle);
  94. * foo(&bundle);
  95. * ures_close(&bundle);
  96. *
  97. * @see LocalUResourceBundlePointer
  98. * @internal
  99. */
  100. class U_COMMON_API StackUResourceBundle {
  101. public:
  102. // No heap allocation. Use only on the stack.
  103. static void* U_EXPORT2 operator new(size_t) noexcept = delete;
  104. static void* U_EXPORT2 operator new[](size_t) noexcept = delete;
  105. #if U_HAVE_PLACEMENT_NEW
  106. static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete;
  107. #endif
  108. StackUResourceBundle();
  109. ~StackUResourceBundle();
  110. UResourceBundle* getAlias() { return &bundle; }
  111. UResourceBundle& ref() { return bundle; }
  112. const UResourceBundle& ref() const { return bundle; }
  113. StackUResourceBundle(const StackUResourceBundle&) = delete;
  114. StackUResourceBundle& operator=(const StackUResourceBundle&) = delete;
  115. StackUResourceBundle(StackUResourceBundle&&) = delete;
  116. StackUResourceBundle& operator=(StackUResourceBundle&&) = delete;
  117. private:
  118. UResourceBundle bundle;
  119. };
  120. U_NAMESPACE_END
  121. #endif /* __cplusplus */
  122. /**
  123. * Opens a resource bundle for the locale;
  124. * if there is not even a base language bundle, then loads the root bundle;
  125. * never falls back to the default locale.
  126. *
  127. * This is used for algorithms that have good pan-Unicode default behavior,
  128. * such as case mappings, collation, and segmentation (BreakIterator).
  129. */
  130. U_CAPI UResourceBundle* U_EXPORT2
  131. ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
  132. /* Some getters used by the copy constructor */
  133. U_CFUNC const char* ures_getName(const UResourceBundle* resB);
  134. #ifdef URES_DEBUG
  135. U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
  136. /**
  137. * If anything was in the RB cache, dump it to the screen.
  138. * @return true if there was anything into the cache
  139. */
  140. U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
  141. #endif
  142. /* Candidates for export */
  143. U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
  144. /**
  145. * Returns a resource that can be located using the pathToResource argument. One needs optional package, locale
  146. * and path inside the locale, for example: "/myData/en/zoneStrings/3". Keys and indexes are supported. Keys
  147. * need to reference data in named structures, while indexes can reference both named and anonymous resources.
  148. * Features a fill-in parameter.
  149. *
  150. * Note, this function does NOT have a syntax for specifying items within a tree. May want to consider a
  151. * syntax that delineates between package/tree and resource.
  152. *
  153. * @param pathToResource a path that will lead to the requested resource
  154. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  155. * Alternatively, you can supply a struct to be filled by this function.
  156. * @param status fills in the outgoing error code.
  157. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  158. */
  159. U_CAPI UResourceBundle* U_EXPORT2
  160. ures_findResource(const char* pathToResource,
  161. UResourceBundle *fillIn, UErrorCode *status);
  162. /**
  163. * Returns a sub resource that can be located using the pathToResource argument. One needs a path inside
  164. * the supplied resource, for example, if you have "en_US" resource bundle opened, you might ask for
  165. * "zoneStrings/3". Keys and indexes are supported. Keys
  166. * need to reference data in named structures, while indexes can reference both
  167. * named and anonymous resources.
  168. * Features a fill-in parameter.
  169. *
  170. * @param resourceBundle a resource
  171. * @param pathToResource a path that will lead to the requested resource
  172. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  173. * Alternatively, you can supply a struct to be filled by this function.
  174. * @param status fills in the outgoing error code.
  175. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  176. */
  177. U_CAPI UResourceBundle* U_EXPORT2
  178. ures_findSubResource(const UResourceBundle *resB,
  179. char* pathToResource,
  180. UResourceBundle *fillIn, UErrorCode *status);
  181. /**
  182. * Returns a functionally equivalent locale (considering keywords) for the specified keyword.
  183. * @param result fillin for the equivalent locale
  184. * @param resultCapacity capacity of the fillin buffer
  185. * @param path path to the tree, or NULL for ICU data
  186. * @param resName top level resource. Example: "collations"
  187. * @param keyword locale keyword. Example: "collation"
  188. * @param locid The requested locale
  189. * @param isAvailable If non-null, pointer to fillin parameter that indicates whether the
  190. * requested locale was available. The locale is defined as 'available' if it physically
  191. * exists within the specified tree.
  192. * @param omitDefault if true, omit keyword and value if default. 'de_DE\@collation=standard' -> 'de_DE'
  193. * @param status error code
  194. * @return the actual buffer size needed for the full locale. If it's greater
  195. * than resultCapacity, the returned full name will be truncated and an error code will be returned.
  196. */
  197. U_CAPI int32_t U_EXPORT2
  198. ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
  199. const char *path, const char *resName, const char *keyword, const char *locid,
  200. UBool *isAvailable, UBool omitDefault, UErrorCode *status);
  201. /**
  202. * Given a tree path and keyword, return a string enumeration of all possible values for that keyword.
  203. * @param path path to the tree, or NULL for ICU data
  204. * @param keyword a particular keyword to consider, must match a top level resource name
  205. * within the tree.
  206. * @param status error code
  207. */
  208. U_CAPI UEnumeration* U_EXPORT2
  209. ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
  210. /**
  211. * Get a resource with multi-level fallback. Normally only the top level resources will
  212. * fallback to its parent. This performs fallback on subresources. For example, when a table
  213. * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
  214. * on the sub-resources because the table is defined in the current resource bundle, but this
  215. * function can perform fallback on the sub-resources of the table.
  216. * @param resB a resource
  217. * @param inKey a key associated with the requested resource
  218. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  219. * Alternatively, you can supply a struct to be filled by this function.
  220. * @param status: fills in the outgoing error code
  221. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  222. * could be a non-failing error
  223. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  224. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  225. */
  226. U_CAPI UResourceBundle* U_EXPORT2
  227. ures_getByKeyWithFallback(const UResourceBundle *resB,
  228. const char* inKey,
  229. UResourceBundle *fillIn,
  230. UErrorCode *status);
  231. /**
  232. * Get a String with multi-level fallback. Normally only the top level resources will
  233. * fallback to its parent. This performs fallback on subresources. For example, when a table
  234. * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
  235. * on the sub-resources because the table is defined in the current resource bundle, but this
  236. * function can perform fallback on the sub-resources of the table.
  237. * @param resB a resource
  238. * @param inKey a key associated with the requested resource
  239. * @param len if not NULL, used to return the length of the string
  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 non-failing error
  243. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  244. * @return returns a pointer to a zero-terminated UChar array which lives in a
  245. * memory mapped/DLL file.
  246. */
  247. U_CAPI const UChar* U_EXPORT2
  248. ures_getStringByKeyWithFallback(const UResourceBundle *resB,
  249. const char* inKey,
  250. int32_t* len,
  251. UErrorCode *status);
  252. #ifdef __cplusplus
  253. U_CAPI void U_EXPORT2
  254. ures_getValueWithFallback(const UResourceBundle *bundle, const char *path,
  255. UResourceBundle *tempFillIn,
  256. icu::ResourceDataValue &value, UErrorCode &errorCode);
  257. /**
  258. * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
  259. * necessary fallback and following any aliases) and calls the specified `sink`'s `put()` method with that
  260. * resource. Then walks the bundle's parent chain, calling `put()` on the sink for each item in the
  261. * parent chain.
  262. * @param bundle The bundle to search
  263. * @param path The path of the desired resource
  264. * @param sink A `ResourceSink` that gets called for each resource in the parent chain
  265. * @param errorCode The error code
  266. */
  267. U_CAPI void U_EXPORT2
  268. ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
  269. icu::ResourceSink &sink, UErrorCode &errorCode);
  270. /**
  271. * Locates the resource specified by `path` in the resource bundle specified by `bundle` (performing any
  272. * necessary fallback and following any aliases) and, if the resource is a table resource, iterates over its
  273. * immediate child resources (again, following any aliases to get the individual resource values), and calls the specified
  274. * `sink`'s `put()` method for each child resource (passing it that resource's key and either its actual value or,
  275. * if that value is an alias, the value you get by following the alias). Then walks back over the bundle's
  276. * parent chain, similarly iterating over each parent table resource's child resources.
  277. * Does not descend beyond one level of table children.
  278. * @param bundle The bundle to search
  279. * @param path The path of the desired resource
  280. * @param sink A `ResourceSink` that gets called for each child resource of the specified resource (and each child
  281. * of the resources in its parent chain).
  282. * @param errorCode The error code. This will be U_RESOURCE_TYPE_MISMATCH if the resource the caller
  283. * is asking for isn't a table resource.
  284. */
  285. U_CAPI void U_EXPORT2
  286. ures_getAllChildrenWithFallback(const UResourceBundle *bundle, const char *path,
  287. icu::ResourceSink &sink, UErrorCode &errorCode);
  288. #endif /* __cplusplus */
  289. /**
  290. * Get a version number by key
  291. * @param resB bundle containing version number
  292. * @param key the key for the version number
  293. * @param ver fillin for the version number
  294. * @param status error code
  295. */
  296. U_CAPI void U_EXPORT2
  297. ures_getVersionByKey(const UResourceBundle *resB,
  298. const char *key,
  299. UVersionInfo ver,
  300. UErrorCode *status);
  301. /**
  302. * Internal function.
  303. * Return the version number associated with this ResourceBundle as a string.
  304. *
  305. * @param resourceBundle The resource bundle for which the version is checked.
  306. * @return A version number string as specified in the resource bundle or its parent.
  307. * The caller does not own this string.
  308. * @see ures_getVersion
  309. */
  310. U_CAPI const char* U_EXPORT2
  311. ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
  312. /**
  313. * Return the name of the Locale associated with this ResourceBundle. This API allows
  314. * you to query for the real locale of the resource. For example, if you requested
  315. * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned.
  316. * For subresources, the locale where this resource comes from will be returned.
  317. * If fallback has occurred, getLocale will reflect this.
  318. *
  319. * This internal version avoids deprecated-warnings in ICU code.
  320. *
  321. * @param resourceBundle resource bundle in question
  322. * @param status just for catching illegal arguments
  323. * @return A Locale name
  324. */
  325. U_CAPI const char* U_EXPORT2
  326. ures_getLocaleInternal(const UResourceBundle* resourceBundle,
  327. UErrorCode* status);
  328. /**
  329. * Same as ures_openDirect() but uses the fill-in parameter instead of allocating a new bundle.
  330. *
  331. * @param r The existing UResourceBundle to fill in. If NULL then status will be
  332. * set to U_ILLEGAL_ARGUMENT_ERROR.
  333. * @param packageName The packageName and locale together point to an ICU udata object,
  334. * as defined by <code> udata_open( packageName, "res", locale, err) </code>
  335. * or equivalent. Typically, packageName will refer to a (.dat) file, or to
  336. * a package registered with udata_setAppData(). Using a full file or directory
  337. * pathname for packageName is deprecated. If NULL, ICU data will be used.
  338. * @param locale specifies the locale for which we want to open the resource
  339. * if NULL, the default locale will be used. If strlen(locale) == 0
  340. * root locale will be used.
  341. * @param status The error code.
  342. * @see ures_openDirect
  343. * @internal
  344. */
  345. U_CAPI void U_EXPORT2
  346. ures_openDirectFillIn(UResourceBundle *r,
  347. const char *packageName,
  348. const char *locale,
  349. UErrorCode *status);
  350. #endif /*URESIMP_H*/