2 ******************************************************************************
4 * Copyright (C) 1998-2004, 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"
21 #include "unicode/putil.h"
26 #include "unicode/ucnv.h"
27 #include "unicode/ustring.h"
31 #define DELIM_LF 0x000A
32 #define DELIM_VT 0x000B
33 #define DELIM_FF 0x000C
34 #define DELIM_CR 0x000D
35 #define DELIM_NEL 0x0085
36 #define DELIM_LS 0x2028
37 #define DELIM_PS 0x2029
39 /* Leave this copyright notice here! */
40 static const char copyright
[] = U_COPYRIGHT_STRING
;
42 /* TODO: is this correct for all codepages? Should we just use \n and let the converter handle it? */
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 f
->fTranslit
->capacity
= newlen
;
172 /* Now, copy any data over */
173 u_strncpy(f
->fTranslit
->buffer
+ f
->fTranslit
->length
,
176 f
->fTranslit
->length
+= *count
;
178 /* Now, translit in place as much as we can */
181 textLength
= f
->fTranslit
->length
;
182 pos
.contextStart
= 0;
183 pos
.contextLimit
= textLength
;
185 pos
.limit
= textLength
;
187 utrans_transIncrementalUChars(f
->fTranslit
->translit
,
188 f
->fTranslit
->buffer
, /* because we shifted */
190 f
->fTranslit
->capacity
,
194 /* now: start/limit point to the transliterated text */
195 /* Transliterated is [buffer..pos.start) */
197 f
->fTranslit
->pos
= pos
.start
;
198 f
->fTranslit
->length
= pos
.limit
;
200 return f
->fTranslit
->buffer
;
204 textLength
= f
->fTranslit
->length
;
205 textLimit
= f
->fTranslit
->length
;
207 utrans_transUChars(f
->fTranslit
->translit
,
208 f
->fTranslit
->buffer
,
210 f
->fTranslit
->capacity
,
215 /* out: converted len */
218 /* Set pointers to 0 */
219 f
->fTranslit
->pos
= 0;
220 f
->fTranslit
->length
= 0;
222 return f
->fTranslit
->buffer
;
229 ufile_flush_translit(UFILE
*f
)
231 #if !UCONFIG_NO_TRANSLITERATION
232 if((!f
)||(!f
->fTranslit
))
236 u_file_write_flush(NULL
, 0, f
, TRUE
);
241 ufile_close_translit(UFILE
*f
)
243 #if !UCONFIG_NO_TRANSLITERATION
244 if((!f
)||(!f
->fTranslit
))
248 ufile_flush_translit(f
);
250 #if !UCONFIG_NO_TRANSLITERATION
251 if(f
->fTranslit
->translit
)
252 utrans_close(f
->fTranslit
->translit
);
254 if(f
->fTranslit
->buffer
)
256 uprv_free(f
->fTranslit
->buffer
);
259 uprv_free(f
->fTranslit
);
267 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
268 u_fputs(const UChar
*s
,
271 int32_t count
= u_file_write(s
, u_strlen(s
), f
);
272 count
+= u_file_write(DELIMITERS
, DELIMITERS_LEN
, f
);
276 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
282 UBool isError
= FALSE
;
284 U16_APPEND(buf
, idx
, sizeof(buf
)/sizeof(*buf
), uc
, isError
);
288 return u_file_write(buf
, idx
, f
) == idx
? uc
: EOF
;
292 U_CAPI
int32_t U_EXPORT2
293 u_file_write_flush( const UChar
*chars
,
298 /* Set up conversion parameters */
299 UErrorCode status
= U_ZERO_ERROR
;
300 const UChar
*mySource
= chars
;
301 const UChar
*sourceAlias
= chars
;
302 const UChar
*mySourceEnd
;
303 char charBuffer
[UFILE_CHARBUFFER_SIZE
];
304 char *myTarget
= charBuffer
;
306 int32_t numConverted
= 0;
309 int32_t charsLeft
= f
->str
.fLimit
- f
->str
.fPos
;
310 if (flush
&& charsLeft
> count
) {
313 written
= ufmt_min(count
, charsLeft
);
314 u_strncpy(f
->str
.fPos
, chars
, written
);
315 f
->str
.fPos
+= written
;
320 count
= u_strlen(chars
);
322 mySourceEnd
= chars
+ count
;
324 #if !UCONFIG_NO_TRANSLITERATION
325 if((f
->fTranslit
) && (f
->fTranslit
->translit
))
327 /* Do the transliteration */
328 mySource
= u_file_translit(f
, chars
, &count
, flush
);
329 sourceAlias
= mySource
;
330 mySourceEnd
= mySource
+ count
;
334 /* Perform the conversion in a loop */
336 status
= U_ZERO_ERROR
;
337 sourceAlias
= mySource
;
338 if(f
->fConverter
!= NULL
) { /* We have a valid converter */
339 ucnv_fromUnicode(f
->fConverter
,
341 charBuffer
+ UFILE_CHARBUFFER_SIZE
,
347 } else { /*weiv: do the invariant conversion */
348 u_UCharsToChars(mySource
, myTarget
, count
);
351 numConverted
= (int32_t)(myTarget
- charBuffer
);
353 if (numConverted
> 0) {
354 /* write the converted bytes */
360 written
+= numConverted
;
362 myTarget
= charBuffer
;
364 while(status
== U_BUFFER_OVERFLOW_ERROR
);
366 /* return # of chars written */
370 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
371 u_file_write( const UChar
*chars
,
375 return u_file_write_flush(chars
,count
,f
,FALSE
);
379 /* private function used for buffering input */
381 ufile_fill_uchar_buffer(UFILE
*f
)
384 const char *mySource
;
385 const char *mySourceEnd
;
392 char charBuffer
[UFILE_CHARBUFFER_SIZE
];
393 u_localized_string
*str
;
395 if (f
->fFile
== NULL
) {
396 /* There is nothing to do. It's a string. */
401 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
402 if (f
->fFileno
== 0 && dataSize
> 0) {
403 /* Don't read from stdin too many times. There is still some data. */
407 /* shift the buffer if it isn't empty */
409 uprv_memmove(f
->fUCBuffer
, str
->fPos
, dataSize
* sizeof(UChar
));
413 /* record how much buffer space is available */
414 availLength
= UFILE_UCHARBUFFER_SIZE
- dataSize
;
416 /* Determine the # of codepage bytes needed to fill our UChar buffer */
417 /* weiv: if converter is NULL, we use invariant converter with charwidth = 1)*/
418 maxCPBytes
= availLength
/ (f
->fConverter
!=NULL
?(2*ucnv_getMinCharSize(f
->fConverter
)):1);
420 /* Read in the data to convert */
421 if (f
->fFileno
== 0) {
422 /* Special case. Read from stdin one line at a time. */
423 char *retStr
= fgets(charBuffer
, ufmt_min(maxCPBytes
, UFILE_CHARBUFFER_SIZE
), f
->fFile
);
424 bytesRead
= (retStr
? uprv_strlen(charBuffer
) : 0);
428 bytesRead
= (int32_t)fread(charBuffer
,
430 ufmt_min(maxCPBytes
, UFILE_CHARBUFFER_SIZE
),
434 /* Set up conversion parameters */
435 status
= U_ZERO_ERROR
;
436 mySource
= charBuffer
;
437 mySourceEnd
= charBuffer
+ bytesRead
;
438 myTarget
= f
->fUCBuffer
+ dataSize
;
439 bufferSize
= UFILE_UCHARBUFFER_SIZE
;
441 if(f
->fConverter
!= NULL
) { /* We have a valid converter */
442 /* Perform the conversion */
443 ucnv_toUnicode(f
->fConverter
,
445 f
->fUCBuffer
+ bufferSize
,
449 (UBool
)(feof(f
->fFile
) != 0),
452 } else { /*weiv: do the invariant conversion */
453 u_charsToUChars(mySource
, myTarget
, bytesRead
);
454 myTarget
+= bytesRead
;
457 /* update the pointers into our array */
458 str
->fPos
= str
->fBuffer
;
459 str
->fLimit
= myTarget
;
462 U_CAPI UChar
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
473 u_localized_string
*str
;
476 /* Caller screwed up. We need to write the null terminatior. */
480 /* fill the buffer if needed */
482 if (str
->fPos
>= str
->fLimit
) {
483 ufile_fill_uchar_buffer(f
);
486 /* subtract 1 from n to compensate for the terminator */
489 /* determine the amount of data in the buffer */
490 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
492 /* if 0 characters were left, return 0 */
496 /* otherwise, iteratively fill the buffer and copy */
500 while (dataSize
> 0 && count
< n
) {
503 /* Find how much to copy */
512 /* Copy UChars until we find the first occurrence of a delimiter character */
513 while (alias
< limit
&& !IS_FIRST_STRING_DELIMITER(*alias
)) {
515 *(sItr
++) = *(alias
++);
517 /* Preserve the newline */
518 if (alias
< limit
&& IS_FIRST_STRING_DELIMITER(*alias
)) {
519 if (CAN_HAVE_COMBINED_STRING_DELIMITER(*alias
)) {
523 *(sItr
++) = *(alias
++);
526 /* If we have a CRLF combination, preserve that too. */
528 if (currDelim
&& IS_COMBINED_STRING_DELIMITER(currDelim
, *alias
)) {
530 *(sItr
++) = *(alias
++);
532 currDelim
= 1; /* This isn't a newline, but it's used to say
533 that we should break later. We've checked all
534 possible newline combinations even across buffer
538 /* update the current buffer position */
541 /* if we found a delimiter */
542 if (currDelim
== 1) {
547 /* refill the buffer */
548 ufile_fill_uchar_buffer(f
);
550 /* determine the amount of data in the buffer */
551 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
554 /* add the terminator and return s */
559 U_CFUNC UBool U_EXPORT2
560 ufile_getch(UFILE
*f
, UChar
*ch
)
562 UBool isValidChar
= FALSE
;
565 /* if we have an available character in the buffer, return it */
566 if(f
->str
.fPos
< f
->str
.fLimit
){
567 *ch
= *(f
->str
.fPos
)++;
571 /* otherwise, fill the buffer and return the next character */
572 if(f
->str
.fPos
>= f
->str
.fLimit
) {
573 ufile_fill_uchar_buffer(f
);
575 if(f
->str
.fPos
< f
->str
.fLimit
) {
576 *ch
= *(f
->str
.fPos
)++;
583 U_CAPI UChar U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
591 U_CFUNC UBool U_EXPORT2
592 ufile_getch32(UFILE
*f
, UChar32
*c32
)
594 UBool isValidChar
= FALSE
;
595 u_localized_string
*str
;
599 /* Fill the buffer if it is empty */
601 if (f
&& str
->fPos
+ 1 >= str
->fLimit
) {
602 ufile_fill_uchar_buffer(f
);
605 /* Get the next character in the buffer */
606 if (str
->fPos
< str
->fLimit
) {
607 *c32
= *(str
->fPos
)++;
608 if (U_IS_LEAD(*c32
)) {
609 if (str
->fPos
< str
->fLimit
) {
610 UChar c16
= *(str
->fPos
)++;
611 *c32
= U16_GET_SUPPLEMENTARY(*c32
, c16
);
626 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
630 ufile_getch32(f
, &ch
);
634 U_CAPI UChar32 U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
635 u_fungetc(UChar32 ch
,
638 u_localized_string
*str
;
642 /* if we're at the beginning of the buffer, sorry! */
643 if (str
->fPos
== str
->fBuffer
644 || (U_IS_LEAD(ch
) && (str
->fPos
- 1) == str
->fBuffer
))
649 /* otherwise, put the character back */
650 /* Remember, read them back on in the reverse order. */
652 if (*--(str
->fPos
) != U16_TRAIL(ch
)
653 || *--(str
->fPos
) != U16_LEAD(ch
))
658 else if (*--(str
->fPos
) != ch
) {
665 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
666 u_file_read( UChar
*chars
,
672 u_localized_string
*str
= &f
->str
;
676 /* determine the amount of data in the buffer */
677 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
679 /* fill the buffer */
680 ufile_fill_uchar_buffer(f
);
681 dataSize
= (int32_t)(str
->fLimit
- str
->fPos
);
684 /* Make sure that we don't read too much */
685 if (dataSize
> (count
- read
)) {
686 dataSize
= count
- read
;
689 /* copy the current data in the buffer */
690 memcpy(chars
+ read
, str
->fPos
, dataSize
* sizeof(UChar
));
692 /* update number of items read */
695 /* update the current buffer position */
696 str
->fPos
+= dataSize
;
698 while (dataSize
!= 0 && read
< count
);