/*
*******************************************************************************
*
-* Copyright (C) 1998-2004, International Business Machines
+* Copyright (C) 1998-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
#include "unicode/utypes.h"
#include "unicode/putil.h"
+#include "unicode/uchar.h"
#include "unicode/ucnv.h"
#include "unicode/ucnv_err.h"
+#include "unicode/ustring.h"
+#include "unicode/utf16.h"
#include "filestrm.h"
#include "cstring.h"
#include "cmemory.h"
#include "ustrfmt.h"
-#include "unicode/ustring.h"
-#include "unicode/uchar.h"
#include "ucbuf.h"
#include <stdio.h>
+#if !UCONFIG_NO_CONVERSION
+
+
#define MAX_IN_BUF 1000
#define MAX_U_BUF 1500
-#define CONTEXT_LEN 15
+#define CONTEXT_LEN 20
struct UCHARBUF {
UChar* buffer;
/* unread the bytes beyond what was consumed for U+FEFF */
T_FileStream_rewind(in);
if (*signatureLength > 0) {
- numRead = T_FileStream_read(in, start, *signatureLength);
+ T_FileStream_read(in, start, *signatureLength);
}
if(*cp==NULL){
}else{
cbufSize = T_FileStream_size(buf->in);
cbuf = (char*)uprv_malloc(cbufSize);
+ if (cbuf == NULL) {
+ *error = U_MEMORY_ALLOCATION_ERROR;
+ return NULL;
+ }
inputRead= T_FileStream_read(buf->in,cbuf,cbufSize);
buf->remaining-=inputRead;
}
(UBool)(buf->remaining==0),error);
if(U_FAILURE(*error)){
- char context[CONTEXT_LEN];
- char preContext[CONTEXT_LEN];
- char postContext[CONTEXT_LEN];
+ char context[CONTEXT_LEN+1];
+ char preContext[CONTEXT_LEN+1];
+ char postContext[CONTEXT_LEN+1];
int8_t len = CONTEXT_LEN;
int32_t start=0;
int32_t stop =0;
return U_EOF;
}
}
- if(UTF_IS_LEAD(*(buf->currentPos))){
- retVal=UTF16_GET_PAIR_VALUE(*(buf->currentPos++),*(buf->currentPos++));
+ if(U16_IS_LEAD(*(buf->currentPos))){
+ retVal=U16_GET_SUPPLEMENTARY(buf->currentPos[0],buf->currentPos[1]);
+ buf->currentPos+=2;
}else{
retVal = *(buf->currentPos++);
}
*/
if(c32==0xFFFFFFFF){
if(buf->showWarning) {
- char context[20];
- int32_t len = 20;
- if(length < len) {
+ char context[CONTEXT_LEN+1];
+ int32_t len = CONTEXT_LEN;
+ if(length < len) {
len = length;
}
context[len]= 0 ; /* null terminate the buffer */
if(in!=NULL){
UCHARBUF* buf =(UCHARBUF*) uprv_malloc(sizeof(UCHARBUF));
fileSize = T_FileStream_size(in);
- if(buf){
- buf->in=in;
- buf->conv=NULL;
- buf->showWarning = showWarning;
- buf->isBuffered = buffered;
- buf->signatureLength=0;
- if(*cp==NULL || **cp=='\0'){
- /* don't have code page name... try to autodetect */
- ucbuf_autodetect_fs(in,cp,&buf->conv,&buf->signatureLength,error);
- }else if(ucbuf_isCPKnown(*cp)){
- /* discard BOM */
- ucbuf_autodetect_fs(in,&knownCp,&buf->conv,&buf->signatureLength,error);
- }
- if(U_SUCCESS(*error) && buf->conv==NULL) {
- buf->conv=ucnv_open(*cp,error);
- }
- if(U_FAILURE(*error)){
- ucnv_close(buf->conv);
- uprv_free(buf);
- return NULL;
- }
-
- if((buf->conv==NULL) && (buf->showWarning==TRUE)){
- fprintf(stderr,"###WARNING: No converter defined. Using codepage of system.\n");
- }
- buf->remaining=fileSize-buf->signatureLength;
- if(buf->isBuffered){
- buf->bufCapacity=MAX_U_BUF;
- }else{
- buf->bufCapacity=buf->remaining+buf->signatureLength+1/*for terminating nul*/;
- }
- buf->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR * buf->bufCapacity );
- if (buf->buffer == NULL) {
- *error = U_MEMORY_ALLOCATION_ERROR;
- return NULL;
- }
- buf->currentPos=buf->buffer;
- buf->bufLimit=buf->buffer;
- if(U_FAILURE(*error)){
- fprintf(stderr, "Could not open codepage [%s]: %s\n", *cp, u_errorName(*error));
- return NULL;
- }
- buf=ucbuf_fillucbuf(buf,error);
- return buf;
+ if(buf == NULL){
+ *error = U_MEMORY_ALLOCATION_ERROR;
+ T_FileStream_close(in);
+ return NULL;
+ }
+ buf->in=in;
+ buf->conv=NULL;
+ buf->showWarning = showWarning;
+ buf->isBuffered = buffered;
+ buf->signatureLength=0;
+ if(*cp==NULL || **cp=='\0'){
+ /* don't have code page name... try to autodetect */
+ ucbuf_autodetect_fs(in,cp,&buf->conv,&buf->signatureLength,error);
+ }else if(ucbuf_isCPKnown(*cp)){
+ /* discard BOM */
+ ucbuf_autodetect_fs(in,&knownCp,&buf->conv,&buf->signatureLength,error);
+ }
+ if(U_SUCCESS(*error) && buf->conv==NULL) {
+ buf->conv=ucnv_open(*cp,error);
+ }
+ if(U_FAILURE(*error)){
+ ucnv_close(buf->conv);
+ uprv_free(buf);
+ T_FileStream_close(in);
+ return NULL;
+ }
+
+ if((buf->conv==NULL) && (buf->showWarning==TRUE)){
+ fprintf(stderr,"###WARNING: No converter defined. Using codepage of system.\n");
+ }
+ buf->remaining=fileSize-buf->signatureLength;
+ if(buf->isBuffered){
+ buf->bufCapacity=MAX_U_BUF;
}else{
+ buf->bufCapacity=buf->remaining+buf->signatureLength+1/*for terminating nul*/;
+ }
+ buf->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR * buf->bufCapacity );
+ if (buf->buffer == NULL) {
*error = U_MEMORY_ALLOCATION_ERROR;
+ ucbuf_close(buf);
return NULL;
}
-
+ buf->currentPos=buf->buffer;
+ buf->bufLimit=buf->buffer;
+ if(U_FAILURE(*error)){
+ fprintf(stderr, "Could not open codepage [%s]: %s\n", *cp, u_errorName(*error));
+ ucbuf_close(buf);
+ return NULL;
+ }
+ ucbuf_fillucbuf(buf,error);
+ if(U_FAILURE(*error)){
+ ucbuf_close(buf);
+ return NULL;
+ }
+ return buf;
}
*error =U_FILE_ACCESS_ERROR;
return NULL;
/* decrement currentPos pointer
* if not at the begining of buffer
*/
- UChar escaped[8] ={'\0'};
- int32_t len =0;
- if(c > 0xFFFF){
- len = uprv_itou(escaped,8,c,16,8);
- }else{
- len=uprv_itou(escaped,8,c,16,4);
- }
if(buf->currentPos!=buf->buffer){
if(*(buf->currentPos-1)==c){
buf->currentPos--;
- }else if(u_strncmp(buf->currentPos-len,escaped,len) == 0){
- while(--len>0){
- buf->currentPos--;
- }
+ } else {
+ /* ungetc failed - did not match. */
}
+ } else {
+ /* ungetc failed - beginning of buffer. */
}
}
/* A compiler warning will appear if all paths don't contain a return statement. */
/* return NULL;*/
}
+#endif