--- parser3/src/main/pa_charset.C 2003/03/06 12:02:04 1.33.2.14 +++ parser3/src/main/pa_charset.C 2003/03/24 17:52:19 1.33.2.19.2.7 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan(http://paf.design.ru) */ -static const char* IDENT_CHARSET_C="$Date: 2003/03/06 12:02:04 $"; +static const char* IDENT_CHARSET_C="$Date: 2003/03/24 17:52:19 $"; #include "pa_charset.h" #include "pa_charsets.h" @@ -68,16 +68,16 @@ static void element2case(unsigned char f // methods extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c -Charset::Charset(StringPtr aname, StringPtr afile_spec): +Charset::Charset(const String& aname, const String* afile_spec): fname(aname), - fname_cstr(aname->cstr()) { + fname_cstr(aname.cstrm()) { for(char *c=fname_cstr; *c; c++) - *c = toupper(*c); + *c = toupper(*c); if(afile_spec) { fisUTF8=false; - load_definition(afile_spec); + load_definition(*afile_spec); #ifdef XML addEncoding(fname_cstr); #endif @@ -99,7 +99,7 @@ Charset::~Charset() { #endif } -void Charset::load_definition(StringPtr afile_spec) { +void Charset::load_definition(const String& afile_spec) { // pcre_tables // lowcase, flipcase, bits digit+word+whitespace, masks @@ -117,8 +117,7 @@ void Charset::load_definition(StringPtr tables.toTableSize++; // loading text - Pool pool_for_load_only; - char *data=file_read_text(pool_for_load_only, *UTF8_charset, afile_spec); + char *data=file_read_text(UTF8_charset, afile_spec); // ignore header getrow(&data); @@ -148,7 +147,7 @@ void Charset::load_definition(StringPtr // charset if(tables.toTableSize>MAX_CHARSET_UNI_CODES) throw Exception("parser.runtime", - afile_spec, + &afile_spec, "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); XMLCh unicode=(XMLCh)to_wchar_code(cell); @@ -208,7 +207,7 @@ static XMLByte xlatOneTo(const XMLCh toX return not_found; } -void Charset::transcode(Pool& pool, +void Charset::transcode( const Charset& source_charset, const void* source_body, size_t source_content_length, const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length ) { @@ -220,17 +219,17 @@ void Charset::transcode(Pool& pool, switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) { default: // 0x00 - source_charset.transcodeToCharset(pool, dest_charset, + source_charset.transcodeToCharset(dest_charset, source_body, source_content_length, dest_body, dest_content_length); break; case 0x01: - source_charset.transcodeToUTF8(pool, + source_charset.transcodeToUTF8( source_body, source_content_length, dest_body, dest_content_length); break; case 0x10: - dest_charset.transcodeFromUTF8(pool, + dest_charset.transcodeFromUTF8( source_body, source_content_length, dest_body, dest_content_length); break; @@ -409,7 +408,7 @@ static int transcodeFromUTF8( default: throw Exception(0, - Exception::undefined_source, + 0, "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); } tmpVal-=gUTFOffsets[trailingBytes]; @@ -424,7 +423,7 @@ static int transcodeFromUTF8( outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal; } else throw Exception(0, - Exception::undefined_source, + 0, "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal); } @@ -446,11 +445,11 @@ of ocetes consumed. } /// @todo not so memory-hungry with prescan -void Charset::transcodeToUTF8(Pool& pool, +void Charset::transcodeToUTF8( const void* source_body, size_t source_content_length, const void *& adest_body, size_t& dest_content_length) const { dest_content_length=source_content_length*6/*so that surly enough, max utf8 seq len=6*/; - XMLByte *dest_body=new(pool) XMLByte[dest_content_length]; + XMLByte *dest_body=new XMLByte[dest_content_length]; if(::transcodeToUTF8( (XMLByte *)source_body, source_content_length, @@ -463,11 +462,11 @@ void Charset::transcodeToUTF8(Pool& pool // return adest_body=dest_body; } -void Charset::transcodeFromUTF8(Pool& pool, +void Charset::transcodeFromUTF8( const void* source_body, size_t source_content_length, const void *& adest_body, size_t& dest_content_length) const { dest_content_length=source_content_length*6/*so that surly enough, "ÿ" has max ratio */; - XMLByte *dest_body=new(pool) XMLByte[dest_content_length]; + XMLByte *dest_body=new XMLByte[dest_content_length]; if(::transcodeFromUTF8( (XMLByte *)source_body, source_content_length, @@ -482,7 +481,7 @@ void Charset::transcodeFromUTF8(Pool& po } /// transcode using both charsets -void Charset::transcodeToCharset(Pool& pool, +void Charset::transcodeToCharset( const Charset& dest_charset, const void* source_body, size_t source_content_length, const void *& adest_body, size_t& adest_content_length) const { @@ -491,7 +490,7 @@ void Charset::transcodeToCharset(Pool& p adest_content_length=source_content_length; } else { size_t dest_content_length=source_content_length; - unsigned char *dest_body=new(pool) unsigned char[dest_content_length]; + unsigned char *dest_body=new unsigned char[dest_content_length]; const XMLByte* srcPtr=(XMLByte *)source_body; const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length; @@ -539,8 +538,6 @@ static int xml256CharEncodingOut void Charset::addEncoding(char *name_cstr) { xmlCharEncodingHandler* handler=new xmlCharEncodingHandler; - fcreated_handler=xmlCharEncodingHandlerPtr(fcreated_handler); - handler->name=name_cstr; handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; @@ -548,99 +545,98 @@ void Charset::addEncoding(char *name_cst xmlRegisterCharEncodingHandler(handler); } -void Charset::initTranscoder(StringPtr source, const char* name_cstr) { +void Charset::initTranscoder(const String& name, const char* name_cstr) { ftranscoder=xmlFindCharEncodingHandler(name_cstr); - transcoder(source); // check right way + transcoder(name); // check right way } -xmlCharEncodingHandler& Charset::transcoder(StringPtr source) { +xmlCharEncodingHandler& Charset::transcoder(const String& name) { if(!ftranscoder) throw Exception("parser.runtime", - source, + &name, "unsupported encoding"); return *ftranscoder; } -CharPtr Charset::transcode_cstr(xmlChar* s) { +const char* Charset::transcode_cstr(xmlChar* s) { if(!s) return ""; int inlen=strlen((const char* )s); int outlen=inlen+1; // max - char *out=new char[outlen]; + char *out=new(PointerFreeGC) char[outlen]; int error; - if(xmlCharEncodingOutputFunc output=transcoder(Exception::undefined_source).output) { + if(xmlCharEncodingOutputFunc output=transcoder(fname).output) { error=output( (unsigned char*)out, &outlen, (const unsigned char*)s, &inlen, - transcoder(Exception::undefined_source).outputInfo); + transcoder(fname).outputInfo); } else { memcpy(out, s, outlen=inlen); error=0; } if(error<0) - throw Exception(Exception::undefined_type, - Exception::undefined_source, + throw Exception(0, + 0, "transcode_cstr failed (%d)", error); out[outlen/*surely would be less then on input*/]=0; return out; } -StringPtr Charset::transcode(xmlChar* s -#ifndef NO_STRING_ORIGIN - , StringPtr origin -#endif - ) { - StringPtr result(new String()); - result->APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); - return result; +const String& Charset::transcode(xmlChar* s) { + return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); } const char* Charset::transcode_cstr(GdomeDOMString* s) { return s?transcode_cstr(BAD_CAST s->str):""; } -StringPtr Charset::transcode(GdomeDOMString* s -#ifndef NO_STRING_ORIGIN - , const String *origin -#endif - ) { - StringPtr result(new String()); - result->APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); - return result; +const String& Charset::transcode(GdomeDOMString* s) { + return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); } /// @test less memory using -maybe- xmlParserInputBufferCreateMem -xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) { +void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, + const char* buf, size_t buf_size) { unsigned char* out; int outlen; int error; - if(xmlCharEncodingInputFunc input=transcoder(Exception::undefined_source).input) { + if(xmlCharEncodingInputFunc input=transcoder(fname).input) { outlen=buf_size*6/*max*/; - out=(unsigned char*)malloc(sizeof(xmlChar)*(outlen+1)); + out=(unsigned char*)malloc_func(outlen+1); error=input( out, &outlen, (const unsigned char *)buf, (int *)&buf_size, - transcoder(Exception::undefined_source).inputInfo); + transcoder(fname).inputInfo); } else { outlen=buf_size; - out=(unsigned char*)malloc(sizeof(xmlChar)*(outlen+1)); + out=(unsigned char*)malloc_func(outlen+1); memcpy(out, buf, outlen); error=0; } if(error<0) - throw Exception(Exception::undefined_type, - Exception::undefined_source, + throw Exception(0, + 0, "transcode_buf failed (%d)", error); out[outlen/*surely would be less then on input*/]=0; - return (xmlChar*)out; + return out; +} + +xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) { + return static_cast(transcode_buf2mchar(xmlMalloc, buf, buf_size)); +} +static void* g_malloc_wrapper(size_t size) { + return g_malloc(size); +} +gchar* Charset::transcode_buf2gchar(const char* buf, size_t buf_size) { + return static_cast(transcode_buf2mchar(g_malloc_wrapper, buf, buf_size)); } GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { - return GdomeDOMString_auto_ptr((gchar*)transcode_buf2xchar(buf, buf_size)); + return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); } -GdomeDOMString_auto_ptr Charset::transcode(StringPtr s) { - CharPtr cstr=s->cstr(String::UL_UNSPECIFIED); +GdomeDOMString_auto_ptr Charset::transcode(const String& s) { + const char* cstr=s.cstr(String::L_UNSPECIFIED); return transcode_buf2dom(cstr, strlen(cstr)); }