Annotation of parser3/src/main/pa_pool.C, revision 1.43

1.13      paf         1: /** @file
1.14      paf         2:        Parser: pool class.
                      3: 
1.10      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.39      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.14      paf         6: 
1.43    ! paf         7:        $Id: pa_pool.C,v 1.42 2001/11/16 11:07:58 paf Exp $
1.2       paf         8: */
1.1       paf         9: 
                     10: #include "pa_pool.h"
1.7       paf        11: #include "pa_exception.h"
1.22      parser     12: #include "pa_common.h"
1.41      paf        13: #include "pa_sapi.h"
1.22      parser     14: 
1.26      parser     15: #ifdef XML
1.22      parser     16: #include <util/PlatformUtils.hpp>
1.26      parser     17: #endif
1.22      parser     18: 
                     19: Pool::Pool(void *astorage) : 
1.43    ! paf        20:        fstorage(astorage), fcontext(0)        !            21:        ftotal_allocated(0), ftotal_times(0)
1.26      parser     22: #ifdef XML
                     23:        , transcoder(0) 
                     24: #endif
                     25:        {
                     26: #ifdef XML
1.25      parser     27:        charset=new(*this) String(*this, "UTF-8");
1.29      parser     28: #else
                     29:        charset=new(*this) String(*this, "");
1.26      parser     30: #endif
1.22      parser     31: }
                     32: 
1.24      parser     33: Pool::~Pool() {
1.26      parser     34: #ifdef XML
1.24      parser     35:        delete transcoder;
1.26      parser     36: #endif
1.22      parser     37: }
1.1       paf        38: 
1.21      parser     39: void Pool::fail_alloc(size_t size) const {
1.42      paf        40:        SAPI::die("out of pooled memory: failed to allocate %u bytes, "
1.41      paf        41:                "total allocated %u in %u times", 
                     42:                size, 
                     43:                ftotal_allocated, ftotal_times);
1.7       paf        44: }
1.21      parser     45: 
                     46: void Pool::fail_register_cleanup() const {
1.41      paf        47:        SAPI::die("failed to register cleanup");
1.22      parser     48: }
                     49: 
1.25      parser     50: void Pool::set_charset(const String &new_charset) {
                     51:        if(new_charset!=*charset) {
1.29      parser     52: #ifdef XML
1.24      parser     53:                delete transcoder;  transcoder=0; // flag "we need new transcoder"
1.29      parser     54: #endif
1.25      parser     55:                charset=&new_charset; // for this charset
1.22      parser     56:        }
                     57: }
                     58: 
1.29      parser     59: #ifdef XML
1.22      parser     60: void Pool::update_transcoder() {
                     61:        if(transcoder)
                     62:                return;
                     63: 
                     64:        XMLTransService::Codes resValue;
1.25      parser     65:        transcoder=XMLPlatformUtils::fgTransService->makeNewTranscoderFor(charset->cstr(), resValue, 60);
1.24      parser     66:        if(!transcoder)
1.35      parser     67:                throw Exception(0, 0,
1.25      parser     68:                        charset,
1.24      parser     69:                        "unsupported encoding");
1.22      parser     70: }
                     71: 
1.25      parser     72: const char *Pool::transcode_cstr(const XalanDOMString& s) { 
1.22      parser     73:        update_transcoder();
                     74: 
1.24      parser     75:        const unsigned int len=s.size()*2;
1.22      parser     76:        XMLByte* dest=(XMLByte *)malloc((len+1)*sizeof(XMLByte));
                     77:        bool error=true;
                     78:        try {
                     79:                if(transcoder) {
                     80:                        unsigned int charsEaten;
                     81:                        unsigned int size=transcoder->transcodeTo(
                     82:                                s.c_str(), s.length(),
1.24      parser     83:                                dest, len,
1.22      parser     84:                                charsEaten,
1.24      parser     85:                                XMLTranscoder::UnRep_RepChar //UnRep_Throw
1.22      parser     86:                        );
                     87:                        dest[size]=0;
                     88:                        error=false;
                     89:                }
1.32      parser     90:        } catch(XMLException& e) {
1.38      parser     91:                Exception::provide_source(*this, 0, e);
1.22      parser     92:        }
                     93:        return (const char *)dest;
1.25      parser     94: }
                     95: String& Pool::transcode(const XalanDOMString& s) { 
                     96:        return *new(*this) String(*this, transcode_cstr(s)); 
1.32      parser     97: }
                     98: 
1.36      parser     99: std::auto_ptr<XalanDOMString> Pool::transcode_buf(const char *buf, size_t buf_size) { 
1.32      parser    100:        update_transcoder();
                    101: 
                    102:        unsigned int dest_size=0;
                    103:        XMLCh* dest=(XMLCh *)malloc((buf_size+1)*sizeof(XMLCh));
                    104:        unsigned char *charSizes=(unsigned char *)malloc(buf_size*sizeof(unsigned char));
1.37      parser    105:        XalanDOMString *result;
1.32      parser    106:        try {
                    107:                if(transcoder) {
                    108:                        unsigned int bytesEaten;
                    109:                        unsigned int dest_size=transcoder->transcodeFrom(
                    110:                                (unsigned char *)buf,
                    111:                                (const unsigned int)buf_size,
                    112:                                dest, (const unsigned int)buf_size,
                    113:                                bytesEaten,
                    114:                                charSizes
                    115:                        );
1.37      parser    116:                        result=new XalanDOMString(dest, dest_size);
1.32      parser    117:                }
                    118:        } catch(XMLException& e) {
1.38      parser    119:                Exception::provide_source(*this, 0, e);
1.37      parser    120:                result=0; //calm, compiler
1.32      parser    121:        }
                    122:        
1.37      parser    123:        return std::auto_ptr<XalanDOMString>(result);
1.32      parser    124: }
1.36      parser    125: std::auto_ptr<XalanDOMString> Pool::transcode(const String& s) { 
1.40      paf       126:        const char *cstr=s.cstr(String::UL_UNSPECIFIED);
1.32      parser    127: 
                    128:        return transcode_buf(cstr, strlen(cstr)); 
1.22      parser    129: }
1.26      parser    130: 
                    131: #endif

E-mail: