ICU-954 implement rewind function

X-SVN-Rev: 4685
This commit is contained in:
Ram Viswanadha 2001-05-16 16:34:10 +00:00
parent 062dca80cb
commit d40c8f47e8

View File

@ -24,6 +24,7 @@
#define MAX_BUF 1000 #define MAX_BUF 1000
/* Autodetects UTF8, UTF-16-BigEndian and UTF-16-LittleEndian BOMs*/
U_CAPI UBool U_EXPORT2 U_CAPI UBool U_EXPORT2
ucbuf_autodetect(FileStream* in,const char** cp){ ucbuf_autodetect(FileStream* in,const char** cp){
UBool autodetect = FALSE; UBool autodetect = FALSE;
@ -46,6 +47,7 @@ ucbuf_autodetect(FileStream* in,const char** cp){
return autodetect; return autodetect;
} }
/* fill the uchar buffer */
static UCHARBUF* static UCHARBUF*
ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){
UChar* pTarget=NULL; UChar* pTarget=NULL;
@ -100,6 +102,8 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){
return buf; return buf;
} }
/* get a UChar from the stream*/
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
ucbuf_getc(UCHARBUF* buf,UErrorCode* err){ ucbuf_getc(UCHARBUF* buf,UErrorCode* err){
UChar32 c =0; UChar32 c =0;
@ -130,6 +134,7 @@ _charAt(int32_t offset, void *context) {
return ((UCHARBUF*) context)->currentPos[offset]; return ((UCHARBUF*) context)->currentPos[offset];
} }
/* getc and escape it */
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) { ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) {
int32_t length; int32_t length;
@ -176,7 +181,7 @@ ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) {
return c32; return c32;
} }
/* open a UCHARBUF */
U_CAPI UCHARBUF* U_EXPORT2 U_CAPI UCHARBUF* U_EXPORT2
ucbuf_open(FileStream* in, const char* cp,UErrorCode* err){ ucbuf_open(FileStream* in, const char* cp,UErrorCode* err){
@ -207,19 +212,25 @@ ucbuf_open(FileStream* in, const char* cp,UErrorCode* err){
} }
} }
static void /* TODO: this method will fail if at the
ucbuf_closebuf(UCHARBUF* buf){ begining of buffer and the uchar to unget
uprv_free(buf->buffer); is from the previous buffer. Need to implement
} system to take care of that situation.
*/
U_CAPI void U_EXPORT2 U_CAPI void U_EXPORT2
ucbuf_ungetc(UChar32 c,UCHARBUF* buf){ ucbuf_ungetc(UChar32 c,UCHARBUF* buf){
if(buf->currentPos!=buf->buffer){ if(buf->currentPos!=buf->buffer){
buf->currentPos--; buf->currentPos--;
} }
} }
/* frees the resources of UChar* buffer */
static void
ucbuf_closebuf(UCHARBUF* buf){
uprv_free(buf->buffer);
}
/* close the buf and release resources*/
U_CAPI void U_EXPORT2 U_CAPI void U_EXPORT2
ucbuf_close(UCHARBUF* buf){ ucbuf_close(UCHARBUF* buf){
if(buf->conv){ if(buf->conv){
@ -231,3 +242,17 @@ ucbuf_close(UCHARBUF* buf){
ucbuf_closebuf(buf); ucbuf_closebuf(buf);
uprv_free(buf); uprv_free(buf);
} }
/* 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);
}
}