uresimp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 URES_MAX_BUFFER_SIZE 256
  27. #define EMPTY_SET 0x2205
  28. struct UResourceDataEntry;
  29. typedef struct UResourceDataEntry UResourceDataEntry;
  30. /*
  31. * Note: If we wanted to make this structure smaller, then we could try
  32. * to use one UResourceDataEntry pointer for fAlias and fPool, with a separate
  33. * flag to distinguish whether this struct is for a real bundle with a pool,
  34. * or for an alias entry for which we won't use the pool after loading.
  35. */
  36. struct UResourceDataEntry {
  37. char *fName; /* name of the locale for bundle - still to decide whether it is original or fallback */
  38. char *fPath; /* path to bundle - used for distinguishing between resources with the same name */
  39. UResourceDataEntry *fParent; /*next resource in fallback chain*/
  40. UResourceDataEntry *fAlias;
  41. UResourceDataEntry *fPool;
  42. ResourceData fData; /* data for low level access */
  43. char fNameBuffer[3]; /* A small buffer of free space for fName. The free space is due to struct padding. */
  44. uint32_t fCountExisting; /* how much is this resource used */
  45. UErrorCode fBogus;
  46. /* int32_t fHashKey;*/ /* for faster access in the hashtable */
  47. };
  48. #define RES_BUFSIZE 64
  49. #define RES_PATH_SEPARATOR '/'
  50. #define RES_PATH_SEPARATOR_S "/"
  51. struct UResourceBundle {
  52. const char *fKey; /*tag*/
  53. UResourceDataEntry *fData; /*for low-level access*/
  54. char *fVersion;
  55. UResourceDataEntry *fTopLevelData; /* for getting the valid locale */
  56. char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
  57. // TODO(ICU-20769): Try to change the by-value fResData into a pointer,
  58. // with the struct in only one place for each bundle.
  59. // Also replace class ResourceDataValue.resData with a pResData pointer again.
  60. ResourceData fResData;
  61. char fResBuf[RES_BUFSIZE];
  62. int32_t fResPathLen;
  63. Resource fRes;
  64. UBool fHasFallback;
  65. UBool fIsTopLevel;
  66. uint32_t fMagic1; /* For determining if it's a stack object */
  67. uint32_t fMagic2; /* For determining if it's a stack object */
  68. int32_t fIndex;
  69. int32_t fSize;
  70. /*const UResourceBundle *fParentRes;*/ /* needed to get the actual locale for a child resource */
  71. };
  72. U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
  73. #ifdef __cplusplus
  74. U_NAMESPACE_BEGIN
  75. /**
  76. * \class StackUResourceBundle
  77. * "Smart pointer" like class, closes a UResourceBundle via ures_close().
  78. *
  79. * This code:
  80. *
  81. * StackUResourceBundle bundle;
  82. * foo(bundle.getAlias());
  83. *
  84. * Is equivalent to this code:
  85. *
  86. * UResourceBundle bundle;
  87. * ures_initStackObject(&bundle);
  88. * foo(&bundle);
  89. * ures_close(&bundle);
  90. *
  91. * @see LocalUResourceBundlePointer
  92. * @internal
  93. */
  94. class U_COMMON_API StackUResourceBundle {
  95. public:
  96. // No heap allocation. Use only on the stack.
  97. static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
  98. static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
  99. #if U_HAVE_PLACEMENT_NEW
  100. static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
  101. #endif
  102. StackUResourceBundle();
  103. ~StackUResourceBundle();
  104. UResourceBundle* getAlias() { return &bundle; }
  105. UResourceBundle& ref() { return bundle; }
  106. const UResourceBundle& ref() const { return bundle; }
  107. StackUResourceBundle(const StackUResourceBundle&) = delete;
  108. StackUResourceBundle& operator=(const StackUResourceBundle&) = delete;
  109. StackUResourceBundle(StackUResourceBundle&&) = delete;
  110. StackUResourceBundle& operator=(StackUResourceBundle&&) = delete;
  111. private:
  112. UResourceBundle bundle;
  113. };
  114. U_NAMESPACE_END
  115. #endif /* __cplusplus */
  116. /**
  117. * Opens a resource bundle for the locale;
  118. * if there is not even a base language bundle, then loads the root bundle;
  119. * never falls back to the default locale.
  120. *
  121. * This is used for algorithms that have good pan-Unicode default behavior,
  122. * such as case mappings, collation, and segmentation (BreakIterator).
  123. */
  124. U_CAPI UResourceBundle* U_EXPORT2
  125. ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
  126. /* Some getters used by the copy constructor */
  127. U_CFUNC const char* ures_getName(const UResourceBundle* resB);
  128. #ifdef URES_DEBUG
  129. U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
  130. /**
  131. * If anything was in the RB cache, dump it to the screen.
  132. * @return TRUE if there was anything into the cache
  133. */
  134. U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
  135. #endif
  136. /*U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd);*/
  137. /*U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd);*/
  138. /*U_CFUNC void ures_freeResPath(UResourceBundle *resB);*/
  139. /* Candidates for export */
  140. U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
  141. /**
  142. * Returns a resource that can be located using the pathToResource argument. One needs optional package, locale
  143. * and path inside the locale, for example: "/myData/en/zoneStrings/3". Keys and indexes are supported. Keys
  144. * need to reference data in named structures, while indexes can reference both named and anonymous resources.
  145. * Features a fill-in parameter.
  146. *
  147. * Note, this function does NOT have a syntax for specifying items within a tree. May want to consider a
  148. * syntax that delineates between package/tree and resource.
  149. *
  150. * @param pathToResource a path that will lead to the requested resource
  151. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  152. * Alternatively, you can supply a struct to be filled by this function.
  153. * @param status fills in the outgoing error code.
  154. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  155. */
  156. U_CAPI UResourceBundle* U_EXPORT2
  157. ures_findResource(const char* pathToResource,
  158. UResourceBundle *fillIn, UErrorCode *status);
  159. /**
  160. * Returns a sub resource that can be located using the pathToResource argument. One needs a path inside
  161. * the supplied resource, for example, if you have "en_US" resource bundle opened, you might ask for
  162. * "zoneStrings/3". Keys and indexes are supported. Keys
  163. * need to reference data in named structures, while indexes can reference both
  164. * named and anonymous resources.
  165. * Features a fill-in parameter.
  166. *
  167. * @param resourceBundle a resource
  168. * @param pathToResource a path that will lead to the requested resource
  169. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  170. * Alternatively, you can supply a struct to be filled by this function.
  171. * @param status fills in the outgoing error code.
  172. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  173. */
  174. U_CAPI UResourceBundle* U_EXPORT2
  175. ures_findSubResource(const UResourceBundle *resB,
  176. char* pathToResource,
  177. UResourceBundle *fillIn, UErrorCode *status);
  178. /**
  179. * Returns a functionally equivalent locale (considering keywords) for the specified keyword.
  180. * @param result fillin for the equivalent locale
  181. * @param resultCapacity capacity of the fillin buffer
  182. * @param path path to the tree, or NULL for ICU data
  183. * @param resName top level resource. Example: "collations"
  184. * @param keyword locale keyword. Example: "collation"
  185. * @param locid The requested locale
  186. * @param isAvailable If non-null, pointer to fillin parameter that indicates whether the
  187. * requested locale was available. The locale is defined as 'available' if it physically
  188. * exists within the specified tree.
  189. * @param omitDefault if TRUE, omit keyword and value if default. 'de_DE\@collation=standard' -> 'de_DE'
  190. * @param status error code
  191. * @return the actual buffer size needed for the full locale. If it's greater
  192. * than resultCapacity, the returned full name will be truncated and an error code will be returned.
  193. */
  194. U_CAPI int32_t U_EXPORT2
  195. ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
  196. const char *path, const char *resName, const char *keyword, const char *locid,
  197. UBool *isAvailable, UBool omitDefault, UErrorCode *status);
  198. /**
  199. * Given a tree path and keyword, return a string enumeration of all possible values for that keyword.
  200. * @param path path to the tree, or NULL for ICU data
  201. * @param keyword a particular keyword to consider, must match a top level resource name
  202. * within the tree.
  203. * @param status error code
  204. */
  205. U_CAPI UEnumeration* U_EXPORT2
  206. ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
  207. /**
  208. * Get a resource with multi-level fallback. Normally only the top level resources will
  209. * fallback to its parent. This performs fallback on subresources. For example, when a table
  210. * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
  211. * on the sub-resources because the table is defined in the current resource bundle, but this
  212. * function can perform fallback on the sub-resources of the table.
  213. * @param resB a resource
  214. * @param inKey a key associated with the requested resource
  215. * @param fillIn if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
  216. * Alternatively, you can supply a struct to be filled by this function.
  217. * @param status: fills in the outgoing error code
  218. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  219. * could be a non-failing error
  220. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  221. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  222. */
  223. U_CAPI UResourceBundle* U_EXPORT2
  224. ures_getByKeyWithFallback(const UResourceBundle *resB,
  225. const char* inKey,
  226. UResourceBundle *fillIn,
  227. UErrorCode *status);
  228. /**
  229. * Get a String with multi-level fallback. Normally only the top level resources will
  230. * fallback to its parent. This performs fallback on subresources. For example, when a table
  231. * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
  232. * on the sub-resources because the table is defined in the current resource bundle, but this
  233. * function can perform fallback on the sub-resources of the table.
  234. * @param resB a resource
  235. * @param inKey a key associated with the requested resource
  236. * @param status: fills in the outgoing error code
  237. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  238. * could be a non-failing error
  239. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  240. * @return a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
  241. */
  242. U_CAPI const UChar* U_EXPORT2
  243. ures_getStringByKeyWithFallback(const UResourceBundle *resB,
  244. const char* inKey,
  245. int32_t* len,
  246. UErrorCode *status);
  247. #ifdef __cplusplus
  248. U_CAPI void U_EXPORT2
  249. ures_getValueWithFallback(const UResourceBundle *bundle, const char *path,
  250. UResourceBundle *tempFillIn,
  251. icu::ResourceDataValue &value, UErrorCode &errorCode);
  252. U_CAPI void U_EXPORT2
  253. ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
  254. icu::ResourceSink &sink, UErrorCode &errorCode);
  255. #endif /* __cplusplus */
  256. /**
  257. * Get a version number by key
  258. * @param resB bundle containing version number
  259. * @param key the key for the version number
  260. * @param ver fillin for the version number
  261. * @param status error code
  262. */
  263. U_CAPI void U_EXPORT2
  264. ures_getVersionByKey(const UResourceBundle *resB,
  265. const char *key,
  266. UVersionInfo ver,
  267. UErrorCode *status);
  268. /**
  269. * Internal function.
  270. * Return the version number associated with this ResourceBundle as a string.
  271. *
  272. * @param resourceBundle The resource bundle for which the version is checked.
  273. * @return A version number string as specified in the resource bundle or its parent.
  274. * The caller does not own this string.
  275. * @see ures_getVersion
  276. */
  277. U_CAPI const char* U_EXPORT2
  278. ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
  279. /**
  280. * Return the name of the Locale associated with this ResourceBundle. This API allows
  281. * you to query for the real locale of the resource. For example, if you requested
  282. * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned.
  283. * For subresources, the locale where this resource comes from will be returned.
  284. * If fallback has occured, getLocale will reflect this.
  285. *
  286. * This internal version avoids deprecated-warnings in ICU code.
  287. *
  288. * @param resourceBundle resource bundle in question
  289. * @param status just for catching illegal arguments
  290. * @return A Locale name
  291. */
  292. U_CAPI const char* U_EXPORT2
  293. ures_getLocaleInternal(const UResourceBundle* resourceBundle,
  294. UErrorCode* status);
  295. /**
  296. * Same as ures_openDirect() but uses the fill-in parameter instead of allocating a new bundle.
  297. *
  298. * @param r The existing UResourceBundle to fill in. If NULL then status will be
  299. * set to U_ILLEGAL_ARGUMENT_ERROR.
  300. * @param packageName The packageName and locale together point to an ICU udata object,
  301. * as defined by <code> udata_open( packageName, "res", locale, err) </code>
  302. * or equivalent. Typically, packageName will refer to a (.dat) file, or to
  303. * a package registered with udata_setAppData(). Using a full file or directory
  304. * pathname for packageName is deprecated. If NULL, ICU data will be used.
  305. * @param locale specifies the locale for which we want to open the resource
  306. * if NULL, the default locale will be used. If strlen(locale) == 0
  307. * root locale will be used.
  308. * @param status The error code.
  309. * @see ures_openDirect
  310. * @internal
  311. */
  312. U_CAPI void U_EXPORT2
  313. ures_openDirectFillIn(UResourceBundle *r,
  314. const char *packageName,
  315. const char *locale,
  316. UErrorCode *status);
  317. #endif /*URESIMP_H*/