udata.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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) 1999-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: udata.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 1999oct25
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __UDATA_H__
  19. #define __UDATA_H__
  20. #include "unicode/utypes.h"
  21. #if U_SHOW_CPLUSPLUS_API
  22. #include "unicode/localpointer.h"
  23. #endif // U_SHOW_CPLUSPLUS_API
  24. U_CDECL_BEGIN
  25. /**
  26. * \file
  27. * \brief C API: Data loading interface
  28. *
  29. * <h2>Information about data loading interface</h2>
  30. *
  31. * This API is used to find and efficiently load data for ICU and applications
  32. * using ICU. It provides an abstract interface that specifies a data type and
  33. * name to find and load the data. Normally this API is used by other ICU APIs
  34. * to load required data out of the ICU data library, but it can be used to
  35. * load data out of other places.
  36. *
  37. * See the User Guide Data Management chapter.
  38. */
  39. #ifndef U_HIDE_INTERNAL_API
  40. /**
  41. * Character used to separate package names from tree names
  42. * @internal ICU 3.0
  43. */
  44. #define U_TREE_SEPARATOR '-'
  45. /**
  46. * String used to separate package names from tree names
  47. * @internal ICU 3.0
  48. */
  49. #define U_TREE_SEPARATOR_STRING "-"
  50. /**
  51. * Character used to separate parts of entry names
  52. * @internal ICU 3.0
  53. */
  54. #define U_TREE_ENTRY_SEP_CHAR '/'
  55. /**
  56. * String used to separate parts of entry names
  57. * @internal ICU 3.0
  58. */
  59. #define U_TREE_ENTRY_SEP_STRING "/"
  60. /**
  61. * Alias for standard ICU data
  62. * @internal ICU 3.0
  63. */
  64. #define U_ICUDATA_ALIAS "ICUDATA"
  65. #endif /* U_HIDE_INTERNAL_API */
  66. /**
  67. * UDataInfo contains the properties about the requested data.
  68. * This is meta data.
  69. *
  70. * <p>This structure may grow in the future, indicated by the
  71. * <code>size</code> field.</p>
  72. *
  73. * <p>ICU data must be at least 8-aligned, and should be 16-aligned.
  74. * The UDataInfo struct begins 4 bytes after the start of the data item,
  75. * so it is 4-aligned.
  76. *
  77. * <p>The platform data property fields help determine if a data
  78. * file can be efficiently used on a given machine.
  79. * The particular fields are of importance only if the data
  80. * is affected by the properties - if there is integer data
  81. * with word sizes > 1 byte, char* text, or UChar* text.</p>
  82. *
  83. * <p>The implementation for the <code>udata_open[Choice]()</code>
  84. * functions may reject data based on the value in <code>isBigEndian</code>.
  85. * No other field is used by the <code>udata</code> API implementation.</p>
  86. *
  87. * <p>The <code>dataFormat</code> may be used to identify
  88. * the kind of data, e.g. a converter table.</p>
  89. *
  90. * <p>The <code>formatVersion</code> field should be used to
  91. * make sure that the format can be interpreted.
  92. * It may be a good idea to check only for the one or two highest
  93. * of the version elements to allow the data memory to
  94. * get more or somewhat rearranged contents, for as long
  95. * as the using code can still interpret the older contents.</p>
  96. *
  97. * <p>The <code>dataVersion</code> field is intended to be a
  98. * common place to store the source version of the data;
  99. * for data from the Unicode character database, this could
  100. * reflect the Unicode version.</p>
  101. *
  102. * @stable ICU 2.0
  103. */
  104. typedef struct {
  105. /** sizeof(UDataInfo)
  106. * @stable ICU 2.0 */
  107. uint16_t size;
  108. /** unused, set to 0
  109. * @stable ICU 2.0*/
  110. uint16_t reservedWord;
  111. /* platform data properties */
  112. /** 0 for little-endian machine, 1 for big-endian
  113. * @stable ICU 2.0 */
  114. uint8_t isBigEndian;
  115. /** see U_CHARSET_FAMILY values in utypes.h
  116. * @stable ICU 2.0*/
  117. uint8_t charsetFamily;
  118. /** sizeof(UChar), one of { 1, 2, 4 }
  119. * @stable ICU 2.0*/
  120. uint8_t sizeofUChar;
  121. /** unused, set to 0
  122. * @stable ICU 2.0*/
  123. uint8_t reservedByte;
  124. /** data format identifier
  125. * @stable ICU 2.0*/
  126. uint8_t dataFormat[4];
  127. /** versions: [0] major [1] minor [2] milli [3] micro
  128. * @stable ICU 2.0*/
  129. uint8_t formatVersion[4];
  130. /** versions: [0] major [1] minor [2] milli [3] micro
  131. * @stable ICU 2.0*/
  132. uint8_t dataVersion[4];
  133. } UDataInfo;
  134. /* API for reading data -----------------------------------------------------*/
  135. /**
  136. * Forward declaration of the data memory type.
  137. * @stable ICU 2.0
  138. */
  139. typedef struct UDataMemory UDataMemory;
  140. /**
  141. * Callback function for udata_openChoice().
  142. * @param context parameter passed into <code>udata_openChoice()</code>.
  143. * @param type The type of the data as passed into <code>udata_openChoice()</code>.
  144. * It may be <code>NULL</code>.
  145. * @param name The name of the data as passed into <code>udata_openChoice()</code>.
  146. * @param pInfo A pointer to the <code>UDataInfo</code> structure
  147. * of data that has been loaded and will be returned
  148. * by <code>udata_openChoice()</code> if this function
  149. * returns <code>true</code>.
  150. * @return true if the current data memory is acceptable
  151. * @stable ICU 2.0
  152. */
  153. typedef UBool U_CALLCONV
  154. UDataMemoryIsAcceptable(void *context,
  155. const char *type, const char *name,
  156. const UDataInfo *pInfo);
  157. /**
  158. * Convenience function.
  159. * This function works the same as <code>udata_openChoice</code>
  160. * except that any data that matches the type and name
  161. * is assumed to be acceptable.
  162. * @param path Specifies an absolute path and/or a basename for the
  163. * finding of the data in the file system.
  164. * <code>NULL</code> for ICU data.
  165. * @param type A string that specifies the type of data to be loaded.
  166. * For example, resource bundles are loaded with type "res",
  167. * conversion tables with type "cnv".
  168. * This may be <code>NULL</code> or empty.
  169. * @param name A string that specifies the name of the data.
  170. * @param pErrorCode An ICU UErrorCode parameter. It must not be <code>NULL</code>.
  171. * @return A pointer (handle) to a data memory object, or <code>NULL</code>
  172. * if an error occurs. Call <code>udata_getMemory()</code>
  173. * to get a pointer to the actual data.
  174. *
  175. * @see udata_openChoice
  176. * @stable ICU 2.0
  177. */
  178. U_CAPI UDataMemory * U_EXPORT2
  179. udata_open(const char *path, const char *type, const char *name,
  180. UErrorCode *pErrorCode);
  181. /**
  182. * Data loading function.
  183. * This function is used to find and load efficiently data for
  184. * ICU and applications using ICU.
  185. * It provides an abstract interface that allows to specify a data
  186. * type and name to find and load the data.
  187. *
  188. * <p>The implementation depends on platform properties and user preferences
  189. * and may involve loading shared libraries (DLLs), mapping
  190. * files into memory, or fopen()/fread() files.
  191. * It may also involve using static memory or database queries etc.
  192. * Several or all data items may be combined into one entity
  193. * (DLL, memory-mappable file).</p>
  194. *
  195. * <p>The data is always preceded by a header that includes
  196. * a <code>UDataInfo</code> structure.
  197. * The caller's <code>isAcceptable()</code> function is called to make
  198. * sure that the data is useful. It may be called several times if it
  199. * rejects the data and there is more than one location with data
  200. * matching the type and name.</p>
  201. *
  202. * <p>If <code>path==NULL</code>, then ICU data is loaded.
  203. * Otherwise, it is separated into a basename and a basename-less directory string.
  204. * The basename is used as the data package name, and the directory is
  205. * logically prepended to the ICU data directory string.</p>
  206. *
  207. * <p>For details about ICU data loading see the User Guide
  208. * Data Management chapter. (https://unicode-org.github.io/icu/userguide/icu_data/)</p>
  209. *
  210. * @param path Specifies an absolute path and/or a basename for the
  211. * finding of the data in the file system.
  212. * <code>NULL</code> for ICU data.
  213. * @param type A string that specifies the type of data to be loaded.
  214. * For example, resource bundles are loaded with type "res",
  215. * conversion tables with type "cnv".
  216. * This may be <code>NULL</code> or empty.
  217. * @param name A string that specifies the name of the data.
  218. * @param isAcceptable This function is called to verify that loaded data
  219. * is useful for the client code. If it returns false
  220. * for all data items, then <code>udata_openChoice()</code>
  221. * will return with an error.
  222. * @param context Arbitrary parameter to be passed into isAcceptable.
  223. * @param pErrorCode An ICU UErrorCode parameter. It must not be <code>NULL</code>.
  224. * @return A pointer (handle) to a data memory object, or <code>NULL</code>
  225. * if an error occurs. Call <code>udata_getMemory()</code>
  226. * to get a pointer to the actual data.
  227. * @stable ICU 2.0
  228. */
  229. U_CAPI UDataMemory * U_EXPORT2
  230. udata_openChoice(const char *path, const char *type, const char *name,
  231. UDataMemoryIsAcceptable *isAcceptable, void *context,
  232. UErrorCode *pErrorCode);
  233. /**
  234. * Close the data memory.
  235. * This function must be called to allow the system to
  236. * release resources associated with this data memory.
  237. * @param pData The pointer to data memory object
  238. * @stable ICU 2.0
  239. */
  240. U_CAPI void U_EXPORT2
  241. udata_close(UDataMemory *pData);
  242. /**
  243. * Get the pointer to the actual data inside the data memory.
  244. * The data is read-only.
  245. *
  246. * ICU data must be at least 8-aligned, and should be 16-aligned.
  247. *
  248. * @param pData The pointer to data memory object
  249. * @stable ICU 2.0
  250. */
  251. U_CAPI const void * U_EXPORT2
  252. udata_getMemory(UDataMemory *pData);
  253. /**
  254. * Get the information from the data memory header.
  255. * This allows to get access to the header containing
  256. * platform data properties etc. which is not part of
  257. * the data itself and can therefore not be accessed
  258. * via the pointer that <code>udata_getMemory()</code> returns.
  259. *
  260. * @param pData pointer to the data memory object
  261. * @param pInfo pointer to a UDataInfo object;
  262. * its <code>size</code> field must be set correctly,
  263. * typically to <code>sizeof(UDataInfo)</code>.
  264. *
  265. * <code>*pInfo</code> will be filled with the UDataInfo structure
  266. * in the data memory object. If this structure is smaller than
  267. * <code>pInfo->size</code>, then the <code>size</code> will be
  268. * adjusted and only part of the structure will be filled.
  269. * @stable ICU 2.0
  270. */
  271. U_CAPI void U_EXPORT2
  272. udata_getInfo(UDataMemory *pData, UDataInfo *pInfo);
  273. /**
  274. * This function bypasses the normal ICU data loading process and
  275. * allows you to force ICU's system data to come out of a user-specified
  276. * area in memory.
  277. *
  278. * ICU data must be at least 8-aligned, and should be 16-aligned.
  279. * See https://unicode-org.github.io/icu/userguide/icu_data
  280. *
  281. * The format of this data is that of the icu common data file, as is
  282. * generated by the pkgdata tool with mode=common or mode=dll.
  283. * You can read in a whole common mode file and pass the address to the start of the
  284. * data, or (with the appropriate link options) pass in the pointer to
  285. * the data that has been loaded from a dll by the operating system,
  286. * as shown in this code:
  287. *
  288. * extern const char U_IMPORT U_ICUDATA_ENTRY_POINT [];
  289. * // U_ICUDATA_ENTRY_POINT is same as entry point specified to pkgdata tool
  290. * UErrorCode status = U_ZERO_ERROR;
  291. *
  292. * udata_setCommonData(&U_ICUDATA_ENTRY_POINT, &status);
  293. *
  294. * It is important that the declaration be as above. The entry point
  295. * must not be declared as an extern void*.
  296. *
  297. * Starting with ICU 4.4, it is possible to set several data packages,
  298. * one per call to this function.
  299. * udata_open() will look for data in the multiple data packages in the order
  300. * in which they were set.
  301. * The position of the linked-in or default-name ICU .data package in the
  302. * search list depends on when the first data item is loaded that is not contained
  303. * in the already explicitly set packages.
  304. * If data was loaded implicitly before the first call to this function
  305. * (for example, via opening a converter, constructing a UnicodeString
  306. * from default-codepage data, using formatting or collation APIs, etc.),
  307. * then the default data will be first in the list.
  308. *
  309. * This function has no effect on application (non ICU) data. See udata_setAppData()
  310. * for similar functionality for application data.
  311. *
  312. * @param data pointer to ICU common data
  313. * @param err outgoing error status <code>U_USING_DEFAULT_WARNING, U_UNSUPPORTED_ERROR</code>
  314. * @stable ICU 2.0
  315. */
  316. U_CAPI void U_EXPORT2
  317. udata_setCommonData(const void *data, UErrorCode *err);
  318. /**
  319. * This function bypasses the normal ICU data loading process for application-specific
  320. * data and allows you to force the it to come out of a user-specified
  321. * pointer.
  322. *
  323. * ICU data must be at least 8-aligned, and should be 16-aligned.
  324. * See https://unicode-org.github.io/icu/userguide/icu_data
  325. *
  326. * The format of this data is that of the icu common data file, like 'icudt26l.dat'
  327. * or the corresponding shared library (DLL) file.
  328. * The application must read in or otherwise construct an image of the data and then
  329. * pass the address of it to this function.
  330. *
  331. *
  332. * Warning: setAppData will set a U_USING_DEFAULT_WARNING code if
  333. * data with the specified path that has already been opened, or
  334. * if setAppData with the same path has already been called.
  335. * Any such calls to setAppData will have no effect.
  336. *
  337. *
  338. * @param packageName the package name by which the application will refer
  339. * to (open) this data
  340. * @param data pointer to the data
  341. * @param err outgoing error status <code>U_USING_DEFAULT_WARNING, U_UNSUPPORTED_ERROR</code>
  342. * @see udata_setCommonData
  343. * @stable ICU 2.0
  344. */
  345. U_CAPI void U_EXPORT2
  346. udata_setAppData(const char *packageName, const void *data, UErrorCode *err);
  347. /**
  348. * Possible settings for udata_setFileAccess()
  349. * @see udata_setFileAccess
  350. * @stable ICU 3.4
  351. */
  352. typedef enum UDataFileAccess {
  353. /** ICU looks for data in single files first, then in packages. (default) @stable ICU 3.4 */
  354. UDATA_FILES_FIRST,
  355. /** An alias for the default access mode. @stable ICU 3.4 */
  356. UDATA_DEFAULT_ACCESS = UDATA_FILES_FIRST,
  357. /** ICU only loads data from packages, not from single files. @stable ICU 3.4 */
  358. UDATA_ONLY_PACKAGES,
  359. /** ICU loads data from packages first, and only from single files
  360. if the data cannot be found in a package. @stable ICU 3.4 */
  361. UDATA_PACKAGES_FIRST,
  362. /** ICU does not access the file system for data loading. @stable ICU 3.4 */
  363. UDATA_NO_FILES,
  364. #ifndef U_HIDE_DEPRECATED_API
  365. /**
  366. * Number of real UDataFileAccess values.
  367. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  368. */
  369. UDATA_FILE_ACCESS_COUNT
  370. #endif // U_HIDE_DEPRECATED_API
  371. } UDataFileAccess;
  372. /**
  373. * This function may be called to control how ICU loads data. It must be called
  374. * before any ICU data is loaded, including application data loaded with
  375. * ures/ResourceBundle or udata APIs. This function is not multithread safe.
  376. * The results of calling it while other threads are loading data are undefined.
  377. * @param access The type of file access to be used
  378. * @param status Error code.
  379. * @see UDataFileAccess
  380. * @stable ICU 3.4
  381. */
  382. U_CAPI void U_EXPORT2
  383. udata_setFileAccess(UDataFileAccess access, UErrorCode *status);
  384. U_CDECL_END
  385. #if U_SHOW_CPLUSPLUS_API
  386. U_NAMESPACE_BEGIN
  387. /**
  388. * \class LocalUDataMemoryPointer
  389. * "Smart pointer" class, closes a UDataMemory via udata_close().
  390. * For most methods see the LocalPointerBase base class.
  391. *
  392. * @see LocalPointerBase
  393. * @see LocalPointer
  394. * @stable ICU 4.4
  395. */
  396. U_DEFINE_LOCAL_OPEN_POINTER(LocalUDataMemoryPointer, UDataMemory, udata_close);
  397. U_NAMESPACE_END
  398. #endif // U_SHOW_CPLUSPLUS_API
  399. #endif