Diff for /parser3/src/main/pa_charset.C between versions 1.33.2.15 and 1.33.2.19.2.7

version 1.33.2.15, 2003/03/06 12:21:19 version 1.33.2.19.2.7, 2003/03/24 17:52:19
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(StringPtr aname, StringPtr 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 99  Charset::~Charset() { Line 99  Charset::~Charset() {
 #endif  #endif
 }  }
   
 void Charset::load_definition(StringPtr 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(StringPtr Line 117  void Charset::load_definition(StringPtr
         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(StringPtr Line 147  void Charset::load_definition(StringPtr
                                 // 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 208  static XMLByte xlatOneTo(const XMLCh toX Line 207  static XMLByte xlatOneTo(const XMLCh toX
     return not_found;      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& source_charset, const void* source_body, size_t source_content_length,
         const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length          const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length
         ) {          ) {
Line 220  void Charset::transcode(Pool& pool, Line 219  void Charset::transcode(Pool& pool,
   
         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;
Line 409  static int transcodeFromUTF8( Line 408  static int transcodeFromUTF8(
   
             default:              default:
                 throw Exception(0,                  throw Exception(0,
                                         Exception::undefined_source,                                          0,
                                         "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes);                                          "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes);
         }          }
         tmpVal-=gUTFOffsets[trailingBytes];          tmpVal-=gUTFOffsets[trailingBytes];
Line 424  static int transcodeFromUTF8( Line 423  static int transcodeFromUTF8(
                                 outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal;                                  outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal;
                 } else                  } else
                         throw Exception(0,                          throw Exception(0,
                                 Exception::undefined_source,                                  0,
                                 "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal);                                  "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal);
         }          }
   
Line 446  of ocetes consumed. Line 445  of ocetes consumed.
 }  }
   
 /// @todo not so memory-hungry with prescan  /// @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* 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(pool) XMLByte[dest_content_length];          XMLByte *dest_body=new XMLByte[dest_content_length];
   
         if(::transcodeToUTF8(          if(::transcodeToUTF8(
                 (XMLByte *)source_body, source_content_length,                  (XMLByte *)source_body, source_content_length,
Line 463  void Charset::transcodeToUTF8(Pool& pool Line 462  void Charset::transcodeToUTF8(Pool& pool
         // return          // return
         adest_body=dest_body;          adest_body=dest_body;
 }  }
 void Charset::transcodeFromUTF8(Pool& pool,  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(pool) XMLByte[dest_content_length];          XMLByte *dest_body=new 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(Pool& po Line 481  void Charset::transcodeFromUTF8(Pool& po
 }  }
   
 /// transcode using both charsets  /// transcode using both charsets
 void Charset::transcodeToCharset(Pool& pool,  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 {
Line 491  void Charset::transcodeToCharset(Pool& p Line 490  void Charset::transcodeToCharset(Pool& p
                 adest_content_length=source_content_length;                  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(pool) unsigned char[dest_content_length];                  unsigned char *dest_body=new 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 539  static int         xml256CharEncodingOut Line 538  static int         xml256CharEncodingOut
   
 void Charset::addEncoding(char *name_cstr) {  void Charset::addEncoding(char *name_cstr) {
         xmlCharEncodingHandler* handler=new xmlCharEncodingHandler;          xmlCharEncodingHandler* handler=new xmlCharEncodingHandler;
         fcreated_handler=xmlCharEncodingHandlerPtr(fcreated_handler);  
   
         handler->name=name_cstr;          handler->name=name_cstr;
         handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables;          handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables;
         handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables;          handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables;
Line 548  void Charset::addEncoding(char *name_cst Line 545  void Charset::addEncoding(char *name_cst
         xmlRegisterCharEncodingHandler(handler);          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);          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)          if(!ftranscoder)
                 throw Exception("parser.runtime",                  throw Exception("parser.runtime",
                         source,                          &name,
                         "unsupported encoding");                          "unsupported encoding");
         return *ftranscoder;          return *ftranscoder;
 }  }
   
 CharPtr 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=new char[outlen];          char *out=new(PointerFreeGC) char[outlen];
                   
         int error;          int error;
         if(xmlCharEncodingOutputFunc output=transcoder(Exception::undefined_source).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,
                         transcoder(Exception::undefined_source).outputInfo);                          transcoder(fname).outputInfo);
         } else {          } else {
                 memcpy(out, s, outlen=inlen);                  memcpy(out, s, outlen=inlen);
                 error=0;                  error=0;
         }          }
         if(error<0)          if(error<0)
                 throw Exception(Exception::undefined_type,                  throw Exception(0,
                         Exception::undefined_source,                          0,
                         "transcode_cstr failed (%d)", error);                          "transcode_cstr failed (%d)", error);
   
         out[outlen/*surely would be less then on input*/]=0;          out[outlen/*surely would be less then on input*/]=0;
         return out;          return out;
 }  }
 StringPtr Charset::transcode(xmlChar* s  const String& Charset::transcode(xmlChar* s) { 
 #ifndef NO_STRING_ORIGIN          return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); 
                 , 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 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):"";
 }  }
 StringPtr Charset::transcode(GdomeDOMString* s  const String& Charset::transcode(GdomeDOMString* s) { 
 #ifndef NO_STRING_ORIGIN          return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); 
                 , StringPtr origin  
 #endif  
                                                    ) {   
         StringPtr result(new String());  
         result->APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line);  
         return result;   
 }  }
   
 /// @test less memory using -maybe- xmlParserInputBufferCreateMem  /// @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;          unsigned char* out;
         int outlen;          int outlen;
         int error;          int error;
         if(xmlCharEncodingInputFunc input=transcoder(Exception::undefined_source).input) {          if(xmlCharEncodingInputFunc input=transcoder(fname).input) {
                 outlen=buf_size*6/*max*/;                  outlen=buf_size*6/*max*/;
                 out=(unsigned char*)malloc(sizeof(xmlChar)*(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,
                         transcoder(Exception::undefined_source).inputInfo);                          transcoder(fname).inputInfo);
         } else {          } else {
                 outlen=buf_size;                  outlen=buf_size;
                 out=(unsigned char*)malloc(sizeof(xmlChar)*(outlen+1));                  out=(unsigned char*)malloc_func(outlen+1);
                 memcpy(out, buf, outlen);                  memcpy(out, buf, outlen);
                 error=0;                  error=0;
         }          }
                   
         if(error<0)          if(error<0)
                 throw Exception(Exception::undefined_type,                  throw Exception(0,
                         Exception::undefined_source,                          0,
                         "transcode_buf failed (%d)", error);                          "transcode_buf failed (%d)", error);
   
         out[outlen/*surely would be less then on input*/]=0;          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<xmlChar*>(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<gchar*>(transcode_buf2mchar(g_malloc_wrapper, buf, buf_size));
 }  }
 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(transcode_buf2gchar(buf, buf_size));
 }  }
 GdomeDOMString_auto_ptr Charset::transcode(StringPtr s) {   GdomeDOMString_auto_ptr Charset::transcode(const String& s) { 
         CharPtr 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)); 
 }  }

Removed from v.1.33.2.15  
changed lines
  Added in v.1.33.2.19.2.7


E-mail: