2 ******************************************************************************
4 * Copyright (C) 1998-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 11/18/98 stephen Creation.
15 * 03/12/99 stephen Modified for new C API.
16 * 07/19/99 stephen Fixed read() and gets()
17 ******************************************************************************
20 #include "unicode/ustdio.h"
22 #if !UCONFIG_NO_CONVERSION
24 #include "unicode/putil.h"
29 #include "unicode/ucnv.h"
30 #include "unicode/ustring.h"
34 #define DELIM_LF 0x000A
35 #define DELIM_VT 0x000B
36 #define DELIM_FF 0x000C
37 #define DELIM_CR 0x000D
38 #define DELIM_NEL 0x0085
39 #define DELIM_LS 0x2028
40 #define DELIM_PS 0x2029
42 /* TODO: is this correct for all codepages? Should we just use \n and let the converter handle it? */
43 #if U_PLATFORM_USES_ONLY_WIN32_API
44 static const UChar DELIMITERS
[] = { DELIM_CR
, DELIM_LF
, 0x0000 };
45 static const uint32_t DELIMITERS_LEN
= 2;
46 /* TODO: Default newline writing should be detected based upon the converter being used. */
48 static const UChar DELIMITERS
[] = { DELIM_LF
, 0x0000 };
49 static const uint32_t DELIMITERS_LEN
= 1;
52 #define IS_FIRST_STRING_DELIMITER(c1) \
53 (UBool)((DELIM_LF <= (c1) && (c1) <= DELIM_CR) \
54 || (c1) == DELIM_NEL \
57 #define CAN_HAVE_COMBINED_STRING_DELIMITER(c1) (UBool)((c1) == DELIM_CR)
58 #define IS_COMBINED_STRING_DELIMITER(c1, c2) \
59 (UBool)((c1) == DELIM_CR && (c2) == DELIM_LF)
62 #if !UCONFIG_NO_TRANSLITERATION
64 U_CAPI UTransliterator
* U_EXPORT2
65 u_fsettransliterator(UFILE
*file
, UFileDirection direction
,
66 UTransliterator
*adopt
, UErrorCode
*status
)
68 UTransliterator
*old
= NULL
;
70 if(U_FAILURE(*status
))
77 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
81 if(direction
& U_READ
)
83 /** TODO: implement */
84 *status
= U_UNSUPPORTED_ERROR
;
88 if(adopt
== NULL
) /* they are clearing it */
90 if(file
->fTranslit
!= NULL
)
92 /* TODO: Check side */
93 old
= file
->fTranslit
->translit
;
94 uprv_free(file
->fTranslit
->buffer
);
95 file
->fTranslit
->buffer
=NULL
;
96 uprv_free(file
->fTranslit
);
102 if(file
->fTranslit
== NULL
)
104 file
->fTranslit
= (UFILETranslitBuffer
*) uprv_malloc(sizeof(UFILETranslitBuffer
));
107 *status
= U_MEMORY_ALLOCATION_ERROR
;
110 file
->fTranslit
->capacity
= 0;
111 file
->fTranslit
->length
= 0;
112 file
->fTranslit
->pos
= 0;
113 file
->fTranslit
->buffer
= NULL
;
117 old
= file
->fTranslit
->translit
;
118 ufile_flush_translit(file
);
121 file
->fTranslit
->translit
= adopt
;
127 static const UChar
* u_file_translit(UFILE
*f
, const UChar
*src
, int32_t *count
, UBool flush
)
130 int32_t junkCount
= 0;
134 UErrorCode status
= U_ZERO_ERROR
;
141 if ((!f
)||(!f
->fTranslit
)||(!f
->fTranslit
->translit
))
147 /* First: slide over everything */
148 if(f
->fTranslit
->length
> f
->fTranslit
->pos
)
150 memmove(f
->fTranslit
->buffer
, f
->fTranslit
->buffer
+ f
->fTranslit
->pos
,
151 (f
->fTranslit
->length
- f
->fTranslit
->pos
)*sizeof(UChar
));
153 f
->fTranslit
->length
-= f
->fTranslit
->pos
; /* always */
154 f
->fTranslit
->pos
= 0;
156 /* Calculate new buffer size needed */
157 newlen
= (*count
+ f
->fTranslit
->length
) * 4;
159 if(newlen
> f
->fTranslit
->capacity
)
161 if(f
->fTranslit
->buffer
== NULL
)
163 f
->fTranslit
->buffer
= (UChar
*)uprv_malloc(newlen
* sizeof(UChar
));
167 f
->fTranslit
->buffer
= (UChar
*)uprv_realloc(f
->fTranslit
->buffer
, newlen
* sizeof(UChar
));
169 /* Check for malloc/realloc failure. */
170 if (f
->fTranslit
->buffer
== NULL
) {
173 f
->fTranslit
->capacity
= newlen
;
176 /* Now, copy any data over */
177 u_strncpy(f
->fTranslit
->buffer
+ f
->fTranslit
->length
,
180 f
->fTranslit
->length
+= *count
;
182 /* Now, translit in place as much as we can */
185 textLength
= f
->fTranslit
->length
;
186 pos
.contextStart
= 0;
187 pos
.contextLimit
= textLength
;
189 pos
.limit
= textLength
;
191 utrans_transIncrementalUChars(f
->fTranslit
->translit
,
192 f
->fTranslit
->buffer
, /* because we shifted */
194 f
->fTranslit
->capacity
,
198 /* now: start/limit point to the transliterated text */
199 /* Transliterated is [buffer..pos.start) */
201 f
->fTranslit
->pos
= pos
.start
;
202 f
->fTranslit
->length
= pos
.limit
;
204 return f
->fTranslit
->buffer
;
208 textLength
= f
->fTranslit
->length
;
209 textLimit
= f
->fTranslit
->length
;
211 utrans_transUChars(f
->fTranslit
->translit
,
212 f
->fTranslit
->buffer
,
214 f
->fTranslit
->capacity
,
219 /* out: converted len */
222 /* Set pointers to 0 */
223 f
->fTranslit
->pos
= 0;
224 f
->fTranslit
->length
= 0;
226 return f
->fTranslit
->buffer
;
233 ufile_flush_translit(UFILE
*f
)
235 #if !UCONFIG_NO_TRANSLITERATION
236 if((!f
)||(!f
->fTranslit
))
240 u_file_write_flush(NULL
, 0, f
, FALSE
, TRUE
);
245 ufile_flush_io(UFILE
*f
)
247 if((!f
) || (!f
->fFile
)) {
248 return; /* skip if no file */
251 u_file_write_flush(NULL
, 0, f
, TRUE
, FALSE
);
256 ufile_close_translit(UFILE
*f
)
258 #if !UCONFIG_NO_TRANSLITERATION
259 if((!f
)||(!f
->fTranslit
))
263 ufile_flush_translit(f
);
265 #if !UCONFIG_NO_TRANSLITERATION
266 if(f
->fTranslit
->translit
)
267 utrans_close(f
->fTranslit
->translit
);
269 if(f
->fTranslit
->buffer
)
271 uprv_free(f
->fTranslit
->buffer
);
274 uprv_free(f
->fTranslit
);
282 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
283 u_fputs(const UChar
*s
,
286 int32_t count
= u_file_write(s
, u_strlen(s
), f
);
287 count
+= u_file_write(DELIMITERS
, DELIMITERS_LEN
, f
);
291 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
297 UBool isError
= FALSE
;
299 U16_APPEND(buf
, idx
, sizeof(buf
)/sizeof(*buf
), uc
, isError
);
303 return u_file_write(buf
, idx
, f
) == idx
? uc
: U_EOF
;
307 U_CFUNC
int32_t U_EXPORT2
308 u_file_write_flush(const UChar
*chars
,
314 /* Set up conversion parameters */
315 UErrorCode status
= U_ZERO_ERROR
;
316 const UChar
*mySource
= chars
;
317 const UChar
*mySourceBegin
;
318 const UChar
*mySourceEnd
;
319 char charBuffer
[UFILE_CHARBUFFER_SIZE
];
320 char *myTarget
= charBuffer
;
322 int32_t numConverted
= 0;
325 count
= u_strlen(chars
);
328 #if !UCONFIG_NO_TRANSLITERATION
329 if((f
->fTranslit
) && (f
->fTranslit
->translit
))
331 /* Do the transliteration */
332 mySource
= u_file_translit(f
, chars
, &count
, flushTranslit
);
336 /* Write to a string. */
338 int32_t charsLeft
= (int32_t)(f
->str
.fLimit
- f
->str
.fPos
);
339 if (flushIO
&& charsLeft
> count
) {
342 written
= ufmt_min(count
, charsLeft
);
343 u_strncpy(f
->str
.fPos
, mySource
, written
);
344 f
->str
.fPos
+= written
;
348 mySourceEnd
= mySource
+ count
;
350 /* Perform the conversion in a loop */
352 mySourceBegin
= mySource
; /* beginning location for this loop */
353 status
= U_ZERO_ERROR
;
354 if(f
->fConverter
!= NULL
) { /* We have a valid converter */
355 ucnv_fromUnicode(f
->fConverter
,
357 charBuffer
+ UFILE_CHARBUFFER_SIZE
,
363 } else { /*weiv: do the invariant conversion */
364 int32_t convertChars
= (int32_t) (mySourceEnd
- mySource
);
365 if (convertChars
> UFILE_CHARBUFFER_SIZE
) {
366 convertChars
= UFILE_CHARBUFFER_SIZE
;
367 status
= U_BUFFER_OVERFLOW_ERROR
;
369 u_UCharsToChars(mySource
, myTarget
, convertChars
);
370 mySource
+= convertChars
;
371 myTarget
+= convertChars
;
373 numConverted
= (int32_t)(myTarget
- charBuffer
);
375 if (numConverted
> 0) {
376 /* write the converted bytes */
382 written
+= (int32_t) (mySource
- mySourceBegin
);
384 myTarget
= charBuffer
;
386 while(status
== U_BUFFER_OVERFLOW_ERROR
);
388 /* return # of chars written */
392 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
393 u_file_write( const UChar
*chars
,
397 return u_file_write_flush(chars
,count
,f
,FALSE
,FALSE
);
401 /* private function used for buffering input */
403 ufile_fill_uchar_buffer(UFILE
*f
)
406 const char *mySource
;
407 const char *mySourceEnd
;
414 char charBuffer
[UFILE_CHARBUFFER_SIZE
];
415 u_localized_string
*str
;
417 if (f
->fFile
== NULL
) {
418 /* There is nothing to do. It's a string. */
423 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
424 if (f
->fFileno
== 0 && dataSize
> 0) {
425 /* Don't read from stdin too many times. There is still some data. */
429 /* shift the buffer if it isn't empty */
431 uprv_memmove(f
->fUCBuffer
, str
->fPos
, dataSize
* sizeof(UChar
)); /* not accessing beyond memory */
435 /* record how much buffer space is available */
436 availLength
= UFILE_UCHARBUFFER_SIZE
- dataSize
;
438 /* Determine the # of codepage bytes needed to fill our UChar buffer */
439 /* weiv: if converter is NULL, we use invariant converter with charwidth = 1)*/
440 maxCPBytes
= availLength
/ (f
->fConverter
!=NULL
?(2*ucnv_getMinCharSize(f
->fConverter
)):1);
442 /* Read in the data to convert */
443 if (f
->fFileno
== 0) {
444 /* Special case. Read from stdin one line at a time. */
445 char *retStr
= fgets(charBuffer
, ufmt_min(maxCPBytes
, UFILE_CHARBUFFER_SIZE
), f
->fFile
);
446 bytesRead
= (int32_t)(retStr
? uprv_strlen(charBuffer
) : 0);
450 bytesRead
= (int32_t)fread(charBuffer
,
452 ufmt_min(maxCPBytes
, UFILE_CHARBUFFER_SIZE
),
456 /* Set up conversion parameters */
457 status
= U_ZERO_ERROR
;
458 mySource
= charBuffer
;
459 mySourceEnd
= charBuffer
+ bytesRead
;
460 myTarget
= f
->fUCBuffer
+ dataSize
;
461 bufferSize
= UFILE_UCHARBUFFER_SIZE
;
463 if(f
->fConverter
!= NULL
) { /* We have a valid converter */
464 /* Perform the conversion */
465 ucnv_toUnicode(f
->fConverter
,
467 f
->fUCBuffer
+ bufferSize
,
471 (UBool
)(feof(f
->fFile
) != 0),
474 } else { /*weiv: do the invariant conversion */
475 u_charsToUChars(mySource
, myTarget
, bytesRead
);
476 myTarget
+= bytesRead
;
479 /* update the pointers into our array */
480 str
->fPos
= str
->fBuffer
;
481 str
->fLimit
= myTarget
;
484 U_CAPI UChar
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
495 u_localized_string
*str
;
498 /* Caller screwed up. We need to write the null terminatior. */
502 /* fill the buffer if needed */
504 if (str
->fPos
>= str
->fLimit
) {
505 ufile_fill_uchar_buffer(f
);
508 /* subtract 1 from n to compensate for the terminator */
511 /* determine the amount of data in the buffer */
512 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
514 /* if 0 characters were left, return 0 */
518 /* otherwise, iteratively fill the buffer and copy */
522 while (dataSize
> 0 && count
< n
) {
525 /* Find how much to copy */
526 if (dataSize
< (n
- count
)) {
530 limit
= alias
+ (n
- count
);
534 /* Copy UChars until we find the first occurrence of a delimiter character */
535 while (alias
< limit
&& !IS_FIRST_STRING_DELIMITER(*alias
)) {
537 *(sItr
++) = *(alias
++);
539 /* Preserve the newline */
540 if (alias
< limit
&& IS_FIRST_STRING_DELIMITER(*alias
)) {
541 if (CAN_HAVE_COMBINED_STRING_DELIMITER(*alias
)) {
545 currDelim
= 1; /* This isn't a newline, but it's used to say
546 that we should break later. We've checked all
547 possible newline combinations even across buffer
551 *(sItr
++) = *(alias
++);
554 /* If we have a CRLF combination, preserve that too. */
556 if (currDelim
&& IS_COMBINED_STRING_DELIMITER(currDelim
, *alias
)) {
558 *(sItr
++) = *(alias
++);
560 currDelim
= 1; /* This isn't a newline, but it's used to say
561 that we should break later. We've checked all
562 possible newline combinations even across buffer
566 /* update the current buffer position */
569 /* if we found a delimiter */
570 if (currDelim
== 1) {
575 /* refill the buffer */
576 ufile_fill_uchar_buffer(f
);
578 /* determine the amount of data in the buffer */
579 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
582 /* add the terminator and return s */
587 U_CFUNC UBool U_EXPORT2
588 ufile_getch(UFILE
*f
, UChar
*ch
)
590 UBool isValidChar
= FALSE
;
593 /* if we have an available character in the buffer, return it */
594 if(f
->str
.fPos
< f
->str
.fLimit
){
595 *ch
= *(f
->str
.fPos
)++;
599 /* otherwise, fill the buffer and return the next character */
600 if(f
->str
.fPos
>= f
->str
.fLimit
) {
601 ufile_fill_uchar_buffer(f
);
603 if(f
->str
.fPos
< f
->str
.fLimit
) {
604 *ch
= *(f
->str
.fPos
)++;
611 U_CAPI UChar U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
619 U_CFUNC UBool U_EXPORT2
620 ufile_getch32(UFILE
*f
, UChar32
*c32
)
622 UBool isValidChar
= FALSE
;
623 u_localized_string
*str
;
627 /* Fill the buffer if it is empty */
629 if (f
&& str
->fPos
+ 1 >= str
->fLimit
) {
630 ufile_fill_uchar_buffer(f
);
633 /* Get the next character in the buffer */
634 if (str
->fPos
< str
->fLimit
) {
635 *c32
= *(str
->fPos
)++;
636 if (U_IS_LEAD(*c32
)) {
637 if (str
->fPos
< str
->fLimit
) {
638 UChar c16
= *(str
->fPos
)++;
639 *c32
= U16_GET_SUPPLEMENTARY(*c32
, c16
);
654 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
658 ufile_getch32(f
, &ch
);
662 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
663 u_fungetc(UChar32 ch
,
666 u_localized_string
*str
;
670 /* if we're at the beginning of the buffer, sorry! */
671 if (str
->fPos
== str
->fBuffer
672 || (U_IS_LEAD(ch
) && (str
->fPos
- 1) == str
->fBuffer
))
677 /* otherwise, put the character back */
678 /* Remember, read them back on in the reverse order. */
680 if (*--(str
->fPos
) != U16_TRAIL(ch
)
681 || *--(str
->fPos
) != U16_LEAD(ch
))
686 else if (*--(str
->fPos
) != ch
) {
693 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
694 u_file_read( UChar
*chars
,
700 u_localized_string
*str
= &f
->str
;
704 /* determine the amount of data in the buffer */
705 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
707 /* fill the buffer */
708 ufile_fill_uchar_buffer(f
);
709 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
712 /* Make sure that we don't read too much */
713 if (dataSize
> (count
- read
)) {
714 dataSize
= count
- read
;
717 /* copy the current data in the buffer */
718 memcpy(chars
+ read
, str
->fPos
, dataSize
* sizeof(UChar
));
720 /* update number of items read */
723 /* update the current buffer position */
724 str
->fPos
+= dataSize
;
726 while (dataSize
!= 0 && read
< count
);