udata.h 16 KB

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