|
|
| version 1.33.2.19.2.2, 2003/03/19 16:28:08 | version 1.33.2.19.2.14, 2003/04/02 10:50:53 |
|---|---|
| Line 68 static void element2case(unsigned char f | Line 68 static void element2case(unsigned char f |
| // methods | // methods |
| extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c | extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c |
| Charset::Charset(const String& aname, const String& afile_spec): | Charset::Charset(const String& aname, const String* afile_spec): |
| fname(aname), | fname(aname), |
| fname_cstr(aname->cstr()) { | fname_cstr(aname.cstrm()) { |
| for(char *c=fname_cstr; *c; c++) | for(char *c=fname_cstr; *c; c++) |
| *c = toupper(*c); | *c = toupper(*c); |
| if(afile_spec) { | if(afile_spec) { |
| fisUTF8=false; | fisUTF8=false; |
| load_definition(afile_spec); | load_definition(*afile_spec); |
| #ifdef XML | #ifdef XML |
| addEncoding(fname_cstr); | addEncoding(fname_cstr); |
| #endif | #endif |
| Line 92 Charset::Charset(const String& aname, co | Line 92 Charset::Charset(const String& aname, co |
| #endif | #endif |
| } | } |
| Charset::~Charset() { | |
| // @todonow unregister encodings | |
| #ifdef XML | |
| // not deleting transcoder, that's not our business | |
| #endif | |
| } | |
| void Charset::load_definition(const String& afile_spec) { | void Charset::load_definition(const String& afile_spec) { |
| // pcre_tables | // pcre_tables |
| // lowcase, flipcase, bits digit+word+whitespace, masks | // lowcase, flipcase, bits digit+word+whitespace, masks |
| Line 117 void Charset::load_definition(const Stri | Line 110 void Charset::load_definition(const Stri |
| tables.toTableSize++; | tables.toTableSize++; |
| // loading text | // loading text |
| Pool pool_for_load_only; | char *data=file_read_text(UTF8_charset, afile_spec); |
| char *data=file_read_text(pool_for_load_only, *UTF8_charset, afile_spec); | |
| // ignore header | // ignore header |
| getrow(&data); | getrow(&data); |
| Line 148 void Charset::load_definition(const Stri | Line 140 void Charset::load_definition(const Stri |
| // charset | // charset |
| if(tables.toTableSize>MAX_CHARSET_UNI_CODES) | if(tables.toTableSize>MAX_CHARSET_UNI_CODES) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| afile_spec, | &afile_spec, |
| "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); | "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); |
| XMLCh unicode=(XMLCh)to_wchar_code(cell); | XMLCh unicode=(XMLCh)to_wchar_code(cell); |
| Line 220 void Charset::transcode( | Line 212 void Charset::transcode( |
| switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) { | switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) { |
| default: // 0x00 | default: // 0x00 |
| source_charset.transcodeToCharset(pool, dest_charset, | source_charset.transcodeToCharset(dest_charset, |
| source_body, source_content_length, | source_body, source_content_length, |
| dest_body, dest_content_length); | dest_body, dest_content_length); |
| break; | break; |
| case 0x01: | case 0x01: |
| source_charset.transcodeToUTF8(pool, | source_charset.transcodeToUTF8( |
| source_body, source_content_length, | source_body, source_content_length, |
| dest_body, dest_content_length); | dest_body, dest_content_length); |
| break; | break; |
| case 0x10: | case 0x10: |
| dest_charset.transcodeFromUTF8(pool, | dest_charset.transcodeFromUTF8( |
| source_body, source_content_length, | source_body, source_content_length, |
| dest_body, dest_content_length); | dest_body, dest_content_length); |
| break; | break; |
| case 0x11: | case 0x11: |
| dest_body=pool.copy((char*)source_body, dest_content_length=source_content_length); | dest_body=source_body; |
| dest_content_length=source_content_length; | |
| break; | break; |
| } | } |
| } | } |
| Line 449 void Charset::transcodeToUTF8( | Line 442 void Charset::transcodeToUTF8( |
| const void* source_body, size_t source_content_length, | const void* source_body, size_t source_content_length, |
| const void *& adest_body, size_t& dest_content_length) const { | 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*/; | dest_content_length=source_content_length*6/*so that surly enough, max utf8 seq len=6*/; |
| XMLByte *dest_body=new XMLByte[dest_content_length]; | XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_content_length]; |
| if(::transcodeToUTF8( | if(::transcodeToUTF8( |
| (XMLByte *)source_body, source_content_length, | (XMLByte *)source_body, source_content_length, |
| Line 466 void Charset::transcodeFromUTF8( | Line 459 void Charset::transcodeFromUTF8( |
| const void* source_body, size_t source_content_length, | const void* source_body, size_t source_content_length, |
| const void *& adest_body, size_t& dest_content_length) const { | const void *& adest_body, size_t& dest_content_length) const { |
| dest_content_length=source_content_length*6/*so that surly enough, "ÿ" has max ratio */; | dest_content_length=source_content_length*6/*so that surly enough, "ÿ" has max ratio */; |
| XMLByte *dest_body=new XMLByte[dest_content_length]; | XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_content_length]; |
| if(::transcodeFromUTF8( | if(::transcodeFromUTF8( |
| (XMLByte *)source_body, source_content_length, | (XMLByte *)source_body, source_content_length, |
| Line 482 void Charset::transcodeFromUTF8( | Line 475 void Charset::transcodeFromUTF8( |
| /// transcode using both charsets | /// transcode using both charsets |
| void Charset::transcodeToCharset( | void Charset::transcodeToCharset( |
| const Charset& dest_charset, | const Charset& dest_charset, |
| const void* source_body, size_t source_content_length, | const void* source_body, size_t source_content_length, |
| const void *& adest_body, size_t& adest_content_length) const { | const void *& adest_body, size_t& adest_content_length) const { |
| if(&dest_charset==this) { | if(&dest_charset==this) { |
| adest_body=pool.copy((char*)source_body, adest_content_length=source_content_length); | adest_body=source_body; |
| adest_content_length=source_content_length; | |
| } else { | } else { |
| size_t dest_content_length=source_content_length; | size_t dest_content_length=source_content_length; |
| unsigned char *dest_body=new unsigned char[dest_content_length]; | unsigned char *dest_body=new(PointerFreeGC) unsigned char[dest_content_length]; |
| const XMLByte* srcPtr=(XMLByte *)source_body; | const XMLByte* srcPtr=(XMLByte *)source_body; |
| const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length; | const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length; |
| Line 510 void Charset::transcodeToCharset( | Line 504 void Charset::transcodeToCharset( |
| } | } |
| #ifdef XML | #ifdef XML |
| static int xml256CharEncodingInputFunc ( | |
| unsigned char *out, | |
| int *outlen, | |
| const unsigned char *in, | |
| int *inlen, | |
| void *info) { | |
| return transcodeToUTF8( | |
| in, *(size_t*)inlen, | |
| out, *(size_t*)outlen, | |
| *(const Charset::Tables *)info); | |
| } | |
| static int xml256CharEncodingOutputFunc ( | |
| unsigned char *out, | |
| int *outlen, | |
| const unsigned char *in, | |
| int *inlen, | |
| void *info) { | |
| return transcodeFromUTF8( | |
| in, *(size_t*)inlen, | |
| out, *(size_t*)outlen, | |
| *(const Charset::Tables *)info); | |
| } | |
| static const Charset::Tables* tables[MAX_CHARSETS]; | |
| #define declareXml256ioFuncs(i) \ | |
| static int xml256CharEncodingInputFunc##i( \ | |
| unsigned char *out, int *outlen, \ | |
| const unsigned char *in, int *inlen,void*) { \ | |
| return transcodeToUTF8( \ | |
| in, *(size_t*)inlen, \ | |
| out, *(size_t*)outlen, \ | |
| *tables[i]); \ | |
| } \ | |
| static int xml256CharEncodingOutputFunc##i( \ | |
| unsigned char *out, int *outlen, \ | |
| const unsigned char *in, int *inlen,void*) { \ | |
| return transcodeFromUTF8( \ | |
| in, *(size_t*)inlen, \ | |
| out, *(size_t*)outlen, \ | |
| *tables[i]); \ | |
| } | |
| declareXml256ioFuncs(0) declareXml256ioFuncs(1) | |
| declareXml256ioFuncs(2) declareXml256ioFuncs(3) | |
| declareXml256ioFuncs(4) declareXml256ioFuncs(5) | |
| declareXml256ioFuncs(6) declareXml256ioFuncs(7) | |
| declareXml256ioFuncs(8) declareXml256ioFuncs(9) | |
| static xmlCharEncodingInputFunc inputFuncs[MAX_CHARSETS]={ | |
| xml256CharEncodingInputFunc0, xml256CharEncodingInputFunc1, | |
| xml256CharEncodingInputFunc2, xml256CharEncodingInputFunc3, | |
| xml256CharEncodingInputFunc4, xml256CharEncodingInputFunc5, | |
| xml256CharEncodingInputFunc6, xml256CharEncodingInputFunc7, | |
| xml256CharEncodingInputFunc8, xml256CharEncodingInputFunc9 | |
| }; | |
| static xmlCharEncodingOutputFunc outputFuncs[MAX_CHARSETS]={ | |
| xml256CharEncodingOutputFunc0, xml256CharEncodingOutputFunc1, | |
| xml256CharEncodingOutputFunc2, xml256CharEncodingOutputFunc3, | |
| xml256CharEncodingOutputFunc4, xml256CharEncodingOutputFunc5, | |
| xml256CharEncodingOutputFunc6, xml256CharEncodingOutputFunc7, | |
| xml256CharEncodingOutputFunc8, xml256CharEncodingOutputFunc9 | |
| }; | |
| static size_t handlers_count=0; | |
| void Charset::addEncoding(char *name_cstr) { | void Charset::addEncoding(char *name_cstr) { |
| xmlCharEncodingHandler* handler=new xmlCharEncodingHandler; | if(handlers_count==MAX_CHARSETS) |
| fcreated_handler=xmlCharEncodingHandlerPtr(handler); | throw Exception(0, |
| 0, | |
| "already allocated %d handlers, no space for new encoding '%s'", | |
| MAX_CHARSETS, name_cstr); | |
| handler->name=name_cstr; | xmlCharEncodingHandler* handler=new(PointerFreeGC) xmlCharEncodingHandler; |
| handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; | { |
| handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; | handler->name=name_cstr; |
| handler->input=inputFuncs[handlers_count]; | |
| handler->output=outputFuncs[handlers_count]; | |
| ::tables[handlers_count]=&tables; | |
| handlers_count++; | |
| } | |
| xmlRegisterCharEncodingHandler(handler); | xmlRegisterCharEncodingHandler(handler); |
| } | } |
| void Charset::initTranscoder(const String* source, const char* name_cstr) { | void Charset::initTranscoder(const String& name, const char* name_cstr) { |
| ftranscoder=xmlFindCharEncodingHandler(name_cstr); | ftranscoder=xmlFindCharEncodingHandler(name_cstr); |
| transcoder(source); // check right way | transcoder(name); // check right way |
| } | } |
| xmlCharEncodingHandler& Charset::transcoder(const String* source) { | xmlCharEncodingHandler& Charset::transcoder(const String& name) { |
| if(!ftranscoder) | if(!ftranscoder) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| source, | &name, |
| "unsupported encoding"); | "unsupported encoding"); |
| return *ftranscoder; | return *ftranscoder; |
| } | } |
| const char* Charset::transcode_cstrxmlChar* s) { | const char* Charset::transcode_cstr(xmlChar* s) { |
| if(!s) | if(!s) |
| return ""; | return ""; |
| int inlen=strlen((const char* )s); | int inlen=strlen((const char* )s); |
| int outlen=inlen+1; // max | int outlen=inlen+1; // max |
| char *out=new char[outlen]; | char *out=new(PointerFreeGC) char[outlen]; |
| int error; | int error; |
| if(xmlCharEncodingOutputFunc output=transcoder(0).output) { | if(xmlCharEncodingOutputFunc output=transcoder(fname).output) { |
| error=output( | error=output( |
| (unsigned char*)out, &outlen, | (unsigned char*)out, &outlen, |
| (const unsigned char*)s, &inlen, | (const unsigned char*)s, &inlen,0); |
| transcoder(0).outputInfo); | |
| } else { | } else { |
| memcpy(out, s, outlen=inlen); | memcpy(out, s, outlen=inlen); |
| error=0; | error=0; |
| Line 585 const char* Charset::transcode_cstrxmlCh | Line 605 const char* Charset::transcode_cstrxmlCh |
| out[outlen/*surely would be less then on input*/]=0; | out[outlen/*surely would be less then on input*/]=0; |
| return out; | return out; |
| } | } |
| const String& Charset::transcodexmlChar* s | const String& Charset::transcode(xmlChar* s) { |
| #ifndef NO_STRING_ORIGIN | return *new String(transcode_cstr(s), 0/*auto-size*/, true); |
| , const String& origin | } |
| #endif | const char* Charset::transcode_cstr(GdomeDOMString* s) { |
| ) { | return s?transcode_cstr(BAD_CAST s->str):""; |
| const String& result(new String()); | } |
| result->APPEND_CLEAN( | const String& Charset::transcode(GdomeDOMString* s) { |
| transcode_cstr(pool, s), 0/*auto-size*/, | return *new String(transcode_cstr(s), 0/*auto-size*/, true); |
| origin->origin().file, origin->origin().line); | |
| return result; | |
| } | |
| const char* Charset::transcode_cstrGdomeDOMString* s) { | |
| return s?transcode_cstr(pool, BAD_CAST s->str):""; | |
| } | |
| const String& Charset::transcodeGdomeDOMString* s | |
| #ifndef NO_STRING_ORIGIN | |
| , const String& origin | |
| #endif | |
| ) { | |
| const String& result(new String()); | |
| result->APPEND_CLEAN(transcode_cstr(pool, s), 0/*auto-size*/, origin->origin().file, origin->origin().line); | |
| return result; | |
| } | } |
| /// @test less memory using -maybe- xmlParserInputBufferCreateMem | /// @test less memory using -maybe- xmlParserInputBufferCreateMem |
| void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, | void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, |
| const char* buf, size_t buf_size) { | const char* buf, size_t buf_size) { |
| unsigned char* out; | unsigned char* out; |
| int outlen; | int outlen; |
| int error; | int error; |
| if(xmlCharEncodingInputFunc input=transcoder(0).input) { | if(xmlCharEncodingInputFunc input=transcoder(fname).input) { |
| outlen=buf_size*6/*max*/; | outlen=buf_size*6/*max*/; |
| out=(unsigned char*)malloc_func(outlen+1); | out=(unsigned char*)malloc_func(outlen+1); |
| error=input( | error=input( |
| out, &outlen, | out, &outlen, |
| (const unsigned char *)buf, (int *)&buf_size, | (const unsigned char *)buf, (int *)&buf_size,0); |
| transcoder(0).inputInfo); | |
| } else { | } else { |
| outlen=buf_size; | outlen=buf_size; |
| out=(unsigned char*)malloc_func(outlen+1); | out=(unsigned char*)malloc_func(outlen+1); |
| Line 651 GdomeDOMString_auto_ptr Charset::transco | Line 656 GdomeDOMString_auto_ptr Charset::transco |
| return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); | return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode(const String& s) { | GdomeDOMString_auto_ptr Charset::transcode(const String& s) { |
| const char* cstr=s->cstr(String::UL_UNSPECIFIED); | const char* cstr=s.cstr(String::L_UNSPECIFIED); |
| return transcode_buf2dom(cstr, strlen(cstr)); | return transcode_buf2dom(cstr, strlen(cstr)); |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode(const StringBody s) { | |
| const char* cstr=s.cstr(); | |
| return transcode_buf2dom(cstr, s.length()); | |
| } | |
| #endif | #endif |