1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2004-05-19 20:59:59 +00:00
|
|
|
* Copyright (C) 1998-2004, International Business Machines
|
2000-01-13 23:54:23 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File ustdio.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 11/18/98 stephen Creation.
|
|
|
|
* 03/12/99 stephen Modified for new C API.
|
|
|
|
* 07/19/99 stephen Fixed read() and gets()
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2000-01-05 19:40:01 +00:00
|
|
|
#include "unicode/ustdio.h"
|
2001-01-03 00:18:57 +00:00
|
|
|
#include "unicode/putil.h"
|
2002-03-14 00:40:08 +00:00
|
|
|
#include "cmemory.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ufile.h"
|
|
|
|
#include "ufmt_cmn.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "unicode/ustring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DELIM_LF 0x000A
|
2003-09-22 23:07:05 +00:00
|
|
|
#define DELIM_VT 0x000B
|
|
|
|
#define DELIM_FF 0x000C
|
|
|
|
#define DELIM_CR 0x000D
|
|
|
|
#define DELIM_NEL 0x0085
|
|
|
|
#define DELIM_LS 0x2028
|
|
|
|
#define DELIM_PS 0x2029
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-06-02 18:56:58 +00:00
|
|
|
/* Leave this copyright notice here! */
|
|
|
|
static const char copyright[] = U_COPYRIGHT_STRING;
|
|
|
|
|
2003-09-22 23:07:05 +00:00
|
|
|
/* TODO: is this correct for all codepages? Should we just use \n and let the converter handle it? */
|
2002-02-23 00:34:39 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
static const UChar DELIMITERS [] = { DELIM_CR, DELIM_LF, 0x0000 };
|
|
|
|
static const uint32_t DELIMITERS_LEN = 2;
|
2004-06-04 17:43:53 +00:00
|
|
|
/* TODO: Default newline writing should be detected based upon the converter being used. */
|
2002-02-23 00:34:39 +00:00
|
|
|
#else
|
|
|
|
static const UChar DELIMITERS [] = { DELIM_LF, 0x0000 };
|
|
|
|
static const uint32_t DELIMITERS_LEN = 1;
|
|
|
|
#endif
|
|
|
|
|
2003-09-22 23:07:05 +00:00
|
|
|
#define IS_FIRST_STRING_DELIMITER(c1) \
|
|
|
|
(UBool)((DELIM_LF <= (c1) && (c1) <= DELIM_CR) \
|
|
|
|
|| (c1) == DELIM_NEL \
|
|
|
|
|| (c1) == DELIM_LS \
|
|
|
|
|| (c1) == DELIM_PS)
|
|
|
|
#define CAN_HAVE_COMBINED_STRING_DELIMITER(c1) (UBool)((c1) == DELIM_CR)
|
|
|
|
#define IS_COMBINED_STRING_DELIMITER(c1, c2) \
|
|
|
|
(UBool)((c1) == DELIM_CR && (c2) == DELIM_LF)
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-13 16:11:04 +00:00
|
|
|
U_CAPI UTransliterator* U_EXPORT2
|
|
|
|
u_fsettransliterator(UFILE *file, UFileDirection direction,
|
2002-03-14 00:40:08 +00:00
|
|
|
UTransliterator *adopt, UErrorCode *status)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
UTransliterator *old = NULL;
|
|
|
|
|
2004-05-20 18:22:13 +00:00
|
|
|
if(U_FAILURE(*status))
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
return adopt;
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
if(!file)
|
|
|
|
{
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return adopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(direction & U_READ)
|
|
|
|
{
|
|
|
|
/** TODO: implement */
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return adopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(adopt == NULL) /* they are clearing it */
|
|
|
|
{
|
|
|
|
if(file->fTranslit != NULL)
|
|
|
|
{
|
|
|
|
/* TODO: Check side */
|
|
|
|
old = file->fTranslit->translit;
|
|
|
|
uprv_free(file->fTranslit->buffer);
|
|
|
|
file->fTranslit->buffer=NULL;
|
|
|
|
uprv_free(file->fTranslit);
|
|
|
|
file->fTranslit=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(file->fTranslit == NULL)
|
|
|
|
{
|
|
|
|
file->fTranslit = (UFILETranslitBuffer*) uprv_malloc(sizeof(UFILETranslitBuffer));
|
|
|
|
if(!file->fTranslit)
|
|
|
|
{
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return adopt;
|
|
|
|
}
|
|
|
|
file->fTranslit->capacity = 0;
|
|
|
|
file->fTranslit->length = 0;
|
|
|
|
file->fTranslit->pos = 0;
|
|
|
|
file->fTranslit->buffer = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
old = file->fTranslit->translit;
|
|
|
|
ufile_flush_translit(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
file->fTranslit->translit = adopt;
|
|
|
|
}
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
return old;
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2002-04-02 03:25:05 +00:00
|
|
|
static const UChar * u_file_translit(UFILE *f, const UChar *src, int32_t *count, UBool flush)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
int32_t newlen;
|
|
|
|
int32_t junkCount = 0;
|
|
|
|
int32_t textLength;
|
|
|
|
int32_t textLimit;
|
|
|
|
UTransPosition pos;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
if(count == NULL)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
count = &junkCount;
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
if ((!f)||(!f->fTranslit)||(!f->fTranslit->translit))
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
/* fast path */
|
|
|
|
return src;
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* First: slide over everything */
|
|
|
|
if(f->fTranslit->length > f->fTranslit->pos)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
memmove(f->fTranslit->buffer, f->fTranslit->buffer + f->fTranslit->pos,
|
|
|
|
(f->fTranslit->length - f->fTranslit->pos)*sizeof(UChar));
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
f->fTranslit->length -= f->fTranslit->pos; /* always */
|
|
|
|
f->fTranslit->pos = 0;
|
|
|
|
|
|
|
|
/* Calculate new buffer size needed */
|
|
|
|
newlen = (*count + f->fTranslit->length) * 4;
|
|
|
|
|
|
|
|
if(newlen > f->fTranslit->capacity)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
if(f->fTranslit->buffer == NULL)
|
|
|
|
{
|
|
|
|
f->fTranslit->buffer = (UChar*)uprv_malloc(newlen * sizeof(UChar));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f->fTranslit->buffer = (UChar*)uprv_realloc(f->fTranslit->buffer, newlen * sizeof(UChar));
|
|
|
|
}
|
|
|
|
f->fTranslit->capacity = newlen;
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* Now, copy any data over */
|
|
|
|
u_strncpy(f->fTranslit->buffer + f->fTranslit->length,
|
|
|
|
src,
|
|
|
|
*count);
|
|
|
|
f->fTranslit->length += *count;
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* Now, translit in place as much as we can */
|
|
|
|
if(flush == FALSE)
|
|
|
|
{
|
|
|
|
textLength = f->fTranslit->length;
|
|
|
|
pos.contextStart = 0;
|
|
|
|
pos.contextLimit = textLength;
|
|
|
|
pos.start = 0;
|
|
|
|
pos.limit = textLength;
|
|
|
|
|
|
|
|
utrans_transIncrementalUChars(f->fTranslit->translit,
|
|
|
|
f->fTranslit->buffer, /* because we shifted */
|
|
|
|
&textLength,
|
|
|
|
f->fTranslit->capacity,
|
|
|
|
&pos,
|
|
|
|
&status);
|
|
|
|
|
|
|
|
/* now: start/limit point to the transliterated text */
|
|
|
|
/* Transliterated is [buffer..pos.start) */
|
|
|
|
*count = pos.start;
|
|
|
|
f->fTranslit->pos = pos.start;
|
|
|
|
f->fTranslit->length = pos.limit;
|
|
|
|
|
|
|
|
return f->fTranslit->buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textLength = f->fTranslit->length;
|
|
|
|
textLimit = f->fTranslit->length;
|
|
|
|
|
|
|
|
utrans_transUChars(f->fTranslit->translit,
|
|
|
|
f->fTranslit->buffer,
|
|
|
|
&textLength,
|
|
|
|
f->fTranslit->capacity,
|
|
|
|
0,
|
|
|
|
&textLimit,
|
|
|
|
&status);
|
|
|
|
|
|
|
|
/* out: converted len */
|
|
|
|
*count = textLimit;
|
|
|
|
|
|
|
|
/* Set pointers to 0 */
|
|
|
|
f->fTranslit->pos = 0;
|
|
|
|
f->fTranslit->length = 0;
|
|
|
|
|
|
|
|
return f->fTranslit->buffer;
|
|
|
|
}
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
void
|
2002-03-13 16:11:04 +00:00
|
|
|
ufile_flush_translit(UFILE *f)
|
|
|
|
{
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
2002-03-14 00:40:08 +00:00
|
|
|
if((!f)||(!f->fTranslit))
|
|
|
|
return;
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
u_file_write_flush(NULL, 0, f, TRUE);
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
void
|
2002-03-13 16:11:04 +00:00
|
|
|
ufile_close_translit(UFILE *f)
|
|
|
|
{
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
2002-03-14 00:40:08 +00:00
|
|
|
if((!f)||(!f->fTranslit))
|
|
|
|
return;
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
ufile_flush_translit(f);
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
2002-03-14 00:40:08 +00:00
|
|
|
if(f->fTranslit->translit)
|
|
|
|
utrans_close(f->fTranslit->translit);
|
|
|
|
|
|
|
|
if(f->fTranslit->buffer)
|
|
|
|
{
|
|
|
|
uprv_free(f->fTranslit->buffer);
|
|
|
|
}
|
2002-03-13 16:11:04 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
uprv_free(f->fTranslit);
|
|
|
|
f->fTranslit = NULL;
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/* Input/output */
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fputs(const UChar *s,
|
2002-03-14 00:40:08 +00:00
|
|
|
UFILE *f)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
int32_t count = u_file_write(s, u_strlen(s), f);
|
|
|
|
count += u_file_write(DELIMITERS, DELIMITERS_LEN, f);
|
|
|
|
return count;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 22:55:14 +00:00
|
|
|
U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fputc(UChar32 uc,
|
2002-03-14 00:40:08 +00:00
|
|
|
UFILE *f)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-05-27 22:55:14 +00:00
|
|
|
UChar buf[2];
|
|
|
|
int32_t idx = 0;
|
|
|
|
UBool isError = FALSE;
|
|
|
|
|
|
|
|
U16_APPEND(buf, idx, sizeof(buf)/sizeof(*buf), uc, isError);
|
|
|
|
if (isError) {
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return u_file_write(buf, idx, f) == idx ? uc : EOF;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-03-13 16:11:04 +00:00
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2002-03-14 00:40:08 +00:00
|
|
|
u_file_write_flush( const UChar *chars,
|
|
|
|
int32_t count,
|
|
|
|
UFILE *f,
|
|
|
|
UBool flush)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
/* Set up conversion parameters */
|
2004-04-16 01:08:35 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const UChar *mySource = chars;
|
|
|
|
const UChar *sourceAlias = chars;
|
|
|
|
const UChar *mySourceEnd;
|
|
|
|
char charBuffer[UFILE_CHARBUFFER_SIZE];
|
|
|
|
char *myTarget = charBuffer;
|
|
|
|
int32_t written = 0;
|
|
|
|
int32_t numConverted = 0;
|
2003-10-11 02:16:12 +00:00
|
|
|
|
2004-05-10 03:59:44 +00:00
|
|
|
if (!f->fFile) {
|
|
|
|
int32_t charsLeft = f->str.fLimit - f->str.fPos;
|
|
|
|
if (flush && charsLeft > count) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
written = ufmt_min(count, charsLeft);
|
|
|
|
u_strncpy(f->str.fPos, chars, written);
|
|
|
|
f->str.fPos += written;
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2003-10-11 02:16:12 +00:00
|
|
|
if (count < 0) {
|
|
|
|
count = u_strlen(chars);
|
|
|
|
}
|
|
|
|
mySourceEnd = chars + count;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
2002-03-14 00:40:08 +00:00
|
|
|
if((f->fTranslit) && (f->fTranslit->translit))
|
|
|
|
{
|
|
|
|
/* Do the transliteration */
|
|
|
|
mySource = u_file_translit(f, chars, &count, flush);
|
|
|
|
sourceAlias = mySource;
|
|
|
|
mySourceEnd = mySource + count;
|
|
|
|
}
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* Perform the conversion in a loop */
|
|
|
|
do {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
sourceAlias = mySource;
|
|
|
|
if(f->fConverter != NULL) { /* We have a valid converter */
|
|
|
|
ucnv_fromUnicode(f->fConverter,
|
|
|
|
&myTarget,
|
2004-04-16 01:08:35 +00:00
|
|
|
charBuffer + UFILE_CHARBUFFER_SIZE,
|
2002-03-14 00:40:08 +00:00
|
|
|
&mySource,
|
|
|
|
mySourceEnd,
|
|
|
|
NULL,
|
|
|
|
flush,
|
|
|
|
&status);
|
|
|
|
} else { /*weiv: do the invariant conversion */
|
|
|
|
u_UCharsToChars(mySource, myTarget, count);
|
|
|
|
myTarget += count;
|
|
|
|
}
|
2004-04-16 01:08:35 +00:00
|
|
|
numConverted = (int32_t)(myTarget - charBuffer);
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2002-03-26 05:33:56 +00:00
|
|
|
if (numConverted > 0) {
|
|
|
|
/* write the converted bytes */
|
2004-04-16 01:08:35 +00:00
|
|
|
fwrite(charBuffer,
|
2002-03-26 05:33:56 +00:00
|
|
|
sizeof(char),
|
|
|
|
numConverted,
|
|
|
|
f->fFile);
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2002-03-26 05:33:56 +00:00
|
|
|
written += numConverted;
|
|
|
|
}
|
2004-04-16 01:08:35 +00:00
|
|
|
myTarget = charBuffer;
|
2002-03-14 00:40:08 +00:00
|
|
|
}
|
|
|
|
while(status == U_BUFFER_OVERFLOW_ERROR);
|
|
|
|
|
|
|
|
/* return # of chars written */
|
|
|
|
return written;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-03-13 16:11:04 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
2002-03-14 00:40:08 +00:00
|
|
|
u_file_write( const UChar *chars,
|
|
|
|
int32_t count,
|
|
|
|
UFILE *f)
|
2002-03-13 16:11:04 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
return u_file_write_flush(chars,count,f,FALSE);
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/* private function used for buffering input */
|
|
|
|
void
|
|
|
|
ufile_fill_uchar_buffer(UFILE *f)
|
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
UErrorCode status;
|
|
|
|
const char *mySource;
|
|
|
|
const char *mySourceEnd;
|
|
|
|
UChar *myTarget;
|
|
|
|
int32_t bufferSize;
|
|
|
|
int32_t maxCPBytes;
|
|
|
|
int32_t bytesRead;
|
|
|
|
int32_t availLength;
|
|
|
|
int32_t dataSize;
|
|
|
|
char charBuffer[UFILE_CHARBUFFER_SIZE];
|
|
|
|
u_localized_string *str;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2004-04-16 04:27:29 +00:00
|
|
|
if (f->fFile == NULL) {
|
|
|
|
/* There is nothing to do. It's a string. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* shift the buffer if it isn't empty */
|
2004-04-16 01:08:35 +00:00
|
|
|
str = &f->str;
|
|
|
|
dataSize = (int32_t)(str->fLimit - str->fPos);
|
2002-03-14 00:40:08 +00:00
|
|
|
if(dataSize != 0) {
|
|
|
|
memmove(f->fUCBuffer,
|
2004-04-16 01:08:35 +00:00
|
|
|
str->fPos,
|
2002-03-14 00:40:08 +00:00
|
|
|
dataSize * sizeof(UChar));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* record how much buffer space is available */
|
|
|
|
availLength = UFILE_UCHARBUFFER_SIZE - dataSize;
|
|
|
|
|
|
|
|
/* Determine the # of codepage bytes needed to fill our UChar buffer */
|
|
|
|
/* weiv: if converter is NULL, we use invariant converter with charwidth = 1)*/
|
|
|
|
maxCPBytes = availLength / (f->fConverter!=NULL?(2*ucnv_getMinCharSize(f->fConverter)):1);
|
|
|
|
|
|
|
|
/* Read in the data to convert */
|
2004-04-16 01:08:35 +00:00
|
|
|
bytesRead = (int32_t)fread(charBuffer,
|
2002-03-14 00:40:08 +00:00
|
|
|
sizeof(char),
|
|
|
|
ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE),
|
|
|
|
f->fFile);
|
2000-10-26 21:42:33 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* Set up conversion parameters */
|
2002-04-30 20:43:51 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2004-04-16 01:08:35 +00:00
|
|
|
mySource = charBuffer;
|
|
|
|
mySourceEnd = charBuffer + bytesRead;
|
2002-04-30 20:43:51 +00:00
|
|
|
myTarget = f->fUCBuffer + dataSize;
|
|
|
|
bufferSize = UFILE_UCHARBUFFER_SIZE;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
if(f->fConverter != NULL) { /* We have a valid converter */
|
|
|
|
/* Perform the conversion */
|
|
|
|
ucnv_toUnicode(f->fConverter,
|
|
|
|
&myTarget,
|
|
|
|
f->fUCBuffer + bufferSize,
|
|
|
|
&mySource,
|
|
|
|
mySourceEnd,
|
|
|
|
NULL,
|
|
|
|
(UBool)(feof(f->fFile) != 0),
|
|
|
|
&status);
|
|
|
|
|
|
|
|
} else { /*weiv: do the invariant conversion */
|
|
|
|
u_charsToUChars(mySource, myTarget, bytesRead);
|
|
|
|
myTarget += bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update the pointers into our array */
|
2004-04-16 01:08:35 +00:00
|
|
|
str->fPos = str->fBuffer;
|
|
|
|
str->fLimit = myTarget;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
2003-10-10 20:55:24 +00:00
|
|
|
u_fgets(UChar *s,
|
|
|
|
int32_t n,
|
|
|
|
UFILE *f)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
int32_t dataSize;
|
|
|
|
int32_t count;
|
|
|
|
UChar *alias;
|
2004-04-16 01:08:35 +00:00
|
|
|
const UChar *limit;
|
2002-08-23 22:13:14 +00:00
|
|
|
UChar *sItr;
|
2003-09-22 23:07:05 +00:00
|
|
|
UChar currDelim = 0;
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_string *str;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-27 21:17:25 +00:00
|
|
|
if (n <= 0) {
|
|
|
|
/* Caller screwed up. We need to write the null terminatior. */
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-23 22:13:14 +00:00
|
|
|
/* fill the buffer if needed */
|
2004-04-16 01:08:35 +00:00
|
|
|
str = &f->str;
|
|
|
|
if (str->fPos >= str->fLimit) {
|
2002-08-23 22:13:14 +00:00
|
|
|
ufile_fill_uchar_buffer(f);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* subtract 1 from n to compensate for the terminator */
|
|
|
|
--n;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/* determine the amount of data in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
dataSize = (int32_t)(str->fLimit - str->fPos);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-23 22:13:14 +00:00
|
|
|
/* if 0 characters were left, return 0 */
|
|
|
|
if (dataSize == 0)
|
|
|
|
return NULL;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2002-08-23 22:13:14 +00:00
|
|
|
/* otherwise, iteratively fill the buffer and copy */
|
|
|
|
count = 0;
|
|
|
|
sItr = s;
|
2003-09-22 23:07:05 +00:00
|
|
|
currDelim = 0;
|
2002-08-23 22:13:14 +00:00
|
|
|
while (dataSize > 0 && count < n) {
|
2004-04-16 01:08:35 +00:00
|
|
|
alias = str->fPos;
|
2002-08-23 22:13:14 +00:00
|
|
|
|
|
|
|
/* Find how much to copy */
|
|
|
|
if (dataSize < n) {
|
2004-04-16 01:08:35 +00:00
|
|
|
limit = str->fLimit;
|
2002-03-27 20:08:42 +00:00
|
|
|
}
|
2002-08-23 22:13:14 +00:00
|
|
|
else {
|
|
|
|
limit = alias + n;
|
2002-03-14 00:40:08 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 23:07:05 +00:00
|
|
|
if (!currDelim) {
|
|
|
|
/* Copy UChars until we find the first occurrence of a delimiter character */
|
|
|
|
while (alias < limit && !IS_FIRST_STRING_DELIMITER(*alias)) {
|
|
|
|
count++;
|
|
|
|
*(sItr++) = *(alias++);
|
|
|
|
}
|
|
|
|
/* Preserve the newline */
|
|
|
|
if (alias < limit && IS_FIRST_STRING_DELIMITER(*alias)) {
|
|
|
|
if (CAN_HAVE_COMBINED_STRING_DELIMITER(*alias)) {
|
|
|
|
currDelim = *alias;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
*(sItr++) = *(alias++);
|
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
}
|
2003-09-22 23:07:05 +00:00
|
|
|
/* If we have a CRLF combination, preserve that too. */
|
|
|
|
if (alias < limit) {
|
|
|
|
if (currDelim && IS_COMBINED_STRING_DELIMITER(currDelim, *alias)) {
|
|
|
|
count++;
|
|
|
|
*(sItr++) = *(alias++);
|
|
|
|
}
|
|
|
|
currDelim = 0;
|
2002-03-27 20:08:42 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* update the current buffer position */
|
2004-04-16 01:08:35 +00:00
|
|
|
str->fPos = alias;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* if we found a delimiter */
|
2004-04-16 01:08:35 +00:00
|
|
|
if (alias < str->fLimit && !currDelim) {
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* break out */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* refill the buffer */
|
|
|
|
ufile_fill_uchar_buffer(f);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-23 22:13:14 +00:00
|
|
|
/* determine the amount of data in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
dataSize = (int32_t)(str->fLimit - str->fPos);
|
2002-08-23 22:13:14 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* add the terminator and return s */
|
2002-08-23 22:13:14 +00:00
|
|
|
*sItr = 0x0000;
|
2002-03-14 00:40:08 +00:00
|
|
|
return s;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-04-28 23:20:46 +00:00
|
|
|
U_CFUNC UBool U_EXPORT2
|
|
|
|
ufile_getch(UFILE *f, UChar *ch)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-04-28 23:20:46 +00:00
|
|
|
UBool isValidChar = FALSE;
|
|
|
|
|
|
|
|
*ch = U_EOF;
|
2002-03-14 00:40:08 +00:00
|
|
|
/* if we have an available character in the buffer, return it */
|
2004-04-28 23:20:46 +00:00
|
|
|
if(f->str.fPos < f->str.fLimit){
|
|
|
|
*ch = *(f->str.fPos)++;
|
|
|
|
isValidChar = TRUE;
|
|
|
|
}
|
|
|
|
else if (f) {
|
|
|
|
/* otherwise, fill the buffer and return the next character */
|
2002-03-14 00:40:08 +00:00
|
|
|
ufile_fill_uchar_buffer(f);
|
2004-04-16 01:08:35 +00:00
|
|
|
if(f->str.fPos < f->str.fLimit) {
|
2004-04-28 23:20:46 +00:00
|
|
|
*ch = *(f->str.fPos)++;
|
|
|
|
isValidChar = TRUE;
|
2003-10-10 20:55:24 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
}
|
2004-04-28 23:20:46 +00:00
|
|
|
return isValidChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fgetc(UFILE *f)
|
|
|
|
{
|
|
|
|
UChar ch;
|
|
|
|
ufile_getch(f, &ch);
|
|
|
|
return ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-05-03 05:21:24 +00:00
|
|
|
U_CFUNC UBool U_EXPORT2
|
|
|
|
ufile_getch32(UFILE *f, UChar32 *c32)
|
2002-03-14 00:40:08 +00:00
|
|
|
{
|
2004-05-03 05:21:24 +00:00
|
|
|
UBool isValidChar = FALSE;
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_string *str;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2004-05-03 05:21:24 +00:00
|
|
|
*c32 = U_EOF;
|
|
|
|
|
2000-07-16 13:43:15 +00:00
|
|
|
/* Fill the buffer if it is empty */
|
2004-04-16 01:08:35 +00:00
|
|
|
str = &f->str;
|
2004-05-03 05:21:24 +00:00
|
|
|
if (f && str->fPos + 1 >= str->fLimit) {
|
2002-03-14 00:40:08 +00:00
|
|
|
ufile_fill_uchar_buffer(f);
|
2000-07-16 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next character in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
if (str->fPos < str->fLimit) {
|
2004-05-03 05:21:24 +00:00
|
|
|
*c32 = *(str->fPos)++;
|
|
|
|
if (U_IS_LEAD(*c32)) {
|
|
|
|
if (str->fPos < str->fLimit) {
|
|
|
|
UChar c16 = *(str->fPos)++;
|
|
|
|
*c32 = U16_GET_SUPPLEMENTARY(*c32, c16);
|
|
|
|
isValidChar = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*c32 = U_EOF;
|
|
|
|
}
|
2003-10-10 20:55:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2004-05-03 05:21:24 +00:00
|
|
|
isValidChar = TRUE;
|
2003-10-10 20:55:24 +00:00
|
|
|
}
|
2000-07-16 13:43:15 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
|
2004-05-03 05:21:24 +00:00
|
|
|
return isValidChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fgetcx(UFILE *f)
|
|
|
|
{
|
|
|
|
UChar32 ch;
|
|
|
|
ufile_getch32(f, &ch);
|
|
|
|
return ch;
|
2000-07-16 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
2003-10-10 20:55:24 +00:00
|
|
|
U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fungetc(UChar32 ch,
|
2002-03-14 00:40:08 +00:00
|
|
|
UFILE *f)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_string *str;
|
|
|
|
|
|
|
|
str = &f->str;
|
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* if we're at the beginning of the buffer, sorry! */
|
2004-04-16 01:08:35 +00:00
|
|
|
if (str->fPos == str->fBuffer
|
|
|
|
|| (U_IS_LEAD(ch) && (str->fPos - 1) == str->fBuffer))
|
2003-10-10 20:55:24 +00:00
|
|
|
{
|
|
|
|
ch = U_EOF;
|
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
else {
|
2003-10-10 20:55:24 +00:00
|
|
|
/* otherwise, put the character back */
|
2004-05-03 06:03:21 +00:00
|
|
|
/* Remember, read them back on in the reverse order. */
|
2003-10-10 20:55:24 +00:00
|
|
|
if (U_IS_LEAD(ch)) {
|
2004-05-03 06:03:21 +00:00
|
|
|
if (*--(str->fPos) != U16_TRAIL(ch)
|
|
|
|
|| *--(str->fPos) != U16_LEAD(ch))
|
|
|
|
{
|
|
|
|
ch = U_EOF;
|
|
|
|
}
|
2003-10-10 20:55:24 +00:00
|
|
|
}
|
2004-05-03 06:03:21 +00:00
|
|
|
else if (*--(str->fPos) != ch) {
|
|
|
|
ch = U_EOF;
|
2003-10-10 20:55:24 +00:00
|
|
|
}
|
2002-03-14 00:40:08 +00:00
|
|
|
}
|
2003-10-10 20:55:24 +00:00
|
|
|
return ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
2002-03-14 00:40:08 +00:00
|
|
|
u_file_read( UChar *chars,
|
|
|
|
int32_t count,
|
|
|
|
UFILE *f)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-14 00:40:08 +00:00
|
|
|
int32_t dataSize;
|
|
|
|
int32_t read;
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_string *str;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* fill the buffer */
|
|
|
|
ufile_fill_uchar_buffer(f);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* determine the amount of data in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
str = &f->str;
|
|
|
|
dataSize = (int32_t)(str->fLimit - str->fPos);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* if the buffer contains the amount requested, just copy */
|
|
|
|
if(dataSize > count) {
|
2004-04-16 01:08:35 +00:00
|
|
|
memcpy(chars, str->fPos, count * sizeof(UChar));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* update the current buffer position */
|
2004-04-16 01:08:35 +00:00
|
|
|
str->fPos += count;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* return # of chars read */
|
|
|
|
return count;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* otherwise, iteratively fill the buffer and copy */
|
|
|
|
read = 0;
|
|
|
|
do {
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* determine the amount of data in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
dataSize = (int32_t)(str->fLimit - str->fPos);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* copy the current data in the buffer */
|
2004-04-16 01:08:35 +00:00
|
|
|
memcpy(chars + read, str->fPos, dataSize * sizeof(UChar));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
/* update number of items read */
|
|
|
|
read += dataSize;
|
|
|
|
|
|
|
|
/* update the current buffer position */
|
2004-04-16 01:08:35 +00:00
|
|
|
str->fPos += dataSize;
|
2002-03-14 00:40:08 +00:00
|
|
|
|
|
|
|
/* refill the buffer */
|
|
|
|
ufile_fill_uchar_buffer(f);
|
|
|
|
|
|
|
|
} while(dataSize != 0 && read < count);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-14 00:40:08 +00:00
|
|
|
return read;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|