/*
*******************************************************************************
*
-* Copyright (C) 1998-2003, International Business Machines
+* Copyright (C) 1998-2006, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
*/
#include "unicode/utypes.h"
+#include "unicode/putil.h"
#include "unicode/ucnv.h"
#include "unicode/ucnv_err.h"
#include "filestrm.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
pTarget = target;
pStart = start;
ucnv_toUnicode(*conv, &pTarget, target+1, &pStart, start+*signatureLength, NULL, FALSE, error);
- *signatureLength = pStart - start;
+ *signatureLength = (int32_t)(pStart - start);
if(*error==U_BUFFER_OVERFLOW_ERROR) {
*error=U_ZERO_ERROR;
}
}
-
return TRUE;
}
static UBool ucbuf_isCPKnown(const char* cp){
if(ucnv_compareNames("UTF-32LE",cp)==0){
return TRUE;
}
- if(ucnv_compareNames("UTF-32BE",cp)==0){
- return TRUE;
- }
if(ucnv_compareNames("SCSU",cp)==0){
return TRUE;
}
- if(ucnv_compareNames("BOCU",cp)==0){
+ if(ucnv_compareNames("BOCU-1",cp)==0){
return TRUE;
}
if(ucnv_compareNames("UTF-7",cp)==0){
}
buf->currentPos = pTarget;
buf->bufLimit=pTarget+outputWritten;
+ *buf->bufLimit=0; /*NUL terminate*/
if(cbuf!=carr){
uprv_free(cbuf);
}
* to c32 or not
*/
if(c32==0xFFFFFFFF){
- if(buf->showWarning) {
- char context[20];
- int32_t len = 20;
- if(length < len) {
- len = length;
- }
- context[len]= 0 ; /* null terminate the buffer */
- u_UCharsToChars( buf->currentPos, context, len);
- fprintf(stderr,"Bad escape: [%c%s]...\n", c1,context);
- }
+ if(buf->showWarning) {
+ char context[20];
+ int32_t len = 20;
+ if(length < len) {
+ len = length;
+ }
+ context[len]= 0 ; /* null terminate the buffer */
+ u_UCharsToChars( buf->currentPos, context, len);
+ fprintf(stderr,"Bad escape: [%c%s]...\n", (int)c1, context);
+ }
*error= U_ILLEGAL_ESCAPE_SEQUENCE;
return c1;
}else if(c32!=c2 || (c32==0x0075 && c2==0x0075 && c1==0x005C) /* for \u0075 c2=0x0075 and c32==0x0075*/){
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->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR* MAX_U_BUF);
- buf->bufCapacity=MAX_U_BUF;
- }else{
- buf->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR * (buf->remaining+buf->signatureLength));
- buf->bufCapacity=buf->remaining+buf->signatureLength;
- }
- 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;
+ ucnv_close(buf->conv);
+ uprv_free(buf);
+ T_FileStream_close(in);
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));
+ ucnv_close(buf->conv);
+ uprv_free(buf);
+ T_FileStream_close(in);
+ return NULL;
+ }
+ buf=ucbuf_fillucbuf(buf,error);
+ return buf;
}
*error =U_FILE_ACCESS_ERROR;
return NULL;
if(buf->isBuffered){
return (T_FileStream_size(buf->in)-buf->signatureLength)/ucnv_getMinCharSize(buf->conv);
}else{
- return buf->bufLimit-buf->buffer;
+ return (int32_t)(buf->bufLimit - buf->buffer);
}
}
return 0;
*error = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
- *len = buf->bufLimit-buf->buffer;
+ *len = (int32_t)(buf->bufLimit - buf->buffer);
return buf->buffer;
}
UChar* savePos =NULL;
UChar c=0x0000;
if(buf->isBuffered){
- /* The input is buffered we have to do more
- * for returning a pointer U_TRUNCATED_CHAR_FOUND
+ /* The input is buffered we have to do more
+ * for returning a pointer U_TRUNCATED_CHAR_FOUND
*/
for(;;){
c = *temp++;
if(buf->remaining==0){
- *err = (UErrorCode) U_EOF;
+ return NULL; /* end of file is reached return NULL */
}
if(temp>=buf->bufLimit && buf->currentPos == buf->buffer){
*err= U_TRUNCATED_CHAR_FOUND;
}else{
ucbuf_fillucbuf(buf,err);
if(U_FAILURE(*err)){
- return NULL;
+ return NULL;
}
}
/*
*/
/* Windows CR LF */
if(c ==0x0d && temp+1<=buf->bufLimit && *(temp+1) == 0x0a ){
- *len = temp++ - buf->currentPos;
+ *len = (int32_t)(temp++ - buf->currentPos);
savePos = buf->currentPos;
buf->currentPos = temp;
return savePos;
/* else */
if (temp>=buf->bufLimit|| ucbuf_isCharNewLine(c)){ /* Unipad inserts 2028 line separators! */
- *len = temp - buf->currentPos;
+ *len = (int32_t)(temp - buf->currentPos);
savePos = buf->currentPos;
buf->currentPos = temp;
return savePos;
c = *temp++;
if(buf->currentPos==buf->bufLimit){
- *err = (UErrorCode) U_EOF;
- return NULL;
+ return NULL; /* end of file is reached return NULL */
}
/* Windows CR LF */
if(c ==0x0d && temp+1<=buf->bufLimit && *(temp+1) == 0x0a ){
- *len = temp++ - buf->currentPos;
+ *len = (int32_t)(temp++ - buf->currentPos);
savePos = buf->currentPos;
buf->currentPos = temp;
return savePos;
}
/* else */
if (temp>=buf->bufLimit|| ucbuf_isCharNewLine(c)) { /* Unipad inserts 2028 line separators! */
- *len = temp - buf->currentPos;
+ *len = (int32_t)(temp - buf->currentPos);
savePos = buf->currentPos;
buf->currentPos = temp;
return savePos;
/* A compiler warning will appear if all paths don't contain a return statement. */
/* return NULL;*/
}
+#endif