Changeset 2475 in Sophya for trunk/SophyaLib/BaseTools
- Timestamp:
- Dec 4, 2003, 5:43:27 PM (22 years ago)
- Location:
- trunk/SophyaLib/BaseTools
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/Makefile
r2457 r2475 3 3 all: $(LIB)libBaseTools.a 4 4 clean: 5 rm -f $(OBJ)datatype.o $(OBJ)dvlist.o $(OBJ)fiondblock.o $(OBJ)mutyv.o $(OBJ)ndatablock.o $(OBJ)pexceptions.o $(OBJ)ppersist.o $(OBJ) rawstream.o $(OBJ)sophyainit.o $(OBJ)gnumd5.o $(OBJ)srandgen.o5 rm -f $(OBJ)datatype.o $(OBJ)dvlist.o $(OBJ)fiondblock.o $(OBJ)mutyv.o $(OBJ)ndatablock.o $(OBJ)pexceptions.o $(OBJ)ppersist.o $(OBJ)ppfbinstream.o $(OBJ)rawstream.o $(OBJ)sophyainit.o $(OBJ)gnumd5.o $(OBJ)srandgen.o 6 6 rm -f $(LIB)libBaseTools.a 7 $(LIB)libBaseTools.a : $(OBJ)datatype.o $(OBJ)dvlist.o $(OBJ)fiondblock.o $(OBJ)gnumd5.o $(OBJ)mutyv.o $(OBJ)ndatablock.o $(OBJ)pexceptions.o $(OBJ)ppersist.o $(OBJ) rawstream.o $(OBJ)sophyainit.o $(OBJ)srandgen.o7 $(LIB)libBaseTools.a : $(OBJ)datatype.o $(OBJ)dvlist.o $(OBJ)fiondblock.o $(OBJ)gnumd5.o $(OBJ)mutyv.o $(OBJ)ndatablock.o $(OBJ)pexceptions.o $(OBJ)ppersist.o $(OBJ)ppfbinstream.o $(OBJ)rawstream.o $(OBJ)sophyainit.o $(OBJ)srandgen.o 8 8 $(ARCXX) $(ARCXXFLAGS) $@ $($(ARARGS)) 9 9 ifeq ($(CXX),cxx) … … 14 14 $(INC)machdefs.h 15 15 $(OBJ)dvlist.o: dvlist.cc $(INC)machdefs.h dvlist.h \ 16 objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h mutyv.h \17 $(INC)strutil.h16 objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h rawstream.h \ 17 mutyv.h $(INC)strutil.h 18 18 $(OBJ)fiondblock.o: fiondblock.cc $(INC)machdefs.h \ 19 pexceptions.h datatype.h fiondblock.h ppersist.h gnumd5.h \19 pexceptions.h datatype.h fiondblock.h ppersist.h gnumd5.h rawstream.h \ 20 20 ndatablock.h anydataobj.h 21 21 $(OBJ)mutyv.o: mutyv.cc mutyv.h $(INC)machdefs.h … … 25 25 $(INC)machdefs.h 26 26 $(OBJ)ppersist.o: ppersist.cc $(INC)machdefs.h \ 27 pexceptions.h ppersist.h gnumd5.h anydataobj.h 27 pexceptions.h ppersist.h gnumd5.h rawstream.h anydataobj.h 28 $(OBJ)ppfbinstream.o: ppfbinstream.cc \ 29 $(INC)machdefs.h ppfbinstream.h rawstream.h \ 30 pexceptions.h 28 31 $(OBJ)rawstream.o: rawstream.cc rawstream.h \ 29 32 $(INC)machdefs.h pexceptions.h 30 33 $(OBJ)sophyainit.o: sophyainit.cc $(INC)machdefs.h \ 31 sophyainit.h pexceptions.h ppersist.h gnumd5.h fiondblock.h \ 32 ndatablock.h anydataobj.h dvlist.h objfio.h mutyv.h sversion.h 34 sophyainit.h pexceptions.h ppersist.h gnumd5.h rawstream.h \ 35 fiondblock.h ndatablock.h anydataobj.h dvlist.h objfio.h mutyv.h \ 36 sversion.h 33 37 $(OBJ)gnumd5.o: gnumd5.c $(INC)machdefs.h bithelp.h \ 34 38 gnumd5.h -
trunk/SophyaLib/BaseTools/objlist.list
r2457 r2475 7 7 pexceptions.o 8 8 ppersist.o 9 ppfbinstream.o 9 10 rawstream.o 10 11 sophyainit.o -
trunk/SophyaLib/BaseTools/ppersist.cc
r2459 r2475 6 6 #include "ppersist.h" 7 7 #include "anydataobj.h" 8 #include <fstream>9 8 #include <iostream> 10 9 #include <typeinfo> 11 10 12 13 #ifdef __mac__ 14 #include "unixmac.h" 15 #include <SIOUX.h> 16 #endif 11 // ------------------------- Historique --------------------------- 12 // Le code de ppersist a ete separe en deux en Decembre 2003 13 // a partir de la version CVS (1.26 - ppersist.cc) et 14 // (1.19 - ppersist.h) . 15 // la partie sur l'ecriture des donnees de base et leurs tableaux 16 // a ete mis dans les fichiers ppfbinstream.h .cc 17 // (Classes PPFBinaryInputStream et PPFBinaryOutputStream 18 // ----------------------------------------------------------------- 19 17 20 18 21 // strptime n'est pas defini sous Linux - Reza Mars 2000 … … 64 67 // 65 68 // Register a new persistence handler (PPersist) class. 66 // The classId is usually a hash of the class name, and69 // The classId is usually a hash of the class< name, and 67 70 // ppclass_name is typeid(PPersistClass).name() . 68 71 // This method is called only through the PPersistRegistrar template … … 178 181 179 182 180 static inline void bswap8(void* p)181 {182 uint_8 tmp = *(uint_8*)p;183 *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |184 ((tmp >> (5*8)) & 0x0000FF00) |185 ((tmp >> (3*8)) & 0x00FF0000) |186 ((tmp >> (1*8)) & 0xFF000000) |187 ((tmp & 0xFF000000) << (1*8)) |188 ((tmp & 0x00FF0000) << (3*8)) |189 ((tmp & 0x0000FF00) << (5*8)) |190 ((tmp & 0x000000FF) << (7*8));191 }192 193 static inline void bswap4(void* p)194 {195 uint_4 tmp = *(uint_4*)p;196 *(uint_4*)p = ((tmp >> 24) & 0x000000FF) |197 ((tmp >> 8) & 0x0000FF00) |198 ((tmp & 0x0000FF00) << 8) |199 ((tmp & 0x000000FF) << 24);200 }201 202 static inline void bswap2(void* p)203 {204 uint_2 tmp = *(uint_2*)p;205 *(uint_2*)p = ((tmp >> 8) & 0x00FF) |206 ((tmp & 0x00FF) << 8);207 }208 183 209 184 … … 397 372 //++ 398 373 PInPersist::PInPersist(string const& flnm, bool scan) 374 : PPFBinaryInputStream(flnm, scan) 399 375 // 400 376 // Constructeur. Ouvre le fichier. 401 377 //-- 402 378 { 403 s = new RawInFileStream(flnm.c_str());404 405 // Read and check header406 407 char rbuf[36];408 GetRawBytes(rbuf, 32);409 if (strncmp(rbuf,"SOS-SOPHYA-PPersistFile", 23) != 0) {410 throw FileFormatExc("PInPersist::PInPersist bad header");411 }412 rbuf[32] = '\0';413 version = atoi(rbuf+25);414 if (version < 2) {415 cerr << "PInPersist::PInPersist(" << flnm << ") Version(=" << version416 << ") < 2 not supported !" << endl;417 throw FileFormatExc("PInPersist::PInPersist() - Unsupported (Old) Version");418 }419 // read endianness420 GetRawBytes(rbuf, 32);421 if (strncmp(rbuf,"BIG-ENDIAN",10) == 0)422 bigEndian = true;423 else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0)424 bigEndian = false;425 else {426 throw FileFormatExc("PInPersist::PInPersist bad header - endianness");427 }428 429 // read creation date430 GetRawBytes(rbuf, 32);431 rbuf[32] = '\0';432 struct tm tm;433 /* #if !(defined(__MWERKS__) || defined(OS_MACOSX)) RZ-DEL */434 strptime(rbuf,"%d/%m/%Y %H:%M:%S GMT",&tm);435 /* #else436 sscanf(rbuf,"%2d/%2d/%4d %2d:%2d:%2d GMT",&tm.tm_mday,&tm.tm_mon,&tm.tm_year,437 &tm.tm_hour,&tm.tm_min,&tm.tm_sec);438 439 tm.tm_mon --;440 tm.tm_year -= 1900;441 #endif RZ-DEL */442 443 creationdate = mktime(&tm);444 filename = flnm; // keep the filename445 seqread = true; // To flag non sequential reads446 if (scan && s->isSeekable()) Scan();447 379 } 448 380 … … 454 386 for(i=objList.begin(); i!= objList.end(); i++) 455 387 if ((*i).second) delete (*i).second; 456 delete s; 457 } 458 459 460 string 461 PInPersist::CreationDateStr() 462 { 463 time_t cdt = CreationDate(); 464 string cdate = ctime(&cdt); 465 return(cdate); 466 } 467 468 void 469 PInPersist::Scan() 470 { 471 // On cherche la liste des tags, a la fin du fichier 472 473 unsigned char ppstype; 474 int_8 debut; 475 debut = s->tellg(); 476 477 s->seekg(-(sizeof(int_8)+1), ios::end); 478 479 GetTypeTag(ppstype); 480 if (ppstype != PPS_EOF) 481 throw FileFormatExc("PInPersist::Scan corrupted file, no eof entry at end of file"); 482 483 int_8 pos; 484 GetRawI8(pos); 485 if (pos < 0) { // no tags 486 s->seekg(debut); 487 return; 488 } 489 490 char buffer[MAXTAGLEN+1]; 491 s->seekg(pos); 492 while (true) { 493 GetTypeTag(ppstype); 494 if (ppstype == PPS_EOF) break; 495 496 if (ppstype != PPS_NAMETAG_TABLE) 497 throw FileFormatExc("PInPersist::Scan corrupted file, bad tag entry"); 498 499 GetRawI8(pos); 500 int_4 len; 501 GetRawI4(len); 502 if (len > MAXTAGLEN) 503 throw FileFormatExc("PInPersist::Scan corrupted file, tag name too long"); 504 GetRawBytes(buffer, len); 505 buffer[len] = '\0'; 506 507 tags[buffer] = pos; 508 } 509 s->seekg(debut); 510 } 511 512 513 int 514 PInPersist::NbNameTags() 515 { 516 return tags.size(); 517 } 518 519 bool 520 PInPersist::GotoPositionTag(int_8 pos) 521 { 522 s->seekg(pos); 523 int_8 tpos; 524 GetRawI8(tpos); 525 if (tpos != pos) 526 throw FileFormatExc("PInPersist::GotoPositionTag() - Wrong tag position!"); 527 return true; 528 } 529 530 bool 531 PInPersist::GotoNameTag(string const& name) 532 { 533 map<string, int_8>::iterator i = tags.find(name); 534 if (i == tags.end()) 535 return false; 536 // throw NotFoundExc("PInPersist::GotoNameTag tag not found"); 537 s->seekg((*i).second); 538 seqread = false; 539 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ? 540 return(true); 541 } 542 543 544 bool 545 PInPersist::GotoNameTagNum(int itag) 546 { 547 if (itag<0 || itag >= (int)tags.size()) return false; 548 map<string, int_8>::iterator i = tags.begin(); 549 for (int j=0; j<itag; j++) i++; 550 s->seekg((*i).second); 551 seqread = false; 552 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ? 553 return(true); 554 } 555 556 string 557 PInPersist::GetTagName(int itag) 558 { 559 if (itag<0 || itag >= (int)tags.size()) return ""; 560 map<string, int_8>::iterator i = tags.begin(); 561 for (int j=0; j<itag; j++) i++; 562 return((*i).first); 563 } 388 } 389 390 564 391 565 392 string … … 573 400 // return(GetClassName(cid)); 574 401 return(""); 575 }576 577 static vector<string> * ret_tag_names = NULL;578 vector<string> const &579 PInPersist::GetNameTags()580 {581 if (ret_tag_names) delete ret_tag_names;582 ret_tag_names = new vector<string> ;583 map<string, int_8>::iterator i;584 for(i=tags.begin(); i!=tags.end(); i++) ret_tag_names->push_back((*i).first);585 return(*ret_tag_names);586 }587 588 bool589 PInPersist::SkipToNextObject()590 {591 // A FAIRE NOV 2003 - REZA592 return true;593 }594 595 //++596 // void PInPersist::GetByte(char& c)597 // void PInPersist::GetBytes(void* ptr, size_t bytes)598 // void PInPersist::GetR4 (r_4& result)599 // void PInPersist::GetR4s (r_4* tab, size_t n)600 // void PInPersist::GetR8 (r_8& result)601 // void PInPersist::GetR8s (r_8* tab, size_t n)602 // void PInPersist::GetI2 (int_2& result)603 // void PInPersist::GetI2s (int_2* tab, size_t n)604 // void PInPersist::GetU2 (uint_2& result)605 // void PInPersist::GetU2s (uint_2* tab, size_t n)606 // void PInPersist::GetI4 (int_4& result)607 // void PInPersist::GetI4s (int_4* tab, size_t n)608 // void PInPersist::GetU4 (uint_4& result)609 // void PInPersist::GetU4s (uint_4* tab, size_t n)610 // void PInPersist::GetI8 (int_8& result)611 // void PInPersist::GetI8s (int_8* tab, size_t n)612 // void PInPersist::GetU8 (uint_8& result)613 // void PInPersist::GetU8s (uint_8* tab, size_t n)614 // Lecture de données portables depuis le fichier PPersist. Pour chaque type615 // de données, on peut lire une valeur, ou un tableau de valeurs.616 // void PInPersist::GetLine(char* ptr, size_t len)617 // Lecture d'une ligne de texte depuis le fichier PPersist.618 //--619 620 621 void622 PInPersist::GetTypeTag(unsigned char& c)623 {624 int_8 tpos;625 c = PPS_NAMETAG_MARK;626 while ( (c == PPS_NAMETAG_MARK) || (c == PPS_POSTAG_MARK) ) {627 GetRawUByte(c);628 if (c == PPS_POSTAG_MARK) GetRawI8(tpos);629 }630 // while (c == PPS_NAMETAG_MARK) { Il ne faut plus faire ca !631 // objList.clear(); $CHECK$ Reza 03/2000632 // GetRawByte(c);633 // }634 }635 636 637 void638 PInPersist::GetRawByte(char& c)639 {640 GetRawBytes(&c, 1);641 }642 643 void644 PInPersist::GetRawUByte(unsigned char& c)645 {646 GetRawBytes(&c, 1);647 }648 649 void650 PInPersist::GetRawBytes(void* ptr, size_t bytes)651 {652 s->read((char*)ptr, bytes);653 }654 655 void656 PInPersist::GetRawI2 (int_2& result)657 {658 GetRawBytes(&result, sizeof(int_2));659 if (bigEndian != IS_BIG_ENDIAN)660 bswap2(&result);661 }662 663 void664 PInPersist::GetRawI4 (int_4& result)665 {666 GetRawBytes(&result, sizeof(int_4));667 if (bigEndian != IS_BIG_ENDIAN)668 bswap4(&result);669 }670 671 void672 PInPersist::GetRawI8 (int_8& result)673 {674 GetRawBytes(&result, sizeof(int_8));675 if (bigEndian != IS_BIG_ENDIAN)676 bswap8(&result);677 }678 679 void680 PInPersist::GetRawU8 (uint_8& result)681 {682 GetRawBytes(&result, sizeof(uint_8));683 if (bigEndian != IS_BIG_ENDIAN)684 bswap8(&result);685 }686 687 void688 PInPersist::CheckTag(short datasz, short datatype)689 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED690 {691 unsigned char ppstype;692 GetTypeTag(ppstype);693 if (ppstype != PPS_SIMPLE + datasz + datatype)694 throw FileFormatExc("PInPersist::CheckTag bad type in ppersist file");695 }696 697 void698 PInPersist::CheckArrayTag(short datasz, size_t sz, short datatype)699 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED700 {701 unsigned char ppstype;702 GetTypeTag(ppstype);703 size_t filesz;704 if (sz <= 0x7fffffff) {705 if (ppstype != PPS_SIMPLE_ARRAY4 + datasz + datatype)706 throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file");707 int_4 ff;708 GetRawI4(ff); filesz=ff;709 } else {710 if (ppstype != PPS_SIMPLE_ARRAY8 + datasz + datatype)711 throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file");712 uint_8 ff;713 GetRawU8(ff); filesz=ff;714 }715 if (filesz != sz)716 throw FileFormatExc("PInPersist::CheckArrayTag bad array size in ppersist file");717 }718 719 void720 PInPersist::GetByte(char& c)721 {722 CheckTag(1,PPS_DATATYPE_CHAR);723 GetRawBytes(&c, 1);724 }725 726 727 void728 PInPersist::GetBytes(void* ptr, size_t bytes)729 {730 CheckArrayTag(1, bytes, PPS_DATATYPE_CHAR);731 GetRawBytes(ptr, bytes);732 }733 void734 PInPersist::GetR4 (r_4& result)735 {736 CheckTag(4,PPS_DATATYPE_FLOAT);737 GetRawBytes(&result, sizeof(r_4));738 if (bigEndian != IS_BIG_ENDIAN)739 bswap4(&result);740 }741 742 743 void744 PInPersist::GetR4s (r_4* tab, size_t n)745 {746 CheckArrayTag(4,n,PPS_DATATYPE_FLOAT);747 GetRawBytes(tab, n*sizeof(r_4));748 if (bigEndian == IS_BIG_ENDIAN) return;749 750 for (unsigned int i=0; i<n; i++)751 bswap4(tab+i);752 753 return;754 }755 756 void757 PInPersist::GetR8 (r_8& result)758 {759 CheckTag(8,PPS_DATATYPE_FLOAT);760 GetRawBytes(&result, sizeof(r_8));761 if (bigEndian != IS_BIG_ENDIAN)762 bswap8(&result);763 }764 765 void766 PInPersist::GetR8s (r_8* tab, size_t n)767 {768 CheckArrayTag(8,n,PPS_DATATYPE_FLOAT);769 GetRawBytes(tab, n*sizeof(r_8));770 if (bigEndian == IS_BIG_ENDIAN) return;771 772 for (unsigned int i=0; i<n; i++)773 bswap8(tab+i);774 775 return;776 }777 778 void779 PInPersist::GetI1 (int_1& result)780 {781 CheckTag(1,PPS_DATATYPE_INTEGER);782 GetRawBytes(&result, sizeof(int_1));783 }784 785 void786 PInPersist::GetI1s (int_1* tab, size_t n)787 {788 CheckArrayTag(1,n,PPS_DATATYPE_INTEGER);789 GetRawBytes(tab, n*sizeof(int_1));790 }791 792 void793 PInPersist::GetU1 (uint_1& result)794 {795 CheckTag(1,PPS_DATATYPE_UNSIGNED);796 GetRawBytes(&result, sizeof(uint_1));797 }798 799 void800 PInPersist::GetU1s (uint_1* tab, size_t n)801 {802 CheckArrayTag(1,n,PPS_DATATYPE_UNSIGNED);803 GetRawBytes(tab, n*sizeof(uint_1));804 }805 806 void807 PInPersist::GetI2 (int_2& result)808 {809 CheckTag(2,PPS_DATATYPE_INTEGER);810 GetRawBytes(&result, sizeof(int_2));811 if (bigEndian != IS_BIG_ENDIAN)812 bswap2(&result);813 }814 815 void816 PInPersist::GetI2s (int_2* tab, size_t n)817 {818 CheckArrayTag(2,n,PPS_DATATYPE_INTEGER);819 GetRawBytes(tab, n*sizeof(int_2));820 if (bigEndian == IS_BIG_ENDIAN) return;821 822 for (unsigned int i=0; i<n; i++)823 bswap2(tab+i);824 825 return;826 }827 828 void829 PInPersist::GetU2 (uint_2& result)830 {831 CheckTag(2,PPS_DATATYPE_UNSIGNED);832 GetRawBytes(&result, sizeof(uint_2));833 if (bigEndian != IS_BIG_ENDIAN)834 bswap2(&result);835 }836 837 void838 PInPersist::GetU2s (uint_2* tab, size_t n)839 {840 CheckArrayTag(2,n,PPS_DATATYPE_UNSIGNED);841 GetRawBytes(tab, n*sizeof(uint_2));842 if (bigEndian == IS_BIG_ENDIAN) return;843 844 for (unsigned int i=0; i<n; i++)845 bswap2(tab+i);846 847 return;848 }849 850 void851 PInPersist::GetI4 (int_4& result)852 {853 CheckTag(4,PPS_DATATYPE_INTEGER);854 GetRawBytes(&result, sizeof(int_4));855 if (bigEndian != IS_BIG_ENDIAN)856 bswap4(&result);857 }858 859 void860 PInPersist::GetI4s (int_4* tab, size_t n)861 {862 CheckArrayTag(4,n,PPS_DATATYPE_INTEGER);863 GetRawBytes(tab, n*sizeof(int_4));864 if (bigEndian == IS_BIG_ENDIAN) return;865 866 for (unsigned int i=0; i<n; i++)867 bswap4(tab+i);868 869 return;870 }871 872 void873 PInPersist::GetU4 (uint_4& result)874 {875 CheckTag(4,PPS_DATATYPE_UNSIGNED);876 GetRawBytes(&result, sizeof(uint_4));877 if (bigEndian != IS_BIG_ENDIAN)878 bswap4(&result);879 }880 881 void882 PInPersist::GetU4s (uint_4* tab, size_t n)883 {884 CheckArrayTag(4,n,PPS_DATATYPE_UNSIGNED);885 GetRawBytes(tab, n*sizeof(uint_4));886 if (bigEndian == IS_BIG_ENDIAN) return;887 888 for (unsigned int i=0; i<n; i++)889 bswap4(tab+i);890 891 return;892 }893 894 895 void896 PInPersist::GetI8 (int_8& result)897 {898 CheckTag(8,PPS_DATATYPE_INTEGER);899 GetRawBytes(&result, sizeof(int_8));900 if (bigEndian != IS_BIG_ENDIAN)901 bswap8(&result);902 }903 904 void905 PInPersist::GetI8s (int_8* tab, size_t n)906 {907 CheckArrayTag(8,n,PPS_DATATYPE_INTEGER);908 GetRawBytes(tab, n*sizeof(int_8));909 if (bigEndian == IS_BIG_ENDIAN) return;910 911 for (unsigned int i=0; i<n; i++)912 bswap8(tab+i);913 914 return;915 }916 917 void918 PInPersist::GetU8 (uint_8& result)919 {920 CheckTag(8,PPS_DATATYPE_UNSIGNED);921 GetRawBytes(&result, sizeof(uint_8));922 if (bigEndian != IS_BIG_ENDIAN)923 bswap8(&result);924 }925 926 void927 PInPersist::GetU8s (uint_8* tab, size_t n)928 {929 CheckArrayTag(8,n,PPS_DATATYPE_UNSIGNED);930 GetRawBytes(tab, n*sizeof(uint_8));931 if (bigEndian == IS_BIG_ENDIAN) return;932 933 for (unsigned int i=0; i<n; i++)934 bswap8(tab+i);935 936 return;937 }938 939 940 void941 PInPersist::GetLine(char* ptr, size_t len)942 {943 string str;944 GetStr(str);945 strncpy(ptr, str.c_str(), len);946 ptr[len] = '\0';947 }948 949 void950 PInPersist::GetStr(string& str)951 {952 unsigned char ppstype;953 GetTypeTag(ppstype);954 if (ppstype != PPS_STRING)955 throw FileFormatExc("PInPersist::GetStr bad type in ppersist file");956 int_4 len;957 GetRawI4(len);958 char * buff = new char[len+1];959 GetRawBytes(buff, len);960 buff[len] = '\0';961 str = buff;962 delete[] buff;963 }964 965 void966 PInPersist::GetZ4 (complex<r_4>& result)967 {968 CheckTag(4,PPS_DATATYPE_COMPLEX);969 r_4 reim[2];970 GetRawBytes(reim, 2*sizeof(r_4));971 if (bigEndian != IS_BIG_ENDIAN) {972 bswap4(reim);973 bswap4(reim+1);974 }975 result = complex<r_4>(reim[0], reim[1]);976 }977 978 void979 PInPersist::GetZ4s (complex<r_4>* tab, size_t n)980 {981 CheckArrayTag(4,n,PPS_DATATYPE_COMPLEX);982 GetRawBytes(tab, n*2*sizeof(r_4));983 if (bigEndian == IS_BIG_ENDIAN) return;984 985 r_4 * p = (r_4 *)tab;986 for (unsigned int i=0; i<n; i++) {987 bswap4(p); p++;988 bswap4(p); p++;989 }990 return;991 }992 993 void994 PInPersist::GetZ8 (complex<r_8>& result)995 {996 CheckTag(8,PPS_DATATYPE_COMPLEX);997 r_8 reim[2];998 GetRawBytes(reim, 2*sizeof(r_8));999 if (bigEndian != IS_BIG_ENDIAN) {1000 bswap8(reim);1001 bswap8(reim+1);1002 }1003 result = complex<r_8>(reim[0], reim[1]);1004 }1005 1006 void1007 PInPersist::GetZ8s (complex<r_8>* tab, size_t n)1008 {1009 CheckArrayTag(8,n,PPS_DATATYPE_COMPLEX);1010 GetRawBytes(tab, n*2*sizeof(r_8));1011 if (bigEndian == IS_BIG_ENDIAN) return;1012 1013 r_8 * p = (r_8 *)tab;1014 for (unsigned int i=0; i<n; i++) {1015 bswap8(p); p++;1016 bswap8(p); p++;1017 }1018 return;1019 }1020 1021 1022 void1023 PInPersist::GetPosTagTable(int_8* ptab, size_t sz)1024 {1025 unsigned char ppstype;1026 GetTypeTag(ppstype);1027 if (ppstype != PPS_POSTAG_TABLE)1028 throw FileFormatExc("PInPersist::GetPosTagTable bad type in ppersist stream");1029 int_4 tsz;1030 GetRawI4(tsz);1031 if (tsz != sz)1032 throw FileFormatExc("PInPersist::GetPosTagTable Size mismatch ");1033 for(int kk=0; kk<tsz; kk++)1034 GetRawI8(ptab[kk]);1035 return;1036 }1037 1038 void1039 PInPersist::GetPosTagTable(vector<int_8>& ptab)1040 {1041 unsigned char ppstype;1042 GetTypeTag(ppstype);1043 if (ppstype != PPS_POSTAG_TABLE)1044 throw FileFormatExc("PInPersist::GetPosTagTable bad type in ppersist stream");1045 ptab.clear();1046 int_4 tsz;1047 GetRawI4(tsz);1048 int_8 tpos;1049 for(int kk=0; kk<tsz; kk++) {1050 GetRawI8(tpos);1051 ptab.push_back(tpos);1052 }1053 return;1054 402 } 1055 403 … … 1125 473 1126 474 1127 void1128 PInPersist::AnalyseTags(int lev)1129 {1130 unsigned char ppstag=0;1131 unsigned char ppst1,ppst2,ppst3,ppst30;1132 int_8 cpos,fsize;1133 uint_8 ui8,cid,oid;1134 int_4 i4;1135 int_8 i8;1136 char * buff;1137 string str;1138 1139 cout << "\n ---------------------------------------------------------- " << endl;1140 cout << " PInPersist::AnalyseTags(Level= " << lev << ")" << endl;1141 1142 1143 cpos = s->tellg();1144 s->seekg(0,ios::end);1145 fsize = s->tellg();1146 s->seekg(cpos,ios::beg);1147 1148 cout << " Version= " << Version() << " FileSize= " << fsize1149 << " Creation Date= " << CreationDateStr() << endl;1150 1151 uint_8 totntags = 0;1152 bool eofok = false;1153 1154 while ( (ppstag != PPS_EOF) && (cpos < fsize) ) {1155 cpos = s->tellg();1156 GetRawUByte(ppstag);1157 totntags++;1158 1159 ppst1 = ppstag&0x0f; // bits 01231160 ppst2 = ppstag&0x30; // bits 451161 ppst3 = ppstag&0xc0; // bits 671162 ppst30 = ppstag&0xc1; // bits 0 671163 if ((ppst2 == 0) && (ppst3 == 0) ) {1164 switch (ppst1) {1165 1166 case PPS_NULL :1167 if (lev > 1) cout << "<PPS_NULL> tag at position " << hex << cpos << dec << endl;1168 break;1169 1170 case PPS_STRING :1171 GetRawI4(i4);1172 if (lev > 1) cout << "<PPS_STRING> tag at position " << hex << cpos << dec1173 << " Length=" << i4 << endl;1174 s->seekg(i4,ios::cur);1175 break;1176 1177 case PPS_OBJECT :1178 GetRawU8(cid);1179 GetRawU8(oid);1180 cout << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid1181 << " ObjectId= " << oid << dec << endl;1182 break;1183 1184 case PPS_REFERENCE :1185 GetRawU8(oid);1186 GetRawI8(i8);1187 cout << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= "1188 << oid << " OrigPos=" << i8 << dec << endl;1189 break;1190 1191 case PPS_NAMETAG_MARK :1192 cout << "<PPS_NAMETAG_MARK> tag at position " << hex << cpos << dec << endl;1193 break;1194 1195 case PPS_POSTAG_MARK :1196 GetRawI8(i8);1197 cout << "<PPS_POSTAG_MARK> tag at position " << hex << cpos1198 << " TPos=" << i8 << dec << endl;1199 break;1200 1201 case PPS_ENDOBJECT :1202 GetRawU8(oid);1203 cout << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= "1204 << oid << dec << endl;1205 break;1206 1207 case PPS_POSTAG_TABLE :1208 GetRawI4(i4);1209 for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);1210 cout << "<PPS_POSTAG_TABLE> tag at position " << hex << cpos << dec << endl;1211 break;1212 1213 case PPS_NAMETAG_TABLE :1214 GetRawI8(i8);1215 GetRawI4(i4);1216 buff = new char[i4+1];1217 GetRawBytes(buff, i4);1218 buff[i4] = '\0'; str = buff;1219 delete[] buff;1220 cout << "<PPS_TAG> tag at position " << hex << cpos << dec1221 << " Name= " << str << endl;1222 break;1223 1224 case PPS_EOF :1225 GetRawI8(i8);1226 cout << "<PPS_EOF> tag at position " << hex << cpos1227 << " TagPos=" << i8 << dec << endl;1228 eofok = true;1229 break;1230 1231 default :1232 cerr << " ERROR : Unexpected tag value " << hex << ppstag1233 << " At position" << cpos << dec << endl;1234 throw FileFormatExc("PInPersist::AnalyseTags() - Unexpected tag value !");1235 }1236 }1237 else {1238 string dtype = "???? x";1239 if (ppst30 == PPS_DATATYPE_COMPLEX) dtype = "COMPLEX x";1240 else if (ppst3 == PPS_DATATYPE_CHAR) dtype = "CHAR x";1241 else if (ppst3 == PPS_DATATYPE_FLOAT) dtype = "FLOAT x";1242 else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x";1243 else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x";1244 int_4 dsize = ppst1;1245 int_4 dsizeskip = dsize;1246 if (ppst30 == PPS_DATATYPE_COMPLEX) {1247 dsize--;1248 dsizeskip = 2*dsize;1249 }1250 char sb[16];1251 sprintf(sb, "%d", dsize);1252 dtype += sb;1253 1254 switch (ppst2) {1255 1256 case PPS_SIMPLE :1257 if (lev > 2) cout << "<PPS_SIMPLE> tag at position " << hex << cpos << dec1258 << " DataType=" << dtype << endl;1259 s->seekg(dsizeskip, ios::cur);1260 break;1261 1262 case PPS_SIMPLE_ARRAY4 :1263 GetRawI4(i4);1264 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY4> tag at position " << hex << cpos << dec1265 << " DataType=" << dtype << " NElts= " << i4 << endl;1266 s->seekg((int_8)dsizeskip*(int_8)i4, ios::cur);1267 break;1268 1269 case PPS_SIMPLE_ARRAY8 :1270 GetRawU8(ui8);1271 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY8> tag at position " << hex << cpos << dec1272 << " DataType=" << dtype << " NElts= " << ui8 << endl;1273 s->seekg((int_8)dsizeskip*ui8, ios::cur);1274 break;1275 }1276 }1277 }1278 if (!eofok)1279 throw FileFormatExc("PInPersist::AnalyseTags() - Not found <PPS_EOF> tag ");1280 1281 cout << " PInPersist::AnalyseTags() - End - Total Number of Tags= " << totntags << endl;1282 cout << " ---------------------------------------------------------- \n" << endl;1283 return;1284 }1285 475 1286 476 void … … 1359 549 //-- 1360 550 POutPersist::POutPersist(string const& flnm, int endianness) 1361 { 1362 if (endianness == -1) 1363 bigEndian = IS_BIG_ENDIAN; 1364 else 1365 bigEndian = endianness; 1366 551 : PPFBinaryOutputStream(flnm, endianness) 552 { 1367 553 // PPS (POutPersist stream) Object Id initialisation 1368 554 pps_OId = 0; 1369 // Output stream creation1370 s = new RawOutFileStream(flnm.c_str());1371 version = 3;1372 // Header1373 PutRawBytes("SOS-SOPHYA-PPersistFile V3 ",32);1374 PutRawBytes(bigEndian1375 ? "BIG-ENDIAN "1376 : "LITTLE-ENDIAN ",32);1377 1378 // ---- GMT creation date of the file1379 time_t tm = time(NULL);1380 char datestring[33];1381 int l=strftime(datestring,32,"%d/%m/%Y %H:%M:%S GMT",gmtime(&tm));1382 for(int i=l; i<32; i++) datestring[i] = ' ';1383 datestring[32] = '\0';1384 PutRawBytes(datestring, 32);1385 filename = flnm;1386 555 } 1387 556 1388 557 POutPersist::~POutPersist() 1389 558 { 1390 if (tags.size() == 0) { 1391 PutRawUByte(PPS_EOF); 1392 PutRawI8(-1); 1393 } else { 1394 int_8 tagPos; 1395 tagPos = s->tellp(); 1396 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) { 1397 string name = (*i).first; 1398 int_8 pos = (*i).second; 1399 PutRawUByte(PPS_NAMETAG_TABLE); // This is a tag 1400 PutRawI8(pos); // position of previous tag 1401 PutRawI4(name.length()); // length of the name 1402 PutRawBytes(name.c_str(), name.length()); // name, without final "0". 1403 } 1404 PutRawUByte(PPS_EOF); 1405 PutRawI8(tagPos); 1406 } 1407 1408 delete s; // Close the stream 1409 } 1410 1411 int_8 1412 POutPersist::WritePositionTag() 1413 { 1414 int_8 tagpos; 1415 tagpos = s->tellp(); 1416 PutRawByte(PPS_POSTAG_MARK); 1417 PutI8(tagpos); 1418 return(tagpos); 1419 } 1420 1421 void 1422 POutPersist::WriteNameTag(string const& name) 1423 { 1424 if (name.length() > MAXTAGLEN) 1425 throw ParmError("POutPersist::WriteNameTag tag name too long"); 1426 1427 if (tags.find(name) != tags.end()) 1428 throw DuplicateIdExc("POutPersist::WriteNameTag duplicate tag name"); 1429 1430 // Get current file position 1431 int_8 tagPos; 1432 tagPos = s->tellp(); 1433 1434 tags[name] = tagPos; 1435 PutRawUByte(PPS_NAMETAG_MARK); // This is a tag 1436 // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000 1437 } 1438 1439 //++ 1440 // void POutPersist::PutByte(char& c) 1441 // void POutPersist::PutBytes(void const* ptr, size_t bytes) 1442 // void POutPersist::PutR4 (r_4 result) 1443 // void POutPersist::PutR4s (r_4 const* tab, size_t n) 1444 // void POutPersist::PutR8 (r_8 result) 1445 // void POutPersist::PutR8s (r_8 const* tab, size_t n) 1446 // void POutPersist::PutI2 (int_2 result) 1447 // void POutPersist::PutI2s (int_2 const* tab, size_t n) 1448 // void POutPersist::PutU2 (uint_2 result) 1449 // void POutPersist::PutU2s (uint_2 const* tab, size_t n) 1450 // void POutPersist::PutI4 (int_4 result) 1451 // void POutPersist::PutI4s (int_4 const* tab, size_t n) 1452 // void POutPersist::PutU4 (uint_4 result) 1453 // void POutPersist::PutU4s (uint_4 const* tab, size_t n) 1454 // void POutPersist::PutI8 (int_8 result) 1455 // void POutPersist::PutI8s (int_8 const* tab, size_t n) 1456 // void POutPersist::PutU8 (uint_8 result) 1457 // void POutPersist::PutU8s (uint_8 const* tab, size_t n) 1458 // void POutPersist::PutStr (string const&) 1459 // Ecriture de données portables.. Pour chaque type 1460 // de données, on peut écrire une valeur, ou un tableau de valeurs. 1461 // void POutPersist::PutLine(char const* ptr, size_t len) 1462 // Ecriture d'une ligne de texte dans le fichier PPersist. 1463 //-- 1464 1465 1466 1467 1468 void 1469 POutPersist::PutRawBytes(void const* ptr, size_t bytes) 1470 { 1471 s->write((char const*)ptr, bytes); 1472 } 1473 1474 void 1475 POutPersist::PutRawByte(char c) 1476 { 1477 PutRawBytes(&c, 1); 1478 } 1479 1480 void 1481 POutPersist::PutRawUByte(unsigned char c) 1482 { 1483 PutRawBytes(&c, 1); 1484 } 1485 1486 void 1487 POutPersist::PutRawI2 (int_2 val) 1488 { 1489 if (bigEndian != IS_BIG_ENDIAN) 1490 bswap2(&val); 1491 1492 PutRawBytes(&val, sizeof(int_2)); 1493 } 1494 1495 void 1496 POutPersist::PutRawI4 (int_4 val) 1497 { 1498 if (bigEndian != IS_BIG_ENDIAN) 1499 bswap4(&val); 1500 1501 PutRawBytes(&val, sizeof(int_4)); 1502 } 1503 1504 void 1505 POutPersist::PutRawI8 (int_8 val) 1506 { 1507 if (bigEndian != IS_BIG_ENDIAN) 1508 bswap8(&val); 1509 1510 PutRawBytes(&val, sizeof(int_8)); 1511 } 1512 1513 void 1514 POutPersist::PutRawU8 (uint_8 val) 1515 { 1516 if (bigEndian != IS_BIG_ENDIAN) 1517 bswap8(&val); 1518 1519 PutRawBytes(&val, sizeof(uint_8)); 1520 } 1521 1522 void 1523 POutPersist::PutArrayTag(short datasz, size_t sz, short datatype) 1524 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED 1525 { 1526 if (sz <= 0x7fffffff) { 1527 PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype); 1528 PutRawI4(sz); 1529 } else { 1530 PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype); 1531 PutRawU8(sz); 1532 } 1533 } 1534 1535 void 1536 POutPersist::PutByte(char c) 1537 { 1538 PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR); 1539 PutRawBytes(&c, 1); 1540 } 1541 1542 1543 1544 void 1545 POutPersist::PutBytes(void const* ptr, size_t bytes) 1546 { 1547 PutArrayTag(1, bytes, PPS_DATATYPE_CHAR); 1548 PutRawBytes(ptr, bytes); 1549 } 1550 1551 void 1552 POutPersist::PutR4 (r_4 val) 1553 { 1554 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT); 1555 1556 if (bigEndian != IS_BIG_ENDIAN) 1557 bswap4(&val); 1558 1559 PutRawBytes(&val, sizeof(r_4)); 1560 } 1561 1562 void 1563 POutPersist::PutR4s (r_4 const* tab, size_t n) 1564 { 1565 PutArrayTag(4, n, PPS_DATATYPE_FLOAT); 1566 1567 if (bigEndian == IS_BIG_ENDIAN) { 1568 PutRawBytes(tab, n*sizeof(r_4)); 1569 } else { 1570 for (unsigned int i=0; i<n; i++) { 1571 r_4 val = tab[i]; 1572 bswap4(&val); 1573 PutRawBytes(&val, sizeof(r_4)); 1574 } 1575 } 1576 } 1577 1578 void 1579 POutPersist::PutR8 (r_8 val) 1580 { 1581 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT); 1582 1583 if (bigEndian != IS_BIG_ENDIAN) 1584 bswap8(&val); 1585 1586 PutRawBytes(&val, sizeof(r_8)); 1587 } 1588 1589 void 1590 POutPersist::PutR8s (r_8 const* tab, size_t n) 1591 { 1592 PutArrayTag(8, n, PPS_DATATYPE_FLOAT); 1593 1594 if (bigEndian == IS_BIG_ENDIAN) { 1595 PutRawBytes(tab, n*sizeof(r_8)); 1596 } else { 1597 for (unsigned int i=0; i<n; i++) { 1598 r_8 val = tab[i]; 1599 bswap8(&val); 1600 PutRawBytes(&val, sizeof(r_8)); 1601 } 1602 } 1603 } 1604 1605 void 1606 POutPersist::PutI1 (int_1 val) 1607 { 1608 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_INTEGER); 1609 PutRawBytes(&val, sizeof(int_1)); 1610 } 1611 1612 void 1613 POutPersist::PutI1s (int_1 const* tab, size_t n) 1614 { 1615 PutArrayTag(1, n, PPS_DATATYPE_INTEGER); 1616 PutRawBytes(tab, n*sizeof(int_1)); 1617 } 1618 1619 void 1620 POutPersist::PutU1 (uint_1 val) 1621 { 1622 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_UNSIGNED); 1623 PutRawBytes(&val, sizeof(uint_1)); 1624 } 1625 1626 void 1627 POutPersist::PutU1s (uint_1 const* tab, size_t n) 1628 { 1629 PutArrayTag(1, n, PPS_DATATYPE_UNSIGNED); 1630 PutRawBytes(tab, n*sizeof(uint_1)); 1631 } 1632 1633 void 1634 POutPersist::PutI2 (int_2 val) 1635 { 1636 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER); 1637 1638 if (bigEndian != IS_BIG_ENDIAN) 1639 bswap2(&val); 1640 1641 PutRawBytes(&val, sizeof(int_2)); 1642 } 1643 1644 void 1645 POutPersist::PutI2s (int_2 const* tab, size_t n) 1646 { 1647 PutArrayTag(2, n, PPS_DATATYPE_INTEGER); 1648 1649 if (bigEndian == IS_BIG_ENDIAN) { 1650 PutRawBytes(tab, n*sizeof(int_2)); 1651 } else { 1652 for (unsigned int i=0; i<n; i++) { 1653 int_2 val = tab[i]; 1654 bswap2(&val); 1655 PutRawBytes(&val, sizeof(int_2)); 1656 } 1657 } 1658 } 1659 1660 void 1661 POutPersist::PutU2 (uint_2 val) 1662 { 1663 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED); 1664 1665 if (bigEndian != IS_BIG_ENDIAN) 1666 bswap2(&val); 1667 1668 PutRawBytes(&val, sizeof(uint_2)); 1669 } 1670 1671 void 1672 POutPersist::PutU2s (uint_2 const* tab, size_t n) 1673 { 1674 PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED); 1675 1676 if (bigEndian == IS_BIG_ENDIAN) { 1677 PutRawBytes(tab, n*sizeof(uint_2)); 1678 } else { 1679 for (unsigned int i=0; i<n; i++) { 1680 uint_2 val = tab[i]; 1681 bswap2(&val); 1682 PutRawBytes(&val, sizeof(uint_2)); 1683 } 1684 } 1685 } 1686 1687 void 1688 POutPersist::PutI4 (int_4 val) 1689 { 1690 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER); 1691 1692 if (bigEndian != IS_BIG_ENDIAN) 1693 bswap4(&val); 1694 1695 PutRawBytes(&val, sizeof(int_4)); 1696 } 1697 1698 void 1699 POutPersist::PutI4s (int_4 const* tab, size_t n) 1700 { 1701 PutArrayTag(4, n, PPS_DATATYPE_INTEGER); 1702 1703 if (bigEndian == IS_BIG_ENDIAN) { 1704 PutRawBytes(tab, n*sizeof(int_4)); 1705 } else { 1706 for (unsigned int i=0; i<n; i++) { 1707 int_4 val = tab[i]; 1708 bswap4(&val); 1709 PutRawBytes(&val, sizeof(int_4)); 1710 } 1711 } 1712 } 1713 1714 void 1715 POutPersist::PutU4 (uint_4 val) 1716 { 1717 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED); 1718 1719 if (bigEndian != IS_BIG_ENDIAN) 1720 bswap4(&val); 1721 1722 PutRawBytes(&val, sizeof(uint_4)); 1723 } 1724 1725 void 1726 POutPersist::PutU4s (uint_4 const* tab, size_t n) 1727 { 1728 PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED); 1729 1730 if (bigEndian == IS_BIG_ENDIAN) { 1731 PutRawBytes(tab, n*sizeof(uint_4)); 1732 } else { 1733 for (unsigned int i=0; i<n; i++) { 1734 uint_4 val = tab[i]; 1735 bswap4(&val); 1736 PutRawBytes(&val, sizeof(uint_4)); 1737 } 1738 } 1739 } 1740 1741 void 1742 POutPersist::PutI8 (int_8 val) 1743 { 1744 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER); 1745 1746 if (bigEndian != IS_BIG_ENDIAN) 1747 bswap8(&val); 1748 1749 PutRawBytes(&val, sizeof(int_8)); 1750 } 1751 1752 void 1753 POutPersist::PutI8s (int_8 const* tab, size_t n) 1754 { 1755 PutArrayTag(8, n, PPS_DATATYPE_INTEGER); 1756 1757 if (bigEndian == IS_BIG_ENDIAN) { 1758 PutRawBytes(tab, n*sizeof(int_8)); 1759 } else { 1760 for (unsigned int i=0; i<n; i++) { 1761 int_8 val = tab[i]; 1762 bswap8(&val); 1763 PutRawBytes(&val, sizeof(int_8)); 1764 } 1765 } 1766 } 1767 1768 void 1769 POutPersist::PutU8 (uint_8 val) 1770 { 1771 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED); 1772 1773 if (bigEndian != IS_BIG_ENDIAN) 1774 bswap8(&val); 1775 1776 PutRawBytes(&val, sizeof(uint_8)); 1777 } 1778 1779 void 1780 POutPersist::PutU8s (uint_8 const* tab, size_t n) 1781 { 1782 PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED); 1783 1784 if (bigEndian == IS_BIG_ENDIAN) { 1785 PutRawBytes(tab, n*sizeof(uint_8)); 1786 } else { 1787 for (unsigned int i=0; i<n; i++) { 1788 uint_8 val = tab[i]; 1789 bswap8(&val); 1790 PutRawBytes(&val, sizeof(uint_8)); 1791 } 1792 } 1793 } 1794 1795 void 1796 POutPersist::PutStr(string const& str) 1797 { 1798 PutRawUByte(PPS_STRING); 1799 PutRawI4(str.length()); 1800 PutRawBytes(str.c_str(), str.length()); 1801 } 1802 1803 void 1804 POutPersist::PutLine(char const* ptr, size_t len) 1805 { 1806 string str = ptr; 1807 PutStr(str); 1808 } 1809 1810 1811 void 1812 POutPersist::PutZ4 (complex<r_4> val) 1813 { 1814 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_COMPLEX); 1815 r_4 reim[2]; 1816 reim[0] = val.real(); 1817 reim[1] = val.imag(); 1818 if (bigEndian != IS_BIG_ENDIAN) { 1819 bswap4(reim); 1820 bswap4(reim+1); 1821 } 1822 PutRawBytes(reim, 2*sizeof(r_4)); 1823 } 1824 1825 void 1826 POutPersist::PutZ4s (complex<r_4> const* tab, size_t n) 1827 { 1828 PutArrayTag(4, n, PPS_DATATYPE_COMPLEX); 1829 1830 if (bigEndian == IS_BIG_ENDIAN) { 1831 PutRawBytes(tab, n*2*sizeof(r_4)); 1832 } else { 1833 for (unsigned int i=0; i<n; i++) { 1834 r_4 reim[2]; 1835 reim[0] = tab[i].real(); 1836 reim[1] = tab[i].imag(); 1837 bswap4(reim); 1838 bswap4(reim+1); 1839 PutRawBytes(reim, 2*sizeof(r_4)); 1840 } 1841 } 1842 } 1843 1844 void 1845 POutPersist::PutZ8 (complex<r_8> val) 1846 { 1847 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_COMPLEX); 1848 r_8 reim[2]; 1849 reim[0] = val.real(); 1850 reim[1] = val.imag(); 1851 if (bigEndian != IS_BIG_ENDIAN) { 1852 bswap8(reim); 1853 bswap8(reim+1); 1854 } 1855 PutRawBytes(reim, 2*sizeof(r_8)); 1856 } 1857 1858 void 1859 POutPersist::PutZ8s (complex<r_8> const* tab, size_t n) 1860 { 1861 PutArrayTag(8, n, PPS_DATATYPE_COMPLEX); 1862 1863 if (bigEndian == IS_BIG_ENDIAN) { 1864 PutRawBytes(tab, n*2*sizeof(r_8)); 1865 } else { 1866 for (unsigned int i=0; i<n; i++) { 1867 r_8 reim[2]; 1868 reim[0] = tab[i].real(); 1869 reim[1] = tab[i].imag(); 1870 bswap8(reim); 1871 bswap8(reim+1); 1872 PutRawBytes(reim, 2*sizeof(r_8)); 1873 } 1874 } 1875 } 1876 1877 void 1878 POutPersist::PutPosTagTable(int_8 const * ptab, size_t sz) 1879 { 1880 PutRawUByte(PPS_POSTAG_TABLE); 1881 int_4 tsz = sz; 1882 PutRawI4(tsz); 1883 for(int kk=0; kk<tsz; kk++) 1884 PutRawI8(ptab[kk]); 1885 return; 1886 } 1887 1888 void 1889 POutPersist::PutPosTagTable(vector<int_8> const& ptab) 1890 { 1891 PutRawUByte(PPS_POSTAG_TABLE); 1892 int_4 tsz = ptab.size(); 1893 PutRawI4(tsz); 1894 for(int kk=0; kk<tsz; kk++) 1895 PutRawI8(ptab[kk]); 1896 return; 1897 } 559 } 560 1898 561 1899 562 void -
trunk/SophyaLib/BaseTools/ppersist.h
r2459 r2475 9 9 // R. Ansari LAL IN2P3/CNRS 03/2000 10 10 11 // -------------------- Historique ----------------------- 12 // Separe en deux (CVS version 1.19 - ppersist.h) 13 // Classes PPFBinaryIn/OutputStream ds ppfbinstream.h 14 // ------------------------------------------------------- 15 11 16 12 17 #include "machdefs.h" 13 18 #include "pexceptions.h" 14 19 #include "gnumd5.h" 15 #include "rawstream.h" 16 17 18 #include <time.h> 19 20 #include <complex> 20 #include "ppfbinstream.h" 21 21 22 #include <string> 22 23 #include <map> … … 67 68 68 69 69 //! Ancestor for PInPersist and POutPersist PPFstreams.70 //! Ancestor for PInPersist and POutPersist object serialisation streams. 70 71 // Handles (statically) the registration of classes. 71 72 72 73 class PIOPersist { 73 74 public: 74 enum {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1};75 75 typedef PPersist* (*ClassCreatorFunc)(); 76 76 … … 92 92 static void Initialize(); // Pour initialiser classList 93 93 94 int Version() {return version;} // PIn/OutPersist version number95 string FileName() { return filename; } // Retourne le nom de fichier96 94 97 95 private: … … 103 101 static map<string, uint_8> * dobjclassNameList; // PPersist classId = f(DataObjClassName) 104 102 105 protected:106 107 enum {PPS_NULL = 0, // this is a null object108 PPS_STRING = 1, // string, length (4b) + data109 PPS_OBJECT = 2, // classId, data...110 PPS_REFERENCE = 3, // objectId111 PPS_NAMETAG_TABLE = 4, // Name tag table (Written at the end of file/stream)112 PPS_EOF = 5, // Just before tag infomation, offset to PPS_TAG113 PPS_ENDOBJECT = 6, // marks the end of a given object information114 PPS_NAMETAG_MARK = 7, // To have a name tag, position marker in a file115 PPS_POSTAG_MARK = 8, // Position tag mark + 8 bytes (=stream position)116 PPS_POSTAG_TABLE = 9, // Position tag table + 8 bytes (=stream position)117 PPS_SIMPLE = 16, // 16 + number of bytes, up to 8 bytes118 PPS_SIMPLE_ARRAY4 = 32, // 32 + number of bytes, up to 8 bytes, then 4 bytes of length119 PPS_SIMPLE_ARRAY8 = 48 // 48 + number of bytes, up to 8 bytes, then 8 bytes of length120 };121 // The following values are used with PPS_SIMPLE and PPS_SIMPLE_ARRAY (Using OR)122 enum {PPS_DATATYPE_CHAR = 0, // 0 : DataType=character123 PPS_DATATYPE_FLOAT = 64, // 64 : DataType=float124 PPS_DATATYPE_COMPLEX = 65, // 65 : DataType=complex125 PPS_DATATYPE_INTEGER = 128, // 128 :DataType=integer126 PPS_DATATYPE_UNSIGNED = 192 // 192 :DataType=Unsigned integer127 };128 129 map<string, int_8> tags;130 string filename;131 int version; // PPersist(In/Out) version132 103 }; 133 104 … … 136 107 137 108 //! Input stream for PPersit objects. 138 class PInPersist : public P IOPersist{109 class PInPersist : public PPFBinaryInputStream, public PIOPersist { 139 110 public: 140 111 PInPersist(string const& flnm, bool scan=true); 141 ~PInPersist(); 142 143 // Gestion des tags 144 bool GotoPositionTag(int_8 pos); 145 bool GotoNameTag(string const& name); 146 int NbNameTags(); 147 bool GotoNameTagNum(int itag); // 0..NbTags-1 148 string GetTagName(int itag); // 0..NbTags-1 149 string GetTagClassName(int itag); // 0..NbTags-1 150 vector<string> const & GetNameTags(); 151 152 // Saut jusqu'au prochain objet 153 bool SkipToNextObject(); 154 155 // Lecture donnees de base et tableaux de donnees de base 156 // Basic data type (and corresponding arrays) reading 157 void GetByte (char& c); 158 void GetBytes(void* ptr, size_t bytes); 159 void GetR4 (r_4&); 160 void GetR4s (r_4*, size_t); 161 void GetR8 (r_8&); 162 void GetR8s (r_8*, size_t); 163 void GetI1 (int_1&); 164 void GetI1s (int_1*, size_t); 165 void GetU1 (uint_1&); 166 void GetU1s (uint_1*, size_t); 167 void GetI2 (int_2&); 168 void GetI2s (int_2*, size_t); 169 void GetU2 (uint_2&); 170 void GetU2s (uint_2*, size_t); 171 void GetI4 (int_4&); 172 void GetI4s (int_4*, size_t); 173 void GetU4 (uint_4&); 174 void GetU4s (uint_4*, size_t); 175 void GetI8 (int_8&); 176 void GetI8s (int_8*, size_t); 177 void GetU8 (uint_8&); 178 void GetU8s (uint_8*, size_t); 179 void GetLine (char* ptr, size_t len); 180 void GetStr (string&); 181 void GetZ4 (complex<r_4> &); 182 void GetZ4s (complex<r_4> *, size_t); 183 void GetZ8 (complex<r_8> &); 184 void GetZ8s (complex<r_8> *, size_t); 185 186 inline void Get(char& c) {GetByte(c);} 187 inline void Get(r_4& x) {GetR4(x);} 188 inline void Get(r_8& x) {GetR8(x);} 189 inline void Get(uint_1& x) {GetU1(x);} 190 inline void Get(int_1& x) {GetI1(x);} 191 inline void Get(uint_2& x) {GetU2(x);} 192 inline void Get(int_2& x) {GetI2(x);} 193 inline void Get(uint_4& x) {GetU4(x);} 194 inline void Get(int_4& x) {GetI4(x);} 195 inline void Get(uint_8& x) {GetU8(x);} 196 inline void Get(int_8& x) {GetI8(x);} 197 inline void Get(complex<r_4> & x) {GetZ4(x);} 198 inline void Get(complex<r_8> & x) {GetZ8(x);} 199 200 inline void Get(r_4* x, size_t n) {GetR4s(x,n);} 201 inline void Get(r_8* x, size_t n) {GetR8s(x,n);} 202 inline void Get(uint_1* x, size_t n) {GetU1s(x,n);} 203 inline void Get(int_1* x, size_t n) {GetI1s(x,n);} 204 inline void Get(uint_2* x, size_t n) {GetU2s(x,n);} 205 inline void Get(int_2* x, size_t n) {GetI2s(x,n);} 206 inline void Get(uint_4* x, size_t n) {GetU4s(x,n);} 207 inline void Get(int_4* x, size_t n) {GetI4s(x,n);} 208 inline void Get(uint_8* x, size_t n) {GetU8s(x,n);} 209 inline void Get(int_8* x, size_t n) {GetI8s(x,n);} 210 inline void Get(string& x) {GetStr(x);} 211 inline void Get(complex<r_4> * x, size_t n) { GetZ4s(x, n); } 212 inline void Get(complex<r_8> * x, size_t n) { GetZ8s(x, n); } 213 214 // Reading a list (table) of position tags 215 void GetPosTagTable(int_8*, size_t); 216 void GetPosTagTable(vector<int_8>&); 217 112 virtual ~PInPersist(); 113 114 // A faire - Reza Dec 2003 115 string GetTagClassName(int itag); 218 116 // Object Reading 219 117 PPersist* ReadObject(); … … 222 120 PPersist* GetPPObject(AnyDataObj * po=NULL); 223 121 224 time_t CreationDate() { return creationdate; }225 string CreationDateStr();226 227 void AnalyseTags(int lev=0); // List (all or some) tags ...228 229 122 // Reza 03/2000 230 123 // Methodes qui pourraient etre protected, mais doivent etre utilisables par PPersist … … 234 127 235 128 protected: 236 void CheckTag (short datasz, short datatype);237 void CheckArrayTag(short datasz, size_t sz, short datatype);238 void GetTypeTag (unsigned char& c);239 void GetRawByte (char& c);240 void GetRawUByte (unsigned char& c);241 void GetRawBytes(void* ptr, size_t bytes);242 void GetRawI2 (int_2&);243 void GetRawI4 (int_4&);244 void GetRawI8 (int_8&);245 void GetRawU8 (uint_8&);246 247 void Scan();248 249 RawInOutStream* s;250 251 bool bigEndian;252 253 time_t creationdate;254 129 255 130 // already read objects 256 131 typedef map<uint_8, PPersist * > ObjList; 257 132 ObjList objList; 258 // Si on a fait une lecture non sequentielle -> seqread = false259 bool seqread;260 133 friend class PPersist; 261 134 }; 262 135 263 136 //! Output stream for PPersit objects. 264 class POutPersist : public P IOPersist{137 class POutPersist : public PPFBinaryOutputStream, public PIOPersist { 265 138 public: 266 139 POutPersist(string const& flnm, int endianness = PPS_NATIVE); 267 ~POutPersist(); 268 269 // Ecriture de tags 270 int_8 WritePositionTag(); 271 void WriteNameTag(string const& name); 272 inline void WriteTag(string const& name) { WriteNameTag(name); } 273 274 void PutByte (char c); 275 void PutBytes(void const* ptr, size_t bytes); 276 void PutR4 (r_4); 277 void PutR4s (r_4 const*, size_t); 278 void PutR8 (r_8); 279 void PutR8s (r_8 const*, size_t); 280 void PutI1 (int_1); 281 void PutI1s (int_1 const*, size_t); 282 void PutU1 (uint_1); 283 void PutU1s (uint_1 const*, size_t); 284 void PutI2 (int_2); 285 void PutI2s (int_2 const*, size_t); 286 void PutU2 (uint_2); 287 void PutU2s (uint_2 const*, size_t); 288 void PutI4 (int_4); 289 void PutI4s (int_4 const*, size_t); 290 void PutU4 (uint_4); 291 void PutU4s (uint_4 const*, size_t); 292 void PutI8 (int_8); 293 void PutI8s (int_8 const*, size_t); 294 void PutU8 (uint_8); 295 void PutU8s (uint_8 const*, size_t); 296 void PutLine (char const* ptr, size_t len=0); // deprecated ? 297 void PutStr (string const&); 298 void PutZ4 (complex<r_4>); 299 void PutZ4s (complex<r_4> const*, size_t); 300 void PutZ8 (complex<r_8>); 301 void PutZ8s (complex<r_8> const*, size_t); 302 303 304 void Put(char c) {PutByte(c);} 305 void Put(r_4 x) {PutR4(x);} 306 void Put(r_8 x) {PutR8(x);} 307 void Put(complex<r_4> x) {PutZ4(x);} 308 void Put(complex<r_8> x) {PutZ8(x);} 309 void Put(uint_1 x) {PutU1(x);} 310 void Put(int_1 x) {PutI1(x);} 311 void Put(uint_2 x) {PutU2(x);} 312 void Put(int_2 x) {PutI2(x);} 313 void Put(uint_4 x) {PutU4(x);} 314 void Put(int_4 x) {PutI4(x);} 315 void Put(uint_8 x) {PutU8(x);} 316 void Put(int_8 x) {PutI8(x);} 317 void Put(r_4 const* x, size_t n) {PutR4s(x,n);} 318 void Put(r_8 const* x, size_t n) {PutR8s(x,n);} 319 void Put(complex<r_4> const* x, size_t n) {PutZ4s(x,n);} 320 void Put(complex<r_8> const* x, size_t n) {PutZ8s(x,n);} 321 void Put(uint_1 const* x, size_t n) {PutU1s(x,n);} 322 void Put(int_1 const* x, size_t n) {PutI1s(x,n);} 323 void Put(uint_2 const* x, size_t n) {PutU2s(x,n);} 324 void Put(int_2 const* x, size_t n) {PutI2s(x,n);} 325 void Put(uint_4 const* x, size_t n) {PutU4s(x,n);} 326 void Put(int_4 const* x, size_t n) {PutI4s(x,n);} 327 void Put(uint_8 const* x, size_t n) {PutU8s(x,n);} 328 void Put(int_8 const* x, size_t n) {PutI8s(x,n);} 329 void Put(string const& s) {PutStr(s);} 330 void Put(PPersist const* x) {PutPPObject(x);} 331 332 // Writing a list of position tag table 333 void PutPosTagTable(int_8 const *, size_t); 334 void PutPosTagTable(vector<int_8> const&); 140 virtual ~POutPersist(); 141 142 // void Put(PPersist const* x) {PutPPObject(x);} 335 143 336 144 // Objet Write - Ecriture des objets … … 342 150 343 151 protected: 344 RawInOutStream* s;345 bool bigEndian;346 347 void PutArrayTag(short datasz, size_t sz, short datatype);348 void PutRawByte (char);349 void PutRawUByte (unsigned char);350 void PutRawI2 (int_2);351 void PutRawI4 (int_4);352 void PutRawI8 (int_8);353 void PutRawU8 (uint_8);354 void PutRawBytes(void const* ptr, size_t bytes);355 152 bool serializeNullAndRepeat(PPersist const* x); 356 153 uint_8 findObjectId(PPersist const* x, int_8 & pos); … … 444 241 #define DObjRegister(ppclassName, className) PIOPersist::RegisterDataObjClass(PIOPersist::Hash(#ppclassName), typeid(className).name()); 445 242 446 } // namespace 243 } // namespace SOPHYA 244 245 // La classe PPFNameTag facilite la manipulation des Nametag ds les fichiers 246 // PPersist - definie ds ppfnametag.h 247 #include "ppfnametag.h" 248 447 249 448 250 #endif
Note:
See TracChangeset
for help on using the changeset viewer.