#include "unicode/ustdio.h"
#include "unicode/putil.h"
#include "cmemory.h"
#include "ufile.h"
#include "ufmt_cmn.h"
#include "unicode/ucnv.h"
#include "unicode/ustring.h"
#include <string.h>
#define DELIM_CR 0x000D
#define DELIM_LF 0x000A
static const char copyright[] = U_COPYRIGHT_STRING;
#ifdef WIN32
static const UChar DELIMITERS [] = { DELIM_CR, DELIM_LF, 0x0000 };
static const uint32_t DELIMITERS_LEN = 2;
#else
static const UChar DELIMITERS [] = { DELIM_LF, 0x0000 };
static const uint32_t DELIMITERS_LEN = 1;
#endif
#define IS_STRING_DELIMITER(s) (UBool)( (s) == DELIM_CR || \
(s) == DELIM_LF )
#if !UCONFIG_NO_TRANSLITERATION
U_CAPI UTransliterator* U_EXPORT2
u_fsettransliterator(UFILE *file, UFileDirection direction,
UTransliterator *adopt, UErrorCode *status)
{
UTransliterator *old = NULL;
if(file==NULL || U_FAILURE(*status))
{
return adopt;
}
if(!file)
{
*status = U_ILLEGAL_ARGUMENT_ERROR;
return adopt;
}
if(direction & U_READ)
{
*status = U_UNSUPPORTED_ERROR;
return adopt;
}
if(adopt == NULL)
{
if(file->fTranslit != NULL)
{
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;
}
return old;
}
static const UChar * u_file_translit(UFILE *f, const UChar *src, int32_t *count, UBool flush)
{
int32_t newlen;
int32_t junkCount = 0;
int32_t textLength;
int32_t textLimit;
UTransPosition pos;
UErrorCode status = U_ZERO_ERROR;
if(count == NULL)
{
count = &junkCount;
}
if ((!f)||(!f->fTranslit)||(!f->fTranslit->translit))
{
return src;
}
if(f->fTranslit->length > f->fTranslit->pos)
{
memmove(f->fTranslit->buffer, f->fTranslit->buffer + f->fTranslit->pos,
(f->fTranslit->length - f->fTranslit->pos)*sizeof(UChar));
}
f->fTranslit->length -= f->fTranslit->pos;
f->fTranslit->pos = 0;
newlen = (*count + f->fTranslit->length) * 4;
if(newlen > f->fTranslit->capacity)
{
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;
}
u_strncpy(f->fTranslit->buffer + f->fTranslit->length,
src,
*count);
f->fTranslit->length += *count;
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,
&textLength,
f->fTranslit->capacity,
&pos,
&status);
#ifdef _DEBUG
if(U_FAILURE(status))
{
fprintf(stderr, " Gack. Translit blew up with a %s\n", u_errorName(status));
return src;
}
#endif
*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);
#ifdef _DEBUG
if(U_FAILURE(status))
{
fprintf(stderr, " Gack. Translit(flush) blew up with a %s\n", u_errorName(status));
return src;
}
#endif
*count = textLimit;
f->fTranslit->pos = 0;
f->fTranslit->length = 0;
return f->fTranslit->buffer;
}
}
#endif
void
ufile_flush_translit(UFILE *f)
{
#if !UCONFIG_NO_TRANSLITERATION
if((!f)||(!f->fTranslit))
return;
#endif
u_file_write_flush(NULL, 0, f, TRUE);
}
void
ufile_close_translit(UFILE *f)
{
#if !UCONFIG_NO_TRANSLITERATION
if((!f)||(!f->fTranslit))
return;
#endif
ufile_flush_translit(f);
#if !UCONFIG_NO_TRANSLITERATION
if(f->fTranslit->translit)
utrans_close(f->fTranslit->translit);
if(f->fTranslit->buffer)
{
uprv_free(f->fTranslit->buffer);
}
uprv_free(f->fTranslit);
f->fTranslit = NULL;
#endif
}
U_CAPI int32_t U_EXPORT2
u_fputs(const UChar *s,
UFILE *f)
{
int32_t count = u_file_write(s, u_strlen(s), f);
count += u_file_write(DELIMITERS, DELIMITERS_LEN, f);
return count;
}
U_CAPI int32_t U_EXPORT2
u_fputc(UChar uc,
UFILE *f)
{
return u_file_write(&uc, 1, f) == 1 ? uc : EOF;
}
U_CAPI int32_t U_EXPORT2
u_file_write_flush( const UChar *chars,
int32_t count,
UFILE *f,
UBool flush)
{
UErrorCode status = U_ZERO_ERROR;
const UChar *mySource = chars;
const UChar *sourceAlias = chars;
const UChar *mySourceEnd = chars + count;
char *myTarget = f->fCharBuffer;
int32_t bufferSize = UFILE_CHARBUFFER_SIZE;
int32_t written = 0;
int32_t numConverted = 0;
#if !UCONFIG_NO_TRANSLITERATION
if((f->fTranslit) && (f->fTranslit->translit))
{
mySource = u_file_translit(f, chars, &count, flush);
sourceAlias = mySource;
mySourceEnd = mySource + count;
}
#endif
do {
status = U_ZERO_ERROR;
sourceAlias = mySource;
if(f->fConverter != NULL) {
ucnv_fromUnicode(f->fConverter,
&myTarget,
f->fCharBuffer + bufferSize,
&mySource,
mySourceEnd,
NULL,
flush,
&status);
} else {
u_UCharsToChars(mySource, myTarget, count);
myTarget += count;
}
numConverted = (int32_t)(myTarget - f->fCharBuffer);
if (numConverted > 0) {
fwrite(f->fCharBuffer,
sizeof(char),
numConverted,
f->fFile);
written += numConverted;
}
myTarget = f->fCharBuffer;
}
while(status == U_BUFFER_OVERFLOW_ERROR);
return written;
}
U_CAPI int32_t U_EXPORT2
u_file_write( const UChar *chars,
int32_t count,
UFILE *f)
{
return u_file_write_flush(chars,count,f,FALSE);
}
void
ufile_fill_uchar_buffer(UFILE *f)
{
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;
dataSize = (int32_t)(f->fUCLimit - f->fUCPos);
if(dataSize != 0) {
memmove(f->fUCBuffer,
f->fUCPos,
dataSize * sizeof(UChar));
}
availLength = UFILE_UCHARBUFFER_SIZE - dataSize;
maxCPBytes = availLength / (f->fConverter!=NULL?(2*ucnv_getMinCharSize(f->fConverter)):1);
bytesRead = (int32_t)fread(f->fCharBuffer,
sizeof(char),
ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE),
f->fFile);
status = U_ZERO_ERROR;
mySource = f->fCharBuffer;
mySourceEnd = f->fCharBuffer + bytesRead;
myTarget = f->fUCBuffer + dataSize;
bufferSize = UFILE_UCHARBUFFER_SIZE;
if(f->fConverter != NULL) {
ucnv_toUnicode(f->fConverter,
&myTarget,
f->fUCBuffer + bufferSize,
&mySource,
mySourceEnd,
NULL,
(UBool)(feof(f->fFile) != 0),
&status);
} else {
u_charsToUChars(mySource, myTarget, bytesRead);
myTarget += bytesRead;
}
f->fUCPos = f->fUCBuffer;
f->fUCLimit = myTarget;
}
U_CAPI UChar* U_EXPORT2
u_fgets(UFILE *f,
int32_t n,
UChar *s)
{
int32_t dataSize;
int32_t count;
UChar *alias;
UChar *limit;
UChar *sItr;
if (n <= 0) {
return NULL;
}
if (f->fUCPos >= f->fUCLimit) {
ufile_fill_uchar_buffer(f);
}
--n;
dataSize = (int32_t)(f->fUCLimit - f->fUCPos);
if (dataSize == 0)
return NULL;
count = 0;
sItr = s;
while (dataSize > 0 && count < n) {
alias = f->fUCPos;
if (dataSize < n) {
limit = f->fUCLimit;
}
else {
limit = alias + n;
}
while (alias < limit && !IS_STRING_DELIMITER(*alias)) {
count++;
*(sItr++) = *(alias++);
}
if (alias < limit && IS_STRING_DELIMITER(*alias)) {
count++;
*(sItr++) = *(alias++);
}
f->fUCPos = alias;
if (alias < f->fUCLimit) {
break;
}
ufile_fill_uchar_buffer(f);
dataSize = (int32_t)(f->fUCLimit - f->fUCPos);
}
*sItr = 0x0000;
return s;
}
U_CAPI UChar U_EXPORT2
u_fgetc(UFILE *f)
{
if(f->fUCPos < f->fUCLimit)
return *(f->fUCPos)++;
else {
ufile_fill_uchar_buffer(f);
if(f->fUCPos < f->fUCLimit)
return *(f->fUCPos)++;
else
return 0xFFFF;
}
}
static UChar U_CALLCONV
_charAt(int32_t offset, void *context) {
return ((UFILE*) context)->fUCPos[offset];
}
U_CAPI UChar32 U_EXPORT2
u_fgetcx(UFILE *f)
{
int32_t length;
int32_t offset;
UChar32 c32;
UChar c16;
if (f->fUCPos >= f->fUCLimit) {
ufile_fill_uchar_buffer(f);
}
if (f->fUCPos < f->fUCLimit) {
c16 = *(f->fUCPos)++;
} else {
c16 = U_EOF;
}
if (c16 != 0x005C ) {
return c16;
}
length = (int32_t)(f->fUCLimit - f->fUCPos);
if (length < 10) {
ufile_fill_uchar_buffer(f);
length = (int32_t)(f->fUCLimit - f->fUCPos);
}
offset = 0;
c32 = u_unescapeAt(_charAt, &offset, length, (void*)f);
f->fUCPos += offset;
return c32;
}
U_CAPI UChar U_EXPORT2
u_fungetc(UChar c,
UFILE *f)
{
if(f->fUCPos == f->fUCBuffer)
return 0xFFFF;
else {
*--(f->fUCPos) = c;
return c;
}
}
U_CAPI int32_t U_EXPORT2
u_file_read( UChar *chars,
int32_t count,
UFILE *f)
{
int32_t dataSize;
int32_t read;
ufile_fill_uchar_buffer(f);
dataSize = (int32_t)(f->fUCLimit - f->fUCPos);
if(dataSize > count) {
memcpy(chars, f->fUCPos, count * sizeof(UChar));
f->fUCPos += count;
return count;
}
read = 0;
do {
dataSize = (int32_t)(f->fUCLimit - f->fUCPos);
memcpy(chars + read, f->fUCPos, dataSize * sizeof(UChar));
read += dataSize;
f->fUCPos += dataSize;
ufile_fill_uchar_buffer(f);
} while(dataSize != 0 && read < count);
return read;
}