ustdio.h 38 KB

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