|
|
| version 1.30, 2004/02/18 12:46:49 | version 1.47, 2011/02/21 22:57:47 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Charset connection decl. | Parser: Charset connection decl. |
| Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 16 static const char * const IDENT_CHARSET_ | Line 16 static const char * const IDENT_CHARSET_ |
| #include "pa_hash.h" | #include "pa_hash.h" |
| #include "pa_array.h" | #include "pa_array.h" |
| // hiding into namespace so to avoid stupid conflict | // pcre.h must be included BEFORE pcre_internal.h |
| namespace PCRE { | |
| #include "pcre.h" | #include "pcre.h" |
| # include "internal.h" | // have to call config.h for using pcre_internal.h as well |
| }; | #include "../lib/pcre/config.h" |
| #include "pcre_internal.h" | |
| #ifdef XML | #ifdef XML |
| #include "libxml/encoding.h" | #include "libxml/encoding.h" |
| #include "gdome.h" | |
| #endif | #endif |
| // defines | // defines |
| Line 33 namespace PCRE { | Line 32 namespace PCRE { |
| #define MAX_CHARSET_UNI_CODES 500 | #define MAX_CHARSET_UNI_CODES 500 |
| # ifndef XMLCh | #ifndef XMLCh |
| typedef unsigned int XMLCh; | typedef unsigned int XMLCh; |
| # endif | #endif |
| # ifndef XMLByte | #ifndef XMLByte |
| typedef unsigned char XMLByte; | typedef unsigned char XMLByte; |
| # endif | #endif |
| // forwards | |
| class GdomeDOMString_auto_ptr; | |
| // helpers | // helpers |
| typedef Hash<const String::Body, String::Body> HashStringString; | typedef HashString<String::Body> HashStringString; |
| /** charset holds name & transcode tables | /** charset holds name & transcode tables |
| registers libxml transcoders | registers libxml transcoders |
| Line 62 public: | Line 57 public: |
| bool isUTF8() const { return fisUTF8; } | bool isUTF8() const { return fisUTF8; } |
| static String::C transcode(const String::C src, | static String::C transcode(const String::C src, |
| const Charset& source_transcoder, | const Charset& source_charset, |
| const Charset& dest_transcoder | const Charset& dest_charset); |
| ); | |
| static String& transcode(const String& src, | static String& transcode(const String& src, |
| const Charset& source_transcoder, | const Charset& source_transcoder, |
| Line 82 public: | Line 76 public: |
| const Charset& source_transcoder, | const Charset& source_transcoder, |
| const Charset& dest_transcoder); | const Charset& dest_transcoder); |
| static String::C escape(const String::C src, | |
| const Charset& source_charset); | |
| static String::Body escape(const String::Body src, | |
| const Charset& source_charset); | |
| static String& escape(const String& src, | |
| const Charset& source_charset); | |
| static String::C escape_JSON(const String::C src, | |
| const Charset& source_charset); | |
| static String::Body escape_JSON(const String::Body src, | |
| const Charset& source_charset); | |
| static String& escape_JSON(const String& src, | |
| const Charset& source_charset); | |
| void store_Char(XMLByte*& outPtr, XMLCh src, XMLByte not_found); | |
| #ifdef XML | #ifdef XML |
| xmlCharEncodingHandler& transcoder(const String::Body NAME); | xmlCharEncodingHandler& transcoder(const String::Body NAME); |
| #endif | #endif |
| Line 130 private: | Line 140 private: |
| bool fisUTF8; | bool fisUTF8; |
| Tables tables; | Tables tables; |
| static size_t calc_escaped_length_UTF8(XMLByte* src, size_t src_length); | |
| static size_t calc_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables); | |
| static size_t calc_escaped_length(const String::C src, const Charset& source_charset); | |
| static size_t escape_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest); | |
| static size_t escape(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables); | |
| static size_t calc_JSON_escaped_length_UTF8(XMLByte* src, size_t src_length); | |
| static size_t calc_JSON_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables); | |
| static size_t calc_JSON_escaped_length(const String::C src, const Charset& source_charset); | |
| static size_t escape_JSON_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest); | |
| static size_t escape_JSON(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables); | |
| #ifdef XML | #ifdef XML |
| private: | private: |
| void addEncoding(char* name_cstr); | void addEncoding(char* name_cstr); |
| void initTranscoder(const String::Body name, const char* name_cstr); | void initTranscoder(const String::Body name, const char* name_cstr); |
| public: | public: |
| /// converts GdomeDOMString string to char* | |
| String::C transcode_cstr(GdomeDOMString* s); | |
| /// converts GdomeDOMString string to parser String | |
| const String& transcode(GdomeDOMString* s); | |
| /// converts xmlChar* null-terminated string to char* | /// converts xmlChar* null-terminated string to char* |
| String::C transcode_cstr(xmlChar* s); | String::C transcode_cstr(const xmlChar* s); |
| /// converts xmlChar* null-terminated string to parser String | /// converts xmlChar* null-terminated string to parser String |
| const String& transcode(xmlChar* s); | const String& transcode(const xmlChar* s); |
| /** converts sized char* to xmlChar* | /** converts sized char* to xmlChar* |
| @returns xmlChar* WHICH CALLER SHOULD FREE | @returns xmlChar* WHICH CALLER SHOULD FREE |
| */ | */ |
| xmlChar* transcode_buf2xchar(const char* buf, size_t buf_size); | xmlChar* transcode_buf2xchar(const char* buf, size_t buf_size); |
| /// converts char* to GdomeDOMString | /// converts parser String to xmlChar* |
| GdomeDOMString_auto_ptr transcode_buf2dom(const char* buf, size_t buf_size); | xmlChar* transcode(const String& s); |
| /// converts parser String to GdomeDOMString | /// converts parser String::Body to xmlChar* |
| GdomeDOMString_auto_ptr transcode(const String& s); | xmlChar* transcode(const String::Body s); |
| /// converts parser String::Body to GdomeDOMString | |
| GdomeDOMString_auto_ptr transcode(const String::Body s); | |
| private: | private: |
| Line 171 private: | Line 187 private: |
| extern Charset::UTF8CaseTable UTF8CaseToUpper; | extern Charset::UTF8CaseTable UTF8CaseToUpper; |
| extern Charset::UTF8CaseTable UTF8CaseToLower; | extern Charset::UTF8CaseTable UTF8CaseToLower; |
| void change_case_UTF8(const XMLByte* srcData, size_t srcLen, | void change_case_UTF8(const XMLByte* srcData, size_t srcLen, |
| XMLByte* toFill, size_t toFillLen, | XMLByte* toFill, size_t toFillLen, |
| const Charset::UTF8CaseTable& table); | const Charset::UTF8CaseTable& table); |
| size_t getUTF8BytePos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t charPos/*position in characters*/); | |
| size_t getUTF8CharPos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t bytePos/*position in bytes*/); | |
| #ifdef XML | size_t lengthUTF8(const XMLByte* srcBegin, const XMLByte* srcEnd); |
| /// Auto-object used to track GdomeDOMString usage | unsigned int lengthUTF8Char(const XMLByte c); |
| class GdomeDOMString_auto_ptr { | |
| GdomeDOMString* fstring; | |
| public: | class UTF8_string_iterator { |
| /// frees astring afterwards!!! | public: |
| explicit GdomeDOMString_auto_ptr(xmlChar* astring) : fstring(gdome_str_mkref_xml(astring)) {} | UTF8_string_iterator(const String& astring): fsrcPtr((XMLByte*)astring.cstr()), fsrcEnd(fsrcPtr + astring.length()) {} |
| explicit GdomeDOMString_auto_ptr(GdomeDOMString* astring=0) : fstring(astring) { | UTF8_string_iterator(XMLByte* asrcPtr, size_t length): fsrcPtr(asrcPtr), fsrcEnd(fsrcPtr + length) {} |
| // not ref-ing, owning | |
| } | bool has_next(); |
| ~GdomeDOMString_auto_ptr() { | XMLCh next() { return fUTF8Char; } |
| if(fstring) | XMLByte getFirstByte(){ return ffirstByte; } |
| gdome_str_unref(fstring); | size_t getCharSize(){ return fcharSize; } |
| } | private: |
| /* GdomeDOMString* get() { | const XMLByte* fsrcPtr; |
| return fstring; | const XMLByte* fsrcEnd; |
| }*/ | size_t fcharSize; |
| GdomeDOMString* use() { | XMLByte ffirstByte; |
| if(fstring) | XMLCh fUTF8Char; |
| gdome_str_ref(fstring); | |
| return fstring; | |
| } | |
| GdomeDOMString* operator->() { | |
| return fstring; | |
| } | |
| /* GdomeDOMString& operator*() { | |
| return* fstring; | |
| }*/ | |
| // copying | |
| GdomeDOMString_auto_ptr(const GdomeDOMString_auto_ptr& src) : fstring(src.fstring) { | |
| gdome_str_ref(fstring); | |
| } | |
| GdomeDOMString_auto_ptr& operator =(const GdomeDOMString_auto_ptr& src) { | |
| if(this == &src) | |
| return* this; | |
| if(fstring) | |
| gdome_str_unref(fstring); | |
| fstring=src.fstring; | |
| if(fstring) | |
| gdome_str_ref(fstring); | |
| return* this; | |
| } | |
| }; | }; |
| #endif | |
| #endif | #endif |