intl/icu/source/io/ustdio.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1998-2011, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************
michael@0 8 *
michael@0 9 * File ustdio.c
michael@0 10 *
michael@0 11 * Modification History:
michael@0 12 *
michael@0 13 * Date Name Description
michael@0 14 * 11/18/98 stephen Creation.
michael@0 15 * 03/12/99 stephen Modified for new C API.
michael@0 16 * 07/19/99 stephen Fixed read() and gets()
michael@0 17 ******************************************************************************
michael@0 18 */
michael@0 19
michael@0 20 #include "unicode/ustdio.h"
michael@0 21 #include "unicode/putil.h"
michael@0 22 #include "cmemory.h"
michael@0 23 #include "cstring.h"
michael@0 24 #include "ufile.h"
michael@0 25 #include "ufmt_cmn.h"
michael@0 26 #include "unicode/ucnv.h"
michael@0 27 #include "unicode/ustring.h"
michael@0 28
michael@0 29 #include <string.h>
michael@0 30
michael@0 31 #define DELIM_LF 0x000A
michael@0 32 #define DELIM_VT 0x000B
michael@0 33 #define DELIM_FF 0x000C
michael@0 34 #define DELIM_CR 0x000D
michael@0 35 #define DELIM_NEL 0x0085
michael@0 36 #define DELIM_LS 0x2028
michael@0 37 #define DELIM_PS 0x2029
michael@0 38
michael@0 39 /* TODO: is this correct for all codepages? Should we just use \n and let the converter handle it? */
michael@0 40 #if U_PLATFORM_USES_ONLY_WIN32_API
michael@0 41 static const UChar DELIMITERS [] = { DELIM_CR, DELIM_LF, 0x0000 };
michael@0 42 static const uint32_t DELIMITERS_LEN = 2;
michael@0 43 /* TODO: Default newline writing should be detected based upon the converter being used. */
michael@0 44 #else
michael@0 45 static const UChar DELIMITERS [] = { DELIM_LF, 0x0000 };
michael@0 46 static const uint32_t DELIMITERS_LEN = 1;
michael@0 47 #endif
michael@0 48
michael@0 49 #define IS_FIRST_STRING_DELIMITER(c1) \
michael@0 50 (UBool)((DELIM_LF <= (c1) && (c1) <= DELIM_CR) \
michael@0 51 || (c1) == DELIM_NEL \
michael@0 52 || (c1) == DELIM_LS \
michael@0 53 || (c1) == DELIM_PS)
michael@0 54 #define CAN_HAVE_COMBINED_STRING_DELIMITER(c1) (UBool)((c1) == DELIM_CR)
michael@0 55 #define IS_COMBINED_STRING_DELIMITER(c1, c2) \
michael@0 56 (UBool)((c1) == DELIM_CR && (c2) == DELIM_LF)
michael@0 57
michael@0 58
michael@0 59 #if !UCONFIG_NO_TRANSLITERATION
michael@0 60
michael@0 61 U_CAPI UTransliterator* U_EXPORT2
michael@0 62 u_fsettransliterator(UFILE *file, UFileDirection direction,
michael@0 63 UTransliterator *adopt, UErrorCode *status)
michael@0 64 {
michael@0 65 UTransliterator *old = NULL;
michael@0 66
michael@0 67 if(U_FAILURE(*status))
michael@0 68 {
michael@0 69 return adopt;
michael@0 70 }
michael@0 71
michael@0 72 if(!file)
michael@0 73 {
michael@0 74 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 75 return adopt;
michael@0 76 }
michael@0 77
michael@0 78 if(direction & U_READ)
michael@0 79 {
michael@0 80 /** TODO: implement */
michael@0 81 *status = U_UNSUPPORTED_ERROR;
michael@0 82 return adopt;
michael@0 83 }
michael@0 84
michael@0 85 if(adopt == NULL) /* they are clearing it */
michael@0 86 {
michael@0 87 if(file->fTranslit != NULL)
michael@0 88 {
michael@0 89 /* TODO: Check side */
michael@0 90 old = file->fTranslit->translit;
michael@0 91 uprv_free(file->fTranslit->buffer);
michael@0 92 file->fTranslit->buffer=NULL;
michael@0 93 uprv_free(file->fTranslit);
michael@0 94 file->fTranslit=NULL;
michael@0 95 }
michael@0 96 }
michael@0 97 else
michael@0 98 {
michael@0 99 if(file->fTranslit == NULL)
michael@0 100 {
michael@0 101 file->fTranslit = (UFILETranslitBuffer*) uprv_malloc(sizeof(UFILETranslitBuffer));
michael@0 102 if(!file->fTranslit)
michael@0 103 {
michael@0 104 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 105 return adopt;
michael@0 106 }
michael@0 107 file->fTranslit->capacity = 0;
michael@0 108 file->fTranslit->length = 0;
michael@0 109 file->fTranslit->pos = 0;
michael@0 110 file->fTranslit->buffer = NULL;
michael@0 111 }
michael@0 112 else
michael@0 113 {
michael@0 114 old = file->fTranslit->translit;
michael@0 115 ufile_flush_translit(file);
michael@0 116 }
michael@0 117
michael@0 118 file->fTranslit->translit = adopt;
michael@0 119 }
michael@0 120
michael@0 121 return old;
michael@0 122 }
michael@0 123
michael@0 124 static const UChar * u_file_translit(UFILE *f, const UChar *src, int32_t *count, UBool flush)
michael@0 125 {
michael@0 126 int32_t newlen;
michael@0 127 int32_t junkCount = 0;
michael@0 128 int32_t textLength;
michael@0 129 int32_t textLimit;
michael@0 130 UTransPosition pos;
michael@0 131 UErrorCode status = U_ZERO_ERROR;
michael@0 132
michael@0 133 if(count == NULL)
michael@0 134 {
michael@0 135 count = &junkCount;
michael@0 136 }
michael@0 137
michael@0 138 if ((!f)||(!f->fTranslit)||(!f->fTranslit->translit))
michael@0 139 {
michael@0 140 /* fast path */
michael@0 141 return src;
michael@0 142 }
michael@0 143
michael@0 144 /* First: slide over everything */
michael@0 145 if(f->fTranslit->length > f->fTranslit->pos)
michael@0 146 {
michael@0 147 memmove(f->fTranslit->buffer, f->fTranslit->buffer + f->fTranslit->pos,
michael@0 148 (f->fTranslit->length - f->fTranslit->pos)*sizeof(UChar));
michael@0 149 }
michael@0 150 f->fTranslit->length -= f->fTranslit->pos; /* always */
michael@0 151 f->fTranslit->pos = 0;
michael@0 152
michael@0 153 /* Calculate new buffer size needed */
michael@0 154 newlen = (*count + f->fTranslit->length) * 4;
michael@0 155
michael@0 156 if(newlen > f->fTranslit->capacity)
michael@0 157 {
michael@0 158 if(f->fTranslit->buffer == NULL)
michael@0 159 {
michael@0 160 f->fTranslit->buffer = (UChar*)uprv_malloc(newlen * sizeof(UChar));
michael@0 161 }
michael@0 162 else
michael@0 163 {
michael@0 164 f->fTranslit->buffer = (UChar*)uprv_realloc(f->fTranslit->buffer, newlen * sizeof(UChar));
michael@0 165 }
michael@0 166 /* Check for malloc/realloc failure. */
michael@0 167 if (f->fTranslit->buffer == NULL) {
michael@0 168 return NULL;
michael@0 169 }
michael@0 170 f->fTranslit->capacity = newlen;
michael@0 171 }
michael@0 172
michael@0 173 /* Now, copy any data over */
michael@0 174 u_strncpy(f->fTranslit->buffer + f->fTranslit->length,
michael@0 175 src,
michael@0 176 *count);
michael@0 177 f->fTranslit->length += *count;
michael@0 178
michael@0 179 /* Now, translit in place as much as we can */
michael@0 180 if(flush == FALSE)
michael@0 181 {
michael@0 182 textLength = f->fTranslit->length;
michael@0 183 pos.contextStart = 0;
michael@0 184 pos.contextLimit = textLength;
michael@0 185 pos.start = 0;
michael@0 186 pos.limit = textLength;
michael@0 187
michael@0 188 utrans_transIncrementalUChars(f->fTranslit->translit,
michael@0 189 f->fTranslit->buffer, /* because we shifted */
michael@0 190 &textLength,
michael@0 191 f->fTranslit->capacity,
michael@0 192 &pos,
michael@0 193 &status);
michael@0 194
michael@0 195 /* now: start/limit point to the transliterated text */
michael@0 196 /* Transliterated is [buffer..pos.start) */
michael@0 197 *count = pos.start;
michael@0 198 f->fTranslit->pos = pos.start;
michael@0 199 f->fTranslit->length = pos.limit;
michael@0 200
michael@0 201 return f->fTranslit->buffer;
michael@0 202 }
michael@0 203 else
michael@0 204 {
michael@0 205 textLength = f->fTranslit->length;
michael@0 206 textLimit = f->fTranslit->length;
michael@0 207
michael@0 208 utrans_transUChars(f->fTranslit->translit,
michael@0 209 f->fTranslit->buffer,
michael@0 210 &textLength,
michael@0 211 f->fTranslit->capacity,
michael@0 212 0,
michael@0 213 &textLimit,
michael@0 214 &status);
michael@0 215
michael@0 216 /* out: converted len */
michael@0 217 *count = textLimit;
michael@0 218
michael@0 219 /* Set pointers to 0 */
michael@0 220 f->fTranslit->pos = 0;
michael@0 221 f->fTranslit->length = 0;
michael@0 222
michael@0 223 return f->fTranslit->buffer;
michael@0 224 }
michael@0 225 }
michael@0 226
michael@0 227 #endif
michael@0 228
michael@0 229 void
michael@0 230 ufile_flush_translit(UFILE *f)
michael@0 231 {
michael@0 232 #if !UCONFIG_NO_TRANSLITERATION
michael@0 233 if((!f)||(!f->fTranslit))
michael@0 234 return;
michael@0 235 #endif
michael@0 236
michael@0 237 u_file_write_flush(NULL, 0, f, FALSE, TRUE);
michael@0 238 }
michael@0 239
michael@0 240
michael@0 241 void
michael@0 242 ufile_flush_io(UFILE *f)
michael@0 243 {
michael@0 244 if((!f) || (!f->fFile)) {
michael@0 245 return; /* skip if no file */
michael@0 246 }
michael@0 247
michael@0 248 u_file_write_flush(NULL, 0, f, TRUE, FALSE);
michael@0 249 }
michael@0 250
michael@0 251
michael@0 252 void
michael@0 253 ufile_close_translit(UFILE *f)
michael@0 254 {
michael@0 255 #if !UCONFIG_NO_TRANSLITERATION
michael@0 256 if((!f)||(!f->fTranslit))
michael@0 257 return;
michael@0 258 #endif
michael@0 259
michael@0 260 ufile_flush_translit(f);
michael@0 261
michael@0 262 #if !UCONFIG_NO_TRANSLITERATION
michael@0 263 if(f->fTranslit->translit)
michael@0 264 utrans_close(f->fTranslit->translit);
michael@0 265
michael@0 266 if(f->fTranslit->buffer)
michael@0 267 {
michael@0 268 uprv_free(f->fTranslit->buffer);
michael@0 269 }
michael@0 270
michael@0 271 uprv_free(f->fTranslit);
michael@0 272 f->fTranslit = NULL;
michael@0 273 #endif
michael@0 274 }
michael@0 275
michael@0 276
michael@0 277 /* Input/output */
michael@0 278
michael@0 279 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 280 u_fputs(const UChar *s,
michael@0 281 UFILE *f)
michael@0 282 {
michael@0 283 int32_t count = u_file_write(s, u_strlen(s), f);
michael@0 284 count += u_file_write(DELIMITERS, DELIMITERS_LEN, f);
michael@0 285 return count;
michael@0 286 }
michael@0 287
michael@0 288 U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 289 u_fputc(UChar32 uc,
michael@0 290 UFILE *f)
michael@0 291 {
michael@0 292 UChar buf[2];
michael@0 293 int32_t idx = 0;
michael@0 294 UBool isError = FALSE;
michael@0 295
michael@0 296 U16_APPEND(buf, idx, sizeof(buf)/sizeof(*buf), uc, isError);
michael@0 297 if (isError) {
michael@0 298 return U_EOF;
michael@0 299 }
michael@0 300 return u_file_write(buf, idx, f) == idx ? uc : U_EOF;
michael@0 301 }
michael@0 302
michael@0 303
michael@0 304 U_CFUNC int32_t U_EXPORT2
michael@0 305 u_file_write_flush(const UChar *chars,
michael@0 306 int32_t count,
michael@0 307 UFILE *f,
michael@0 308 UBool flushIO,
michael@0 309 UBool flushTranslit)
michael@0 310 {
michael@0 311 /* Set up conversion parameters */
michael@0 312 UErrorCode status = U_ZERO_ERROR;
michael@0 313 const UChar *mySource = chars;
michael@0 314 const UChar *mySourceBegin;
michael@0 315 const UChar *mySourceEnd;
michael@0 316 char charBuffer[UFILE_CHARBUFFER_SIZE];
michael@0 317 char *myTarget = charBuffer;
michael@0 318 int32_t written = 0;
michael@0 319 int32_t numConverted = 0;
michael@0 320
michael@0 321 if (count < 0) {
michael@0 322 count = u_strlen(chars);
michael@0 323 }
michael@0 324
michael@0 325 #if !UCONFIG_NO_TRANSLITERATION
michael@0 326 if((f->fTranslit) && (f->fTranslit->translit))
michael@0 327 {
michael@0 328 /* Do the transliteration */
michael@0 329 mySource = u_file_translit(f, chars, &count, flushTranslit);
michael@0 330 }
michael@0 331 #endif
michael@0 332
michael@0 333 /* Write to a string. */
michael@0 334 if (!f->fFile) {
michael@0 335 int32_t charsLeft = (int32_t)(f->str.fLimit - f->str.fPos);
michael@0 336 if (flushIO && charsLeft > count) {
michael@0 337 count++;
michael@0 338 }
michael@0 339 written = ufmt_min(count, charsLeft);
michael@0 340 u_strncpy(f->str.fPos, mySource, written);
michael@0 341 f->str.fPos += written;
michael@0 342 return written;
michael@0 343 }
michael@0 344
michael@0 345 mySourceEnd = mySource + count;
michael@0 346
michael@0 347 /* Perform the conversion in a loop */
michael@0 348 do {
michael@0 349 mySourceBegin = mySource; /* beginning location for this loop */
michael@0 350 status = U_ZERO_ERROR;
michael@0 351 if(f->fConverter != NULL) { /* We have a valid converter */
michael@0 352 ucnv_fromUnicode(f->fConverter,
michael@0 353 &myTarget,
michael@0 354 charBuffer + UFILE_CHARBUFFER_SIZE,
michael@0 355 &mySource,
michael@0 356 mySourceEnd,
michael@0 357 NULL,
michael@0 358 flushIO,
michael@0 359 &status);
michael@0 360 } else { /*weiv: do the invariant conversion */
michael@0 361 int32_t convertChars = (int32_t) (mySourceEnd - mySource);
michael@0 362 if (convertChars > UFILE_CHARBUFFER_SIZE) {
michael@0 363 convertChars = UFILE_CHARBUFFER_SIZE;
michael@0 364 status = U_BUFFER_OVERFLOW_ERROR;
michael@0 365 }
michael@0 366 u_UCharsToChars(mySource, myTarget, convertChars);
michael@0 367 mySource += convertChars;
michael@0 368 myTarget += convertChars;
michael@0 369 }
michael@0 370 numConverted = (int32_t)(myTarget - charBuffer);
michael@0 371
michael@0 372 if (numConverted > 0) {
michael@0 373 /* write the converted bytes */
michael@0 374 fwrite(charBuffer,
michael@0 375 sizeof(char),
michael@0 376 numConverted,
michael@0 377 f->fFile);
michael@0 378
michael@0 379 written += (int32_t) (mySource - mySourceBegin);
michael@0 380 }
michael@0 381 myTarget = charBuffer;
michael@0 382 }
michael@0 383 while(status == U_BUFFER_OVERFLOW_ERROR);
michael@0 384
michael@0 385 /* return # of chars written */
michael@0 386 return written;
michael@0 387 }
michael@0 388
michael@0 389 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 390 u_file_write( const UChar *chars,
michael@0 391 int32_t count,
michael@0 392 UFILE *f)
michael@0 393 {
michael@0 394 return u_file_write_flush(chars,count,f,FALSE,FALSE);
michael@0 395 }
michael@0 396
michael@0 397
michael@0 398 /* private function used for buffering input */
michael@0 399 void
michael@0 400 ufile_fill_uchar_buffer(UFILE *f)
michael@0 401 {
michael@0 402 UErrorCode status;
michael@0 403 const char *mySource;
michael@0 404 const char *mySourceEnd;
michael@0 405 UChar *myTarget;
michael@0 406 int32_t bufferSize;
michael@0 407 int32_t maxCPBytes;
michael@0 408 int32_t bytesRead;
michael@0 409 int32_t availLength;
michael@0 410 int32_t dataSize;
michael@0 411 char charBuffer[UFILE_CHARBUFFER_SIZE];
michael@0 412 u_localized_string *str;
michael@0 413
michael@0 414 if (f->fFile == NULL) {
michael@0 415 /* There is nothing to do. It's a string. */
michael@0 416 return;
michael@0 417 }
michael@0 418
michael@0 419 str = &f->str;
michael@0 420 dataSize = (int32_t)(str->fLimit - str->fPos);
michael@0 421 if (f->fFileno == 0 && dataSize > 0) {
michael@0 422 /* Don't read from stdin too many times. There is still some data. */
michael@0 423 return;
michael@0 424 }
michael@0 425
michael@0 426 /* shift the buffer if it isn't empty */
michael@0 427 if(dataSize != 0) {
michael@0 428 uprv_memmove(f->fUCBuffer, str->fPos, dataSize * sizeof(UChar)); /* not accessing beyond memory */
michael@0 429 }
michael@0 430
michael@0 431
michael@0 432 /* record how much buffer space is available */
michael@0 433 availLength = UFILE_UCHARBUFFER_SIZE - dataSize;
michael@0 434
michael@0 435 /* Determine the # of codepage bytes needed to fill our UChar buffer */
michael@0 436 /* weiv: if converter is NULL, we use invariant converter with charwidth = 1)*/
michael@0 437 maxCPBytes = availLength / (f->fConverter!=NULL?(2*ucnv_getMinCharSize(f->fConverter)):1);
michael@0 438
michael@0 439 /* Read in the data to convert */
michael@0 440 if (f->fFileno == 0) {
michael@0 441 /* Special case. Read from stdin one line at a time. */
michael@0 442 char *retStr = fgets(charBuffer, ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE), f->fFile);
michael@0 443 bytesRead = (int32_t)(retStr ? uprv_strlen(charBuffer) : 0);
michael@0 444 }
michael@0 445 else {
michael@0 446 /* A normal file */
michael@0 447 bytesRead = (int32_t)fread(charBuffer,
michael@0 448 sizeof(char),
michael@0 449 ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE),
michael@0 450 f->fFile);
michael@0 451 }
michael@0 452
michael@0 453 /* Set up conversion parameters */
michael@0 454 status = U_ZERO_ERROR;
michael@0 455 mySource = charBuffer;
michael@0 456 mySourceEnd = charBuffer + bytesRead;
michael@0 457 myTarget = f->fUCBuffer + dataSize;
michael@0 458 bufferSize = UFILE_UCHARBUFFER_SIZE;
michael@0 459
michael@0 460 if(f->fConverter != NULL) { /* We have a valid converter */
michael@0 461 /* Perform the conversion */
michael@0 462 ucnv_toUnicode(f->fConverter,
michael@0 463 &myTarget,
michael@0 464 f->fUCBuffer + bufferSize,
michael@0 465 &mySource,
michael@0 466 mySourceEnd,
michael@0 467 NULL,
michael@0 468 (UBool)(feof(f->fFile) != 0),
michael@0 469 &status);
michael@0 470
michael@0 471 } else { /*weiv: do the invariant conversion */
michael@0 472 u_charsToUChars(mySource, myTarget, bytesRead);
michael@0 473 myTarget += bytesRead;
michael@0 474 }
michael@0 475
michael@0 476 /* update the pointers into our array */
michael@0 477 str->fPos = str->fBuffer;
michael@0 478 str->fLimit = myTarget;
michael@0 479 }
michael@0 480
michael@0 481 U_CAPI UChar* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 482 u_fgets(UChar *s,
michael@0 483 int32_t n,
michael@0 484 UFILE *f)
michael@0 485 {
michael@0 486 int32_t dataSize;
michael@0 487 int32_t count;
michael@0 488 UChar *alias;
michael@0 489 const UChar *limit;
michael@0 490 UChar *sItr;
michael@0 491 UChar currDelim = 0;
michael@0 492 u_localized_string *str;
michael@0 493
michael@0 494 if (n <= 0) {
michael@0 495 /* Caller screwed up. We need to write the null terminatior. */
michael@0 496 return NULL;
michael@0 497 }
michael@0 498
michael@0 499 /* fill the buffer if needed */
michael@0 500 str = &f->str;
michael@0 501 if (str->fPos >= str->fLimit) {
michael@0 502 ufile_fill_uchar_buffer(f);
michael@0 503 }
michael@0 504
michael@0 505 /* subtract 1 from n to compensate for the terminator */
michael@0 506 --n;
michael@0 507
michael@0 508 /* determine the amount of data in the buffer */
michael@0 509 dataSize = (int32_t)(str->fLimit - str->fPos);
michael@0 510
michael@0 511 /* if 0 characters were left, return 0 */
michael@0 512 if (dataSize == 0)
michael@0 513 return NULL;
michael@0 514
michael@0 515 /* otherwise, iteratively fill the buffer and copy */
michael@0 516 count = 0;
michael@0 517 sItr = s;
michael@0 518 currDelim = 0;
michael@0 519 while (dataSize > 0 && count < n) {
michael@0 520 alias = str->fPos;
michael@0 521
michael@0 522 /* Find how much to copy */
michael@0 523 if (dataSize < (n - count)) {
michael@0 524 limit = str->fLimit;
michael@0 525 }
michael@0 526 else {
michael@0 527 limit = alias + (n - count);
michael@0 528 }
michael@0 529
michael@0 530 if (!currDelim) {
michael@0 531 /* Copy UChars until we find the first occurrence of a delimiter character */
michael@0 532 while (alias < limit && !IS_FIRST_STRING_DELIMITER(*alias)) {
michael@0 533 count++;
michael@0 534 *(sItr++) = *(alias++);
michael@0 535 }
michael@0 536 /* Preserve the newline */
michael@0 537 if (alias < limit && IS_FIRST_STRING_DELIMITER(*alias)) {
michael@0 538 if (CAN_HAVE_COMBINED_STRING_DELIMITER(*alias)) {
michael@0 539 currDelim = *alias;
michael@0 540 }
michael@0 541 else {
michael@0 542 currDelim = 1; /* This isn't a newline, but it's used to say
michael@0 543 that we should break later. We've checked all
michael@0 544 possible newline combinations even across buffer
michael@0 545 boundaries. */
michael@0 546 }
michael@0 547 count++;
michael@0 548 *(sItr++) = *(alias++);
michael@0 549 }
michael@0 550 }
michael@0 551 /* If we have a CRLF combination, preserve that too. */
michael@0 552 if (alias < limit) {
michael@0 553 if (currDelim && IS_COMBINED_STRING_DELIMITER(currDelim, *alias)) {
michael@0 554 count++;
michael@0 555 *(sItr++) = *(alias++);
michael@0 556 }
michael@0 557 currDelim = 1; /* This isn't a newline, but it's used to say
michael@0 558 that we should break later. We've checked all
michael@0 559 possible newline combinations even across buffer
michael@0 560 boundaries. */
michael@0 561 }
michael@0 562
michael@0 563 /* update the current buffer position */
michael@0 564 str->fPos = alias;
michael@0 565
michael@0 566 /* if we found a delimiter */
michael@0 567 if (currDelim == 1) {
michael@0 568 /* break out */
michael@0 569 break;
michael@0 570 }
michael@0 571
michael@0 572 /* refill the buffer */
michael@0 573 ufile_fill_uchar_buffer(f);
michael@0 574
michael@0 575 /* determine the amount of data in the buffer */
michael@0 576 dataSize = (int32_t)(str->fLimit - str->fPos);
michael@0 577 }
michael@0 578
michael@0 579 /* add the terminator and return s */
michael@0 580 *sItr = 0x0000;
michael@0 581 return s;
michael@0 582 }
michael@0 583
michael@0 584 U_CFUNC UBool U_EXPORT2
michael@0 585 ufile_getch(UFILE *f, UChar *ch)
michael@0 586 {
michael@0 587 UBool isValidChar = FALSE;
michael@0 588
michael@0 589 *ch = U_EOF;
michael@0 590 /* if we have an available character in the buffer, return it */
michael@0 591 if(f->str.fPos < f->str.fLimit){
michael@0 592 *ch = *(f->str.fPos)++;
michael@0 593 isValidChar = TRUE;
michael@0 594 }
michael@0 595 else {
michael@0 596 /* otherwise, fill the buffer and return the next character */
michael@0 597 if(f->str.fPos >= f->str.fLimit) {
michael@0 598 ufile_fill_uchar_buffer(f);
michael@0 599 }
michael@0 600 if(f->str.fPos < f->str.fLimit) {
michael@0 601 *ch = *(f->str.fPos)++;
michael@0 602 isValidChar = TRUE;
michael@0 603 }
michael@0 604 }
michael@0 605 return isValidChar;
michael@0 606 }
michael@0 607
michael@0 608 U_CAPI UChar U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 609 u_fgetc(UFILE *f)
michael@0 610 {
michael@0 611 UChar ch;
michael@0 612 ufile_getch(f, &ch);
michael@0 613 return ch;
michael@0 614 }
michael@0 615
michael@0 616 U_CFUNC UBool U_EXPORT2
michael@0 617 ufile_getch32(UFILE *f, UChar32 *c32)
michael@0 618 {
michael@0 619 UBool isValidChar = FALSE;
michael@0 620 u_localized_string *str;
michael@0 621
michael@0 622 *c32 = U_EOF;
michael@0 623
michael@0 624 /* Fill the buffer if it is empty */
michael@0 625 str = &f->str;
michael@0 626 if (f && str->fPos + 1 >= str->fLimit) {
michael@0 627 ufile_fill_uchar_buffer(f);
michael@0 628 }
michael@0 629
michael@0 630 /* Get the next character in the buffer */
michael@0 631 if (str->fPos < str->fLimit) {
michael@0 632 *c32 = *(str->fPos)++;
michael@0 633 if (U_IS_LEAD(*c32)) {
michael@0 634 if (str->fPos < str->fLimit) {
michael@0 635 UChar c16 = *(str->fPos)++;
michael@0 636 *c32 = U16_GET_SUPPLEMENTARY(*c32, c16);
michael@0 637 isValidChar = TRUE;
michael@0 638 }
michael@0 639 else {
michael@0 640 *c32 = U_EOF;
michael@0 641 }
michael@0 642 }
michael@0 643 else {
michael@0 644 isValidChar = TRUE;
michael@0 645 }
michael@0 646 }
michael@0 647
michael@0 648 return isValidChar;
michael@0 649 }
michael@0 650
michael@0 651 U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 652 u_fgetcx(UFILE *f)
michael@0 653 {
michael@0 654 UChar32 ch;
michael@0 655 ufile_getch32(f, &ch);
michael@0 656 return ch;
michael@0 657 }
michael@0 658
michael@0 659 U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 660 u_fungetc(UChar32 ch,
michael@0 661 UFILE *f)
michael@0 662 {
michael@0 663 u_localized_string *str;
michael@0 664
michael@0 665 str = &f->str;
michael@0 666
michael@0 667 /* if we're at the beginning of the buffer, sorry! */
michael@0 668 if (str->fPos == str->fBuffer
michael@0 669 || (U_IS_LEAD(ch) && (str->fPos - 1) == str->fBuffer))
michael@0 670 {
michael@0 671 ch = U_EOF;
michael@0 672 }
michael@0 673 else {
michael@0 674 /* otherwise, put the character back */
michael@0 675 /* Remember, read them back on in the reverse order. */
michael@0 676 if (U_IS_LEAD(ch)) {
michael@0 677 if (*--(str->fPos) != U16_TRAIL(ch)
michael@0 678 || *--(str->fPos) != U16_LEAD(ch))
michael@0 679 {
michael@0 680 ch = U_EOF;
michael@0 681 }
michael@0 682 }
michael@0 683 else if (*--(str->fPos) != ch) {
michael@0 684 ch = U_EOF;
michael@0 685 }
michael@0 686 }
michael@0 687 return ch;
michael@0 688 }
michael@0 689
michael@0 690 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 691 u_file_read( UChar *chars,
michael@0 692 int32_t count,
michael@0 693 UFILE *f)
michael@0 694 {
michael@0 695 int32_t dataSize;
michael@0 696 int32_t read = 0;
michael@0 697 u_localized_string *str = &f->str;
michael@0 698
michael@0 699 do {
michael@0 700
michael@0 701 /* determine the amount of data in the buffer */
michael@0 702 dataSize = (int32_t)(str->fLimit - str->fPos);
michael@0 703 if (dataSize <= 0) {
michael@0 704 /* fill the buffer */
michael@0 705 ufile_fill_uchar_buffer(f);
michael@0 706 dataSize = (int32_t)(str->fLimit - str->fPos);
michael@0 707 }
michael@0 708
michael@0 709 /* Make sure that we don't read too much */
michael@0 710 if (dataSize > (count - read)) {
michael@0 711 dataSize = count - read;
michael@0 712 }
michael@0 713
michael@0 714 /* copy the current data in the buffer */
michael@0 715 memcpy(chars + read, str->fPos, dataSize * sizeof(UChar));
michael@0 716
michael@0 717 /* update number of items read */
michael@0 718 read += dataSize;
michael@0 719
michael@0 720 /* update the current buffer position */
michael@0 721 str->fPos += dataSize;
michael@0 722 }
michael@0 723 while (dataSize != 0 && read < count);
michael@0 724
michael@0 725 return read;
michael@0 726 }

mercurial