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