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"
|
2001-11-01 19:43:21 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
2001-05-10 16:54:09 +00:00
|
|
|
#include "filestrm.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "ucbuf.h"
|
2001-11-01 19:43:21 +00:00
|
|
|
#include <stdio.h>
|
2001-05-16 01:09:06 +00:00
|
|
|
|
2001-05-22 18:00:55 +00:00
|
|
|
#define MAX_IN_BUF 1000
|
|
|
|
#define MAX_U_BUF 1500
|
2001-11-01 19:43:21 +00:00
|
|
|
#define CONTEXT_LEN 15
|
2001-05-10 16:54:09 +00:00
|
|
|
|
2001-11-03 02:54:08 +00:00
|
|
|
struct UCHARBUF {
|
|
|
|
UChar* buffer;
|
|
|
|
UChar* currentPos;
|
|
|
|
UChar* bufLimit;
|
|
|
|
int32_t remaining;
|
|
|
|
FileStream* in;
|
|
|
|
UConverter* conv;
|
|
|
|
UBool showWarning; /* makes this API not produce any errors */
|
|
|
|
};
|
|
|
|
|
2001-05-26 01:16:37 +00:00
|
|
|
static UBool ucbuf_autodetect_nrw(FileStream* in, const char** cp,int* numRead){
|
2001-09-28 00:29:40 +00:00
|
|
|
/* initial 0xa5 bytes: make sure that if we read <4 bytes we don't misdetect something */
|
|
|
|
char start[4]={ '\xa5', '\xa5', '\xa5', '\xa5' };
|
|
|
|
UBool autodetect;
|
|
|
|
int signatureLength;
|
|
|
|
|
|
|
|
*numRead=0;
|
|
|
|
*cp="";
|
|
|
|
|
2002-09-06 22:15:37 +00:00
|
|
|
autodetect = TRUE;
|
|
|
|
*numRead=T_FileStream_read(in, start, 4); /* *numRead might be <4 */
|
|
|
|
|
|
|
|
if(*numRead<2){
|
2001-09-28 00:29:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(start[0] == '\xFE' && start[1] == '\xFF') {
|
|
|
|
*cp = "UTF-16BE";
|
|
|
|
signatureLength=2;
|
|
|
|
} else if(start[0] == '\xFF' && start[1] == '\xFE') {
|
|
|
|
if(start[2] == '\x00' && start[3] =='\x00'){
|
|
|
|
*cp="UTF-32LE";
|
|
|
|
signatureLength=4;
|
|
|
|
} else {
|
|
|
|
*cp = "UTF-16LE";
|
|
|
|
signatureLength=2;
|
|
|
|
}
|
|
|
|
} else if(start[0] == '\xEF' && start[1] == '\xBB' && start[2] == '\xBF') {
|
|
|
|
*cp = "UTF-8";
|
|
|
|
signatureLength=3;
|
|
|
|
}else if(start[0] == '\x0E' && start[1] == '\xFE' && start[2] == '\xFF'){
|
|
|
|
*cp ="SCSU";
|
|
|
|
signatureLength=3;
|
2002-02-28 01:42:40 +00:00
|
|
|
}else if(start[0] == '\x00' && start[1] == '\x00' &&
|
2001-10-25 18:47:41 +00:00
|
|
|
start[2] == '\xFE' && start[3]=='\xFF'){
|
2001-09-28 00:29:40 +00:00
|
|
|
*cp = "UTF-32BE";
|
|
|
|
signatureLength=4;
|
|
|
|
}else{
|
|
|
|
signatureLength=0;
|
|
|
|
autodetect=FALSE;
|
|
|
|
}
|
2002-02-28 17:23:53 +00:00
|
|
|
/* T_FileStream_rewind(in);
|
|
|
|
T_FileStream_read(in, start, *numRead);*/
|
|
|
|
while(signatureLength<*numRead) {
|
2001-09-28 00:29:40 +00:00
|
|
|
T_FileStream_ungetc(start[--*numRead], in);
|
2002-02-28 17:23:53 +00:00
|
|
|
}
|
2001-09-28 00:29:40 +00:00
|
|
|
return autodetect;
|
2001-05-26 01:16:37 +00:00
|
|
|
}
|
|
|
|
|
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){
|
2002-02-28 01:42:40 +00:00
|
|
|
UBool autodetect = FALSE;
|
|
|
|
int numRead =0;
|
|
|
|
const char* tcp;
|
|
|
|
autodetect=ucbuf_autodetect_nrw(in,&tcp, &numRead);
|
|
|
|
*cp =tcp;
|
|
|
|
/* rewind the file Stream */
|
|
|
|
T_FileStream_rewind(in);
|
|
|
|
return autodetect;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-05-16 16:34:10 +00:00
|
|
|
/* fill the uchar buffer */
|
2002-02-28 01:42:40 +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;
|
2001-05-26 22:29:39 +00:00
|
|
|
char cbuf[MAX_IN_BUF] = {'\0'};
|
2002-05-23 22:10:23 +00:00
|
|
|
int32_t numRead=0;
|
|
|
|
int32_t offset=0;
|
2001-11-01 19:43:21 +00:00
|
|
|
const char* sourceLimit =NULL;
|
2001-05-22 18:00:55 +00:00
|
|
|
pTarget = buf->buffer;
|
|
|
|
/* check if we arrived here without exhausting the buffer*/
|
|
|
|
if(buf->currentPos<buf->bufLimit){
|
2002-05-23 22:10:23 +00:00
|
|
|
offset = (int32_t)(buf->bufLimit-buf->currentPos);
|
2001-05-22 18:00:55 +00:00
|
|
|
memmove(buf->buffer,buf->currentPos,offset* sizeof(UChar));
|
2001-05-10 21:43:01 +00:00
|
|
|
}
|
2001-05-22 18:00:55 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_IN_BUF-offset));
|
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
/* read the file */
|
2001-05-22 18:00:55 +00:00
|
|
|
numRead=T_FileStream_read(buf->in,cbuf,MAX_IN_BUF-offset);
|
2001-05-10 21:43:01 +00:00
|
|
|
buf->remaining-=numRead;
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
/* just to be sure...*/
|
|
|
|
if ( 0 == numRead )
|
|
|
|
buf->remaining = 0;
|
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
target=pTarget;
|
|
|
|
/* convert the bytes */
|
|
|
|
if(buf->conv){
|
2001-11-01 19:43:21 +00:00
|
|
|
/* set the callback to stop */
|
|
|
|
UConverterToUCallback toUOldAction ;
|
|
|
|
void* toUOldContext;
|
2002-02-28 01:42:40 +00:00
|
|
|
void* toUNewContext=NULL;
|
2001-11-01 19:43:21 +00:00
|
|
|
ucnv_setToUCallBack(buf->conv,
|
|
|
|
UCNV_TO_U_CALLBACK_STOP,
|
|
|
|
toUNewContext,
|
|
|
|
&toUOldAction,
|
|
|
|
(const void**)&toUOldContext,
|
|
|
|
err);
|
2001-05-10 21:43:01 +00:00
|
|
|
/* since state is saved in the converter we add offset to source*/
|
|
|
|
target = pTarget+offset;
|
|
|
|
source = cbuf;
|
2001-11-01 19:43:21 +00:00
|
|
|
sourceLimit = source + numRead;
|
2001-09-20 02:35:51 +00:00
|
|
|
ucnv_toUnicode(buf->conv,&target,target+(MAX_U_BUF-offset),
|
|
|
|
&source,source+numRead,NULL,
|
|
|
|
(UBool)(buf->remaining==0),err);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
if(U_FAILURE(*err)){
|
2001-11-01 19:43:21 +00:00
|
|
|
char context[CONTEXT_LEN];
|
|
|
|
char preContext[CONTEXT_LEN];
|
|
|
|
char postContext[CONTEXT_LEN];
|
|
|
|
int8_t len = CONTEXT_LEN;
|
|
|
|
int32_t start=0;
|
|
|
|
int32_t stop =0;
|
|
|
|
int32_t pos =0;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-03 02:54:08 +00:00
|
|
|
if( buf->showWarning==TRUE){
|
2002-02-28 01:42:40 +00:00
|
|
|
fprintf(stderr,"\n###WARNING: Encountered abnormal bytes while"
|
2001-11-03 02:54:08 +00:00
|
|
|
" converting input stream to target encoding: %s\n",
|
|
|
|
u_errorName(*err));
|
|
|
|
}
|
2001-11-01 19:43:21 +00:00
|
|
|
|
|
|
|
*err = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
/* now get the context chars */
|
|
|
|
ucnv_getInvalidChars(buf->conv,context,&len,err);
|
|
|
|
context[len]= 0 ; /* null terminate the buffer */
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2002-05-23 22:10:23 +00:00
|
|
|
pos = (int32_t)(source - cbuf - len);
|
2001-11-01 19:43:21 +00:00
|
|
|
|
|
|
|
/* for pre-context */
|
|
|
|
start = (pos <=CONTEXT_LEN)? 0 : (pos - (CONTEXT_LEN-1));
|
|
|
|
stop = pos-len;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-01 19:43:21 +00:00
|
|
|
memcpy(preContext,cbuf+start,stop-start);
|
|
|
|
/* null terminate the buffer */
|
|
|
|
preContext[stop-start] = 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-01 19:43:21 +00:00
|
|
|
/* for post-context */
|
|
|
|
start = pos+len;
|
2002-05-23 22:10:23 +00:00
|
|
|
stop = (int32_t)(((pos+CONTEXT_LEN)<= (sourceLimit-cbuf) )? (pos+(CONTEXT_LEN-1)) : (sourceLimit-cbuf));
|
2001-11-01 19:43:21 +00:00
|
|
|
|
|
|
|
memcpy(postContext,source,stop-start);
|
|
|
|
/* null terminate the buffer */
|
|
|
|
postContext[stop-start] = 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-03 02:54:08 +00:00
|
|
|
if(buf->showWarning ==TRUE){
|
|
|
|
/* print out the context */
|
|
|
|
fprintf(stderr,"\tPre-context: %s\n",preContext);
|
|
|
|
fprintf(stderr,"\tContext: %s\n",context);
|
|
|
|
fprintf(stderr,"\tPost-context: %s\n", postContext);
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-01 22:55:16 +00:00
|
|
|
/* reset the converter */
|
|
|
|
ucnv_reset(buf->conv);
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
/* set the call back to substitute
|
2001-11-01 19:43:21 +00:00
|
|
|
* and restart conversion
|
|
|
|
*/
|
|
|
|
ucnv_setToUCallBack(buf->conv,
|
|
|
|
UCNV_TO_U_CALLBACK_SUBSTITUTE,
|
|
|
|
toUNewContext,
|
|
|
|
&toUOldAction,
|
|
|
|
(const void**)&toUOldContext,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/* reset source and target start positions */
|
|
|
|
target = pTarget+offset;
|
|
|
|
source = cbuf;
|
2001-11-01 22:55:16 +00:00
|
|
|
|
2001-11-01 19:43:21 +00:00
|
|
|
/* re convert */
|
|
|
|
ucnv_toUnicode(buf->conv,&target,target+(MAX_U_BUF-offset),
|
|
|
|
&source,sourceLimit,NULL,
|
|
|
|
(UBool)(buf->remaining==0),err);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-11-01 19:43:21 +00:00
|
|
|
}
|
2002-05-23 22:10:23 +00:00
|
|
|
numRead = (int32_t)(target - pTarget);
|
2001-11-01 19:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
target = pTarget;
|
|
|
|
for(i=0;i<numRead;i++){
|
|
|
|
/* printf("%c", (char)(*target++));*/
|
|
|
|
}
|
2001-05-10 21:43:01 +00:00
|
|
|
}
|
2001-11-01 19:43:21 +00:00
|
|
|
#endif
|
|
|
|
|
2001-05-10 21:43:01 +00:00
|
|
|
}else{
|
|
|
|
u_charsToUChars(cbuf,target+offset,numRead);
|
2001-05-22 18:00:55 +00:00
|
|
|
numRead=((buf->remaining>MAX_IN_BUF)? MAX_IN_BUF:numRead+offset);
|
2001-05-10 21:43:01 +00:00
|
|
|
}
|
|
|
|
buf->currentPos = pTarget;
|
|
|
|
buf->bufLimit=pTarget+numRead;
|
|
|
|
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){
|
2001-05-22 17:36:43 +00:00
|
|
|
if(buf->currentPos>=buf->bufLimit){
|
2001-05-10 21:43:01 +00:00
|
|
|
if(buf->remaining==0){
|
|
|
|
return U_EOF;
|
|
|
|
}
|
|
|
|
buf=ucbuf_fillucbuf(buf,err);
|
|
|
|
if(U_FAILURE(*err)){
|
|
|
|
return U_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-22 17:36:43 +00:00
|
|
|
return *(buf->currentPos++);
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* u_unescapeAt() callback to return a UChar*/
|
2002-07-23 23:01:08 +00:00
|
|
|
static UChar U_CALLCONV
|
2001-05-10 21:43:01 +00:00
|
|
|
_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;
|
2001-08-28 01:25:35 +00:00
|
|
|
UChar32 c32,c1,c2;
|
|
|
|
|
2001-05-10 16:54:09 +00:00
|
|
|
/* Fill the buffer if it is empty */
|
2001-08-28 01:25:35 +00:00
|
|
|
if (buf->currentPos >=buf->bufLimit-2) {
|
2001-05-22 17:36:43 +00:00
|
|
|
ucbuf_fillucbuf(buf,err);
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next character in the buffer */
|
|
|
|
if (buf->currentPos < buf->bufLimit) {
|
2001-05-26 01:16:37 +00:00
|
|
|
c1 = *(buf->currentPos)++;
|
2001-05-10 16:54:09 +00:00
|
|
|
} else {
|
2001-05-26 01:16:37 +00:00
|
|
|
c1 = U_EOF;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-09-20 02:35:51 +00:00
|
|
|
c2 = *(buf->currentPos);
|
2001-05-10 16:54:09 +00:00
|
|
|
|
|
|
|
/* If it isn't a backslash, return it */
|
2001-08-28 01:25:35 +00:00
|
|
|
if (c1 != 0x005C) {
|
2001-05-26 01:16:37 +00:00
|
|
|
return c1;
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-05-10 16:54:09 +00:00
|
|
|
/* Determine the amount of data in the buffer */
|
2002-05-23 22:10:23 +00:00
|
|
|
length = (int32_t)(buf->bufLimit - buf->currentPos);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
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);
|
2002-05-23 22:10:23 +00:00
|
|
|
length = (int32_t)(buf->bufLimit - buf->buffer);
|
2001-05-10 16:54:09 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-05-10 16:54:09 +00:00
|
|
|
/* Process the escape */
|
|
|
|
offset = 0;
|
|
|
|
c32 = u_unescapeAt(_charAt, &offset, length, (void*)buf);
|
|
|
|
|
2001-09-20 02:35:51 +00:00
|
|
|
/* check if u_unescapeAt unescaped and converted
|
|
|
|
* to c32 or not
|
|
|
|
*/
|
2002-09-06 22:13:15 +00:00
|
|
|
if(c32==0xFFFFFFFF){
|
|
|
|
*err= U_INVALID_CHAR_FOUND;
|
|
|
|
return c1;
|
|
|
|
}else if(c32!=c2){
|
2001-09-20 02:35:51 +00:00
|
|
|
/* Update the current buffer position */
|
|
|
|
buf->currentPos += offset;
|
|
|
|
}else{
|
2002-02-28 01:42:40 +00:00
|
|
|
/* unescaping failed so we just return
|
2001-09-20 02:35:51 +00:00
|
|
|
* c1 and not consume the buffer
|
|
|
|
* this is useful for rules with escapes
|
|
|
|
* in resouce bundles
|
|
|
|
* eg: \' \\ \"
|
|
|
|
*/
|
|
|
|
return c1;
|
|
|
|
}
|
2001-05-10 16:54:09 +00:00
|
|
|
|
|
|
|
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-11-03 02:54:08 +00:00
|
|
|
ucbuf_open(FileStream* in,const char* cp, UBool showWarning, UErrorCode* err){
|
2001-05-10 21:43:01 +00:00
|
|
|
|
|
|
|
UCHARBUF* buf =(UCHARBUF*) uprv_malloc(sizeof(UCHARBUF));
|
2001-05-26 01:16:37 +00:00
|
|
|
int numRead =0;
|
2001-05-10 21:43:01 +00:00
|
|
|
if(U_FAILURE(*err)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(buf){
|
|
|
|
buf->in=in;
|
2001-11-01 19:43:21 +00:00
|
|
|
buf->conv=NULL;
|
2001-11-03 02:54:08 +00:00
|
|
|
buf->showWarning = showWarning;
|
|
|
|
if(!cp ||(cp && *cp=='\0')){
|
|
|
|
/* don't have code page name... try to autodetect */
|
|
|
|
if(ucbuf_autodetect_nrw(in,&cp,&numRead)){
|
2001-11-01 19:43:21 +00:00
|
|
|
buf->conv=ucnv_open(cp,err);
|
|
|
|
}
|
2001-11-03 02:54:08 +00:00
|
|
|
}else{
|
|
|
|
buf->conv=ucnv_open(cp,err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((buf->conv==NULL) && (buf->showWarning==TRUE)){
|
|
|
|
fprintf(stderr,"###WARNING: No converter defined. Using codepage of system.\n");
|
2001-11-01 19:43:21 +00:00
|
|
|
}
|
2001-05-26 01:16:37 +00:00
|
|
|
buf->remaining=T_FileStream_size(in)-numRead;
|
2001-05-22 18:00:55 +00:00
|
|
|
buf->buffer=(UChar*) uprv_malloc(sizeof(UChar)* MAX_U_BUF);
|
2002-04-02 02:55:31 +00:00
|
|
|
if (buf->buffer == NULL) {
|
|
|
|
*err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-05-22 18:00:55 +00:00
|
|
|
buf->currentPos=buf->buffer;
|
|
|
|
buf->bufLimit=buf->buffer;
|
2001-05-10 21:43:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +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.
|
2002-02-28 01:42:40 +00:00
|
|
|
*/
|
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 17:36:43 +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 */
|
2002-02-28 01:42:40 +00:00
|
|
|
static void
|
2001-05-16 16:34:10 +00:00
|
|
|
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){
|
2001-05-22 17:36:43 +00:00
|
|
|
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);
|
|
|
|
}
|
2001-05-16 16:34:10 +00:00
|
|
|
}
|