00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __loaded__data_io_cpp__
00021 #define __loaded__data_io_cpp__
00022 using namespace std;
00023 #line 1 "data-io.c++"
00024 #include <errno.h>
00025 #include <string.h>
00026 #include <sys/mman.h>
00027 #include <qstring.h>
00028 #include "symbol.h"
00029 #include "array.h"
00030 #include "array-iterator.h"
00031 #include "data-token.h"
00032 #include "null.h"
00033 #include "data-string.h"
00034 #include "data-io.h"
00035 #include "version.h"
00036
00037
00038
00039
00040 DataIo::DataIo(QString qfn, const char * mode)
00041 {
00042 const char *fn;
00043 #if QT_VERSION > 0x040000
00044 QByteArray F = qfn.toAscii();
00045 fn=F.data();
00046 #else
00047 fn=qfn.ascii();
00048 #endif
00049 text = fopen(fn,mode);
00050 opened_file_myself = true;
00051 if (!text)
00052 {
00053 printf("Could not open file \"%s\"\n",fn);
00054 assert(0);
00055 exit(1);
00056 }
00057 version = DATA_VERSION_NR;
00058 }
00059
00060 DataIo::DataIo(FILE * f)
00061 {
00062 text = f;
00063 opened_file_myself = false;
00064 assert(text);
00065 version = DATA_VERSION_NR;
00066 }
00067
00068 DataIo::~DataIo()
00069 {
00070 if (text && opened_file_myself)
00071 fclose(text);
00072 };
00073
00074 void DataIo::visit(Data &data)
00075 {
00076 DataVisitor::visit(data);
00077 };
00078
00079 void DataIo::start_writing()
00080 {
00081 write_fileformat_versionnr();
00082 }
00083
00084 void DataIo::start_reading()
00085 {
00086 version = read_fileformat_versionnr();
00087 }
00088
00089 Data DataIo::read()
00090 {
00091 Data result;
00092 read_into(result);
00093 return result;
00094 }
00095
00096
00097
00098
00099 template <int D, class T>
00100 void DataTexter::visitArray(Array<D,T> & array)
00101 {
00102 if (type_name(T())=="Data" && D==1)
00103 {
00104 if (D>=1) dumpSequence(array);
00105 }
00106 else
00107 {
00108 int mark = push("<"+QString::number(D)+":");
00109 padded(type_name(T())+">\n");
00110 if (D>=1) dumpSequence(array);
00111 pop(mark,"");
00112 }
00113 };
00114
00115 template <class T>
00116 void DataTexter::dumpSequence(Array<1,T> & array)
00117 {
00118 int mark = push("[");
00119 int count = array.size(0);
00120 for(typename Array<1,T>::positions ei(array); ei.more() ; ++ei)
00121 {
00122 int lastline = get_line();
00123 Data data = to_data(*ei);
00124 DataVisitor::visit(data);
00125 if (ei[0]!=count-1)
00126 {
00127 padded(" ");
00128 if (get_line() > lastline && !newline_waiting())
00129 padded("\n");
00130 }
00131 }
00132 pop(mark,"]");
00133 };
00134
00135 #define DUMPER(D) template <class T> \
00136 void DataTexter::dumpSequence(Array<D,T> & array) \
00137 { \
00138 int mark = push("["); \
00139 int count = array.size(0); \
00140 for(ArrayIterator<D,T,true,D-1,false> submatrix(array,0) ; \
00141 submatrix.more() ; ++submatrix) \
00142 {\
00143 int lastline = get_line();\
00144 Array<D-1,T> &sub = submatrix;\
00145 dumpSequence(sub);\
00146 if (submatrix[0]!=count-1)\
00147 {\
00148 if (D==2) padded("\n"); else padded(" ");\
00149 if (get_line() > lastline && !newline_waiting())\
00150 padded("\n");\
00151 }\
00152 }\
00153 pop(mark,"]");\
00154 };
00155
00156 DUMPER(2)
00157 DUMPER(3)
00158 DUMPER(4)
00159 DUMPER(5)
00160 DUMPER(6)
00161 DUMPER(7)
00162 DUMPER(8)
00163 #undef DUMP
00164
00165 void DataTexter::visit(Symbol & symbol)
00166 {
00167 padded(symbol());
00168 };
00169
00170 void DataTexter::visit(Token & token)
00171 {
00172 int mark = push("{");
00173
00174 vector<Symbol> keys = token.keys();
00175 int maximumlength = 0;
00176 for(unsigned int i = 0 ; i < keys.size() ; i ++)
00177 {
00178 int l = keys[i].length();
00179 if (l>maximumlength)
00180 maximumlength = l;
00181 }
00182
00183
00184 bool first = true;
00185 int lastline = 0;
00186 for(unsigned int i = 0 ; i < keys.size() ; i++)
00187 {
00188 if (!first)
00189 {
00190
00191 padded("\n");
00192 if (get_line() > lastline && !newline_waiting())
00193 padded("\n");
00194 }
00195 else first = false;
00196
00197 Symbol symbol = keys[i];
00198 Data value = token[symbol];
00199 lastline = get_line();
00200 {
00201 int mark = push("");
00202 int lastline = get_line();
00203 visit(symbol);
00204 int kl = symbol.length();
00205 while(kl<maximumlength)
00206 {
00207 padded(" ");
00208 kl++;
00209 }
00210 padded(" : ");
00211 if (get_line() > lastline && !newline_waiting())
00212 padded("\n");
00213 DataIo::visit(value);
00214 pop(mark,"");
00215 }
00216 }
00217 pop(mark,"}");
00218 }
00219
00220 void DataTexter::visit(String & str)
00221 {
00222 int linelength = 80;
00223 int wordsplitboundary = 70;
00224
00225 #if QT_VERSION > 0x040000
00226 QByteArray utf8 = str().toUtf8();
00227 const char *text=utf8;
00228 int sl = utf8.length();
00229 #else
00230 QCString utf8 = str().utf8();
00231 const char *text=utf8;
00232 int sl = utf8.length();
00233 #endif
00234
00235 int psp = 1+(sl/wordsplitboundary);
00236 int splitat[psp];
00237 int nextsplitpos = 0;
00238 for(int i = 0 ; i < psp ; i++)
00239 splitat[i]=sl+1;
00240
00241 int curlen = 0;
00242 int lastbreak = 0;
00243 int laststart = 0;
00244 bool lastbreak_canbeset = true;
00245 for(int i = 0 ; i < sl ; i++)
00246 {
00247 char c = text[i];
00248 if (c=='\n')
00249 {
00250 curlen = 0;
00251 laststart = i+1;
00252 lastbreak = i+1;
00253 }
00254 if (c==' ' || c=='\t')
00255 {
00256 if (lastbreak_canbeset)
00257 {
00258 lastbreak = i;
00259 lastbreak_canbeset=false;
00260 }
00261 }
00262 else lastbreak_canbeset=true;
00263 if (curlen>=linelength)
00264 {
00265 if (lastbreak-laststart > wordsplitboundary)
00266 {
00267 splitat[nextsplitpos++]=lastbreak;
00268 laststart = lastbreak;
00269 curlen = i - lastbreak;
00270 }
00271 else
00272 {
00273 splitat[nextsplitpos++]=i++;
00274 laststart = i+1;
00275 lastbreak = i+1;
00276 curlen = 0;
00277 }
00278 }
00279 curlen++;
00280 }
00281
00282 int mark = get_margin();
00283 set_margin(get_pos());
00284 padded("'");
00285 nextsplitpos=0;
00286 for(int i = 0 ; i < sl ; i++)
00287 {
00288 char c = text[i];
00289
00290 if (i==splitat[nextsplitpos])
00291 {
00292 padded("\n|");
00293 nextsplitpos++;
00294 }
00295
00296 if (c=='\n')
00297 {
00298 padded("\n>");
00299 continue;
00300 }
00301
00302 else
00303 {
00304 if (c=='\'')
00305 padded("''");
00306 else
00307 {
00308 char c2[2];
00309 c2[0]=c;
00310 c2[1]=0;
00311 padded(c2);
00312 }
00313 }
00314 }
00315 pop(mark,"\'");
00316 }
00317
00318 DataTexter::DataTexter(QString fn, const char* mode) :
00319 DataIo(fn,mode), newline(true)
00320 {
00321 lines = 0;
00322 written = 0;
00323 margin = 0;
00324 }
00325
00326 DataTexter::DataTexter(FILE * f) : DataIo(f), newline(true)
00327 {
00328 lines = 0;
00329 written = 0;
00330 margin = 0;
00331 }
00332
00333 void DataTexter::padded(QString str)
00334 {
00335 #if QT_VERSION > 0x040000
00336 padded((const char*)str.toAscii());
00337 #else
00338 padded(str.ascii());
00339 #endif
00340 }
00341
00342 void DataTexter::padded(const char *t)
00343 {
00344
00345 int text_length = strlen(t);
00346 while(text_length>0)
00347 {
00348 char a = t[0];
00349 if (a=='\n')
00350 {
00351 newline=true;
00352 written=margin;
00353 fprintf(text,"\n");
00354 lines++;
00355 }
00356 else
00357 {
00358 if (newline)
00359 for(int i = 0; i < margin ; i ++)
00360 fprintf(text," ");
00361 fprintf(text,"%c",a);
00362 written++;
00363 newline=false;
00364 }
00365 t++;
00366 text_length--;
00367 }
00368 }
00369
00370 int DataTexter::push(const char* t)
00371 {
00372 padded(t);
00373 int old_margin = get_margin();
00374 set_margin(get_pos());
00375 return old_margin;
00376 }
00377
00378 int DataTexter::push(QString str)
00379 {
00380 #if QT_VERSION > 0x040000
00381 QByteArray cs = str.toUtf8();
00382 #else
00383 QCString cs = str.utf8();
00384 #endif
00385 return push((const char*)cs);
00386 }
00387
00388 int DataTexter::push(const char t)
00389 {
00390 char c2[2];
00391 c2[0]=t;
00392 c2[1]=0;
00393 return push(c2);
00394 }
00395
00396 void DataTexter::pop(int om, const char t)
00397 {
00398 char c2[2];
00399 c2[0]=t;
00400 c2[1]=0;
00401 pop(om,c2);
00402 }
00403
00404 void DataTexter::pop(int old_margin, const char* t)
00405 {
00406 set_margin(old_margin);
00407 padded(t);
00408 };
00409
00410 void DataTexter::visit(Float4 & f4)
00411 {
00412 char temp[500];
00413 sprintf(temp,"%gf",(float4)f4);
00414 padded(temp);
00415 }
00416
00417 void DataTexter::visit(Float8 & f8)
00418 {
00419 char temp[500];
00420 sprintf(temp,"%gg",(float8)f8);
00421 padded(temp);
00422 }
00423
00424 void DataTexter::visitSignedNumber(signed long long a, char sig)
00425 {
00426 char temp[500];
00427 if (a>=0)
00428 sprintf(temp,"+%lld%c",a,sig);
00429 else
00430 sprintf(temp,"-%lld%c",-a,sig);
00431 padded(temp);
00432 }
00433
00434 void DataTexter::visitUnsignedNumber(unsigned long long a, char sig)
00435 {
00436 char temp[500];
00437 sprintf(temp,"%lld%c",a,sig);
00438 padded(temp);
00439 }
00440
00441 void yyset_in(FILE * i);
00442 extern Data parse_result;
00443 int yyparse();
00444
00445 void DataTexter::read_into(Data& result)
00446 {
00447 if (!text)
00448 {
00449 result = Null();
00450 return;
00451 }
00452 static bool busy_reading = false;
00453 assert(!busy_reading);
00454 busy_reading = true;
00455 yyset_in(text);
00456 parse_result = Data();
00457 yyparse();
00458 busy_reading=false;
00459 result = parse_result;
00460 }
00461
00462 Data DataTexter::read_file(QString filename)
00463 {
00464 DataTexter texter(filename,"rb");
00465 texter.start_reading();
00466 return texter.read();
00467 }
00468
00469 Data DataTexter::read_file(FILE * f)
00470 {
00471 DataTexter texter(f);
00472 texter.start_reading();
00473 return texter.read();
00474 }
00475
00476 void DataTexter::write(Data data, QString filename)
00477 {
00478 DataTexter dt(filename,"wb");
00479 dt.start_writing();
00480 dt.write(data);
00481 dt.padded("\n");
00482 }
00483
00484 void DataTexter::write(Data data, FILE *file)
00485 {
00486 DataTexter dt(file);
00487 dt.start_writing();
00488 dt.write(data);
00489 dt.padded("\n");
00490 }
00491
00492 void DataTexter::write(Data data)
00493 {
00494 DataIo::visit(data);
00495 };
00496
00497 void DataTexter::write_fileformat_versionnr()
00498 {
00499
00500 fprintf(text,"0x%08x\n",version);
00501 }
00502
00503 int DataTexter::read_fileformat_versionnr()
00504 {
00505 int version;
00506
00507 fscanf(text,"0x%08x\n",&version);
00508 return version;
00509 }
00510
00511
00512
00513
00514 DataBinner::DataBinner(QString fn, const char* mode) : DataIo(fn,mode)
00515 {
00516 file_in_use=fn;
00517 mapped_region = NULL;
00518 mapped_size = 0;
00519 };
00520
00521 DataBinner::DataBinner(FILE *f) : DataIo(f)
00522 {
00523 file_in_use="<unknown>";
00524 mapped_region = NULL;
00525 mapped_size = 0;
00526 };
00527
00528 DataBinner::~DataBinner()
00529 {
00530 munmap_textfile();
00531 }
00532
00533 bool DataBinner::write(Data data, QString filename)
00534 {
00535 DataBinner dt(filename,"wb");
00536 dt.start_writing();
00537 dt.write(data);
00538 return dt.munmap_textfile();
00539 }
00540
00541 bool DataBinner::write(Data data, FILE *file)
00542 {
00543 DataBinner dt(file);
00544 dt.start_writing();
00545 dt.write(data);
00546 return dt.munmap_textfile();
00547 }
00548
00549 Data DataBinner::read_file(QString filename)
00550 {
00551 DataBinner binner(filename,"rb");
00552 binner.start_reading();
00553 return binner.read();
00554 }
00555
00556 void DataBinner::write_fileformat_versionnr()
00557 {
00558 assert(sizeof(version)==4);
00559 size_t w = fwrite(&version,4,1,text);
00560 assert(w = 1);
00561 }
00562
00563 int DataBinner::read_fileformat_versionnr()
00564 {
00565 if (cur_ptr+4>mapped_region+file_size)
00566 {
00567 #if QT_VERSION > 0x040000
00568
00569 QByteArray ba=file_in_use.toAscii();
00570 #else
00571 QByteArray ba=file_in_use.ascii();
00572 #endif
00573 printf("Could not read fileformat version from file %s\n",
00574 (const char*)ba);
00575 exit(150);
00576 }
00577 int version=*(int*)cur_ptr;
00578 cur_ptr+=4;
00579 assert(sizeof(version)==4);
00580 return version;
00581 }
00582
00583
00584
00585
00586 void DataBinner::start_reading()
00587 {
00588 mmap_textfile();
00589 DataIo::start_reading();
00590 }
00591
00592 void DataBinner::mmap_textfile()
00593 {
00594 #ifdef LINUX
00595 fseek(text,0,SEEK_END);
00596 file_size = mapped_size = ftell(text);
00597 fseek(text,0,SEEK_SET);
00598 fileno(text);
00599
00600 int pagesize = getpagesize();
00601 mapped_size/=pagesize;
00602 mapped_size++;
00603 mapped_size*=pagesize;
00604 mapped_region = (char*)mmap(NULL, mapped_size,
00605 PROT_READ,
00606 MAP_PRIVATE | MAP_POPULATE,
00607 fileno(text), 0);
00608 cur_ptr = mapped_region;
00609 assert(mapped_region);
00610 assert(mapped_region != MAP_FAILED);
00611 #endif
00612
00613 #ifdef WINDOWS
00614
00618 fseek(text,0,SEEK_END);
00619 file_size = mapped_size = ftell(text);
00620 fseek(text,0,SEEK_SET);
00621 mapped_region = (char*)malloc(mapped_size);
00622 assert(mapped_region);
00623
00624 int pagesize = 4096;
00625 long int remaining = mapped_size;
00626 char* ptr = mapped_region;
00627 while(remaining)
00628 {
00629 int toread = remaining;
00630 if (toread>pagesize) toread=pagesize;
00631 int read = fread(ptr,pagesize,1,text);
00632 assert(read==toread);
00633 remaining-=read;
00634 ptr+=read;
00635 }
00636 cur_ptr = mapped_region;
00637 #endif
00638 }
00639
00640 bool DataBinner::munmap_textfile()
00641 {
00642 #ifdef LINUX
00643 if (mapped_region==NULL || mapped_size==0) return true;
00644 int err=munmap(mapped_region,mapped_size);
00645 if (err!=0)
00646 {
00647 printf("Could not unmap %s - error: %s. Mapped_size=%d\n",
00648 file_in_use.toAscii().data(),
00649 strerror(errno),
00650 mapped_size);
00651 return false;
00652 }
00653 else
00654 {
00655 mapped_region=NULL;
00656 mapped_size=0;
00657 }
00658 #endif
00659
00660 #ifdef WINDOWS
00661 if (mapped_region)
00662 {
00663 free(mapped_region);
00664 mapped_region=NULL;
00665 mapped_size=0;
00666 }
00667 #endif
00668 return true;
00669 }
00670
00671 void DataBinner::write_internal(const char * ptre, int size)
00672 {
00673 size_t w = fwrite(ptre,size,1,text);
00674 assert(w = 1);
00675 };
00676
00677 void DataBinner::read_internal(char * ptre, int size)
00678 {
00679 if (cur_ptr+size>mapped_region+file_size)
00680 {
00681 #if QT_VERSION > 0x040000
00682 QByteArray ba=file_in_use.toAscii();
00683 #else
00684 QByteArray ba=file_in_use.ascii();
00685 #endif
00686 printf("Could not read full data from file %s\n",(const char*)ba);
00687 exit(150);
00688 }
00689 memcpy(ptre,cur_ptr,size);
00690 cur_ptr+=size;
00691 };
00692
00693
00694
00695
00696 template <int D, class T>
00697 void DataBinner::visitArray(Array<D,T> & array)
00698 {
00699 write_typenr(type_array);
00700 write_internal((unsigned1)D);
00701 write_typenr_of(T());
00702 for(int d = 0 ; d < D ; d++)
00703 write_internal((unsigned8)array.size(d));
00704 for(typename Array<D,T>::ordered element(array) ; element.more() ; ++element)
00705 write_internal((T&)element);
00706 };
00707
00708 Data DataBinner::read_array()
00709 {
00710 unsigned1 dimension;
00711 read_internal(dimension);
00712 int T = read_typenr();
00713 const char * type = NULL;
00714 switch(T)
00715 {
00716 case type_data : type = "Data"; break;
00717 case type_signed1 : type = "signed1"; break;
00718 case type_signed2 : type = "signed2"; break;
00719 case type_signed4 : type = "signed4"; break;
00720 case type_signed8 : type = "signed8"; break;
00721 case type_unsigned1 : type = "unsigned1"; break;
00722 case type_unsigned2 : type = "unsigned2"; break;
00723 case type_unsigned4 : type = "unsigned4"; break;
00724 case type_unsigned8 : type = "unsigned8"; break;
00725 case type_float4 : type = "float4"; break;
00726 case type_float8 : type = "float8"; break;
00727 default:
00728 printf("Unknown type %d in DataBinner::read_array\n",T);
00729 assert(0);
00730 }
00731 #define ARRAY_TYPE(D,T) \
00732 if (D==dimension && strcmp(type,#T)==0) return read_array_internal<D,T>();
00733 ARRAY_TYPES
00734 #undef ARRAY_TYPE
00735 assert(0);
00736 }
00737
00738 template <int D, class T>
00739 Data DataBinner::read_array_internal()
00740 {
00741 Size<D> size;
00742 unsigned8 dim_length;
00743 for(int i = 0 ; i < D ; i++)
00744 {
00745 read_internal(dim_length);
00746 size[i] = dim_length;
00747 }
00748
00749 Array<D,T> result(size);
00750 typename Array<D,T>::ordered element(result);
00751
00752 if (element.linear() && typeid(T)!=typeid(Data))
00753 while(element.more())
00754 {
00755
00756 read_internal((char*)element.current(),element.size()*sizeof(T));
00757 element.skipBlock();
00758 }
00759 else
00760 for(typename Array<D,T>::ordered element(result) ; element.more() ;
00761 ++element)
00762 read_internal((T&)element);
00763 return result;
00764 }
00765
00766
00767
00768
00769 void DataBinner::write_internal(Data data)
00770 {
00771 write(data);
00772 };
00773
00774 void DataBinner::write_internal(String data)
00775 {
00776 write(data);
00777 };
00778
00779 void DataBinner::read_internal(Data &data)
00780 {
00781 read_into(data);
00782 }
00783
00784 void DataBinner::read_internal(String &data)
00785 {
00786 data=read_string();
00787 }
00788
00789 void DataBinner::read_into(Data & result)
00790 {
00791
00792 int type_nr = read_typenr();
00793 switch(type_nr)
00794 {
00795 case type_array: result = read_array(); return;
00796 case type_symbol: result =read_symbol();return;
00797 case type_string: result =read_string();return;
00798 case type_token: result =read_token();return;
00799 case type_signed8: result =read_signed8();return;
00800 case type_signed4: result =read_signed4();return;
00801 case type_signed2: result =read_signed2();return;
00802 case type_signed1: result =read_signed1();return;
00803 case type_unsigned8: result =read_unsigned8();return;
00804 case type_unsigned4: result =read_unsigned4();return;
00805 case type_unsigned2: result =read_unsigned2();return;
00806 case type_unsigned1: result =read_unsigned1();return;
00807 case type_float4: result =read_float4();return;
00808 case type_float8: result =read_float8();return;
00809 }
00810 printf("Unknown type number %d\n",type_nr);
00811 assert(0);
00812 }
00813
00814 void DataBinner::write(Data data)
00815 {
00816 DataIo::visit(data);
00817 }
00818
00819
00820
00821
00822 void DataBinner::write_typenr(int type)
00823 {
00824
00825 write_internal((unsigned1)type);
00826 };
00827
00828 int DataBinner::read_typenr()
00829 {
00830 unsigned1 t;
00831 read_internal(t);
00832
00833 return t;
00834 }
00835
00836 void DataBinner::write_typenr_of(Data)
00837 {
00838 write_typenr(type_data);
00839 };
00840
00841 void DataBinner::write_typenr_of(signed1)
00842 {
00843 write_typenr(type_signed1);
00844 };
00845
00846 void DataBinner::write_typenr_of(signed2)
00847 {
00848 write_typenr(type_signed2);
00849 };
00850
00851 void DataBinner::write_typenr_of(signed4)
00852 {
00853 write_typenr(type_signed4);
00854 };
00855
00856 void DataBinner::write_typenr_of(signed8)
00857 {
00858 write_typenr(type_signed8);
00859 };
00860
00861 void DataBinner::write_typenr_of(unsigned1)
00862 {
00863 write_typenr(type_unsigned1);
00864 };
00865
00866 void DataBinner::write_typenr_of(unsigned2)
00867 {
00868 write_typenr(type_unsigned2);
00869 };
00870
00871 void DataBinner::write_typenr_of(unsigned4)
00872 {
00873 write_typenr(type_unsigned4);
00874 };
00875
00876 void DataBinner::write_typenr_of(unsigned8)
00877 {
00878 write_typenr(type_unsigned8);
00879 };
00880
00881 void DataBinner::write_typenr_of(float4)
00882 {
00883 write_typenr(type_float4);
00884 };
00885
00886 void DataBinner::write_typenr_of(float8)
00887 {
00888 write_typenr(type_float8);
00889 };
00890
00891
00892
00893
00894 void DataBinner::write_internal(signed1 s)
00895 {
00896 write_internal((char*)&s,1);
00897 };
00898
00899 void DataBinner::write_internal(signed2 s)
00900 {
00901 write_internal((char*)&s,2);
00902 };
00903
00904 void DataBinner::write_internal(signed4 s)
00905 {
00906 write_internal((char*)&s,4);
00907 };
00908
00909 void DataBinner::write_internal(signed8 s)
00910 {
00911 write_internal((char*)&s,8);
00912 };
00913
00914 void DataBinner::write_internal(float4 s)
00915 {
00916 write_internal((char*)&s,4);
00917 };
00918
00919 void DataBinner::write_internal(float8 s)
00920 {
00921 write_internal((char*)&s,8);
00922 };
00923
00924 void DataBinner::write_internal(unsigned1 u)
00925 {
00926 write_internal((char*)&u,1);
00927 };
00928
00929 void DataBinner::write_internal(unsigned2 u)
00930 {
00931 write_internal((char*)&u,2);
00932 };
00933
00934 void DataBinner::write_internal(unsigned4 u)
00935 {
00936 write_internal((char*)&u,4);
00937 };
00938
00939 void DataBinner::write_internal(unsigned8 u)
00940 {
00941 write_internal((char*)&u,8);
00942 };
00943
00944 #define READ_INTERNAL(TYPE) \
00945 void DataBinner::read_internal(TYPE &s) \
00946 {\
00947 s = *(TYPE*)cur_ptr;\
00948 cur_ptr+=sizeof(TYPE);\
00949 };
00950
00951 READ_INTERNAL(signed1);
00952 READ_INTERNAL(signed2);
00953 READ_INTERNAL(signed4);
00954 READ_INTERNAL(signed8);
00955 READ_INTERNAL(unsigned1);
00956 READ_INTERNAL(unsigned2);
00957 READ_INTERNAL(unsigned4);
00958 READ_INTERNAL(unsigned8);
00959 READ_INTERNAL(float4);
00960 READ_INTERNAL(float8);
00961
00962
00963
00964
00965 void DataBinner::visit(Token & token)
00966 {
00967 write_typenr(type_token);
00968
00969 vector<Symbol> keys = token.keys();
00970 unsigned4 length = keys.size();
00971 write_internal(length);
00972 for(unsigned int i = 0 ; i < length ; i++)
00973 {
00974 Symbol symbol = keys[i];
00975 write_symbol(symbol);
00976 Data value = token[symbol];
00977 write_internal(value);
00978 }
00979 }
00980
00981 Token DataBinner::read_token()
00982 {
00983 Token result;
00984 unsigned4 length;
00985 read_internal(length);
00986 while(length--)
00987 {
00988 Symbol symbol = read_symbol();
00989 read_internal(result[symbol]);
00990 }
00991 return result;
00992 }
00993
00994
00995
00996
00997 void DataBinner::visit(Symbol & symbol)
00998 {
00999 write_typenr(type_symbol);
01000 write_symbol(symbol);
01001 }
01002
01003 void DataBinner::write_symbol(Symbol s)
01004 {
01005 write_internal(s());
01006 }
01007
01008 Symbol DataBinner::read_symbol()
01009 {
01010 return Symbol(read_internal_qstring());
01011 }
01012
01013
01014
01015
01016 void DataBinner::visit(Signed1 &nr)
01017 {
01018 write_typenr_of(nr());
01019 write_internal(nr());
01020 }
01021
01022 void DataBinner::visit(Unsigned1 &nr)
01023 {
01024 write_typenr_of(nr());
01025 write_internal(nr());
01026 }
01027
01028 void DataBinner::visit(Signed2 &nr)
01029 {
01030 write_typenr_of(nr());
01031 write_internal(nr());
01032 }
01033
01034 void DataBinner::visit(Unsigned2 &nr)
01035 {
01036 write_typenr_of(nr());
01037 write_internal(nr());
01038 }
01039
01040 void DataBinner::visit(Signed4 &nr)
01041 {
01042 write_typenr_of(nr());
01043 write_internal(nr());
01044 }
01045
01046 void DataBinner::visit(Unsigned4 &nr)
01047 {
01048 write_typenr_of(nr());
01049 write_internal(nr());
01050 }
01051
01052 void DataBinner::visit(Signed8 &nr)
01053 {
01054 write_typenr_of(nr());
01055 write_internal(nr());
01056 }
01057
01058 void DataBinner::visit(Unsigned8 &nr)
01059 {
01060 write_typenr_of(nr());
01061 write_internal(nr());
01062 }
01063
01064 Signed8 DataBinner::read_signed8()
01065 {
01066 signed8 s;
01067 read_internal(s);
01068 return Signed8(s);
01069 }
01070
01071 Signed4 DataBinner::read_signed4()
01072 {
01073 signed4 s;
01074 read_internal(s);
01075 return Signed4(s);
01076 }
01077
01078 Signed2 DataBinner::read_signed2()
01079 {
01080 signed2 s;
01081 read_internal(s);
01082 return Signed2(s);
01083 }
01084
01085 Signed1 DataBinner::read_signed1()
01086 {
01087 signed1 s;
01088 read_internal(s);
01089 return Signed1(s);
01090 }
01091
01092 Unsigned8 DataBinner::read_unsigned8()
01093 {
01094 unsigned8 s;
01095 read_internal(s);
01096 return Unsigned8(s);
01097 }
01098
01099 Unsigned4 DataBinner::read_unsigned4()
01100 {
01101 unsigned4 s;
01102 read_internal(s);
01103 return Unsigned4(s);
01104 }
01105
01106 Unsigned2 DataBinner::read_unsigned2()
01107 {
01108 unsigned2 s;
01109 read_internal(s);
01110 return Unsigned2(s);
01111 }
01112
01113 Unsigned1 DataBinner::read_unsigned1()
01114 {
01115 unsigned1 s;
01116 read_internal(s);
01117 return Unsigned1(s);
01118 }
01119
01120 void DataBinner::visit(Float4 &nr)
01121 {
01122 write_typenr_of(nr());
01123 write_internal(nr());
01124 }
01125
01126 void DataBinner::visit(Float8 &nr)
01127 {
01128 write_typenr_of(nr());
01129 write_internal(nr());
01130 }
01131
01132 Float4 DataBinner::read_float4()
01133 {
01134 float4 s;
01135 read_internal(s);
01136 return Float4(s);
01137 }
01138
01139 Float8 DataBinner::read_float8()
01140 {
01141 float8 s;
01142 read_internal(s);
01143 return Float8(s);
01144 }
01145
01146
01147
01148
01149
01150 void DataBinner::visit(String & s)
01151 {
01152 write_typenr(type_string);
01153 write_string(s);
01154 }
01155
01156 void DataBinner::write_string(String s)
01157 {
01158 write_internal(s());
01159 }
01160
01161 String DataBinner::read_string()
01162 {
01163 return read_internal_qstring();
01164 }
01165
01166 void DataBinner::write_internal(QString s)
01167 {
01168 #if QT_VERSION > 0x040000
01169 QByteArray cs = s.toUtf8();
01170 #else
01171 QCString cs = s.utf8();
01172 #endif
01173 const char * T = cs;
01174 unsigned4 l = strlen(T);
01175 write_internal(l);
01176 write_internal(T,l+1);
01177 }
01178
01179 QString DataBinner::read_internal_qstring()
01180 {
01181 unsigned4 l;
01182 read_internal(l);
01183 char str[l+1];
01184 read_internal(str,l+1);
01185 return QString::fromUtf8(strdup(str));
01186 }
01187 #endif // __loaded__data_io_cpp__