ustdio.h 39 KB

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