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