ustdio.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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) 1998-2015, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File ustdio.h
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 10/16/98 stephen Creation.
  17. * 11/06/98 stephen Modified per code review.
  18. * 03/12/99 stephen Modified for new C API.
  19. * 07/19/99 stephen Minor doc update.
  20. * 02/01/01 george Added sprintf & sscanf with all of its variants
  21. ******************************************************************************
  22. */
  23. #ifndef USTDIO_H
  24. #define USTDIO_H
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include "unicode/utypes.h"
  28. #include "unicode/ucnv.h"
  29. #include "unicode/utrans.h"
  30. #include "unicode/unum.h"
  31. #if U_SHOW_CPLUSPLUS_API
  32. #include "unicode/localpointer.h"
  33. #endif // U_SHOW_CPLUSPLUS_API
  34. #if !UCONFIG_NO_CONVERSION
  35. /*
  36. TODO
  37. The following is a small list as to what is currently wrong/suggestions for
  38. ustdio.
  39. * Make sure that * in the scanf format specification works for all formats.
  40. * Each UFILE takes up at least 2KB.
  41. Look into adding setvbuf() for configurable buffers.
  42. * This library does buffering. The OS should do this for us already. Check on
  43. this, and remove it from this library, if this is the case. Double buffering
  44. wastes a lot of time and space.
  45. * Test stdin and stdout with the u_f* functions
  46. * Testing should be done for reading and writing multi-byte encodings,
  47. and make sure that a character that is contained across buffer boundaries
  48. works even for incomplete characters.
  49. * Make sure that the last character is flushed when the file/string is closed.
  50. * snprintf should follow the C99 standard for the return value, which is
  51. return the number of characters (excluding the trailing '\0')
  52. which would have been written to the destination string regardless
  53. of available space. This is like pre-flighting.
  54. * Everything that uses %s should do what operator>> does for UnicodeString.
  55. It should convert one byte at a time, and once a character is
  56. converted then check to see if it's whitespace or in the scanset.
  57. If it's whitespace or in the scanset, put all the bytes back (do nothing
  58. for sprintf/sscanf).
  59. * If bad string data is encountered, make sure that the function fails
  60. without memory leaks and the unconvertable characters are valid
  61. substitution or are escaped characters.
  62. * u_fungetc() can't unget a character when it's at the beginning of the
  63. internal conversion buffer. For example, read the buffer size # of
  64. characters, and then ungetc to get the previous character that was
  65. at the end of the last buffer.
  66. * u_fflush() and u_fclose should return an int32_t like C99 functions.
  67. 0 is returned if the operation was successful and EOF otherwise.
  68. * u_fsettransliterator does not support U_READ side of transliteration.
  69. * The format specifier should limit the size of a format or honor it in
  70. order to prevent buffer overruns. (e.g. %256.256d).
  71. * u_fread and u_fwrite don't exist. They're needed for reading and writing
  72. data structures without any conversion.
  73. * u_file_read and u_file_write are used for writing strings. u_fgets and
  74. u_fputs or u_fread and u_fwrite should be used to do this.
  75. * The width parameter for all scanf formats, including scanset, needs
  76. better testing. This prevents buffer overflows.
  77. * Figure out what is suppose to happen when a codepage is changed midstream.
  78. Maybe a flush or a rewind are good enough.
  79. * Make sure that a UFile opened with "rw" can be used after using
  80. u_fflush with a u_frewind.
  81. * scanf(%i) should detect what type of number to use.
  82. * Add more testing of the alternate format, %#
  83. * Look at newline handling of fputs/puts
  84. * Think more about codeunit/codepoint error handling/support in %S,%s,%C,%c,%[]
  85. * Complete the file documentation with proper doxygen formatting.
  86. See http://oss.software.ibm.com/pipermail/icu/2003-July/005647.html
  87. */
  88. /**
  89. * \file
  90. * \brief C API: Unicode stdio-like API
  91. *
  92. * <h2>Unicode stdio-like C API</h2>
  93. *
  94. * <p>This API provides an stdio-like API wrapper around ICU's other
  95. * formatting and parsing APIs. It is meant to ease the transition of adding
  96. * Unicode support to a preexisting applications using stdio. The following
  97. * is a small list of noticeable differences between stdio and ICU I/O's
  98. * ustdio implementation.</p>
  99. *
  100. * <ul>
  101. * <li>Locale specific formatting and parsing is only done with file IO.</li>
  102. * <li>u_fstropen can be used to simulate file IO with strings.
  103. * This is similar to the iostream API, and it allows locale specific
  104. * formatting and parsing to be used.</li>
  105. * <li>This API provides uniform formatting and parsing behavior between
  106. * platforms (unlike the standard stdio implementations found on various
  107. * platforms).</li>
  108. * <li>This API is better suited for text data handling than binary data
  109. * handling when compared to the typical stdio implementation.</li>
  110. * <li>You can specify a Transliterator while using the file IO.</li>
  111. * <li>You can specify a file's codepage separately from the default
  112. * system codepage.</li>
  113. * </ul>
  114. *
  115. * <h2>Formatting and Parsing Specification</h2>
  116. *
  117. * General printf format:<br>
  118. * %[format modifier][width][.precision][type modifier][format]
  119. *
  120. * General scanf format:<br>
  121. * %[*][format modifier][width][type modifier][format]
  122. *
  123. <table cellspacing="3">
  124. <tr><td>format</td><td>default<br>printf<br>type</td><td>default<br>scanf<br>type</td><td>description</td></tr>
  125. <tr><td>%E</td><td>double</td><td>float</td><td>Scientific with an uppercase exponent</td></tr>
  126. <tr><td>%e</td><td>double</td><td>float</td><td>Scientific with a lowercase exponent</td></tr>
  127. <tr><td>%G</td><td>double</td><td>float</td><td>Use %E or %f for best format</td></tr>
  128. <tr><td>%g</td><td>double</td><td>float</td><td>Use %e or %f for best format</td></tr>
  129. <tr><td>%f</td><td>double</td><td>float</td><td>Simple floating point without the exponent</td></tr>
  130. <tr><td>%X</td><td>int32_t</td><td>int32_t</td><td>ustdio special uppercase hex radix formatting</td></tr>
  131. <tr><td>%x</td><td>int32_t</td><td>int32_t</td><td>ustdio special lowercase hex radix formatting</td></tr>
  132. <tr><td>%d</td><td>int32_t</td><td>int32_t</td><td>Decimal format</td></tr>
  133. <tr><td>%i</td><td>int32_t</td><td>int32_t</td><td>Same as %d</td></tr>
  134. <tr><td>%n</td><td>int32_t</td><td>int32_t</td><td>count (write the number of UTF-16 codeunits read/written)</td></tr>
  135. <tr><td>%o</td><td>int32_t</td><td>int32_t</td><td>ustdio special octal radix formatting</td></tr>
  136. <tr><td>%u</td><td>uint32_t</td><td>uint32_t</td><td>Decimal format</td></tr>
  137. <tr><td>%p</td><td>void *</td><td>void *</td><td>Prints the pointer value</td></tr>
  138. <tr><td>%s</td><td>char *</td><td>char *</td><td>Use default converter or specified converter from fopen</td></tr>
  139. <tr><td>%c</td><td>char</td><td>char</td><td>Use default converter or specified converter from fopen<br>
  140. When width is specified for scanf, this acts like a non-NULL-terminated char * string.<br>
  141. By default, only one char is written.</td></tr>
  142. <tr><td>%S</td><td>UChar *</td><td>UChar *</td><td>Null terminated UTF-16 string</td></tr>
  143. <tr><td>%C</td><td>UChar</td><td>UChar</td><td>16-bit Unicode code unit<br>
  144. When width is specified for scanf, this acts like a non-NULL-terminated UChar * string<br>
  145. By default, only one codepoint is written.</td></tr>
  146. <tr><td>%[]</td><td>&nbsp;</td><td>UChar *</td><td>Null terminated UTF-16 string which contains the filtered set of characters specified by the UnicodeSet</td></tr>
  147. <tr><td>%%</td><td>&nbsp;</td><td>&nbsp;</td><td>Show a percent sign</td></tr>
  148. </table>
  149. Format modifiers
  150. <table>
  151. <tr><td>modifier</td><td>formats</td><td>type</td><td>comments</td></tr>
  152. <tr><td>%h</td><td>%d, %i, %o, %x</td><td>int16_t</td><td>short format</td></tr>
  153. <tr><td>%h</td><td>%u</td><td>uint16_t</td><td>short format</td></tr>
  154. <tr><td>%h</td><td>c</td><td>char</td><td><b>(Unimplemented)</b> Use invariant converter</td></tr>
  155. <tr><td>%h</td><td>s</td><td>char *</td><td><b>(Unimplemented)</b> Use invariant converter</td></tr>
  156. <tr><td>%h</td><td>C</td><td>char</td><td><b>(Unimplemented)</b> 8-bit Unicode code unit</td></tr>
  157. <tr><td>%h</td><td>S</td><td>char *</td><td><b>(Unimplemented)</b> Null terminated UTF-8 string</td></tr>
  158. <tr><td>%l</td><td>%d, %i, %o, %x</td><td>int32_t</td><td>long format (no effect)</td></tr>
  159. <tr><td>%l</td><td>%u</td><td>uint32_t</td><td>long format (no effect)</td></tr>
  160. <tr><td>%l</td><td>c</td><td>N/A</td><td><b>(Unimplemented)</b> Reserved for future implementation</td></tr>
  161. <tr><td>%l</td><td>s</td><td>N/A</td><td><b>(Unimplemented)</b> Reserved for future implementation</td></tr>
  162. <tr><td>%l</td><td>C</td><td>UChar32</td><td><b>(Unimplemented)</b> 32-bit Unicode code unit</td></tr>
  163. <tr><td>%l</td><td>S</td><td>UChar32 *</td><td><b>(Unimplemented)</b> Null terminated UTF-32 string</td></tr>
  164. <tr><td>%ll</td><td>%d, %i, %o, %x</td><td>int64_t</td><td>long long format</td></tr>
  165. <tr><td>%ll</td><td>%u</td><td>uint64_t</td><td><b>(Unimplemented)</b> long long format</td></tr>
  166. <tr><td>%-</td><td><i>all</i></td><td>N/A</td><td>Left justify</td></tr>
  167. <tr><td>%+</td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Always show the plus or minus sign. Needs data for plus sign.</td></tr>
  168. <tr><td>% </td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Instead of a "+" output a blank character for positive numbers.</td></tr>
  169. <tr><td>%#</td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Precede octal value with 0, hex with 0x and show the
  170. decimal point for floats.</td></tr>
  171. <tr><td>%<i>n</i></td><td><i>all</i></td><td>N/A</td><td>Width of input/output. num is an actual number from 0 to
  172. some large number.</td></tr>
  173. <tr><td>%.<i>n</i></td><td>%e, %f, %g, %E, %F, %G</td><td>N/A</td><td>Significant digits precision. num is an actual number from
  174. 0 to some large number.<br>If * is used in printf, then the precision is passed in as an argument before the number to be formatted.</td></tr>
  175. </table>
  176. printf modifier
  177. %* int32_t Next argument after this one specifies the width
  178. scanf modifier
  179. %* N/A This field is scanned, but not stored
  180. <p>If you are using this C API instead of the ustream.h API for C++,
  181. you can use one of the following u_fprintf examples to display a UnicodeString.</p>
  182. <pre><code>
  183. UFILE *out = u_finit(stdout, NULL, NULL);
  184. UnicodeString string1("string 1");
  185. UnicodeString string2("string 2");
  186. u_fprintf(out, "%S\n", string1.getTerminatedBuffer());
  187. u_fprintf(out, "%.*S\n", string2.length(), string2.getBuffer());
  188. u_fclose(out);
  189. </code></pre>
  190. */
  191. /**
  192. * When an end of file is encountered, this value can be returned.
  193. * @see u_fgetc
  194. * @stable 3.0
  195. */
  196. #define U_EOF 0xFFFF
  197. /** Forward declaration of a Unicode-aware file @stable 3.0 */
  198. typedef struct UFILE UFILE;
  199. /**
  200. * Enum for which direction of stream a transliterator applies to.
  201. * @see u_fsettransliterator
  202. * @stable ICU 3.0
  203. */
  204. typedef enum {
  205. U_READ = 1,
  206. U_WRITE = 2,
  207. U_READWRITE =3 /* == (U_READ | U_WRITE) */
  208. } UFileDirection;
  209. /**
  210. * Open a UFILE.
  211. * A UFILE is a wrapper around a FILE* that is locale and codepage aware.
  212. * That is, data written to a UFILE will be formatted using the conventions
  213. * specified by that UFILE's Locale; this data will be in the character set
  214. * specified by that UFILE's codepage.
  215. * @param filename The name of the file to open. Must be 0-terminated.
  216. * @param perm The read/write permission for the UFILE; one of "r", "w", "rw"
  217. * @param locale The locale whose conventions will be used to format
  218. * and parse output. If this parameter is NULL, the default locale will
  219. * be used.
  220. * @param codepage The codepage in which data will be written to and
  221. * read from the file. If this parameter is NULL the system default codepage
  222. * will be used.
  223. * @return A new UFILE, or NULL if an error occurred.
  224. * @stable ICU 3.0
  225. */
  226. U_CAPI UFILE* U_EXPORT2
  227. u_fopen(const char *filename,
  228. const char *perm,
  229. const char *locale,
  230. const char *codepage);
  231. /**
  232. * Open a UFILE with a UChar* filename
  233. * A UFILE is a wrapper around a FILE* that is locale and codepage aware.
  234. * That is, data written to a UFILE will be formatted using the conventions
  235. * specified by that UFILE's Locale; this data will be in the character set
  236. * specified by that UFILE's codepage.
  237. * @param filename The name of the file to open. Must be 0-terminated.
  238. * @param perm The read/write permission for the UFILE; one of "r", "w", "rw"
  239. * @param locale The locale whose conventions will be used to format
  240. * and parse output. If this parameter is NULL, the default locale will
  241. * be used.
  242. * @param codepage The codepage in which data will be written to and
  243. * read from the file. If this parameter is NULL the system default codepage
  244. * will be used.
  245. * @return A new UFILE, or NULL if an error occurred.
  246. * @stable ICU 54
  247. */
  248. U_CAPI UFILE* U_EXPORT2
  249. u_fopen_u(const UChar *filename,
  250. const char *perm,
  251. const char *locale,
  252. const char *codepage);
  253. /**
  254. * Open a UFILE on top of an existing FILE* stream. The FILE* stream
  255. * ownership remains with the caller. To have the UFILE take over
  256. * ownership and responsibility for the FILE* stream, use the
  257. * function u_fadopt.
  258. * @param f The FILE* to which this UFILE will attach and use.
  259. * @param locale The locale whose conventions will be used to format
  260. * and parse output. If this parameter is NULL, the default locale will
  261. * be used.
  262. * @param codepage The codepage in which data will be written to and
  263. * read from the file. If this parameter is NULL, data will be written and
  264. * read using the default codepage for <TT>locale</TT>, unless <TT>locale</TT>
  265. * is NULL, in which case the system default codepage will be used.
  266. * @return A new UFILE, or NULL if an error occurred.
  267. * @stable ICU 3.0
  268. */
  269. U_CAPI UFILE* U_EXPORT2
  270. u_finit(FILE *f,
  271. const char *locale,
  272. const char *codepage);
  273. /**
  274. * Open a UFILE on top of an existing FILE* stream. The FILE* stream
  275. * ownership is transferred to the new UFILE. It will be closed when the
  276. * UFILE is closed.
  277. * @param f The FILE* which this UFILE will take ownership of.
  278. * @param locale The locale whose conventions will be used to format
  279. * and parse output. If this parameter is NULL, the default locale will
  280. * be used.
  281. * @param codepage The codepage in which data will be written to and
  282. * read from the file. If this parameter is NULL, data will be written and
  283. * read using the default codepage for <TT>locale</TT>, unless <TT>locale</TT>
  284. * is NULL, in which case the system default codepage will be used.
  285. * @return A new UFILE, or NULL if an error occurred. If an error occurs
  286. * the ownership of the FILE* stream remains with the caller.
  287. * @stable ICU 4.4
  288. */
  289. U_CAPI UFILE* U_EXPORT2
  290. u_fadopt(FILE *f,
  291. const char *locale,
  292. const char *codepage);
  293. /**
  294. * Create a UFILE that can be used for localized formatting or parsing.
  295. * The u_sprintf and u_sscanf functions do not read or write numbers for a
  296. * specific locale. The ustdio.h file functions can be used on this UFILE.
  297. * The string is usable once u_fclose or u_fflush has been called on the
  298. * returned UFILE.
  299. * @param stringBuf The string used for reading or writing.
  300. * @param capacity The number of code units available for use in stringBuf
  301. * @param locale The locale whose conventions will be used to format
  302. * and parse output. If this parameter is NULL, the default locale will
  303. * be used.
  304. * @return A new UFILE, or NULL if an error occurred.
  305. * @stable ICU 3.0
  306. */
  307. U_CAPI UFILE* U_EXPORT2
  308. u_fstropen(UChar *stringBuf,
  309. int32_t capacity,
  310. const char *locale);
  311. /**
  312. * Close a UFILE. Implies u_fflush first.
  313. * @param file The UFILE to close.
  314. * @stable ICU 3.0
  315. * @see u_fflush
  316. */
  317. U_CAPI void U_EXPORT2
  318. u_fclose(UFILE *file);
  319. #if U_SHOW_CPLUSPLUS_API
  320. U_NAMESPACE_BEGIN
  321. /**
  322. * \class LocalUFILEPointer
  323. * "Smart pointer" class, closes a UFILE via u_fclose().
  324. * For most methods see the LocalPointerBase base class.
  325. *
  326. * @see LocalPointerBase
  327. * @see LocalPointer
  328. * @stable ICU 4.4
  329. */
  330. U_DEFINE_LOCAL_OPEN_POINTER(LocalUFILEPointer, UFILE, u_fclose);
  331. U_NAMESPACE_END
  332. #endif
  333. /**
  334. * Tests if the UFILE is at the end of the file stream.
  335. * @param f The UFILE from which to read.
  336. * @return Returns true after the first read operation that attempts to
  337. * read past the end of the file. It returns false if the current position is
  338. * not end of file.
  339. * @stable ICU 3.0
  340. */
  341. U_CAPI UBool U_EXPORT2
  342. u_feof(UFILE *f);
  343. /**
  344. * Flush output of a UFILE. Implies a flush of
  345. * converter/transliterator state. (That is, a logical break is
  346. * made in the output stream - for example if a different type of
  347. * output is desired.) The underlying OS level file is also flushed.
  348. * Note that for a stateful encoding, the converter may write additional
  349. * bytes to return the stream to default state.
  350. * @param file The UFILE to flush.
  351. * @stable ICU 3.0
  352. */
  353. U_CAPI void U_EXPORT2
  354. u_fflush(UFILE *file);
  355. /**
  356. * Rewind the file pointer to the beginning of the file.
  357. * @param file The UFILE to rewind.
  358. * @stable ICU 3.0
  359. */
  360. U_CAPI void
  361. u_frewind(UFILE *file);
  362. /**
  363. * Get the FILE* associated with a UFILE.
  364. * @param f The UFILE
  365. * @return A FILE*, owned by the UFILE. (The FILE <EM>must not</EM> be modified or closed)
  366. * @stable ICU 3.0
  367. */
  368. U_CAPI FILE* U_EXPORT2
  369. u_fgetfile(UFILE *f);
  370. #if !UCONFIG_NO_FORMATTING
  371. /**
  372. * Get the locale whose conventions are used to format and parse output.
  373. * This is the same locale passed in the preceding call to<TT>u_fsetlocale</TT>
  374. * or <TT>u_fopen</TT>.
  375. * @param file The UFILE to set.
  376. * @return The locale whose conventions are used to format and parse output.
  377. * @stable ICU 3.0
  378. */
  379. U_CAPI const char* U_EXPORT2
  380. u_fgetlocale(UFILE *file);
  381. /**
  382. * Set the locale whose conventions will be used to format and parse output.
  383. * @param locale The locale whose conventions will be used to format
  384. * and parse output.
  385. * @param file The UFILE to query.
  386. * @return NULL if successful, otherwise a negative number.
  387. * @stable ICU 3.0
  388. */
  389. U_CAPI int32_t U_EXPORT2
  390. u_fsetlocale(UFILE *file,
  391. const char *locale);
  392. #endif
  393. /**
  394. * Get the codepage in which data is written to and read from the UFILE.
  395. * This is the same codepage passed in the preceding call to
  396. * <TT>u_fsetcodepage</TT> or <TT>u_fopen</TT>.
  397. * @param file The UFILE to query.
  398. * @return The codepage in which data is written to and read from the UFILE,
  399. * or NULL if an error occurred.
  400. * @stable ICU 3.0
  401. */
  402. U_CAPI const char* U_EXPORT2
  403. u_fgetcodepage(UFILE *file);
  404. /**
  405. * Set the codepage in which data will be written to and read from the UFILE.
  406. * All Unicode data written to the UFILE will be converted to this codepage
  407. * before it is written to the underlying FILE*. It it generally a bad idea to
  408. * mix codepages within a file. This should only be called right
  409. * after opening the <TT>UFile</TT>, or after calling <TT>u_frewind</TT>.
  410. * @param codepage The codepage in which data will be written to
  411. * and read from the file. For example <TT>"latin-1"</TT> or <TT>"ibm-943"</TT>.
  412. * A value of NULL means the default codepage for the UFILE's current
  413. * locale will be used.
  414. * @param file The UFILE to set.
  415. * @return 0 if successful, otherwise a negative number.
  416. * @see u_frewind
  417. * @stable ICU 3.0
  418. */
  419. U_CAPI int32_t U_EXPORT2
  420. u_fsetcodepage(const char *codepage,
  421. UFILE *file);
  422. /**
  423. * Returns an alias to the converter being used for this file.
  424. * @param f The UFILE to get the value from
  425. * @return alias to the converter (The converter <EM>must not</EM> be modified or closed)
  426. * @stable ICU 3.0
  427. */
  428. U_CAPI UConverter* U_EXPORT2 u_fgetConverter(UFILE *f);
  429. #if !UCONFIG_NO_FORMATTING
  430. /**
  431. * Returns an alias to the number formatter being used for this file.
  432. * @param f The UFILE to get the value from
  433. * @return alias to the number formatter (The formatter <EM>must not</EM> be modified or closed)
  434. * @stable ICU 51
  435. */
  436. U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *f);
  437. /* Output functions */
  438. /**
  439. * Write formatted data to <TT>stdout</TT>.
  440. * @param patternSpecification A pattern specifying how <TT>u_printf</TT> will
  441. * interpret the variable arguments received and format the data.
  442. * @return The number of Unicode characters written to <TT>stdout</TT>
  443. * @stable ICU 49
  444. */
  445. U_CAPI int32_t U_EXPORT2
  446. u_printf(const char *patternSpecification,
  447. ... );
  448. /**
  449. * Write formatted data to a UFILE.
  450. * @param f The UFILE to which to write.
  451. * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will
  452. * interpret the variable arguments received and format the data.
  453. * @return The number of Unicode characters written to <TT>f</TT>.
  454. * @stable ICU 3.0
  455. */
  456. U_CAPI int32_t U_EXPORT2
  457. u_fprintf(UFILE *f,
  458. const char *patternSpecification,
  459. ... );
  460. /**
  461. * Write formatted data to a UFILE.
  462. * This is identical to <TT>u_fprintf</TT>, except that it will
  463. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  464. * @param f The UFILE to which to write.
  465. * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will
  466. * interpret the variable arguments received and format the data.
  467. * @param ap The argument list to use.
  468. * @return The number of Unicode characters written to <TT>f</TT>.
  469. * @see u_fprintf
  470. * @stable ICU 3.0
  471. */
  472. U_CAPI int32_t U_EXPORT2
  473. u_vfprintf(UFILE *f,
  474. const char *patternSpecification,
  475. va_list ap);
  476. /**
  477. * Write formatted data to <TT>stdout</TT>.
  478. * @param patternSpecification A pattern specifying how <TT>u_printf_u</TT> will
  479. * interpret the variable arguments received and format the data.
  480. * @return The number of Unicode characters written to <TT>stdout</TT>
  481. * @stable ICU 49
  482. */
  483. U_CAPI int32_t U_EXPORT2
  484. u_printf_u(const UChar *patternSpecification,
  485. ... );
  486. /**
  487. * Get a UFILE for <TT>stdout</TT>.
  488. * @return UFILE that writes to <TT>stdout</TT>
  489. * @stable ICU 49
  490. */
  491. U_CAPI UFILE * U_EXPORT2
  492. u_get_stdout(void);
  493. /**
  494. * Write formatted data to a UFILE.
  495. * @param f The UFILE to which to write.
  496. * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will
  497. * interpret the variable arguments received and format the data.
  498. * @return The number of Unicode characters written to <TT>f</TT>.
  499. * @stable ICU 3.0
  500. */
  501. U_CAPI int32_t U_EXPORT2
  502. u_fprintf_u(UFILE *f,
  503. const UChar *patternSpecification,
  504. ... );
  505. /**
  506. * Write formatted data to a UFILE.
  507. * This is identical to <TT>u_fprintf_u</TT>, except that it will
  508. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  509. * @param f The UFILE to which to write.
  510. * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will
  511. * interpret the variable arguments received and format the data.
  512. * @param ap The argument list to use.
  513. * @return The number of Unicode characters written to <TT>f</TT>.
  514. * @see u_fprintf_u
  515. * @stable ICU 3.0
  516. */
  517. U_CAPI int32_t U_EXPORT2
  518. u_vfprintf_u(UFILE *f,
  519. const UChar *patternSpecification,
  520. va_list ap);
  521. #endif
  522. /**
  523. * Write a Unicode to a UFILE. The null (U+0000) terminated UChar*
  524. * <TT>s</TT> will be written to <TT>f</TT>, excluding the NULL terminator.
  525. * A newline will be added to <TT>f</TT>.
  526. * @param s The UChar* to write.
  527. * @param f The UFILE to which to write.
  528. * @return A non-negative number if successful, EOF otherwise.
  529. * @see u_file_write
  530. * @stable ICU 3.0
  531. */
  532. U_CAPI int32_t U_EXPORT2
  533. u_fputs(const UChar *s,
  534. UFILE *f);
  535. /**
  536. * Write a UChar to a UFILE.
  537. * @param uc The UChar to write.
  538. * @param f The UFILE to which to write.
  539. * @return The character written if successful, EOF otherwise.
  540. * @stable ICU 3.0
  541. */
  542. U_CAPI UChar32 U_EXPORT2
  543. u_fputc(UChar32 uc,
  544. UFILE *f);
  545. /**
  546. * Write Unicode to a UFILE.
  547. * The ustring passed in will be converted to the UFILE's underlying
  548. * codepage before it is written.
  549. * @param ustring A pointer to the Unicode data to write.
  550. * @param count The number of Unicode characters to write
  551. * @param f The UFILE to which to write.
  552. * @return The number of Unicode characters written.
  553. * @see u_fputs
  554. * @stable ICU 3.0
  555. */
  556. U_CAPI int32_t U_EXPORT2
  557. u_file_write(const UChar *ustring,
  558. int32_t count,
  559. UFILE *f);
  560. /* Input functions */
  561. #if !UCONFIG_NO_FORMATTING
  562. /**
  563. * Read formatted data from a UFILE.
  564. * @param f The UFILE from which to read.
  565. * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will
  566. * interpret the variable arguments received and parse the data.
  567. * @return The number of items successfully converted and assigned, or EOF
  568. * if an error occurred.
  569. * @stable ICU 3.0
  570. */
  571. U_CAPI int32_t U_EXPORT2
  572. u_fscanf(UFILE *f,
  573. const char *patternSpecification,
  574. ... );
  575. /**
  576. * Read formatted data from a UFILE.
  577. * This is identical to <TT>u_fscanf</TT>, except that it will
  578. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  579. * @param f The UFILE from which to read.
  580. * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will
  581. * interpret the variable arguments received and parse the data.
  582. * @param ap The argument list to use.
  583. * @return The number of items successfully converted and assigned, or EOF
  584. * if an error occurred.
  585. * @see u_fscanf
  586. * @stable ICU 3.0
  587. */
  588. U_CAPI int32_t U_EXPORT2
  589. u_vfscanf(UFILE *f,
  590. const char *patternSpecification,
  591. va_list ap);
  592. /**
  593. * Read formatted data from a UFILE.
  594. * @param f The UFILE from which to read.
  595. * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will
  596. * interpret the variable arguments received and parse the data.
  597. * @return The number of items successfully converted and assigned, or EOF
  598. * if an error occurred.
  599. * @stable ICU 3.0
  600. */
  601. U_CAPI int32_t U_EXPORT2
  602. u_fscanf_u(UFILE *f,
  603. const UChar *patternSpecification,
  604. ... );
  605. /**
  606. * Read formatted data from a UFILE.
  607. * This is identical to <TT>u_fscanf_u</TT>, except that it will
  608. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  609. * @param f The UFILE from which to read.
  610. * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will
  611. * interpret the variable arguments received and parse the data.
  612. * @param ap The argument list to use.
  613. * @return The number of items successfully converted and assigned, or EOF
  614. * if an error occurred.
  615. * @see u_fscanf_u
  616. * @stable ICU 3.0
  617. */
  618. U_CAPI int32_t U_EXPORT2
  619. u_vfscanf_u(UFILE *f,
  620. const UChar *patternSpecification,
  621. va_list ap);
  622. #endif
  623. /**
  624. * Read one line of text into a UChar* string from a UFILE. The newline
  625. * at the end of the line is read into the string. The string is always
  626. * null terminated
  627. * @param f The UFILE from which to read.
  628. * @param n The maximum number of characters - 1 to read.
  629. * @param s The UChar* to receive the read data. Characters will be
  630. * stored successively in <TT>s</TT> until a newline or EOF is
  631. * reached. A null character (U+0000) will be appended to <TT>s</TT>.
  632. * @return A pointer to <TT>s</TT>, or NULL if no characters were available.
  633. * @stable ICU 3.0
  634. */
  635. U_CAPI UChar* U_EXPORT2
  636. u_fgets(UChar *s,
  637. int32_t n,
  638. UFILE *f);
  639. /**
  640. * Read a UChar from a UFILE. It is recommended that <TT>u_fgetcx</TT>
  641. * used instead for proper parsing functions, but sometimes reading
  642. * code units is needed instead of codepoints.
  643. *
  644. * @param f The UFILE from which to read.
  645. * @return The UChar value read, or U+FFFF if no character was available.
  646. * @stable ICU 3.0
  647. */
  648. U_CAPI UChar U_EXPORT2
  649. u_fgetc(UFILE *f);
  650. /**
  651. * Read a UChar32 from a UFILE.
  652. *
  653. * @param f The UFILE from which to read.
  654. * @return The UChar32 value read, or U_EOF if no character was
  655. * available, or U+FFFFFFFF if an ill-formed character was
  656. * encountered.
  657. * @see u_unescape()
  658. * @stable ICU 3.0
  659. */
  660. U_CAPI UChar32 U_EXPORT2
  661. u_fgetcx(UFILE *f);
  662. /**
  663. * Unget a UChar from a UFILE.
  664. * If this function is not the first to operate on <TT>f</TT> after a call
  665. * to <TT>u_fgetc</TT>, the results are undefined.
  666. * If this function is passed a character that was not received from the
  667. * previous <TT>u_fgetc</TT> or <TT>u_fgetcx</TT> call, the results are undefined.
  668. * @param c The UChar to put back on the stream.
  669. * @param f The UFILE to receive <TT>c</TT>.
  670. * @return The UChar32 value put back if successful, U_EOF otherwise.
  671. * @stable ICU 3.0
  672. */
  673. U_CAPI UChar32 U_EXPORT2
  674. u_fungetc(UChar32 c,
  675. UFILE *f);
  676. /**
  677. * Read Unicode from a UFILE.
  678. * Bytes will be converted from the UFILE's underlying codepage, with
  679. * subsequent conversion to Unicode. The data will not be NULL terminated.
  680. * @param chars A pointer to receive the Unicode data.
  681. * @param count The number of Unicode characters to read.
  682. * @param f The UFILE from which to read.
  683. * @return The number of Unicode characters read.
  684. * @stable ICU 3.0
  685. */
  686. U_CAPI int32_t U_EXPORT2
  687. u_file_read(UChar *chars,
  688. int32_t count,
  689. UFILE *f);
  690. #if !UCONFIG_NO_TRANSLITERATION
  691. /**
  692. * Set a transliterator on the UFILE. The transliterator will be owned by the
  693. * UFILE.
  694. * @param file The UFILE to set transliteration on
  695. * @param adopt The UTransliterator to set. Can be NULL, which will
  696. * mean that no transliteration is used.
  697. * @param direction either U_READ, U_WRITE, or U_READWRITE - sets
  698. * which direction the transliterator is to be applied to. If
  699. * U_READWRITE, the "Read" transliteration will be in the inverse
  700. * direction.
  701. * @param status ICU error code.
  702. * @return The previously set transliterator, owned by the
  703. * caller. If U_READWRITE is specified, only the WRITE transliterator
  704. * is returned. In most cases, the caller should call utrans_close()
  705. * on the result of this function.
  706. * @stable ICU 3.0
  707. */
  708. U_CAPI UTransliterator* U_EXPORT2
  709. u_fsettransliterator(UFILE *file, UFileDirection direction,
  710. UTransliterator *adopt, UErrorCode *status);
  711. #endif
  712. /* Output string functions */
  713. #if !UCONFIG_NO_FORMATTING
  714. /**
  715. * Write formatted data to a Unicode string.
  716. *
  717. * @param buffer The Unicode String to which to write.
  718. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  719. * interpret the variable arguments received and format the data.
  720. * @return The number of Unicode code units written to <TT>buffer</TT>. This
  721. * does not include the terminating null character.
  722. * @stable ICU 3.0
  723. */
  724. U_CAPI int32_t U_EXPORT2
  725. u_sprintf(UChar *buffer,
  726. const char *patternSpecification,
  727. ... );
  728. /**
  729. * Write formatted data to a Unicode string. When the number of code units
  730. * required to store the data exceeds <TT>count</TT>, then <TT>count</TT> code
  731. * units of data are stored in <TT>buffer</TT> and a negative value is
  732. * returned. When the number of code units required to store the data equals
  733. * <TT>count</TT>, the string is not null terminated and <TT>count</TT> is
  734. * returned.
  735. *
  736. * @param buffer The Unicode String to which to write.
  737. * @param count The number of code units to read.
  738. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  739. * interpret the variable arguments received and format the data.
  740. * @return The number of Unicode characters that would have been written to
  741. * <TT>buffer</TT> had count been sufficiently large. This does not include
  742. * the terminating null character.
  743. * @stable ICU 3.0
  744. */
  745. U_CAPI int32_t U_EXPORT2
  746. u_snprintf(UChar *buffer,
  747. int32_t count,
  748. const char *patternSpecification,
  749. ... );
  750. /**
  751. * Write formatted data to a Unicode string.
  752. * This is identical to <TT>u_sprintf</TT>, except that it will
  753. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  754. *
  755. * @param buffer The Unicode string to which to write.
  756. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  757. * interpret the variable arguments received and format the data.
  758. * @param ap The argument list to use.
  759. * @return The number of Unicode characters written to <TT>buffer</TT>.
  760. * @see u_sprintf
  761. * @stable ICU 3.0
  762. */
  763. U_CAPI int32_t U_EXPORT2
  764. u_vsprintf(UChar *buffer,
  765. const char *patternSpecification,
  766. va_list ap);
  767. /**
  768. * Write formatted data to a Unicode string.
  769. * This is identical to <TT>u_snprintf</TT>, except that it will
  770. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.<br><br>
  771. * When the number of code units required to store the data exceeds
  772. * <TT>count</TT>, then <TT>count</TT> code units of data are stored in
  773. * <TT>buffer</TT> and a negative value is returned. When the number of code
  774. * units required to store the data equals <TT>count</TT>, the string is not
  775. * null terminated and <TT>count</TT> is returned.
  776. *
  777. * @param buffer The Unicode string to which to write.
  778. * @param count The number of code units to read.
  779. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  780. * interpret the variable arguments received and format the data.
  781. * @param ap The argument list to use.
  782. * @return The number of Unicode characters that would have been written to
  783. * <TT>buffer</TT> had count been sufficiently large.
  784. * @see u_sprintf
  785. * @stable ICU 3.0
  786. */
  787. U_CAPI int32_t U_EXPORT2
  788. u_vsnprintf(UChar *buffer,
  789. int32_t count,
  790. const char *patternSpecification,
  791. va_list ap);
  792. /**
  793. * Write formatted data to a Unicode string.
  794. *
  795. * @param buffer The Unicode string to which to write.
  796. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  797. * interpret the variable arguments received and format the data.
  798. * @return The number of Unicode characters written to <TT>buffer</TT>.
  799. * @stable ICU 3.0
  800. */
  801. U_CAPI int32_t U_EXPORT2
  802. u_sprintf_u(UChar *buffer,
  803. const UChar *patternSpecification,
  804. ... );
  805. /**
  806. * Write formatted data to a Unicode string. When the number of code units
  807. * required to store the data exceeds <TT>count</TT>, then <TT>count</TT> code
  808. * units of data are stored in <TT>buffer</TT> and a negative value is
  809. * returned. When the number of code units required to store the data equals
  810. * <TT>count</TT>, the string is not null terminated and <TT>count</TT> is
  811. * returned.
  812. *
  813. * @param buffer The Unicode string to which to write.
  814. * @param count The number of code units to read.
  815. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  816. * interpret the variable arguments received and format the data.
  817. * @return The number of Unicode characters that would have been written to
  818. * <TT>buffer</TT> had count been sufficiently large.
  819. * @stable ICU 3.0
  820. */
  821. U_CAPI int32_t U_EXPORT2
  822. u_snprintf_u(UChar *buffer,
  823. int32_t count,
  824. const UChar *patternSpecification,
  825. ... );
  826. /**
  827. * Write formatted data to a Unicode string.
  828. * This is identical to <TT>u_sprintf_u</TT>, except that it will
  829. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  830. *
  831. * @param buffer The Unicode string to which to write.
  832. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  833. * interpret the variable arguments received and format the data.
  834. * @param ap The argument list to use.
  835. * @return The number of Unicode characters written to <TT>f</TT>.
  836. * @see u_sprintf_u
  837. * @stable ICU 3.0
  838. */
  839. U_CAPI int32_t U_EXPORT2
  840. u_vsprintf_u(UChar *buffer,
  841. const UChar *patternSpecification,
  842. va_list ap);
  843. /**
  844. * Write formatted data to a Unicode string.
  845. * This is identical to <TT>u_snprintf_u</TT>, except that it will
  846. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  847. * When the number of code units required to store the data exceeds
  848. * <TT>count</TT>, then <TT>count</TT> code units of data are stored in
  849. * <TT>buffer</TT> and a negative value is returned. When the number of code
  850. * units required to store the data equals <TT>count</TT>, the string is not
  851. * null terminated and <TT>count</TT> is returned.
  852. *
  853. * @param buffer The Unicode string to which to write.
  854. * @param count The number of code units to read.
  855. * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will
  856. * interpret the variable arguments received and format the data.
  857. * @param ap The argument list to use.
  858. * @return The number of Unicode characters that would have been written to
  859. * <TT>f</TT> had count been sufficiently large.
  860. * @see u_sprintf_u
  861. * @stable ICU 3.0
  862. */
  863. U_CAPI int32_t U_EXPORT2
  864. u_vsnprintf_u(UChar *buffer,
  865. int32_t count,
  866. const UChar *patternSpecification,
  867. va_list ap);
  868. /* Input string functions */
  869. /**
  870. * Read formatted data from a Unicode string.
  871. *
  872. * @param buffer The Unicode string from which to read.
  873. * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will
  874. * interpret the variable arguments received and parse the data.
  875. * @return The number of items successfully converted and assigned, or EOF
  876. * if an error occurred.
  877. * @stable ICU 3.0
  878. */
  879. U_CAPI int32_t U_EXPORT2
  880. u_sscanf(const UChar *buffer,
  881. const char *patternSpecification,
  882. ... );
  883. /**
  884. * Read formatted data from a Unicode string.
  885. * This is identical to <TT>u_sscanf</TT>, except that it will
  886. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  887. *
  888. * @param buffer The Unicode string from which to read.
  889. * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will
  890. * interpret the variable arguments received and parse the data.
  891. * @param ap The argument list to use.
  892. * @return The number of items successfully converted and assigned, or EOF
  893. * if an error occurred.
  894. * @see u_sscanf
  895. * @stable ICU 3.0
  896. */
  897. U_CAPI int32_t U_EXPORT2
  898. u_vsscanf(const UChar *buffer,
  899. const char *patternSpecification,
  900. va_list ap);
  901. /**
  902. * Read formatted data from a Unicode string.
  903. *
  904. * @param buffer The Unicode string from which to read.
  905. * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will
  906. * interpret the variable arguments received and parse the data.
  907. * @return The number of items successfully converted and assigned, or EOF
  908. * if an error occurred.
  909. * @stable ICU 3.0
  910. */
  911. U_CAPI int32_t U_EXPORT2
  912. u_sscanf_u(const UChar *buffer,
  913. const UChar *patternSpecification,
  914. ... );
  915. /**
  916. * Read formatted data from a Unicode string.
  917. * This is identical to <TT>u_sscanf_u</TT>, except that it will
  918. * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.
  919. *
  920. * @param buffer The Unicode string from which to read.
  921. * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will
  922. * interpret the variable arguments received and parse the data.
  923. * @param ap The argument list to use.
  924. * @return The number of items successfully converted and assigned, or EOF
  925. * if an error occurred.
  926. * @see u_sscanf_u
  927. * @stable ICU 3.0
  928. */
  929. U_CAPI int32_t U_EXPORT2
  930. u_vsscanf_u(const UChar *buffer,
  931. const UChar *patternSpecification,
  932. va_list ap);
  933. #endif
  934. #endif
  935. #endif