--- parser3/src/main/pa_string.C 2001/10/17 15:44:47 1.111 +++ parser3/src/main/pa_string.C 2001/10/24 09:03:42 1.113 @@ -4,7 +4,7 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_string.C,v 1.111 2001/10/17 15:44:47 parser Exp $ + $Id: pa_string.C,v 1.113 2001/10/24 09:03:42 parser Exp $ */ #include "pa_config_includes.h" @@ -153,7 +153,7 @@ String& String::real_append(STRING_APPEN char String::first_char() const { if(!fused_rows) - THROW(0, 0, + throw Exception(0, 0, this, "getting first char of empty string"); @@ -508,7 +508,7 @@ bool String::match(const unsigned char * bool *was_global) const { if(!regexp.size()) - THROW(0, 0, + throw Exception(0, 0, aorigin, "regexp is empty"); const char *pattern=regexp.cstr(UL_AS_IS); @@ -522,14 +522,14 @@ bool String::match(const unsigned char * pcre_tables); if(!code) - THROW(0, 0, + throw Exception(0, 0, ®exp.mid(erroffset, regexp.size()), "regular expression syntax error - %s", errptr); int info_substrings=pcre_info(code, 0, 0); if(info_substrings<0) { pcre_free(code); - THROW(0, 0, + throw Exception(0, 0, aorigin, "pcre_info error (%d)", info_substrings); @@ -569,7 +569,7 @@ bool String::match(const unsigned char * if(exec_substrings<0) { pcre_free(code); - THROW(0, 0, + throw Exception(0, 0, aorigin, "regular expression execute error (%d)", exec_substrings); @@ -617,7 +617,7 @@ String& String::change_case(Pool& pool, b=0; break; default: - PTHROW(0, 0, + throw Exception(0, 0, this, "unknown change case kind #%d", static_cast(kind)); // never @@ -814,7 +814,7 @@ double String::as_double() const { result=(double)strtod(cstr, &error_pos); if(*error_pos/*not EOS*/) - THROW(0, 0, + throw Exception(0, 0, this, "invalid number (double)"); @@ -834,9 +834,75 @@ int String::as_int() const { result=(int)strtol(cstr, &error_pos, 0); if(*error_pos/*not EOS*/) - THROW(0, 0, + throw Exception(0, 0, this, "invalid number (int)"); return result; } + +/* @todo maybe network order worth spending some effort? + don't bothering myself with network byte order, + am not planning to be able to move resulting file across platforms + for now +*/ +void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const { + buf_size= + prolog_size + +fused_rows*(sizeof(Untaint_lang)+sizeof(size_t)) + +size(); + buf=malloc(buf_size); + char *cur=(char *)buf+prolog_size; + + const Chunk *chunk=&head; + do { + const Chunk::Row *row=chunk->rows; + for(size_t i=0; icount; i++) { + if(row==append_here) + goto break2; + + // lang + memcpy(cur, &row->item.lang, sizeof(Untaint_lang)); + cur+=sizeof(Untaint_lang); + // size + memcpy(cur, &row->item.size, sizeof(size_t)); + cur+=sizeof(size_t); + // bytes + memcpy(cur, row->item.ptr, row->item.size); + cur+=row->item.size; + + row++; + } + chunk=row->link; + } while(chunk); +break2: + ; +} + +/* @todo maybe network order worth spending some effort? + don't bothering myself with network byte order, + am not planning to be able to move resulting file across platforms + for now +*/ +#ifndef DOXYGEN +struct Serialized_piece { + String::Untaint_lang lang; + size_t size; + char ptr[1]; +}; +#endif + +void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) { + char *cur=((char *)buf)+prolog_size; + buf_size-=prolog_size; + + while(buf_size) { + Serialized_piece& p=*(Serialized_piece *)cur; + APPEND(p.ptr, p.size, p.lang, file, 0); + + size_t piece_size=sizeof(p.lang)+sizeof(p.size)+p.size; + cur+=piece_size; + buf_size-=piece_size; + } +} +