2001-05-10 16:54:09 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998-2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File ucbuf.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 05/10/01 Ram Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "filestrm.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "ucbuf.h"
|
2001-05-16 01:09:06 +00:00
|
|
|
|
2001-05-10 16:54:09 +00:00
|
|
|
#define MAX_BUF 1000
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* Autodetects UTF8, UTF-16-BigEndian and UTF-16-LittleEndian BOMs*/
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_autodetect(FileStream* in,const char** cp){
|
2001-05-10 16:54:09 +00:00
|
|
|
UBool autodetect = FALSE;
|
|
|
|
char start[3];
|
|
|
|
int cap =T_FileStream_size(in);
|
2001-05-22 15:54:03 +00:00
|
|
|
if(cap>0){
|
|
|
|
T_FileStream_read(in, start, 3);
|
|
|
|
if(start[0] == '\xFE' && start[1] == '\xFF') {
|
|
|
|
*cp = "UTF16_BigEndian";
|
|
|
|
autodetect = TRUE;
|
|
|
|
} else if(start[0] == '\xFF' && start[1] == '\xFE') {
|
|
|
|
*cp = "UTF16_LittleEndian";
|
|
|
|
autodetect = TRUE;
|
|
|
|
} else if(start[0] == '\xEF' && start[1] == '\xBB' && start[2] == '\xBF') {
|
|
|
|
*cp = "UTF8";
|
|
|
|
autodetect = TRUE;
|
|
|
|
}
|
|
|
|
if(!autodetect){
|
|
|
|
T_FileStream_rewind(in);
|
|
|
|
}
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
return autodetect;
|
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* fill the uchar buffer */
|
2001-05-16 01:09:06 +00:00
|
|
|
static UCHARBUF*
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){
|
|
|
|
UChar* pTarget=NULL;
|
|
|
|
UChar* target=NULL;
|
|
|
|
const char* source=NULL;
|
|
|
|
char* cbuf =NULL;
|
|
|
|
int numRead=0;
|
|
|
|
int offset=0;
|
|
|
|
|
|
|
|
cbuf =(char*)uprv_malloc(sizeof(char) * MAX_BUF);
|
|
|
|
if(buf->buffer==NULL){
|
|
|
|
/* allocate buffers */
|
|
|
|
pTarget = (UChar*) uprv_malloc(sizeof(UChar)* MAX_BUF);
|
|
|
|
}else{
|
|
|
|
pTarget = buf->buffer;
|
|
|
|
/* check if we arrived here without exhausting the buffer*/
|
|
|
|
if(buf->currentPos<buf->bufLimit){
|
|
|
|
offset= buf->bufLimit-buf->currentPos;
|
|
|
|
memmove(buf->buffer,buf->currentPos,offset* sizeof(UChar));
|
|
|
|
memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_BUF-offset));
|
|
|
|
}else{
|
|
|
|
memset(pTarget,0xff,sizeof(UChar)*MAX_BUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the file */
|
2001-05-10 16:54:09 +00:00
|
|
|
numRead=T_FileStream_read(buf->in,cbuf,MAX_BUF-offset);
|
2001-05-10 21:43:01 +00:00
|
|
|
buf->remaining-=numRead;
|
|
|
|
|
|
|
|
target=pTarget;
|
|
|
|
/* convert the bytes */
|
|
|
|
if(buf->conv){
|
|
|
|
/* since state is saved in the converter we add offset to source*/
|
|
|
|
target = pTarget+offset;
|
|
|
|
source = cbuf;
|
2001-05-22 15:54:03 +00:00
|
|
|
ucnv_toUnicode(buf->conv,&target,target+numRead,&source,source+numRead,NULL,(UBool)(buf->remaining==0),err);
|
2001-05-10 21:43:01 +00:00
|
|
|
numRead= target-pTarget;
|
|
|
|
if(U_FAILURE(*err)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
u_charsToUChars(cbuf,target+offset,numRead);
|
|
|
|
numRead=((buf->remaining>MAX_BUF)? MAX_BUF:numRead+offset);
|
|
|
|
}
|
|
|
|
buf->buffer= pTarget;
|
|
|
|
buf->currentPos = pTarget;
|
|
|
|
buf->bufLimit=pTarget+numRead;
|
2001-05-22 15:54:03 +00:00
|
|
|
uprv_free(cbuf);
|
2001-05-10 21:43:01 +00:00
|
|
|
return buf;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* get a UChar from the stream*/
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_getc(UCHARBUF* buf,UErrorCode* err){
|
|
|
|
UChar32 c =0;
|
|
|
|
if(buf->currentPos<buf->bufLimit){
|
|
|
|
c = *(buf->currentPos);
|
|
|
|
buf->currentPos++;
|
|
|
|
return c;
|
|
|
|
}else{
|
|
|
|
if(buf->remaining==0){
|
|
|
|
return U_EOF;
|
|
|
|
}
|
|
|
|
buf=ucbuf_fillucbuf(buf,err);
|
|
|
|
if(U_FAILURE(*err)){
|
|
|
|
return U_EOF;
|
|
|
|
}
|
|
|
|
c = *(buf->currentPos);
|
|
|
|
buf->currentPos++;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* u_unescapeAt() callback to return a UChar*/
|
2001-05-10 21:43:01 +00:00
|
|
|
static UChar
|
|
|
|
_charAt(int32_t offset, void *context) {
|
2001-05-10 16:54:09 +00:00
|
|
|
return ((UCHARBUF*) context)->currentPos[offset];
|
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* getc and escape it */
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2001-05-10 16:54:09 +00:00
|
|
|
ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) {
|
|
|
|
int32_t length;
|
|
|
|
int32_t offset;
|
|
|
|
UChar32 c32;
|
|
|
|
UChar c16;
|
|
|
|
|
|
|
|
/* Fill the buffer if it is empty */
|
|
|
|
if (buf->currentPos >=buf->bufLimit) {
|
|
|
|
ucbuf_fillucbuf(buf,err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next character in the buffer */
|
|
|
|
if (buf->currentPos < buf->bufLimit) {
|
|
|
|
c16 = *(buf->currentPos)++;
|
|
|
|
} else {
|
|
|
|
c16 = U_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it isn't a backslash, return it */
|
|
|
|
if (c16 != 0x005C /*'\\'*/) {
|
|
|
|
return c16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the amount of data in the buffer */
|
|
|
|
length = buf->bufLimit-buf->currentPos;
|
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
/* The longest escape sequence is \Uhhhhhhhh; make sure
|
2001-05-10 16:54:09 +00:00
|
|
|
we have at least that many characters */
|
|
|
|
if (length < 10) {
|
|
|
|
|
|
|
|
/* fill the buffer */
|
|
|
|
ucbuf_fillucbuf(buf,err);
|
|
|
|
length = buf->bufLimit-buf->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the escape */
|
|
|
|
offset = 0;
|
|
|
|
c32 = u_unescapeAt(_charAt, &offset, length, (void*)buf);
|
|
|
|
|
|
|
|
/* Update the current buffer position */
|
|
|
|
buf->currentPos += offset;
|
|
|
|
|
|
|
|
return c32;
|
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* open a UCHARBUF */
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI UCHARBUF* U_EXPORT2
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_open(FileStream* in, const char* cp,UErrorCode* err){
|
|
|
|
|
|
|
|
UCHARBUF* buf =(UCHARBUF*) uprv_malloc(sizeof(UCHARBUF));
|
|
|
|
if(U_FAILURE(*err)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(buf){
|
|
|
|
buf->in=in;
|
2001-05-16 01:09:06 +00:00
|
|
|
buf->remaining=T_FileStream_size(in);
|
2001-05-10 21:43:01 +00:00
|
|
|
buf->buffer=NULL;
|
|
|
|
buf->currentPos=NULL;
|
|
|
|
buf->bufLimit=NULL;
|
|
|
|
if(*cp!='\0'){
|
|
|
|
buf->conv=ucnv_open(cp,err);
|
|
|
|
}else{
|
|
|
|
buf->conv=NULL;
|
|
|
|
}
|
|
|
|
if(U_FAILURE(*err)){
|
2001-05-10 23:24:57 +00:00
|
|
|
fprintf(stderr, "Could not open codepage [%s]: %s\n", cp, u_errorName(*err));
|
2001-05-10 21:43:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf=ucbuf_fillucbuf(buf,err);
|
|
|
|
return buf;
|
|
|
|
}else{
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* TODO: this method will fail if at the
|
2001-05-22 15:54:03 +00:00
|
|
|
* begining of buffer and the uchar to unget
|
|
|
|
* is from the previous buffer. Need to implement
|
|
|
|
* system to take care of that situation.
|
|
|
|
*/
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_ungetc(UChar32 c,UCHARBUF* buf){
|
2001-05-22 15:54:03 +00:00
|
|
|
/* decrement currentPos pointer
|
|
|
|
* if not at the begining of buffer
|
|
|
|
*/
|
2001-05-10 21:43:01 +00:00
|
|
|
if(buf->currentPos!=buf->buffer){
|
|
|
|
buf->currentPos--;
|
|
|
|
}
|
2001-05-16 16:34:10 +00:00
|
|
|
}
|
2001-05-10 16:54:09 +00:00
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* frees the resources of UChar* buffer */
|
|
|
|
static void
|
|
|
|
ucbuf_closebuf(UCHARBUF* buf){
|
|
|
|
uprv_free(buf->buffer);
|
2001-05-21 19:38:13 +00:00
|
|
|
buf->buffer = NULL;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* close the buf and release resources*/
|
2001-05-16 01:09:06 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-05-10 21:43:01 +00:00
|
|
|
ucbuf_close(UCHARBUF* buf){
|
|
|
|
if(buf->conv){
|
|
|
|
ucnv_close(buf->conv);
|
|
|
|
}
|
|
|
|
buf->in=NULL;
|
|
|
|
buf->currentPos=NULL;
|
|
|
|
buf->bufLimit=NULL;
|
|
|
|
ucbuf_closebuf(buf);
|
|
|
|
uprv_free(buf);
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
2001-05-16 16:34:10 +00:00
|
|
|
|
|
|
|
/* rewind the buf and file stream */
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucbuf_rewind(UCHARBUF* buf){
|
|
|
|
if(buf){
|
|
|
|
const char* cp="";
|
|
|
|
buf->currentPos=buf->buffer;
|
|
|
|
buf->bufLimit=buf->buffer;
|
|
|
|
ucnv_reset(buf->conv);
|
|
|
|
T_FileStream_rewind(buf->in);
|
|
|
|
ucbuf_autodetect(buf->in,&cp);
|
|
|
|
buf->remaining=T_FileStream_size(buf->in);
|
|
|
|
}
|
|
|
|
}
|