--- parser3/src/main/pa_charset.C 2002/02/08 08:32:34 1.20 +++ parser3/src/main/pa_charset.C 2003/03/06 08:21:09 1.33.2.13 @@ -1,25 +1,19 @@ /** @file Parser: Charset connection implementation. - Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan(http://paf.design.ru) - - $Id: pa_charset.C,v 1.20 2002/02/08 08:32:34 paf Exp $ */ +static const char* IDENT_CHARSET_C="$Date: 2003/03/06 08:21:09 $"; + #include "pa_charset.h" +#include "pa_charsets.h" #ifdef XML #include "libxml/encoding.h" #endif -// globals - - -// consts - -#define MAX_CHARSET_UNI_CODES 500 - // helpers inline void prepare_case_tables(unsigned char *tables) { @@ -37,7 +31,7 @@ inline void cstr2ctypes(unsigned char *t ctypes_table[c]|=bit; } } -inline unsigned int to_wchar_code(const char *cstr) { +inline unsigned int to_wchar_code(const char* cstr) { if(!cstr || !*cstr) return 0; if(cstr[1]==0) @@ -46,7 +40,7 @@ inline unsigned int to_wchar_code(const char *error_pos; return(unsigned int)strtol(cstr, &error_pos, 0); } -inline bool to_bool(const char *cstr) { +inline bool to_bool(const char* cstr) { return cstr && *cstr!=0; } static void element2ctypes(unsigned char c, bool belongs, @@ -74,18 +68,18 @@ static void element2case(unsigned char f // methods extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c -Charset::Charset(Pool& apool, const String& aname, const String *request_file_spec): Pooled(apool), - fname(aname) { +Charset::Charset(StringPtr aname, StringPtr afile_spec): + fname(aname), + fname_cstr(aname->cstr()) { - char *name_cstr=fname.cstr(); - for(char *c=name_cstr; *c; c++) + for(char *c=fname_cstr; *c; c++) *c = toupper(*c); - if(request_file_spec) { + if(afile_spec) { fisUTF8=false; - loadDefinition(*request_file_spec); + load_definition(afile_spec); #ifdef XML - addEncoding(name_cstr); + addEncoding(fname_cstr); #endif } else { fisUTF8=true; @@ -94,17 +88,18 @@ Charset::Charset(Pool& apool, const Stri } #ifdef XML - initTranscoder(&aname, name_cstr); + initTranscoder(fname, fname_cstr); #endif } Charset::~Charset() { + // @todonow unregister encodings #ifdef XML // not deleting transcoder, that's not our business #endif } -void Charset::loadDefinition(const String& request_file_spec) { +void Charset::load_definition(StringPtr afile_spec) { // pcre_tables // lowcase, flipcase, bits digit+word+whitespace, masks @@ -115,16 +110,15 @@ void Charset::loadDefinition(const Strin cstr2ctypes(pcre_tables,(const unsigned char *)"*+?{^.$|()[", ctype_meta); // charset - memset(tables.fromTable, 0, sizeof(tables.fromTable)); - tables.toTable=(Charset_TransRec *)calloc(sizeof(Charset_TransRec)*MAX_CHARSET_UNI_CODES); - tables.toTableSize=0; + memset(&tables, 0, sizeof(tables)); // strangly vital tables.toTable[tables.toTableSize].intCh=0; tables.toTable[tables.toTableSize].extCh=(XMLByte)0; tables.toTableSize++; // loading text - char *data=file_read_text(pool(), request_file_spec); + Pool pool_for_load_only; + char *data=file_read_text(pool_for_load_only, *UTF8_charset, afile_spec); // ignore header getrow(&data); @@ -153,8 +147,8 @@ void Charset::loadDefinition(const Strin case 8: // charset if(tables.toTableSize>MAX_CHARSET_UNI_CODES) - throw Exception(0, 0, - &request_file_spec, + throw Exception("parser.runtime", + afile_spec, "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); XMLCh unicode=(XMLCh)to_wchar_code(cell); @@ -191,7 +185,8 @@ void Charset::sort_ToTable() { } static XMLByte xlatOneTo(const XMLCh toXlat, - const Charset::Tables& tables) { + const Charset::Tables& tables, + XMLByte not_found) { unsigned int lowOfs = 0; unsigned int hiOfs = tables.toTableSize - 1; XMLByte curByte = 0; @@ -210,7 +205,7 @@ static XMLByte xlatOneTo(const XMLCh toX return tables.toTable[midOfs].extCh; } while(lowOfs+1name=name_cstr; handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; @@ -533,78 +547,100 @@ void Charset::addEncoding(char *name_cst xmlRegisterCharEncodingHandler(handler); } -void Charset::initTranscoder(const String *source, const char *name_cstr) { +void Charset::initTranscoder(const String *source, const char* name_cstr) { ftranscoder=xmlFindCharEncodingHandler(name_cstr); transcoder(source); // check right way } xmlCharEncodingHandler *Charset::transcoder(const String *source) { if(!ftranscoder) - throw Exception(0, 0, + throw Exception("parser.runtime", source, "unsupported encoding"); return ftranscoder; } -const char *Charset::transcode_cstr(xmlChar *s) { +CharPtr Charset::transcode_cstr(xmlChar *s) { if(!s) return ""; - int inlen=strlen((const char *)s); + int inlen=strlen((const char* )s); int outlen=inlen+1; // max - char *out=(char *)malloc(outlen*sizeof(char)); + char *out=new char[outlen]; - int size; + int error; if(xmlCharEncodingOutputFunc output=transcoder(0)->output) { - size=output( + error=output( (unsigned char*)out, &outlen, (const unsigned char*)s, &inlen, transcoder(0)->outputInfo); - } else - memcpy(out, s, size=inlen); - if(size<0) - throw Exception(0, 0, + } else { + memcpy(out, s, outlen=inlen); + error=0; + } + if(error<0) + throw Exception(0, 0, - "transcode_cstr failed (%d)", size); + "transcode_cstr failed (%d)", error); - out[size]=0; + out[outlen/*surely would be less then on input*/]=0; return out; } -String& Charset::transcode(xmlChar *s) { - return *NEW String(pool(), transcode_cstr(s)); +String& Charset::transcode(xmlChar *s +#ifndef NO_STRING_ORIGIN + , const String *origin +#endif + ) { + String& result=*NEW String(pool()); + result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); + return result; } -const char *Charset::transcode_cstr(GdomeDOMString *s) { +const char* Charset::transcode_cstr(GdomeDOMString *s) { return s?transcode_cstr(BAD_CAST s->str):""; } -String& Charset::transcode(GdomeDOMString *s) { - return *NEW String(pool(), transcode_cstr(s)); +String& Charset::transcode(GdomeDOMString *s +#ifndef NO_STRING_ORIGIN + , const String *origin +#endif + ) { + String& result=*NEW String(pool()); + result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); + return result; } /// @test less memory using -maybe- xmlParserInputBufferCreateMem -GdomeDOMString_auto_ptr Charset::transcode_buf(const char *buf, size_t buf_size) { - int outlen=buf_size*6/*max*/+1; - unsigned char *out=(unsigned char*)malloc(outlen*sizeof(unsigned char)); - - int size; +xmlChar *Charset::transcode_buf2xchar(Pool& pool, const char* buf, size_t buf_size) { + unsigned char *out; + int outlen; + int error; if(xmlCharEncodingInputFunc input=transcoder(0)->input) { - size=input( + outlen=buf_size*6/*max*/; + out=new(pool) unsigned char[outlen+1]; + error=input( out, &outlen, (const unsigned char *)buf, (int *)&buf_size, transcoder(0)->inputInfo); - } else - memcpy(out, buf, size=buf_size); + } else { + outlen=buf_size; + out=new(pool) unsigned char[outlen+1]; + memcpy(out, buf, outlen); + error=0; + } - if(size<0) - throw Exception(0, 0, + if(error<0) + throw Exception(0, 0, - "transcode_buf failed (%d)", size); + "transcode_buf failed (%d)", error); - out[size]=0; - return GdomeDOMString_auto_ptr((gchar*)out); + out[outlen/*surely would be less then on input*/]=0; + return (xmlChar *)out; +} +GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { + return GdomeDOMString_auto_ptr((gchar*)transcode_buf2xchar(buf, buf_size)); } GdomeDOMString_auto_ptr Charset::transcode(const String& s) { - const char *cstr=s.cstr(String::UL_UNSPECIFIED); + CharPtr cstr=s.cstr(String::UL_UNSPECIFIED); - return transcode_buf(cstr, strlen(cstr)); + return transcode_buf2dom(cstr, strlen(cstr)); } #endif