|
|
| version 1.33.2.2, 2003/01/28 11:38:50 | version 1.33.2.6, 2003/01/31 12:34:35 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Charset connection implementation. | Parser: Charset connection implementation. |
| Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru) | Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_CHARSET_C="$Date$"; | static const char* IDENT_CHARSET_C="$Date$"; |
| #include "pa_charset.h" | #include "pa_charset.h" |
| //#include "pa_globals.h" | #include "pa_value_includes.h" |
| #ifdef XML | #ifdef XML |
| #include "libxml/encoding.h" | #include "libxml/encoding.h" |
| Line 18 static const char* IDENT_CHARSET_C="$Dat | Line 18 static const char* IDENT_CHARSET_C="$Dat |
| #define CHARSET_UTF8_NAME "UTF-8" | #define CHARSET_UTF8_NAME "UTF-8" |
| CharsetPtr UTF8_charset(new Charset(ConstStringPtr(new String(CHARSET_UTF8_NAME)), | CharsetPtr UTF8_charset(new Charset(StringPtr(new String(CHARSET_UTF8_NAME)), |
| ConstStringPtrZero/*no file=system*/, | StringPtr(0)/*no file=system*/)); |
| 0/* no file=no file read pool*/)); | |
| // consts | // consts |
| Line 43 inline void cstr2ctypes(unsigned char *t | Line 42 inline void cstr2ctypes(unsigned char *t |
| ctypes_table[c]|=bit; | 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) | if(!cstr || !*cstr) |
| return 0; | return 0; |
| if(cstr[1]==0) | if(cstr[1]==0) |
| Line 52 inline unsigned int to_wchar_code(const | Line 51 inline unsigned int to_wchar_code(const |
| char *error_pos; | char *error_pos; |
| return(unsigned int)strtol(cstr, &error_pos, 0); | 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; | return cstr && *cstr!=0; |
| } | } |
| static void element2ctypes(unsigned char c, bool belongs, | static void element2ctypes(unsigned char c, bool belongs, |
| Line 80 static void element2case(unsigned char f | Line 79 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(ConstStringPtr aname, ConstStringPtr afile_spec, Pool* apool_for_load): | Charset::Charset(StringPtr aname, StringPtr afile_spec): |
| fname(aname), | fname(aname), |
| fname_cstr(aname->cstr()) { | fname_cstr(aname->cstr()) { |
| for(char *c=fname_cstr; *c; c++) | for(char *c=fname_cstr; *c; c++) |
| *c = toupper(*c); | *c = toupper(*c); |
| if(afile_spec && apool_for_load) { | if(afile_spec) { |
| fisUTF8=false; | fisUTF8=false; |
| loadDefinition(*apool_for_load, fname); | load_definition(fname); |
| #ifdef XML | #ifdef XML |
| addEncoding(fname_cstr); | addEncoding(fname_cstr); |
| #endif | #endif |
| Line 111 Charset::~Charset() { | Line 110 Charset::~Charset() { |
| #endif | #endif |
| } | } |
| void Charset::loadDefinition(Pool& apool_for_load, ConstStringPtr afile_spec) { | void Charset::load_definition(StringPtr afile_spec) { |
| // pcre_tables | // pcre_tables |
| // lowcase, flipcase, bits digit+word+whitespace, masks | // lowcase, flipcase, bits digit+word+whitespace, masks |
| Line 123 void Charset::loadDefinition(Pool& apool | Line 122 void Charset::loadDefinition(Pool& apool |
| // charset | // charset |
| memset(tables.fromTable, 0, sizeof(tables.fromTable)); | memset(tables.fromTable, 0, sizeof(tables.fromTable)); |
| tables.toTable=(Charset_TransRec *)apool_for_load.calloc(sizeof(Charset_TransRec)*MAX_CHARSET_UNI_CODES); | tables.toTable=(Charset_TransRec *)pool_for_load.calloc(sizeof(Charset_TransRec)*MAX_CHARSET_UNI_CODES); |
| tables.toTableSize=0; | tables.toTableSize=0; |
| // strangly vital | // strangly vital |
| tables.toTable[tables.toTableSize].intCh=0; | tables.toTable[tables.toTableSize].intCh=0; |
| Line 131 void Charset::loadDefinition(Pool& apool | Line 130 void Charset::loadDefinition(Pool& apool |
| tables.toTableSize++; | tables.toTableSize++; |
| // loading text | // loading text |
| char *data=file_read_text(apool_for_load, *UTF8_charset, afile_spec); | char *data=file_read_text(pool_for_load, *UTF8_charset, afile_spec); |
| // ignore header | // ignore header |
| getrow(&data); | getrow(&data); |
| Line 560 void Charset::addEncoding(char *name_cst | Line 559 void Charset::addEncoding(char *name_cst |
| xmlRegisterCharEncodingHandler(handler); | 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); | ftranscoder=xmlFindCharEncodingHandler(name_cstr); |
| transcoder(source); // check right way | transcoder(source); // check right way |
| } | } |
| Line 573 xmlCharEncodingHandler *Charset::transco | Line 572 xmlCharEncodingHandler *Charset::transco |
| return ftranscoder; | return ftranscoder; |
| } | } |
| const char *Charset::transcode_cstr(xmlChar *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=(char *)malloc(outlen*sizeof(char)); | char *out=(char *)malloc(outlen*sizeof(char)); |
| Line 608 String& Charset::transcode(xmlChar *s | Line 607 String& Charset::transcode(xmlChar *s |
| result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); | result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); |
| return result; | return result; |
| } | } |
| const char *Charset::transcode_cstr(GdomeDOMString *s) { | const char* Charset::transcode_cstr(GdomeDOMString *s) { |
| return s?transcode_cstr(BAD_CAST s->str):""; | return s?transcode_cstr(BAD_CAST s->str):""; |
| } | } |
| String& Charset::transcode(GdomeDOMString *s | String& Charset::transcode(GdomeDOMString *s |
| Line 622 String& Charset::transcode(GdomeDOMStrin | Line 621 String& Charset::transcode(GdomeDOMStrin |
| } | } |
| /// @test less memory using -maybe- xmlParserInputBufferCreateMem | /// @test less memory using -maybe- xmlParserInputBufferCreateMem |
| xmlChar *Charset::transcode_buf2xchar(const char *buf, size_t buf_size) { | xmlChar *Charset::transcode_buf2xchar(const char* buf, size_t buf_size) { |
| unsigned char *out; | unsigned char *out; |
| int outlen; | int outlen; |
| int error; | int error; |
| Line 648 xmlChar *Charset::transcode_buf2xchar(co | Line 647 xmlChar *Charset::transcode_buf2xchar(co |
| out[outlen/*surely would be less then on input*/]=0; | out[outlen/*surely would be less then on input*/]=0; |
| return (xmlChar *)out; | return (xmlChar *)out; |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char *buf, size_t 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((gchar*)transcode_buf2xchar(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::UL_UNSPECIFIED); |
| return transcode_buf2dom(cstr, strlen(cstr)); | return transcode_buf2dom(cstr, strlen(cstr)); |
| } | } |