Changeset 802 in Sophya
- Timestamp:
- Apr 3, 2000, 7:32:27 PM (25 years ago)
- Location:
- trunk/SophyaLib/BaseTools
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/dvlist.cc
r754 r802 270 270 // ostream& operator << (ostream& s, DVList& dvl) 271 271 // sortie sur flot "s" (Appel a "Print(s)"). 272 // void Write(string const& fn)273 // Ecriture d'un fichier PPersist de nom "fn"274 // void Read(string const& fn)275 // Lecture d'un fichier PPersist de nom "fn"276 272 //-- 277 273 … … 350 346 { 351 347 char buf[512]; 348 string sfw; 352 349 353 350 int lc = dobj->Comment().length(); 354 if (lc > 511) lc = 511;355 351 if (lc > 0) { 356 352 sprintf(buf,"Comment: ( %6d ) ", lc); 357 s .PutLine(buf);358 s.Put Bytes(dobj->Comment().c_str(), lc);359 } 360 s .PutLine("----Variable-List---------------");353 sfw = buf; s.PutStr(sfw); 354 s.PutStr(dobj->Comment()); 355 } 356 sfw = "----Variable-List---------------"; s.PutStr(sfw); 361 357 DVList::ValList::const_iterator it; 362 358 for(it = dobj->Begin(); it != dobj->End(); it++) { … … 364 360 case 'I' : 365 361 sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.iv ); 366 s .PutLine(buf);362 sfw = buf; s.PutStr(sfw); 367 363 break; 368 364 case 'D' : 369 365 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.dv ); 370 s .PutLine(buf);366 sfw = buf; s.PutStr(sfw); 371 367 break; 372 368 case 'S' : 373 369 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.strv ); 374 s .PutLine(buf);370 sfw = buf; s.PutStr(sfw); 375 371 break; 376 372 default : … … 380 376 if ((*it).second.elcomm.length() > 0) { 381 377 sprintf(buf,"# %s", (*it).second.elcomm.substr(0,256).c_str()); 382 s .PutLine(buf);383 } 384 } 385 386 s .PutLine("ZZZZZ--End-of-Varible-List------");378 sfw = buf; s.PutStr(sfw); 379 } 380 } 381 382 sfw = "ZZZZZ--End-of-Varible-List------"; s.PutStr(sfw); 387 383 } 388 384 … … 391 387 { 392 388 char buf[512]; 389 string sfr; 393 390 int_8 j,iv; 394 391 r_8 dv; … … 397 394 dobj->Clear(); 398 395 399 s.GetLine(buf, 511); // Pour lire les "------- " 400 if (buf[0] != '-') { // Il y a un champ commentaire a lire 401 buf[18] ='\0'; 402 int lc = atoi(buf+11); 403 if (lc > 511) { 404 cerr << "DVList::ReadSelf() Pb/Bug ?? CommentLength= " << lc << endl; 405 lc = 511; 406 } 407 s.GetBytes(buf, lc); 408 buf[lc] ='\0'; 409 dobj->Comment() = buf; 396 s.GetStr(sfr); // Pour lire les "------- " 397 if (sfr[0] != '-') { // Il y a un champ commentaire a lire 398 s.GetStr(sfr); 399 dobj->Comment() = sfr; 410 400 } 411 401 412 402 string key=""; 413 403 while(ok) { 414 s.GetLine(buf, 511); 404 s.GetStr(sfr); 405 strncpy(buf, sfr.c_str(), 512); 415 406 buf[511] = '\0'; 416 407 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; } -
trunk/SophyaLib/BaseTools/machdefs.h
r682 r802 264 264 #if defined(__KCC__) 265 265 /* KCC V 3.2 ne compile pas i/ostream s ; long p = s.tellg(); OK en V 3.3 */ 266 #define STREAMPOS_IS_CLASS267 266 #define HAS_STR_NPOS 268 267 #ifndef ANSI_TEMPLATES -
trunk/SophyaLib/BaseTools/ndatablock.cc
r773 r802 9 9 #include "pexceptions.h" 10 10 #include "ndatablock.h" 11 #include "objfio.h"12 11 13 12 // define DEBUG_NDATABLOCK … … 613 612 } 614 613 615 ////////////////////////////////////////////////////////////////616 // -------------------------------------------------------------------------617 // Les objets delegues pour la gestion de persistance618 // -------------------------------------------------------------------------619 620 /*621 template <class T>622 void ObjFileIO< NDataBlock<T> >::ReadSelf(PInPersist& is)623 template <class T>624 void ObjFileIO< NDataBlock<T> >::WriteSelf(POutPersist& os)625 */626 627 // Pour pouvoir ecrire des tableaux de complex, en attendant628 // PIn/POutPersist::Get/Put(complex<>)629 #include "piocmplx.h"630 631 template <class T>632 FIO_NDataBlock<T>::FIO_NDataBlock()633 {634 dobj=new NDataBlock<T>;635 ownobj=true;636 }637 638 template <class T>639 FIO_NDataBlock<T>::FIO_NDataBlock(string const & filename)640 {641 dobj=new NDataBlock<T>;642 ownobj=true;643 Read(filename);644 }645 646 template <class T>647 FIO_NDataBlock<T>::FIO_NDataBlock(const NDataBlock<T> & obj)648 {649 dobj = new NDataBlock<T>(obj);650 ownobj=true;651 }652 653 template <class T>654 FIO_NDataBlock<T>::FIO_NDataBlock(NDataBlock<T> * obj)655 {656 dobj = obj;657 ownobj=false;658 }659 660 template <class T>661 FIO_NDataBlock<T>::~FIO_NDataBlock()662 {663 if (ownobj && dobj) delete dobj;664 }665 666 template <class T>667 AnyDataObj* FIO_NDataBlock<T>::DataObj()668 {669 return(dobj);670 }671 672 template <class T>673 void FIO_NDataBlock<T>::SetDataObj(AnyDataObj & o)674 {675 NDataBlock<T> * po = dynamic_cast< NDataBlock<T> * >(&o);676 if (po == NULL) return;677 if (ownobj && dobj) delete dobj;678 dobj = po; ownobj = false;679 }680 681 template <class T>682 void FIO_NDataBlock<T>::ReadSelf(PInPersist& is)683 {684 // On lit les 3 premiers uint_8685 uint_8 itab[3];686 is.Get(itab, 3);687 if (dobj == NULL) dobj = new NDataBlock<T>(itab[1]);688 else if (itab[1] != dobj->Size()) dobj->ReSize(itab[1]);689 // On lit le tableau de nombres690 PIOSReadArray(is, dobj->Data(), dobj->Size());691 }692 693 694 template <class T>695 void FIO_NDataBlock<T>::WriteSelf(POutPersist& os) const696 {697 if (dobj == NULL) return; // Attention - $CHECK$ Reza 26/04/99698 // On ecrit 3 uint_8699 // 0 : Numero de version, 1 : Taille, 2 reserve a l700 uint_8 itab[3];701 itab[0] = 1;702 itab[1] = dobj->Size();703 itab[2] = 0;704 os.Put(itab, 3);705 // On ecrit le tableau de nombres706 PIOSWriteArray(os, dobj->Data(), dobj->Size());707 }708 614 709 615 /////////////////////////////////////////////////////////////// … … 718 624 #pragma define_template NDataBlock<r_4> 719 625 #pragma define_template NDataBlock<r_8> 720 #pragma define_template NDataBlock< complex<float> > 721 #pragma define_template NDataBlock< complex<double> > 722 // Instances des delegues FileIO (PPersist) 723 #pragma define_template FIO_NDataBlock<uint_1> 724 #pragma define_template FIO_NDataBlock<uint_2> 725 #pragma define_template FIO_NDataBlock<int_2> 726 #pragma define_template FIO_NDataBlock<int_4> 727 #pragma define_template FIO_NDataBlock<int_8> 728 #pragma define_template FIO_NDataBlock<uint_4> 729 #pragma define_template FIO_NDataBlock<uint_8> 730 #pragma define_template FIO_NDataBlock<r_8> 731 #pragma define_template FIO_NDataBlock<r_4> 732 #pragma define_template FIO_NDataBlock< complex<float> > 733 #pragma define_template FIO_NDataBlock< complex<double> > 626 #pragma define_template NDataBlock< complex<r_4> > 627 #pragma define_template NDataBlock< complex<r_8> > 734 628 #endif 735 629 … … 744 638 template class NDataBlock<r_4>; 745 639 template class NDataBlock<r_8>; 746 template class NDataBlock< complex<float> >; 747 template class NDataBlock< complex<double> >; 748 // Instances des delegues FileIO (PPersist) 749 template class FIO_NDataBlock<uint_1>; 750 template class FIO_NDataBlock<uint_2>; 751 template class FIO_NDataBlock<int_2>; 752 template class FIO_NDataBlock<int_4>; 753 template class FIO_NDataBlock<int_8>; 754 template class FIO_NDataBlock<uint_4>; 755 template class FIO_NDataBlock<uint_8>; 756 template class FIO_NDataBlock<r_8>; 757 template class FIO_NDataBlock<r_4>; 758 template class FIO_NDataBlock< complex<float> >; 759 template class FIO_NDataBlock< complex<double> >; 760 #endif 640 template class NDataBlock< complex<r_4> >; 641 template class NDataBlock< complex<r_8> >; 642 #endif -
trunk/SophyaLib/BaseTools/ndatablock.h
r773 r802 8 8 #include "machdefs.h" 9 9 #include "anydataobj.h" 10 #include "ppersist.h"11 10 #include <iostream.h> 12 11 … … 160 159 161 160 162 // Classe pour la gestion de persistance163 template <class T>164 class FIO_NDataBlock : public PPersist {165 public:166 FIO_NDataBlock();167 FIO_NDataBlock(string const & filename);168 FIO_NDataBlock(const NDataBlock<T> & obj);169 FIO_NDataBlock(NDataBlock<T> * obj);170 virtual ~FIO_NDataBlock();171 virtual AnyDataObj* DataObj();172 virtual void SetDataObj(AnyDataObj & o);173 inline operator NDataBlock<T>() { return(*dobj); }174 protected :175 virtual void ReadSelf(PInPersist&);176 virtual void WriteSelf(POutPersist&) const;177 NDataBlock<T> * dobj;178 bool ownobj;179 };180 181 template <class T>182 inline POutPersist& operator << (POutPersist& os, NDataBlock<T> & obj)183 { FIO_NDataBlock<T> fio(&obj); fio.Write(os); return(os); }184 template <class T>185 inline PInPersist& operator >> (PInPersist& is, NDataBlock<T> & obj)186 { FIO_NDataBlock<T> fio(&obj); fio.Read(is); return(is); }187 161 188 162 } // Fin du namespace -
trunk/SophyaLib/BaseTools/ppersist.cc
r754 r802 5 5 #include "pexceptions.h" 6 6 #include "ppersist.h" 7 #include "anydataobj.h" 7 8 #include <fstream.h> 8 9 #include <iostream.h> … … 13 14 #include "unixmac.h" 14 15 #include <SIOUX.h> 16 #endif 17 18 // strptime n'est pas defini sous Linux - Reza Mars 2000 19 #if defined(OS_LINUX) 20 extern "C" { 21 char *strptime(char *buf, const char *format, const struct tm *tm); 22 } 15 23 #endif 16 24 … … 35 43 36 44 MD5_CONTEXT PIOPersist::ctx; 37 PIOPersist::ClassList * PIOPersist::classList = NULL; // $CHECK$ Reza 26/04/99 38 map<string, uint_8> * PIOPersist::typeids = NULL; 45 PIOPersist::ClassList * PIOPersist::ppclassList = NULL; // $CHECK$ Reza 26/04/99 46 map<string, uint_8> * PIOPersist::ppclassNameList = NULL; 47 map<string, uint_8> * PIOPersist::dobjclassNameList = NULL; 39 48 40 49 //++ … … 44 53 //-- 45 54 { 46 classList = new PIOPersist::ClassList; 47 typeids = new map<string, uint_8>; 48 } 49 50 //++ 51 void 52 PIOPersist::RegisterClass(uint_8 classId, string typname, ClassCreatorFunc f) 55 ppclassList = new PIOPersist::ClassList; 56 ppclassNameList = new map<string, uint_8>; 57 dobjclassNameList = new map<string, uint_8>; 58 } 59 60 //++ 61 void 62 PIOPersist::RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f) 53 63 // 54 // Register a new persist antclass.55 // The classId is usually a hash of the class name, and this56 // method is called only through the PPersistRegistrar template57 // class, with the PPRegister(className) macro.64 // Register a new persistence handler (PPersist) class. 65 // The classId is usually a hash of the class name, and 66 // ppclass_name is typeid(PPersistClass).name() . 67 // This method is called only through the PPersistRegistrar template 58 68 // 59 69 //-- 60 70 { 61 if (classList->size() && (classList->find(classId) != classList->end())) { 62 cerr << "RegisterClass : Error, " << hex << classId << dec 63 << " already registered." << endl; 64 throw(DuplicateIdExc("PIOPersist::RegisterClass")); 71 if (ppclassList->size() && (ppclassList->find(classId) != ppclassList->end()) ) { 72 cerr << "RegisterClass : Error, " << hex << classId << dec 73 << " already registered." << endl; 74 throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass")); 75 } 76 if (ppclassNameList->size() && (ppclassNameList->find(ppclass_name) != ppclassNameList->end())) { 77 cerr << "RegisterClass : Error (2) " << ppclass_name 78 << " already registered." << endl; 79 throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass (3)")); 65 80 } 66 81 67 (*classList)[classId] = f; 68 (*typeids)[typname] = classId; 69 } 70 82 (*ppclassList)[classId] = f; 83 (*ppclassNameList)[ppclass_name] = classId; 84 } 85 86 //++ 87 void 88 PIOPersist::RegisterDataObjClass(uint_8 classId, string class_name) 89 // Register a new DataObj class corresponding to a PPersist classId 90 // class_typename should be typeid(DataObject).name() 91 //-- 92 { 93 if (ppclassList->find(classId) == ppclassList->end() ) 94 throw( NotFoundExc("PIOPersist::RegisterDataObjClass() Not found classId") ); 95 if (dobjclassNameList->size() && (dobjclassNameList->find(class_name) != dobjclassNameList->end())) 96 throw(DuplicateIdExc("PIOPersist::RegisterDataObjClass")); 97 98 (*dobjclassNameList)[class_name] = classId; 99 } 100 101 // class_typename should be typeid(DataObject).name(), to be 102 // used by POutPersist::PutDataObject() methods. 71 103 72 104 PIOPersist::ClassCreatorFunc 73 105 PIOPersist::FindCreatorFunc(uint_8 classId) 74 { 75 ClassList::iterator i = classList->find(classId); 76 if (i == classList->end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc")); 106 // Returns the PPersist class creator function for the specified classId 107 { 108 ClassList::iterator i = ppclassList->find(classId); 109 if (i == ppclassList->end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc() Not found classId")); 77 110 return (*i).second; 111 } 112 113 string 114 PIOPersist::getPPClassName(uint_8 classId) 115 // Returns the PPersist class name for the specified classId 116 { 117 map<string, uint_8>::iterator i; 118 for (i= ppclassNameList->begin(); i != ppclassNameList->end(); i++) 119 if ( (*i).second == classId ) return (*i).first; 120 121 throw(NotFoundExc("PIOPersist::getPPClassName() Not found classId")); 122 } 123 124 uint_8 125 PIOPersist::getPPClassId(string const & typ_name) 126 // Returns the classId for the specified PPersist class type name 127 { 128 map<string, uint_8>::iterator i = ppclassNameList->find(typ_name); 129 if (i == ppclassNameList->end()) 130 throw(NotFoundExc("PIOPersist::getPPClassId() Not found className")); 131 return (*i).second; 132 } 133 134 uint_8 135 PIOPersist::getPPClassId(PPersist const & ppo) 136 // Returns the classId for the specified PPersist class 137 { 138 string typ_name = typeid(ppo).name() ; 139 return (getPPClassId(typ_name) ); 140 } 141 142 143 string 144 PIOPersist::getDataObjClassName(uint_8 classId) 145 // Returns the PPersist class name for the specified classId 146 { 147 map<string, uint_8>::iterator i; 148 for (i= dobjclassNameList->begin(); i != dobjclassNameList->end(); i++) 149 if ( (*i).second == classId ) return (*i).first; 150 151 throw(NotFoundExc("PIOPersist::getDataObjClassName() Not found classId")); 152 } 153 154 uint_8 155 PIOPersist::getDataObjClassId(string const & typ_name) 156 // Returns the classId for the specified PPersist class type name 157 { 158 map<string, uint_8>::iterator i = dobjclassNameList->find(typ_name); 159 if (i == dobjclassNameList->end()) 160 throw(NotFoundExc("PIOPersist::getDataObjClassId() Not found className")); 161 return (*i).second; 162 } 163 164 uint_8 165 PIOPersist::getDataObjClassId(AnyDataObj const & o) 166 // Returns the classId for the specified PPersist class 167 { 168 string typ_name = typeid(o).name() ; 169 return (getDataObjClassId(typ_name) ); 78 170 } 79 171 … … 140 232 //-- 141 233 { 142 s.Put Object(this);234 s.PutPPObject(this); 143 235 } 144 236 … … 156 248 // We should be the exact type 157 249 // Check tag value 158 char ppstype;250 unsigned char ppstype,ppstag; 159 251 s.GetTypeTag(ppstype); 160 if (ppstype != PInPersist::PPS_OBJECT) { 161 throw FileFormatExc("PPersist::Read : not an object in flow"); 162 } 163 164 // Check class id 165 uint_8 classId; 166 s.GetRawU8(classId); 167 uint_8 myClassId = PIOPersist::getTypeId(typeid(*this).name()); 168 if (classId != myClassId) { 169 throw FileFormatExc("PPersist::Read : not the same object type"); 170 } 171 172 ReadSelf(s); 252 if ( (ppstype != PInPersist::PPS_OBJECT) && ( ppstype != PInPersist::PPS_REFERENCE ) ) { 253 } 254 if (ppstype == PInPersist::PPS_OBJECT) { 255 // Check class id 256 uint_8 classId; 257 s.GetRawU8(classId); 258 uint_8 oid,oid2; 259 s.GetRawU8(oid); 260 if (classId != PIOPersist::getPPClassId(*this) ) 261 throw FileFormatExc("PPersist::Read (): not the same object type"); 262 ReadSelf(s); 263 // Read the ENDOBJECT 264 s.GetRawUByte(ppstag); 265 if (ppstag != PInPersist::PPS_ENDOBJECT) 266 throw FileFormatExc("PPersist::Read() No PPS_ENDOBJECT tag"); 267 s.GetRawU8(oid2); 268 if (oid2 != oid) 269 throw FileFormatExc("PPersist::Read() Inconsistent PPS-OId at PPS_ENDOBJECT "); 270 s.KeepOId(oid, *this); // Object should be kept with its PPS_OId (if oid > 0) 271 } 272 else if ( ppstype == PInPersist::PPS_REFERENCE ) 273 s.ReadReference(*this); 274 275 else throw FileFormatExc("PPersist::Read() : not an object in flow"); 276 173 277 } 174 278 … … 181 285 { 182 286 s.WriteTag(tag); 183 s.Put Object(this);287 s.PutPPObject(this); 184 288 } 185 289 … … 196 300 } 197 301 302 // Renvoie l'identificateur de l'objet - par defaut=0 303 304 uint_8 305 PPersist::getMemOId() const 306 { 307 return(0); 308 } 309 310 // Ces deux methodes doivent etre redefinies si getMemOId() renvoie non nul (>0) 311 // ShareDataReference() et CloneSharedReference() 312 void 313 PPersist::ShareDataReference(PPersist & pcs) 314 { 315 throw NotAvailableOperation("PPersist::ShareDataReference() - Unsupported operation !"); 316 } 317 318 PPersist * 319 PPersist::CloneSharedReference() 320 { 321 throw NotAvailableOperation("PPersist::CloneSharedReference() - Unsupported operation !"); 322 } 198 323 199 324 //++ … … 233 358 throw FileFormatExc("PInPersist::PInPersist bad header"); 234 359 } 235 version = atoi(rbuf+24); 236 360 rbuf[32] = '\0'; 361 version = atoi(rbuf+25); 362 if (version < 2) { 363 cerr << "PInPersist::PInPersist(" << flnm << ") Version(=" << version 364 << ") < 2 not supported !" << endl; 365 throw FileFormatExc("PInPersist::PInPersist() - Unsupported (Old) Version"); 366 } 237 367 // read endianness 238 368 GetRawBytes(rbuf, 32); … … 249 379 rbuf[32] = '\0'; 250 380 struct tm tm; 251 #ifndef __MWERKS__ 381 #ifndef __MWERKS__ 252 382 strptime(rbuf,"%d/%m/%Y %H:%M:%S GMT",&tm); 253 383 #else … … 258 388 #endif 259 389 creationdate = mktime(&tm); 260 390 filename = flnm; // keep the filename 391 seqread = true; // To flag non sequential reads 261 392 if (scan) Scan(); 262 393 } … … 266 397 PInPersist::~PInPersist() 267 398 { 399 ObjList::iterator i; 400 for(i=objList.begin(); i!= objList.end(); i++) 401 if ((*i).second) delete (*i).second; 268 402 delete s; 269 403 } 270 404 271 405 406 string 407 PInPersist::CreationDateStr() 408 { 409 time_t cdt = CreationDate(); 410 string cdate = ctime(&cdt); 411 return(cdate); 412 } 413 272 414 void 273 415 PInPersist::Scan() … … 275 417 // On cherche la liste des tags, a la fin du fichier 276 418 277 char ppstype;419 unsigned char ppstype; 278 420 size_t debut; 279 421 #ifdef STREAMPOS_IS_CLASS … … 333 475 // throw NotFoundExc("PInPersist::GotoTag tag not found"); 334 476 s->seekg((*i).second); 335 objList.clear(); // $CHECK$ EA 171199 477 seqread = false; 478 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ? 336 479 return(true); 337 480 } … … 344 487 for (int j=0; j<itag; j++) i++; 345 488 s->seekg((*i).second); 346 objList.clear(); // $CHECK$ EA 171199489 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ? 347 490 return(true); 348 491 } … … 355 498 for (int j=0; j<itag; j++) i++; 356 499 return((*i).first); 500 } 501 502 string 503 PInPersist::GetTagClassName(int itag) 504 { 505 // A faire 506 // if (itag<0 || itag >= (int)tags.size()) return ""; 507 // map<string, int_8>::iterator i = tags.begin(); 508 // for (int j=0; j<itag; j++) i++; 509 // uint_8 cid = (*i).second; 510 // return(GetClassName(cid)); 511 return(""); 357 512 } 358 513 … … 424 579 425 580 void 426 PInPersist::GetTypeTag(char& c) 427 { 428 GetRawByte(c); 429 while (c == PPS_TAG_MARK) { 430 objList.clear(); 431 GetRawByte(c); 432 } 581 PInPersist::GetTypeTag(unsigned char& c) 582 { 583 c = PPS_TAG_MARK; 584 while (c == PPS_TAG_MARK) GetRawUByte(c); 585 // while (c == PPS_TAG_MARK) { Il ne faut plus faire ca ! 586 // objList.clear(); $CHECK$ Reza 03/2000 587 // GetRawByte(c); 588 // } 433 589 } 434 590 … … 441 597 442 598 void 599 PInPersist::GetRawUByte(unsigned char& c) 600 { 601 GetRawBytes(&c, 1); 602 } 603 604 void 443 605 PInPersist::GetRawBytes(void* ptr, size_t bytes) 444 606 { … … 479 641 480 642 void 481 PInPersist::CheckTag(short datasz) 482 { 483 char ppstype; 643 PInPersist::CheckTag(short datasz, short datatype) 644 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED 645 { 646 unsigned char ppstype; 484 647 GetTypeTag(ppstype); 485 if (ppstype != PPS_SIMPLE + datasz )648 if (ppstype != PPS_SIMPLE + datasz + datatype) 486 649 throw FileFormatExc("PInPersist::CheckTag bad type in ppersist file"); 487 650 } 488 651 489 652 void 490 PInPersist::CheckArrayTag(short datasz, size_t sz) 491 { 492 char ppstype; 653 PInPersist::CheckArrayTag(short datasz, size_t sz, short datatype) 654 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED 655 { 656 unsigned char ppstype; 493 657 GetTypeTag(ppstype); 494 658 size_t filesz; 495 if (sz <= 0x7fff) { 496 if (ppstype != PPS_SIMPLE_ARRAY + datasz) 497 throw FileFormatExc("PInPersist::CheckTag bad type in ppersist file"); 498 int_2 ff; 499 GetRawI2(ff); filesz=ff; 500 } else if (sz <= 0x7fffffff) { 501 if (ppstype != PPS_SIMPLE_ARRAY4 + datasz) 502 throw FileFormatExc("PInPersist::CheckTag bad type in ppersist file"); 659 if (sz <= 0x7fffffff) { 660 if (ppstype != PPS_SIMPLE_ARRAY4 + datasz + datatype) 661 throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file"); 503 662 int_4 ff; 504 663 GetRawI4(ff); filesz=ff; 505 664 } else { 506 if (ppstype != PPS_SIMPLE_ARRAY8 + datasz )507 throw FileFormatExc("PInPersist::Check Tag bad type in ppersist file");665 if (ppstype != PPS_SIMPLE_ARRAY8 + datasz + datatype) 666 throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file"); 508 667 uint_8 ff; 509 668 GetRawU8(ff); filesz=ff; 510 669 } 511 670 if (filesz != sz) 512 throw FileFormatExc("PInPersist::Check Tag bad array size in ppersist file");671 throw FileFormatExc("PInPersist::CheckArrayTag bad array size in ppersist file"); 513 672 } 514 673 … … 516 675 PInPersist::GetByte(char& c) 517 676 { 518 CheckTag(1 );677 CheckTag(1,PPS_DATATYPE_CHAR); 519 678 GetRawBytes(&c, 1); 520 679 } 521 680 681 522 682 void 523 683 PInPersist::GetBytes(void* ptr, size_t bytes) 524 684 { 525 CheckArrayTag(1, bytes );685 CheckArrayTag(1, bytes, PPS_DATATYPE_CHAR); 526 686 GetRawBytes(ptr, bytes); 527 687 } … … 529 689 PInPersist::GetR4 (r_4& result) 530 690 { 531 CheckTag(4 );691 CheckTag(4,PPS_DATATYPE_FLOAT); 532 692 GetRawBytes(&result, sizeof(r_4)); 533 693 if (bigEndian != IS_BIG_ENDIAN) … … 539 699 PInPersist::GetR4s (r_4* tab, size_t n) 540 700 { 541 CheckArrayTag(4,n );701 CheckArrayTag(4,n,PPS_DATATYPE_FLOAT); 542 702 GetRawBytes(tab, n*sizeof(r_4)); 543 703 if (bigEndian == IS_BIG_ENDIAN) return; … … 552 712 PInPersist::GetR8 (r_8& result) 553 713 { 554 CheckTag(8 );714 CheckTag(8,PPS_DATATYPE_FLOAT); 555 715 GetRawBytes(&result, sizeof(r_8)); 556 716 if (bigEndian != IS_BIG_ENDIAN) … … 561 721 PInPersist::GetR8s (r_8* tab, size_t n) 562 722 { 563 CheckArrayTag(8,n );723 CheckArrayTag(8,n,PPS_DATATYPE_FLOAT); 564 724 GetRawBytes(tab, n*sizeof(r_8)); 565 725 if (bigEndian == IS_BIG_ENDIAN) return; … … 574 734 PInPersist::GetI2 (int_2& result) 575 735 { 576 CheckTag(2 );736 CheckTag(2,PPS_DATATYPE_INTEGER); 577 737 GetRawBytes(&result, sizeof(int_2)); 578 738 if (bigEndian != IS_BIG_ENDIAN) … … 583 743 PInPersist::GetI2s (int_2* tab, size_t n) 584 744 { 585 CheckArrayTag(2,n );745 CheckArrayTag(2,n,PPS_DATATYPE_INTEGER); 586 746 GetRawBytes(tab, n*sizeof(int_2)); 587 747 if (bigEndian == IS_BIG_ENDIAN) return; … … 596 756 PInPersist::GetU2 (uint_2& result) 597 757 { 598 CheckTag(2 );758 CheckTag(2,PPS_DATATYPE_UNSIGNED); 599 759 GetRawBytes(&result, sizeof(uint_2)); 600 760 if (bigEndian != IS_BIG_ENDIAN) … … 605 765 PInPersist::GetU2s (uint_2* tab, size_t n) 606 766 { 607 CheckArrayTag(2,n );767 CheckArrayTag(2,n,PPS_DATATYPE_UNSIGNED); 608 768 GetRawBytes(tab, n*sizeof(uint_2)); 609 769 if (bigEndian == IS_BIG_ENDIAN) return; … … 618 778 PInPersist::GetI4 (int_4& result) 619 779 { 620 CheckTag(4 );780 CheckTag(4,PPS_DATATYPE_INTEGER); 621 781 GetRawBytes(&result, sizeof(int_4)); 622 782 if (bigEndian != IS_BIG_ENDIAN) … … 627 787 PInPersist::GetI4s (int_4* tab, size_t n) 628 788 { 629 CheckArrayTag(4,n );789 CheckArrayTag(4,n,PPS_DATATYPE_INTEGER); 630 790 GetRawBytes(tab, n*sizeof(int_4)); 631 791 if (bigEndian == IS_BIG_ENDIAN) return; … … 640 800 PInPersist::GetU4 (uint_4& result) 641 801 { 642 CheckTag(4 );802 CheckTag(4,PPS_DATATYPE_UNSIGNED); 643 803 GetRawBytes(&result, sizeof(uint_4)); 644 804 if (bigEndian != IS_BIG_ENDIAN) … … 649 809 PInPersist::GetU4s (uint_4* tab, size_t n) 650 810 { 651 CheckArrayTag(4,n );811 CheckArrayTag(4,n,PPS_DATATYPE_UNSIGNED); 652 812 GetRawBytes(tab, n*sizeof(uint_4)); 653 813 if (bigEndian == IS_BIG_ENDIAN) return; … … 663 823 PInPersist::GetI8 (int_8& result) 664 824 { 665 CheckTag(8 );825 CheckTag(8,PPS_DATATYPE_INTEGER); 666 826 GetRawBytes(&result, sizeof(int_8)); 667 827 if (bigEndian != IS_BIG_ENDIAN) … … 672 832 PInPersist::GetI8s (int_8* tab, size_t n) 673 833 { 674 CheckArrayTag(8,n );834 CheckArrayTag(8,n,PPS_DATATYPE_INTEGER); 675 835 GetRawBytes(tab, n*sizeof(int_8)); 676 836 if (bigEndian == IS_BIG_ENDIAN) return; … … 685 845 PInPersist::GetU8 (uint_8& result) 686 846 { 687 CheckTag(8 );847 CheckTag(8,PPS_DATATYPE_UNSIGNED); 688 848 GetRawBytes(&result, sizeof(uint_8)); 689 849 if (bigEndian != IS_BIG_ENDIAN) … … 694 854 PInPersist::GetU8s (uint_8* tab, size_t n) 695 855 { 696 CheckArrayTag(8,n );856 CheckArrayTag(8,n,PPS_DATATYPE_UNSIGNED); 697 857 GetRawBytes(tab, n*sizeof(uint_8)); 698 858 if (bigEndian == IS_BIG_ENDIAN) return; … … 708 868 PInPersist::GetLine(char* ptr, size_t len) 709 869 { 710 char ppstype; 711 GetTypeTag(ppstype); 712 if (ppstype != PPS_LINE) 713 throw FileFormatExc("PInPersist::GetLine bad type in ppersist file"); 714 s->getline(ptr, len, '\n'); 870 string str; 871 GetStr(str); 872 strncpy(ptr, str.c_str(), len); 873 ptr[len] = '\0'; 715 874 } 716 875 … … 718 877 PInPersist::GetStr(string& str) 719 878 { 720 char ppstype;879 unsigned char ppstype; 721 880 GetTypeTag(ppstype); 722 881 if (ppstype != PPS_STRING) 723 throw FileFormatExc("PInPersist::Get Linebad type in ppersist file");724 int_ 2len;725 GetRawI 2(len);882 throw FileFormatExc("PInPersist::GetStr bad type in ppersist file"); 883 int_4 len; 884 GetRawI4(len); 726 885 char * buff = new char[len+1]; 727 886 GetRawBytes(buff, len); … … 731 890 } 732 891 892 733 893 PPersist* 734 894 PInPersist::ReadObject() 735 895 { 896 return(GetPPObject()); 897 } 898 899 void 900 PInPersist::GetObject(AnyDataObj & o) 901 { 902 GetPPObject(&o); 903 return; 904 } 905 906 void 907 PInPersist::GetObject(AnyDataObj & o, string tagname) 908 { 909 GotoTag(tagname); 910 GetPPObject(&o); 911 return; 912 } 913 914 PPersist* 915 PInPersist::GetPPObject(AnyDataObj * po) 916 { 736 917 // Get tag 737 char ppstype;918 unsigned char ppstype; 738 919 GetTypeTag(ppstype); 739 920 if (ppstype != PPS_OBJECT && ppstype != PPS_REFERENCE && ppstype != PPS_NULL) { … … 747 928 uint_8 classId; 748 929 GetRawU8(classId); 930 uint_8 oid,oid2; 931 GetRawU8(oid); 749 932 750 933 // Get factory method … … 756 939 // Create object 757 940 PPersist* object = f(); 941 // If a DataObject was specified , we assign it to the PPersistObject 942 if (po != NULL) object->SetDataObj(*po); 943 758 944 object->ReadSelf(*this); 759 assignObjectId(object); 945 unsigned char ppstag; 946 // Read the ENDOBJECT 947 GetRawUByte(ppstag); 948 if (ppstag != PPS_ENDOBJECT) 949 throw FileFormatExc("PInPersist::ReadObject No PPS_ENDOBJECT tag"); 950 GetRawU8(oid2); 951 if (oid2 != oid) 952 throw FileFormatExc("PInPersist::ReadObject Inconsistent PPS-OId at PPS_ENDOBJECT "); 953 954 KeepOId(oid, *object); 760 955 return object; 761 } else { 762 // Get object id 763 int_4 id; 764 GetRawI4(id); 765 if (id <0 || id>=objList.size()) { 766 char msg[200]; 767 sprintf(msg, "PInPersist::ReadObject invalid object id for reference: %d/%d",id,objList.size()); 768 throw FileFormatExc(msg); 956 } 957 else if (ppstype == PPS_REFERENCE) 958 return ReadReference(); 959 960 else throw FileFormatExc("PInPersist::ReadObject invalide Tag Type !"); 961 } 962 963 964 void 965 PInPersist::AnalyseTags(int lev) 966 { 967 unsigned char ppstag=0; 968 unsigned char ppst1,ppst2,ppst3; 969 uint_8 cpos,fsize; 970 uint_8 ui8,cid,oid; 971 int_4 i4; 972 int_8 i8; 973 char * buff; 974 string str; 975 976 cout << "\n ---------------------------------------------------------- " << endl; 977 cout << " PInPersist::AnalyseTags(Level= " << lev << ")" << endl; 978 979 980 #ifdef STREAMPOS_IS_CLASS 981 cpos = s->tellg().offset(); 982 #else 983 cpos = s->tellg(); 984 #endif 985 s->seekg(0,ios::end); 986 #ifdef STREAMPOS_IS_CLASS 987 fsize = s->tellg().offset(); 988 #else 989 fsize = s->tellg(); 990 #endif 991 s->seekg(cpos,ios::beg); 992 993 cout << " Version= " << Version() << " FileSize= " << fsize 994 << " Creation Date= " << CreationDateStr() << endl; 995 996 uint_8 totntags = 0; 997 bool eofok = false; 998 999 while ( (ppstag != PPS_EOF) && (cpos < fsize) ) { 1000 #ifdef STREAMPOS_IS_CLASS 1001 cpos = s->tellg().offset(); 1002 #else 1003 cpos = s->tellg(); 1004 #endif 1005 GetRawUByte(ppstag); 1006 totntags++; 1007 1008 ppst1 = ppstag&0x0f; // bits 0123 1009 ppst2 = ppstag&0x30; // bits 45 1010 ppst3 = ppstag&0xc0; // bits 67 1011 if ((ppst2 == 0) && (ppst3 == 0) ) { 1012 switch (ppst1) { 1013 1014 case PPS_NULL : 1015 if (lev > 1) cout << "<PPS_NULL> tag at position " << hex << cpos << dec << endl; 1016 break; 1017 1018 case PPS_STRING : 1019 GetRawI4(i4); 1020 if (lev > 1) cout << "<PPS_STRING> tag at position " << hex << cpos << dec 1021 << " Length=" << i4 << endl; 1022 s->seekg(i4,ios::cur); 1023 break; 1024 1025 case PPS_OBJECT : 1026 GetRawU8(cid); 1027 GetRawU8(oid); 1028 cout << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid 1029 << " ObjectId= " << oid << dec << endl; 1030 break; 1031 1032 case PPS_REFERENCE : 1033 GetRawU8(oid); 1034 GetRawI8(i8); 1035 cout << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= " 1036 << oid << " OrigPos=" << i8 << dec << endl; 1037 break; 1038 1039 case PPS_TAG_MARK : 1040 cout << "<PPS_TAG_MARK> tag at position " << hex << cpos << dec << endl; 1041 break; 1042 1043 case PPS_ENDOBJECT : 1044 GetRawU8(oid); 1045 cout << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= " 1046 << oid << dec << endl; 1047 break; 1048 1049 case PPS_TAG : 1050 GetRawI8(i8); 1051 GetRawI4(i4); 1052 buff = new char[i4+1]; 1053 GetRawBytes(buff, i4); 1054 buff[i4] = '\0'; str = buff; 1055 delete[] buff; 1056 cout << "<PPS_TAG> tag at position " << hex << cpos << dec 1057 << " Name= " << str << endl; 1058 break; 1059 1060 case PPS_EOF : 1061 GetRawI8(i8); 1062 cout << "<PPS_EOF> tag at position " << hex << cpos 1063 << " TagPos=" << i8 << dec << endl; 1064 eofok = true; 1065 break; 1066 1067 default : 1068 cerr << " ERROR : Unexpected tag value " << hex << ppstag 1069 << " At position" << cpos << dec << endl; 1070 throw FileFormatExc("PInPersist::AnalyseTags() - Unexpected tag value !"); 1071 } 769 1072 } 770 return objList[id]; 771 } 772 } 773 774 int_4 775 PInPersist::assignObjectId(PPersist* x) 776 { 777 objList.push_back(x); 778 return objList.size()-1; 1073 else { 1074 string dtype = "???? x"; 1075 if (ppst3 == PPS_DATATYPE_CHAR) dtype = "CHAR x"; 1076 else if (ppst3 == PPS_DATATYPE_FLOAT) dtype = "FLOAT x"; 1077 else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x"; 1078 else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x"; 1079 int_4 dsize = ppst1; 1080 char sb[16]; 1081 sprintf(sb, "%d", dsize); 1082 dtype += sb; 1083 1084 switch (ppst2) { 1085 1086 case PPS_SIMPLE : 1087 if (lev > 2) cout << "<PPS_SIMPLE> tag at position " << hex << cpos << dec 1088 << " DataType=" << dtype << endl; 1089 s->seekg(dsize, ios::cur); 1090 break; 1091 1092 case PPS_SIMPLE_ARRAY4 : 1093 GetRawI4(i4); 1094 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY4> tag at position " << hex << cpos << dec 1095 << " DataType=" << dtype << " NElts= " << i4 << endl; 1096 s->seekg((uint_8)dsize*(uint_8)i4, ios::cur); 1097 break; 1098 1099 case PPS_SIMPLE_ARRAY8 : 1100 GetRawU8(ui8); 1101 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY8> tag at position " << hex << cpos << dec 1102 << " DataType=" << dtype << " NElts= " << ui8 << endl; 1103 s->seekg((uint_8)dsize*ui8, ios::cur); 1104 break; 1105 } 1106 } 1107 } 1108 if (!eofok) 1109 throw FileFormatExc("PInPersist::AnalyseTags() - Not found <PPS_EOF> tag "); 1110 1111 cout << " PInPersist::AnalyseTags() - End - Total Number of Tags= " << totntags << endl; 1112 cout << " ---------------------------------------------------------- \n" << endl; 1113 return; 1114 } 1115 1116 void 1117 PInPersist::ReadReference(PPersist & ppo) 1118 { 1119 PPersist * pr = ReadReference(); 1120 ppo.ShareDataReference(*pr); 1121 } 1122 1123 1124 PPersist * 1125 PInPersist::ReadReference() 1126 { 1127 uint_8 oid; 1128 int_8 pos; 1129 GetRawU8(oid); 1130 GetRawI8(pos); 1131 // cerr << " DBG - PInPersist::ReadReference-A " << oid << " Pos= " << pos << endl; 1132 map<uint_8, PPersist *>::iterator i = objList.find(oid); 1133 if (i != objList.end()) return (*i).second; 1134 else { // We may have skeeped it ! 1135 // Let's try to read it 1136 int_8 cpos; 1137 #ifdef STREAMPOS_IS_CLASS 1138 cpos = s->tellg().offset(); 1139 #else 1140 cpos = s->tellg(); 1141 #endif 1142 s->seekg(pos); 1143 PPersist* ppo = ReadObject(); 1144 s->seekg(cpos); 1145 delete ppo; 1146 // cerr << " DBG - PInPersist::ReadReference-B ... " << endl; 1147 1148 map<uint_8, PPersist *>::iterator i2 = objList.find(oid); 1149 if (i2 == objList.end()) 1150 throw FileFormatExc("PInPersist::ReadReference() Not found PPS_OId "); 1151 return (*i2).second; 1152 } 1153 } 1154 1155 1156 void 1157 PInPersist::KeepOId(uint_8 oid, PPersist & ppo) 1158 { 1159 if ((oid&0x1) == 0) return; // This is not an object which can be referenced 1160 // cerr << " DBG - PInPersist::KeepOId() " << oid << endl; 1161 if ((objList.size() > 0) && (objList.find(oid) != objList.end()) ) { 1162 // Ceci ne devrait arriver que si on lit dans le desordre (avec GotoTag) 1163 // et pas avec une lecture sequentielle ... Reza 03/2000 1164 // cerr << "PInPersist::KeepOId()/Warning - already present PPS_ObjectId ! " << oid << endl; 1165 if (seqread) throw FileFormatExc("PInPersist::KeepOId() already present PPS_ObjectId "); 1166 PPersist *pp = (*objList.find(oid)).second; 1167 ppo.ShareDataReference(*pp); 1168 } 1169 else { 1170 PPersist * npp = ppo.CloneSharedReference(); 1171 if (npp == NULL) throw PError("PInPersist::KeepOId() NULL returned by PPersist.Clone() ! "); 1172 objList[oid] = npp; 1173 } 1174 return; 779 1175 } 780 1176 … … 803 1199 bigEndian = endianness; 804 1200 1201 // PPS (POutPersist stream) Object Id initialisation 1202 pps_OId = 0; 805 1203 // Output stream creation 806 1204 s = new ofstream(flnm.c_str(),ios::out | IOS_BIN); 807 1205 808 1206 // Header 809 PutRawBytes("SOS-SOPHYA-PPersistFile V 1",32);1207 PutRawBytes("SOS-SOPHYA-PPersistFile V2 ",32); 810 1208 PutRawBytes(bigEndian 811 1209 ? "BIG-ENDIAN " … … 819 1217 datestring[32] = '\0'; 820 1218 PutRawBytes(datestring, 32); 1219 filename = flnm; 821 1220 } 822 1221 … … 824 1223 { 825 1224 if (tags.size() == 0) { 826 PutRaw Byte(PPS_EOF);1225 PutRawUByte(PPS_EOF); 827 1226 PutRawI8(-1); 828 1227 } else { … … 836 1235 string name = (*i).first; 837 1236 int_8 pos = (*i).second; 838 PutRaw Byte(PPS_TAG); // This is a tag1237 PutRawUByte(PPS_TAG); // This is a tag 839 1238 PutRawI8(pos); // position of previous tag 840 1239 PutRawI4(name.length()); // length of the name 841 1240 PutRawBytes(name.c_str(), name.length()); // name, without final "0". 842 1241 } 843 PutRaw Byte(PPS_EOF);1242 PutRawUByte(PPS_EOF); 844 1243 PutRawI8(tagPos); 845 1244 } … … 868 1267 869 1268 tags[name] = tagPos; 870 PutRaw Byte(PPS_TAG_MARK); // This is a tag871 objList.clear(); // $CHECK$ EA 1711991269 PutRawUByte(PPS_TAG_MARK); // This is a tag 1270 // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000 872 1271 } 873 1272 … … 914 1313 915 1314 void 1315 POutPersist::PutRawUByte(unsigned char c) 1316 { 1317 PutRawBytes(&c, 1); 1318 } 1319 1320 void 916 1321 POutPersist::PutRawI2 (int_2 val) 917 1322 { … … 950 1355 951 1356 void 952 POutPersist::PutArrayTag(short datasz, size_t sz) 953 { 954 if (sz <= 0x7fff) { 955 PutRawByte(PPS_SIMPLE_ARRAY + datasz); 956 PutRawI2(sz); 957 } else if (sz <= 0x7fffffff) { 958 PutRawByte(PPS_SIMPLE_ARRAY4 + datasz); 1357 POutPersist::PutArrayTag(short datasz, size_t sz, short datatype) 1358 // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED 1359 { 1360 if (sz <= 0x7fffffff) { 1361 PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype); 959 1362 PutRawI4(sz); 960 1363 } else { 961 PutRaw Byte(PPS_SIMPLE_ARRAY8 + datasz);1364 PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype); 962 1365 PutRawU8(sz); 963 1366 } … … 967 1370 POutPersist::PutByte(char c) 968 1371 { 969 PutRawByte(PPS_SIMPLE + 1 );1372 PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR); 970 1373 PutRawBytes(&c, 1); 971 1374 } … … 976 1379 POutPersist::PutBytes(void const* ptr, size_t bytes) 977 1380 { 978 PutArrayTag(1, bytes );1381 PutArrayTag(1, bytes, PPS_DATATYPE_CHAR); 979 1382 PutRawBytes(ptr, bytes); 980 1383 } … … 983 1386 POutPersist::PutR4 (r_4 val) 984 1387 { 985 PutRaw Byte(PPS_SIMPLE + 4);1388 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT); 986 1389 987 1390 if (bigEndian != IS_BIG_ENDIAN) … … 994 1397 POutPersist::PutR4s (r_4 const* tab, size_t n) 995 1398 { 996 PutArrayTag(4, n );1399 PutArrayTag(4, n, PPS_DATATYPE_FLOAT); 997 1400 998 1401 if (bigEndian == IS_BIG_ENDIAN) { … … 1010 1413 POutPersist::PutR8 (r_8 val) 1011 1414 { 1012 PutRaw Byte(PPS_SIMPLE + 8);1415 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT); 1013 1416 1014 1417 if (bigEndian != IS_BIG_ENDIAN) … … 1021 1424 POutPersist::PutR8s (r_8 const* tab, size_t n) 1022 1425 { 1023 PutArrayTag(8, n );1426 PutArrayTag(8, n, PPS_DATATYPE_FLOAT); 1024 1427 1025 1428 if (bigEndian == IS_BIG_ENDIAN) { … … 1037 1440 POutPersist::PutI2 (int_2 val) 1038 1441 { 1039 PutRaw Byte(PPS_SIMPLE + 2);1442 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER); 1040 1443 1041 1444 if (bigEndian != IS_BIG_ENDIAN) … … 1048 1451 POutPersist::PutI2s (int_2 const* tab, size_t n) 1049 1452 { 1050 PutArrayTag(2, n );1453 PutArrayTag(2, n, PPS_DATATYPE_INTEGER); 1051 1454 1052 1455 if (bigEndian == IS_BIG_ENDIAN) { … … 1064 1467 POutPersist::PutU2 (uint_2 val) 1065 1468 { 1066 PutRaw Byte(PPS_SIMPLE + 2);1469 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED); 1067 1470 1068 1471 if (bigEndian != IS_BIG_ENDIAN) … … 1075 1478 POutPersist::PutU2s (uint_2 const* tab, size_t n) 1076 1479 { 1077 PutArrayTag(2, n );1480 PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED); 1078 1481 1079 1482 if (bigEndian == IS_BIG_ENDIAN) { … … 1091 1494 POutPersist::PutI4 (int_4 val) 1092 1495 { 1093 PutRaw Byte(PPS_SIMPLE + 4);1496 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER); 1094 1497 1095 1498 if (bigEndian != IS_BIG_ENDIAN) … … 1102 1505 POutPersist::PutI4s (int_4 const* tab, size_t n) 1103 1506 { 1104 PutArrayTag(4, n );1507 PutArrayTag(4, n, PPS_DATATYPE_INTEGER); 1105 1508 1106 1509 if (bigEndian == IS_BIG_ENDIAN) { … … 1118 1521 POutPersist::PutU4 (uint_4 val) 1119 1522 { 1120 PutRaw Byte(PPS_SIMPLE + 4);1523 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED); 1121 1524 1122 1525 if (bigEndian != IS_BIG_ENDIAN) … … 1129 1532 POutPersist::PutU4s (uint_4 const* tab, size_t n) 1130 1533 { 1131 PutArrayTag(4, n );1534 PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED); 1132 1535 1133 1536 if (bigEndian == IS_BIG_ENDIAN) { … … 1145 1548 POutPersist::PutI8 (int_8 val) 1146 1549 { 1147 PutRaw Byte(PPS_SIMPLE + 8);1550 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER); 1148 1551 1149 1552 if (bigEndian != IS_BIG_ENDIAN) … … 1156 1559 POutPersist::PutI8s (int_8 const* tab, size_t n) 1157 1560 { 1158 PutArrayTag(8, n );1561 PutArrayTag(8, n, PPS_DATATYPE_INTEGER); 1159 1562 1160 1563 if (bigEndian == IS_BIG_ENDIAN) { … … 1172 1575 POutPersist::PutU8 (uint_8 val) 1173 1576 { 1174 PutRaw Byte(PPS_SIMPLE + 8);1577 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED); 1175 1578 1176 1579 if (bigEndian != IS_BIG_ENDIAN) … … 1183 1586 POutPersist::PutU8s (uint_8 const* tab, size_t n) 1184 1587 { 1185 PutArrayTag(8, n );1588 PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED); 1186 1589 1187 1590 if (bigEndian == IS_BIG_ENDIAN) { … … 1199 1602 POutPersist::PutStr(string const& str) 1200 1603 { 1201 PutRaw Byte(PPS_STRING);1202 PutRawI 2(str.length());1604 PutRawUByte(PPS_STRING); 1605 PutRawI4(str.length()); 1203 1606 PutRawBytes(str.c_str(), str.length()); 1204 1607 } … … 1207 1610 POutPersist::PutLine(char const* ptr, size_t len) 1208 1611 { 1209 PutRawByte(PPS_LINE); 1210 1211 if (len == 0) len = strlen(ptr); 1212 PutRawBytes(ptr, len); 1213 PutRawByte('\n'); 1214 } 1215 1216 void 1217 POutPersist::PutObject(PPersist const* obj) 1218 { 1219 if (serializeNullAndRepeat(obj)) return; 1220 1221 PutRawByte(PPS_OBJECT); 1222 PutRawU8(getTypeId(typeid(*obj).name())); 1223 //PutRawU8(PIOPersist::Hash(typeid(*obj).name())); 1224 assignObjectId(obj); 1612 string str = ptr; 1613 PutStr(str); 1614 } 1615 1616 1617 void 1618 POutPersist::PutObject(AnyDataObj & o) 1619 { 1620 ClassCreatorFunc f = FindCreatorFunc(getDataObjClassId(o)); 1621 if (!f) 1622 throw NotFoundExc("PInPersist::PutObject() class not registered"); 1623 PPersist* ppo = f(); 1624 ppo->SetDataObj(o); 1625 PutPPObject(ppo); 1626 } 1627 1628 void 1629 POutPersist::PutObject(AnyDataObj & o, string tagname) 1630 { 1631 WriteTag(tagname); 1632 PutObject(o); 1633 } 1634 1635 1636 void 1637 POutPersist::PutPPObject(PPersist const* obj) 1638 { 1639 if (serializeNullAndRepeat(obj)) return; // NULL object or already written in stream 1640 1641 // We have to write the object 1642 uint_8 oid = assignObjectId(obj); // We assing a PPS Object Id 1643 PutRawUByte(PPS_OBJECT); // We write the Object Tag 1644 PutRawU8(getPPClassId(*obj)); // Writing the PPersist ClassId 1645 PutRawU8(oid); // Write the PPS Object Id 1225 1646 obj->WriteSelf(*this); 1647 PutRawUByte(PPS_ENDOBJECT); // We write the End-Of-Object Tag 1648 PutRawU8(oid); // and again its PPS Object Id 1226 1649 } 1227 1650 … … 1230 1653 { 1231 1654 if (x == NULL) { 1232 PutRaw Byte(PPS_NULL);1655 PutRawUByte(PPS_NULL); 1233 1656 return true; 1234 1657 } 1235 1658 1236 int_4 id = findObjectId(x); 1237 if (id >= 0) { 1238 PutRawByte(PPS_REFERENCE); 1239 PutRawI4(id); 1659 int_8 pos; 1660 uint_8 id = findObjectId(x, pos); 1661 if (id > 0) { 1662 PutRawUByte(PPS_REFERENCE); 1663 PutRawU8(id); // Writing the corresponding object Id 1664 PutRawI8(pos); // The original object position 1240 1665 return true; 1241 1666 } 1242 1667 1243 return false; 1244 } 1245 1246 int_41668 return false; // Object have to be written in stream ... 1669 } 1670 1671 uint_8 1247 1672 POutPersist::assignObjectId(PPersist const* x) 1248 1673 { 1249 int_4 id = objList.size(); 1250 objList[x] = id; 1674 pps_OId += 16; // We keep the three first bytes for future usage 1675 // Bit 1 non zero -> Object can be referenced 1676 uint_8 id = pps_OId; 1677 uint_8 mid = x->getMemOId(); 1678 if (mid > 0) { 1679 int_8 pos; 1680 if (findObjectId(x,pos) > 0) 1681 throw PError("POutPersist::assignObjectId() Error - Already serialized object ! "); 1682 id += 1; // Object Id starting at zero 1683 objreftag rt; 1684 rt.ppsoid = id; 1685 #ifdef STREAMPOS_IS_CLASS 1686 rt.ppspos = s->tellp().offset(); 1687 #else 1688 rt.ppspos = s->tellp(); 1689 #endif 1690 objList[mid] = rt; 1691 } 1251 1692 return id; 1252 1693 } 1253 1694 1254 int_4 1255 POutPersist::findObjectId(PPersist const* x) 1256 { 1257 ObjList::iterator i = objList.find(x); 1258 if (i == objList.end()) return -1; 1259 return (*i).second; 1260 } 1261 1262 1695 uint_8 1696 POutPersist::findObjectId(PPersist const* x, int_8 & pos) 1697 { 1698 pos = -1; 1699 uint_8 mid = x->getMemOId(); 1700 if (mid == 0) return(0); 1701 ObjList::iterator i = objList.find(mid); 1702 if (i == objList.end()) return 0; 1703 pos = (*i).second.ppspos; 1704 return (*i).second.ppsoid; 1705 } 1706 1707 -
trunk/SophyaLib/BaseTools/ppersist.h
r754 r802 7 7 // 8 8 // E. Aubourg CEA DAPNIA/SPP 1999 9 // R. Ansari LAL IN2P3/CNRS 03/2000 9 10 10 11 … … 51 52 52 53 virtual AnyDataObj* DataObj()=0; // Retourne l'objet reel 53 virtual void SetDataObj(AnyDataObj &)=0; 54 54 virtual void SetDataObj(AnyDataObj &)=0; 55 56 virtual uint_8 getMemOId() const ; // Renvoie l'identificateur de l'objet - par defaut=0 57 // Ces deux methodes doivent etre redefinies si getMemOId() renvoie non nul (>0) 58 virtual void ShareDataReference(PPersist &); 59 virtual PPersist* CloneSharedReference(); 60 // doit etre surcharge pour renvoyer un mem-oid correct 55 61 protected: 56 62 virtual void ReadSelf(PInPersist&)=0; 57 virtual void WriteSelf(POutPersist&) const=0; 63 virtual void WriteSelf(POutPersist&) const=0; 58 64 59 65 friend class PInPersist; … … 71 77 typedef PPersist* (*ClassCreatorFunc)(); 72 78 73 static void RegisterClass(uint_8 classId, string typname, ClassCreatorFunc f); 79 static void RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f); 80 static void RegisterDataObjClass(uint_8 classId, string class_name); 81 74 82 static ClassCreatorFunc FindCreatorFunc(uint_8 classId); 83 84 static string getPPClassName(uint_8 classId); 85 static uint_8 getPPClassId(string const & typ_name); 86 static uint_8 getPPClassId(PPersist const & ppo); 87 static string getDataObjClassName(uint_8 classId); 88 static uint_8 getDataObjClassId(string const & typ_name); 89 static uint_8 getDataObjClassId(AnyDataObj const & o); 90 75 91 static uint_8 Hash(string const& typname) { 76 92 md5_init(&ctx); … … 82 98 83 99 static void Initialize(); // Pour initialiser classList 84 static uint_8 getTypeId(string s) {return (*typeids)[s];} 100 101 string FileName() { return filename; } // Retourne le nom de fichier 102 85 103 private: 86 104 87 105 typedef map<uint_8, ClassCreatorFunc, less<uint_8> > ClassList; 88 106 // Pas de createur appele pour objets statiques sur Linux - $CHECK$ Reza 26/04/99 89 static ClassList * classList; 90 static map<string, uint_8> * typeids; 107 static ClassList * ppclassList; // PPersist class list 108 static map<string, uint_8> * ppclassNameList; // PPersist classId = f(PPersistClassName) 109 static map<string, uint_8> * dobjclassNameList; // PPersist classId = f(DataObjClassName) 91 110 92 111 protected: … … 98 117 PPS_TAG = 4, // tag entries 99 118 PPS_EOF = 5, // Just before tag infomation, offset to PPS_TAG 100 PPS_ LINE = 6, // '\n'-terminated, deprecated ?101 PPS_TAG_MARK = 7, // to reset object count when skipped119 PPS_ENDOBJECT = 6, // marks the end of a given object information 120 PPS_TAG_MARK = 7, // To have a name tag, position marker in a file 102 121 PPS_SIMPLE = 16, // 16 + number of bytes, up to 8 bytes 103 PPS_SIMPLE_ARRAY = 32, // 32 + number of bytes, up to 8 bytes, then 2 bytes of length 104 PPS_SIMPLE_ARRAY4 = 64, // 64 + number of bytes, up to 8 bytes, then 4 bytes of length 105 PPS_SIMPLE_ARRAY8 = 128 // 64 + number of bytes, up to 8 bytes, then 8 bytes of length 122 PPS_SIMPLE_ARRAY4 = 32, // 32 + number of bytes, up to 8 bytes, then 4 bytes of length 123 PPS_SIMPLE_ARRAY8 = 48 // 48 + number of bytes, up to 8 bytes, then 8 bytes of length 124 }; 125 // The following values are used PPS_SIMPLE and PPS_SIMPLE_ARRAY (Using OR) 126 enum {PPS_DATATYPE_CHAR = 0, // 0 : DataType=character 127 PPS_DATATYPE_FLOAT = 64, // 64 : DataType=float 128 PPS_DATATYPE_INTEGER = 128, // 128 :DataType=integer 129 PPS_DATATYPE_UNSIGNED = 192 // 192 :DataType=Unsigned integer 106 130 }; 107 131 108 132 map<string, int_8> tags; 133 string filename; 109 134 }; 110 135 … … 121 146 bool GotoTagNum(int itag); // 0..NbTags-1 122 147 string GetTagName(int itag); // 0..NbTags-1 148 string GetTagClassName(int itag); // 0..NbTags-1 123 149 vector<string> const & GetTagNames(); 124 150 … … 163 189 void Get(string& x) {GetStr(x);} 164 190 191 // Object Reading 165 192 PPersist* ReadObject(); 166 193 void GetObject(AnyDataObj & o); 194 void GetObject(AnyDataObj & o, string tagname); 195 PPersist* GetPPObject(AnyDataObj * po=NULL); 167 196 168 197 int Version() {return version;} 169 198 time_t CreationDate() { return creationdate; } 199 string CreationDateStr(); 200 201 void AnalyseTags(int lev=0); // List (all or some) tags ... 202 203 // Reza 03/2000 204 // Methodes qui pourraient etre protected, mais doivent etre utilisables par PPersist 205 void ReadReference(PPersist & ppo); // Fill the ppo object from the reference tag 206 PPersist * ReadReference(); // Creates object from the reference tag 207 void KeepOId(uint_8 oid, PPersist & ppo); // Keeps the ppo in the objList 170 208 171 209 protected: 172 void CheckTag (short datasz );173 void CheckArrayTag(short datasz, size_t sz );174 void GetTypeTag ( char& c);210 void CheckTag (short datasz, short datatype); 211 void CheckArrayTag(short datasz, size_t sz, short datatype); 212 void GetTypeTag (unsigned char& c); 175 213 void GetRawByte (char& c); 214 void GetRawUByte (unsigned char& c); 176 215 void GetRawBytes(void* ptr, size_t bytes); 177 216 void GetRawI2 (int_2&); … … 179 218 void GetRawI8 (int_8&); 180 219 void GetRawU8 (uint_8&); 181 void GetObject(PPersist*); // Object has been allocated with correct type 182 int_4 assignObjectId(PPersist* x); 220 183 221 void Scan(); 184 char* GetCStr(uint_2 l);185 222 186 223 istream* s; … … 191 228 time_t creationdate; 192 229 193 // already read objects , id = order in array194 typedef vector<PPersist*> ObjList;230 // already read objects 231 typedef map<uint_8, PPersist * > ObjList; 195 232 ObjList objList; 196 233 // Si on a fait une lecture non sequentielle -> seqread = false 234 bool seqread; 197 235 friend class PPersist; 198 236 }; … … 225 263 void PutLine (char const* ptr, size_t len=0); // deprecated ? 226 264 void PutStr (string const&); 227 void PutObject (PPersist const*); // Like doing Write(stream) on object 265 void PutPPObject (PPersist const*); // Like doing Write(stream) on PPersist object 266 267 void PutObject(AnyDataObj & o); // Creates the corresponding PPersist Object and call Write() 268 void PutObject(AnyDataObj & o, string tagname); 228 269 229 270 void Put(char c) {PutByte(c);} … … 245 286 void Put(int_8 const* x, size_t n) {PutI8s(x,n);} 246 287 void Put(string const& s) {PutStr(s);} 247 void Put(PPersist const* x) {Put Object(x);}288 void Put(PPersist const* x) {PutPPObject(x);} 248 289 249 290 … … 252 293 bool bigEndian; 253 294 254 void PutArrayTag(short datasz, size_t sz );295 void PutArrayTag(short datasz, size_t sz, short datatype); 255 296 void PutRawByte (char); 297 void PutRawUByte (unsigned char); 256 298 void PutRawI2 (int_2); 257 299 void PutRawI4 (int_4); … … 260 302 void PutRawBytes(void const* ptr, size_t bytes); 261 303 bool serializeNullAndRepeat(PPersist const* x); 262 int_4 findObjectId(PPersist const* x); 263 int_4 assignObjectId(PPersist const* x); 264 265 // already serialized objects 266 typedef map<PPersist const*, int_4, less<PPersist const*> > ObjList; 304 uint_8 findObjectId(PPersist const* x, int_8 & pos); 305 uint_8 assignObjectId(PPersist const* x); 306 307 // objreftag contains the assigned POutStream Object Id and the stream position 308 // of the original written object 309 typedef struct { uint_8 ppsoid; int_8 ppspos; } objreftag; 310 // already serialized objects are kept in a map as a function of the Objects memory Id 311 typedef map<uint_8, objreftag, less<uint_8> > ObjList; 267 312 ObjList objList; 313 uint_8 pps_OId; // PPS Object Id 268 314 }; 269 315 … … 306 352 #endif 307 353 308 inline POutPersist& operator << (POutPersist& c, PPersist const& obj) 309 { 310 obj.Write(c); 311 return c; 312 } 313 314 inline PInPersist& operator >> (PInPersist& c, PPersist& obj) 315 { 316 obj.Read(c); 317 return c; 318 } 319 354 355 // --- Cela risque d'etre dangereux --- On le laisse au niveau des DataObj 356 // Reza 24/3/2000 357 // inline POutPersist& operator << (POutPersist& c, PPersist const& obj) 358 // { 359 // obj.Write(c); 360 // return c; 361 // } 362 363 // inline PInPersist& operator >> (PInPersist& c, PPersist& obj) 364 // { 365 // obj.Read(c); 366 // return c; 367 // } 368 320 369 // Utility class to 321 370 // - compute the class ID from a MD5 hash of the class name … … 326 375 public: 327 376 static PPersist* Create() {return new T();} 328 static void Register(string id) { PIOPersist::RegisterClass(Hash(id),typeid(T).name(),Create);}377 static void Register(string id) { PIOPersist::RegisterPPHandlerClass(Hash(id), typeid(T).name(), Create); } 329 378 static uint_8 Hash(string id) { 330 379 return PIOPersist::Hash(id); … … 333 382 334 383 #define PPRegister(className) PPersistRegistrar<className>::Register(#className); 384 #define DObjRegister(ppclassName, className) PIOPersist::RegisterDataObjClass(PIOPersist::Hash(#ppclassName), typeid(className).name()); 335 385 336 386 } // namespace -
trunk/SophyaLib/BaseTools/sophyainit.cc
r754 r802 11 11 12 12 #include "ppersist.h" 13 #include " ndatablock.h"13 #include "fiondblock.h" 14 14 #include "dvlist.h" 15 15 … … 36 36 PIOPersist::Initialize(); 37 37 PPRegister(FIO_NDataBlock<uint_1>); 38 DObjRegister(FIO_NDataBlock<uint_1>, NDataBlock<uint_1>); 38 39 PPRegister(FIO_NDataBlock<uint_2>); 40 DObjRegister(FIO_NDataBlock<uint_2>, NDataBlock<uint_2>); 39 41 PPRegister(FIO_NDataBlock<int_2>); 42 DObjRegister(FIO_NDataBlock<int_2>, NDataBlock<int_2>); 40 43 PPRegister(FIO_NDataBlock<int_4>); 44 DObjRegister(FIO_NDataBlock<int_4>, NDataBlock<int_4>); 41 45 PPRegister(FIO_NDataBlock<int_8>); 46 DObjRegister(FIO_NDataBlock<int_8>, NDataBlock<int_8>); 42 47 PPRegister(FIO_NDataBlock<uint_4>); 48 DObjRegister(FIO_NDataBlock<uint_4>, NDataBlock<uint_4>); 43 49 PPRegister(FIO_NDataBlock<uint_8>); 50 DObjRegister(FIO_NDataBlock<uint_8>, NDataBlock<uint_8>); 44 51 PPRegister(FIO_NDataBlock<r_4>); 52 DObjRegister(FIO_NDataBlock<r_4>, NDataBlock<r_4>); 45 53 PPRegister(FIO_NDataBlock<r_8>); 46 PPRegister(FIO_NDataBlock< complex<float> >); 47 PPRegister(FIO_NDataBlock< complex<double> >); 54 DObjRegister(FIO_NDataBlock<r_8>, NDataBlock<r_8>); 55 PPRegister(FIO_NDataBlock< complex<r_4> >); 56 DObjRegister(FIO_NDataBlock< complex<r_4> >, NDataBlock< complex<r_4> >); 57 PPRegister(FIO_NDataBlock< complex<r_8> >); 58 DObjRegister(FIO_NDataBlock< complex<r_8> >, NDataBlock< complex<r_8> >); 48 59 49 60 PPRegister(ObjFileIO<DVList>); 61 DObjRegister(ObjFileIO<DVList>, DVList); 50 62 51 63
Note:
See TracChangeset
for help on using the changeset viewer.