|
|
| version 1.25, 2001/09/21 08:38:28 | version 1.59.2.1, 2003/01/22 15:39:08 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: pool class. | Parser: pool class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| static const char* IDENT_POOL_C="$Date$"; | |
| #include "pa_pool.h" | #include "pa_pool.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_sapi.h" | |
| //#include "pa_charset.h" | |
| static void *fail_alloc(char *what, size_t size) { | |
| SAPI::die("out of memory: failed to %s %u bytes", | |
| what, size); | |
| // never reached | |
| return 0; | |
| } | |
| #include <PlatformSupport/DOMStringHelper.hpp> | void *pa_malloc(size_t size) { |
| #include <util/PlatformUtils.hpp> | if(void *result=malloc(size)) |
| return result; | |
| return fail_alloc("allocate", size); | |
| } | |
| void *pa_calloc(size_t size) { | |
| if(void *result=calloc(1, size)) | |
| return result; | |
| return fail_alloc("allocate", size); | |
| } | |
| void pa_free(void *ptr) { | |
| free(ptr); | |
| } | |
| void *pa_realloc(void *ptr, size_t size) { | |
| if(void *result=realloc(ptr, size)) | |
| return result; | |
| return fail_alloc("reallocate to", size); | |
| } | |
| /* | |
| Pool::Pool(void *astorage) : | Pool::Pool(void *astorage) : |
| fstorage(astorage), fcontext(0), ftag(0), fexception(0), | fstorage(astorage), fcontext(0), |
| transcoder(0) { | ftotal_allocated(0), ftotal_times(0), |
| charset=new(*this) String(*this, "UTF-8"); | source_charset(0), client_charset(0) |
| { | |
| } | } |
| Pool::~Pool() { | void *Pool::copy(const void *buf, const size_t size) { |
| delete transcoder; | if(!buf || !size) |
| return 0; | |
| void *result=malloc(size); | |
| memcpy(result, buf, size); | |
| return result; | |
| } | } |
| char *Pool::copy(const char *cstr) { | |
| if(cstr) { | |
| size_t size=strlen(cstr)+1; | |
| return (char *)copy(cstr, size); | |
| } | |
| return 0; | |
| } | |
| void Pool::fail_alloc(size_t size) const { | void Pool::fail_alloc(size_t size) const { |
| fexception->_throw(0, 0, | SAPI::die("out of pool memory: failed to allocate %u bytes; " |
| 0, | "already allocated on pool: %u bytes in %u times", |
| "failed to allocate %u bytes", size); | size, |
| ftotal_allocated, ftotal_times); | |
| } | } |
| void Pool::fail_register_cleanup() const { | void Pool::fail_register_cleanup() const { |
| fexception->_throw(0, 0, | SAPI::die("failed to register cleanup"); |
| 0, | |
| "failed to register cleanup"); | |
| } | } |
| void Pool::set_charset(const String &new_charset) { | void Pool::set_source_charset(Charset& acharset) { |
| if(new_charset!=*charset) { | source_charset=&acharset; |
| delete transcoder; transcoder=0; // flag "we need new transcoder" | } |
| charset=&new_charset; // for this charset | Charset& Pool::get_source_charset() { |
| } | if(!source_charset) |
| throw Exception(0, | |
| 0, | |
| "no source charset defined yet"); | |
| return *source_charset; | |
| } | } |
| void Pool::update_transcoder() { | void Pool::set_client_charset(Charset& acharset) { |
| if(transcoder) | client_charset=&acharset; |
| return; | } |
| Charset& Pool::get_client_charset() { | |
| XMLTransService::Codes resValue; | if(!client_charset) |
| transcoder=XMLPlatformUtils::fgTransService->makeNewTranscoderFor(charset->cstr(), resValue, 60); | throw Exception(0, |
| if(!transcoder) | 0, |
| THROW(0, 0, | "no client charset defined yet"); |
| charset, | return *client_charset; |
| "unsupported encoding"); | |
| } | } |
| #ifdef XML | |
| const char *Pool::transcode_cstr(const XalanDOMString& s) { | const char *Pool::transcode_cstr(xmlChar *s) { |
| update_transcoder(); | return get_source_charset().transcode_cstr(s); |
| } | |
| const unsigned int len=s.size()*2; | |
| XMLByte* dest=(XMLByte *)malloc((len+1)*sizeof(XMLByte)); | String& Pool::transcode(xmlChar *s |
| bool error=true; | #ifndef NO_STRING_ORIGIN |
| try { | , const String *origin |
| if(transcoder) { | #endif |
| unsigned int charsEaten; | ) { |
| unsigned int size=transcoder->transcodeTo( | return get_source_charset().transcode(s, origin); |
| s.c_str(), s.length(), | } |
| dest, len, | |
| charsEaten, | const char *Pool::transcode_cstr(GdomeDOMString *s) { |
| XMLTranscoder::UnRep_RepChar //UnRep_Throw | return get_source_charset().transcode_cstr(s); |
| ); | } |
| dest[size]=0; | |
| error=false; | String& Pool::transcode(GdomeDOMString *s |
| } | #ifndef NO_STRING_ORIGIN |
| } catch(...) { | , const String *origin |
| } | #endif |
| if(error) { | ) { |
| memset(dest, '?', s.size()); | return get_source_charset().transcode(s, origin); |
| ((char *)dest)[s.size()]=0; | |
| } | |
| return (const char *)dest; | |
| } | } |
| String& Pool::transcode(const XalanDOMString& s) { | |
| return *new(*this) String(*this, transcode_cstr(s)); | xmlChar *Pool::transcode_buf2xchar(const char *buf, size_t buf_size) { |
| return get_source_charset().transcode_buf2xchar(buf, buf_size); | |
| } | } |
| void Pool::_throw(const String *source, const XSLException& e) { | GdomeDOMString_auto_ptr Pool::transcode(const String& s) { |
| if(e.getURI().empty()) | return get_source_charset().transcode(s); |
| THROW(0, 0, | |
| source, | |
| "%s (%s)", | |
| transcode(e.getMessage()), // message for exception | |
| transcode(e.getType()) // type of exception | |
| ); | |
| else | |
| THROW(0, 0, | |
| source, | |
| "%s (%s) %s(%d:%d)'", | |
| transcode(e.getMessage()), // message for exception | |
| transcode(e.getType()), // type of exception | |
| transcode(e.getURI()), // URI for the associated document, if any | |
| e.getLineNumber(), // line number, or -1 if unknown | |
| e.getColumnNumber() // column number, or -1 if unknown | |
| ); | |
| } | } |
| #endif | |
| */ |