[241] | 1 | #include "machdefs.h"
|
---|
[251] | 2 | #include <stdio.h>
|
---|
| 3 | #include <sys/types.h>
|
---|
| 4 | #include <time.h>
|
---|
[241] | 5 | #include "pexceptions.h"
|
---|
[219] | 6 | #include "ppersist.h"
|
---|
[802] | 7 | #include "anydataobj.h"
|
---|
[219] | 8 | #include <fstream.h>
|
---|
[661] | 9 | #include <iostream.h>
|
---|
[241] | 10 | #include <typeinfo>
|
---|
[219] | 11 |
|
---|
| 12 |
|
---|
| 13 | #ifdef __mac__
|
---|
| 14 | #include "unixmac.h"
|
---|
| 15 | #include <SIOUX.h>
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
[802] | 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 | }
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
[241] | 25 | #define MAXTAGLEN 255
|
---|
| 26 |
|
---|
[219] | 27 | //++
|
---|
[241] | 28 | // Class PIOPersist
|
---|
[219] | 29 | // Lib Outils++
|
---|
| 30 | // include ppersist.h
|
---|
| 31 | //
|
---|
[241] | 32 | // Root class for persistant files. Handles the registration of
|
---|
| 33 | // persistant classes
|
---|
[219] | 34 | //--
|
---|
| 35 |
|
---|
| 36 | //++
|
---|
[241] | 37 | // Links See
|
---|
[219] | 38 | // PPersist
|
---|
[241] | 39 | // PInPersist
|
---|
| 40 | // POutPersist
|
---|
[219] | 41 | //--
|
---|
| 42 |
|
---|
| 43 |
|
---|
[742] | 44 | MD5_CONTEXT PIOPersist::ctx;
|
---|
[802] | 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;
|
---|
[241] | 48 |
|
---|
[269] | 49 | //++
|
---|
| 50 | void
|
---|
| 51 | PIOPersist::Initialize()
|
---|
| 52 | // Initialisation globale (objets statiques) $CHECK$ Reza 26/04/99
|
---|
| 53 | //--
|
---|
| 54 | {
|
---|
[802] | 55 | ppclassList = new PIOPersist::ClassList;
|
---|
| 56 | ppclassNameList = new map<string, uint_8>;
|
---|
| 57 | dobjclassNameList = new map<string, uint_8>;
|
---|
[269] | 58 | }
|
---|
[241] | 59 |
|
---|
[219] | 60 | //++
|
---|
| 61 | void
|
---|
[802] | 62 | PIOPersist::RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f)
|
---|
[219] | 63 | //
|
---|
[802] | 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
|
---|
[219] | 68 | //
|
---|
| 69 | //--
|
---|
| 70 | {
|
---|
[802] | 71 | if (ppclassList->size() && (ppclassList->find(classId) != ppclassList->end()) ) {
|
---|
| 72 | cerr << "RegisterClass : Error, " << hex << classId << dec
|
---|
| 73 | << " already registered." << endl;
|
---|
[816] | 74 | throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass() Already registered (1)"));
|
---|
[802] | 75 | }
|
---|
| 76 | if (ppclassNameList->size() && (ppclassNameList->find(ppclass_name) != ppclassNameList->end())) {
|
---|
| 77 | cerr << "RegisterClass : Error (2) " << ppclass_name
|
---|
| 78 | << " already registered." << endl;
|
---|
[816] | 79 | throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass() Already registered(2)"));
|
---|
[219] | 80 | }
|
---|
| 81 |
|
---|
[802] | 82 | (*ppclassList)[classId] = f;
|
---|
| 83 | (*ppclassNameList)[ppclass_name] = classId;
|
---|
[219] | 84 | }
|
---|
| 85 |
|
---|
[802] | 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 | {
|
---|
[816] | 93 | if (ppclassList->find(classId) == ppclassList->end() ) {
|
---|
| 94 | cerr << "PIOPersist::RegisterDataObjClass() Error (1) "
|
---|
| 95 | << hex << classId << dec << " Not Found !" << endl;
|
---|
| 96 | throw( NotFoundExc("PIOPersist::RegisterDataObjClass() Not found classId ") );
|
---|
| 97 | }
|
---|
| 98 | if (dobjclassNameList->size() && (dobjclassNameList->find(class_name) != dobjclassNameList->end())) {
|
---|
| 99 | cerr << "PIOPersist::RegisterDataObjClass() Error (2)" << class_name
|
---|
| 100 | << " already registered." << endl;
|
---|
| 101 | throw(DuplicateIdExc("PIOPersist::RegisterDataObjClass() - Already registered"));
|
---|
| 102 | }
|
---|
[219] | 103 |
|
---|
[802] | 104 | (*dobjclassNameList)[class_name] = classId;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | // class_typename should be typeid(DataObject).name(), to be
|
---|
| 108 | // used by POutPersist::PutDataObject() methods.
|
---|
| 109 |
|
---|
[241] | 110 | PIOPersist::ClassCreatorFunc
|
---|
| 111 | PIOPersist::FindCreatorFunc(uint_8 classId)
|
---|
[802] | 112 | // Returns the PPersist class creator function for the specified classId
|
---|
[219] | 113 | {
|
---|
[802] | 114 | ClassList::iterator i = ppclassList->find(classId);
|
---|
| 115 | if (i == ppclassList->end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc() Not found classId"));
|
---|
[241] | 116 | return (*i).second;
|
---|
[219] | 117 | }
|
---|
| 118 |
|
---|
[802] | 119 | string
|
---|
| 120 | PIOPersist::getPPClassName(uint_8 classId)
|
---|
| 121 | // Returns the PPersist class name for the specified classId
|
---|
| 122 | {
|
---|
| 123 | map<string, uint_8>::iterator i;
|
---|
| 124 | for (i= ppclassNameList->begin(); i != ppclassNameList->end(); i++)
|
---|
| 125 | if ( (*i).second == classId ) return (*i).first;
|
---|
[219] | 126 |
|
---|
[802] | 127 | throw(NotFoundExc("PIOPersist::getPPClassName() Not found classId"));
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | uint_8
|
---|
| 131 | PIOPersist::getPPClassId(string const & typ_name)
|
---|
| 132 | // Returns the classId for the specified PPersist class type name
|
---|
| 133 | {
|
---|
| 134 | map<string, uint_8>::iterator i = ppclassNameList->find(typ_name);
|
---|
| 135 | if (i == ppclassNameList->end())
|
---|
| 136 | throw(NotFoundExc("PIOPersist::getPPClassId() Not found className"));
|
---|
| 137 | return (*i).second;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | uint_8
|
---|
| 141 | PIOPersist::getPPClassId(PPersist const & ppo)
|
---|
| 142 | // Returns the classId for the specified PPersist class
|
---|
| 143 | {
|
---|
| 144 | string typ_name = typeid(ppo).name() ;
|
---|
| 145 | return (getPPClassId(typ_name) );
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | string
|
---|
| 150 | PIOPersist::getDataObjClassName(uint_8 classId)
|
---|
| 151 | // Returns the PPersist class name for the specified classId
|
---|
| 152 | {
|
---|
| 153 | map<string, uint_8>::iterator i;
|
---|
| 154 | for (i= dobjclassNameList->begin(); i != dobjclassNameList->end(); i++)
|
---|
| 155 | if ( (*i).second == classId ) return (*i).first;
|
---|
| 156 |
|
---|
| 157 | throw(NotFoundExc("PIOPersist::getDataObjClassName() Not found classId"));
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | uint_8
|
---|
| 161 | PIOPersist::getDataObjClassId(string const & typ_name)
|
---|
| 162 | // Returns the classId for the specified PPersist class type name
|
---|
| 163 | {
|
---|
| 164 | map<string, uint_8>::iterator i = dobjclassNameList->find(typ_name);
|
---|
| 165 | if (i == dobjclassNameList->end())
|
---|
| 166 | throw(NotFoundExc("PIOPersist::getDataObjClassId() Not found className"));
|
---|
| 167 | return (*i).second;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | uint_8
|
---|
| 171 | PIOPersist::getDataObjClassId(AnyDataObj const & o)
|
---|
| 172 | // Returns the classId for the specified PPersist class
|
---|
| 173 | {
|
---|
| 174 | string typ_name = typeid(o).name() ;
|
---|
| 175 | return (getDataObjClassId(typ_name) );
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[219] | 179 | //++
|
---|
| 180 | // Class PPersist
|
---|
| 181 | // Lib Outils++
|
---|
| 182 | // include ppersist.h
|
---|
| 183 | //
|
---|
| 184 | // Classe de base pour des objets persistants. Pour créer un objet
|
---|
| 185 | // persistant :
|
---|
| 186 | // - Hériter de PPersist.
|
---|
| 187 | // - Définir un numéro d'identification de la classe, unique dans Peida
|
---|
| 188 | // - Implémenter "ClassId()"
|
---|
| 189 | // - Implémenter "WriteSelf" et "ReadSelf", qui doivent écrire toutes les variables
|
---|
| 190 | // membres que l'on souhaite écrire, et les relire dans le même ordre.
|
---|
| 191 | // Pour écrire une référence à un objet : l'objet doit être un PPersist,
|
---|
| 192 | // et il suffit d'appeler "Write" sur cet objet, et "PPersistMgr::ReadObject".
|
---|
| 193 | // Si plusieurs objets font référence au même, pour éviter de l'écrire plusieurs
|
---|
| 194 | // fois, il faut que cet objet soit un PShPersist.
|
---|
| 195 | // - Pour que le fichier soit portable, écrire et lire les variables membres en utilisant
|
---|
| 196 | // les fonctions PutXX/GetXX de PInPersist/POutPersist.
|
---|
| 197 | //
|
---|
| 198 | // Attention: les méthodes à redéfinir sont WriteSelf et ReadSelf, mais il ne faut jamais
|
---|
| 199 | // les appeler directement. Seuls Write et Read peuvent être appelées par l'utilisateur.
|
---|
| 200 | //--
|
---|
| 201 |
|
---|
| 202 | //++
|
---|
[241] | 203 | // Links See
|
---|
[219] | 204 | // PInPersist
|
---|
| 205 | // POutPersist
|
---|
[241] | 206 | // PIOPersist
|
---|
[219] | 207 | //--
|
---|
| 208 |
|
---|
| 209 | //++
|
---|
| 210 | void
|
---|
| 211 | PPersist::Write(string const& fn) const
|
---|
| 212 | //
|
---|
| 213 | // Ecrit l'objet dans un nouveau fichier ppersist "fn".
|
---|
| 214 | //--
|
---|
| 215 | {
|
---|
| 216 | POutPersist of(fn);
|
---|
| 217 | Write(of);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | //++
|
---|
| 221 | void
|
---|
| 222 | PPersist::Read(string const& fn)
|
---|
| 223 | //
|
---|
| 224 | // Relit l'objet dans le fichier ppersist "fn". Il faut connaître a priori
|
---|
| 225 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
| 226 | // objet, utiliser PPersistMgr::ReadObject.
|
---|
| 227 | //--
|
---|
| 228 | {
|
---|
| 229 | PInPersist inf(fn);
|
---|
| 230 | Read(inf);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | //++
|
---|
| 234 | void
|
---|
| 235 | PPersist::Write(POutPersist& s) const
|
---|
| 236 | //
|
---|
| 237 | // Ecrit l'objet dans le fichier PPersist.
|
---|
| 238 | //--
|
---|
| 239 | {
|
---|
[802] | 240 | s.PutPPObject(this);
|
---|
[219] | 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | //++
|
---|
| 245 | void
|
---|
| 246 | PPersist::Read(PInPersist& s)
|
---|
| 247 | //
|
---|
| 248 | // Relit l'objet dans le fichier ppersist. Il faut connaître a priori
|
---|
| 249 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
[241] | 250 | // objet, utiliser PInPersist::ReadObject.
|
---|
| 251 | // Il faut qu'on soit un objet ecrit
|
---|
[219] | 252 | //--
|
---|
| 253 | {
|
---|
[241] | 254 | // We should be the exact type
|
---|
| 255 | // Check tag value
|
---|
[802] | 256 | unsigned char ppstype,ppstag;
|
---|
[588] | 257 | s.GetTypeTag(ppstype);
|
---|
[802] | 258 | if ( (ppstype != PInPersist::PPS_OBJECT) && ( ppstype != PInPersist::PPS_REFERENCE ) ) {
|
---|
[241] | 259 | }
|
---|
[802] | 260 | if (ppstype == PInPersist::PPS_OBJECT) {
|
---|
| 261 | // Check class id
|
---|
| 262 | uint_8 classId;
|
---|
| 263 | s.GetRawU8(classId);
|
---|
| 264 | uint_8 oid,oid2;
|
---|
| 265 | s.GetRawU8(oid);
|
---|
| 266 | if (classId != PIOPersist::getPPClassId(*this) )
|
---|
| 267 | throw FileFormatExc("PPersist::Read (): not the same object type");
|
---|
| 268 | ReadSelf(s);
|
---|
| 269 | // Read the ENDOBJECT
|
---|
| 270 | s.GetRawUByte(ppstag);
|
---|
| 271 | if (ppstag != PInPersist::PPS_ENDOBJECT)
|
---|
| 272 | throw FileFormatExc("PPersist::Read() No PPS_ENDOBJECT tag");
|
---|
| 273 | s.GetRawU8(oid2);
|
---|
| 274 | if (oid2 != oid)
|
---|
| 275 | throw FileFormatExc("PPersist::Read() Inconsistent PPS-OId at PPS_ENDOBJECT ");
|
---|
| 276 | s.KeepOId(oid, *this); // Object should be kept with its PPS_OId (if oid > 0)
|
---|
[241] | 277 | }
|
---|
[802] | 278 | else if ( ppstype == PInPersist::PPS_REFERENCE )
|
---|
| 279 | s.ReadReference(*this);
|
---|
[219] | 280 |
|
---|
[802] | 281 | else throw FileFormatExc("PPersist::Read() : not an object in flow");
|
---|
| 282 |
|
---|
[219] | 283 | }
|
---|
| 284 |
|
---|
| 285 | //++
|
---|
[241] | 286 | void
|
---|
| 287 | PPersist::Write(POutPersist& s, string const& tag) const
|
---|
[219] | 288 | //
|
---|
[241] | 289 | // Ecrit l'objet dans le fichier PPersist avec un tag
|
---|
[219] | 290 | //--
|
---|
| 291 | {
|
---|
[241] | 292 | s.WriteTag(tag);
|
---|
[802] | 293 | s.PutPPObject(this);
|
---|
[219] | 294 | }
|
---|
| 295 |
|
---|
| 296 | //++
|
---|
| 297 | void
|
---|
[241] | 298 | PPersist::ReadAtTag(PInPersist& s, string const& tag)
|
---|
[219] | 299 | //
|
---|
| 300 | // Lit l'objet à la position du tag numéro "tagid".
|
---|
| 301 | //--
|
---|
| 302 | {
|
---|
[241] | 303 | if (!s.GotoTag(tag))
|
---|
| 304 | throw NotFoundExc("PPersist::ReadAtTag tag not found");
|
---|
| 305 | Read(s);
|
---|
[219] | 306 | }
|
---|
| 307 |
|
---|
[802] | 308 | // Renvoie l'identificateur de l'objet - par defaut=0
|
---|
[219] | 309 |
|
---|
[802] | 310 | uint_8
|
---|
| 311 | PPersist::getMemOId() const
|
---|
| 312 | {
|
---|
| 313 | return(0);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | // Ces deux methodes doivent etre redefinies si getMemOId() renvoie non nul (>0)
|
---|
| 317 | // ShareDataReference() et CloneSharedReference()
|
---|
| 318 | void
|
---|
| 319 | PPersist::ShareDataReference(PPersist & pcs)
|
---|
| 320 | {
|
---|
| 321 | throw NotAvailableOperation("PPersist::ShareDataReference() - Unsupported operation !");
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | PPersist *
|
---|
| 325 | PPersist::CloneSharedReference()
|
---|
| 326 | {
|
---|
| 327 | throw NotAvailableOperation("PPersist::CloneSharedReference() - Unsupported operation !");
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[219] | 330 | //++
|
---|
| 331 | // virtual void PPersist::ReadSelf(PInPersist&)=0
|
---|
| 332 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Read
|
---|
| 333 | // et PPersistMgr::ReadObject. Il faut relire les variables membres,
|
---|
| 334 | // dans l'ordre où elles ont été écrites par WriteSelf.
|
---|
| 335 | // virtual void PPersist::WriteSelf(POutPersist&) const=0
|
---|
| 336 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Write.
|
---|
| 337 | // Il faut écrire les variables membres,
|
---|
| 338 | // dans l'ordre où elles seront relues par ReadSelf.
|
---|
| 339 | //--
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | //++
|
---|
| 344 | // Class PInPersist
|
---|
| 345 | // Lib Outils++
|
---|
| 346 | // include ppersist.h
|
---|
| 347 | //
|
---|
| 348 | // Fichier d'objets persistants, en lecture.
|
---|
| 349 | //--
|
---|
| 350 |
|
---|
| 351 | //++
|
---|
| 352 | PInPersist::PInPersist(string const& flnm, bool scan)
|
---|
| 353 | //
|
---|
| 354 | // Constructeur. Ouvre le fichier.
|
---|
| 355 | //--
|
---|
| 356 | {
|
---|
[241] | 357 | s = new ifstream(flnm.c_str(),ios::in | IOS_BIN);
|
---|
| 358 |
|
---|
| 359 | // Read and check header
|
---|
| 360 |
|
---|
[219] | 361 | char rbuf[36];
|
---|
[241] | 362 | GetRawBytes(rbuf, 32);
|
---|
[576] | 363 | if (strncmp(rbuf,"SOS-SOPHYA-PPersistFile", 23) != 0) {
|
---|
[241] | 364 | throw FileFormatExc("PInPersist::PInPersist bad header");
|
---|
[219] | 365 | }
|
---|
[802] | 366 | rbuf[32] = '\0';
|
---|
| 367 | version = atoi(rbuf+25);
|
---|
| 368 | if (version < 2) {
|
---|
| 369 | cerr << "PInPersist::PInPersist(" << flnm << ") Version(=" << version
|
---|
| 370 | << ") < 2 not supported !" << endl;
|
---|
| 371 | throw FileFormatExc("PInPersist::PInPersist() - Unsupported (Old) Version");
|
---|
| 372 | }
|
---|
[241] | 373 | // read endianness
|
---|
| 374 | GetRawBytes(rbuf, 32);
|
---|
| 375 | if (strncmp(rbuf,"BIG-ENDIAN",10) == 0)
|
---|
| 376 | bigEndian = true;
|
---|
| 377 | else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0)
|
---|
| 378 | bigEndian = false;
|
---|
| 379 | else {
|
---|
| 380 | throw FileFormatExc("PInPersist::PInPersist bad header - endianness");
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | // read creation date
|
---|
| 384 | GetRawBytes(rbuf, 32);
|
---|
| 385 | rbuf[32] = '\0';
|
---|
| 386 | struct tm tm;
|
---|
[802] | 387 | #ifndef __MWERKS__
|
---|
[742] | 388 | strptime(rbuf,"%d/%m/%Y %H:%M:%S GMT",&tm);
|
---|
| 389 | #else
|
---|
| 390 | sscanf(rbuf,"%2d/%2d/%4d %2d:%2d:%2d GMT",&tm.tm_mday,&tm.tm_mon,&tm.tm_year,
|
---|
| 391 | &tm.tm_hour,&tm.tm_min,&tm.tm_sec);
|
---|
| 392 | tm.tm_mon --;
|
---|
| 393 | tm.tm_year -= 1900;
|
---|
| 394 | #endif
|
---|
[241] | 395 | creationdate = mktime(&tm);
|
---|
[802] | 396 | filename = flnm; // keep the filename
|
---|
| 397 | seqread = true; // To flag non sequential reads
|
---|
[241] | 398 | if (scan) Scan();
|
---|
[219] | 399 | }
|
---|
| 400 |
|
---|
| 401 |
|
---|
| 402 |
|
---|
| 403 | PInPersist::~PInPersist()
|
---|
| 404 | {
|
---|
[802] | 405 | ObjList::iterator i;
|
---|
| 406 | for(i=objList.begin(); i!= objList.end(); i++)
|
---|
| 407 | if ((*i).second) delete (*i).second;
|
---|
[219] | 408 | delete s;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 |
|
---|
[802] | 412 | string
|
---|
| 413 | PInPersist::CreationDateStr()
|
---|
| 414 | {
|
---|
| 415 | time_t cdt = CreationDate();
|
---|
| 416 | string cdate = ctime(&cdt);
|
---|
| 417 | return(cdate);
|
---|
| 418 | }
|
---|
| 419 |
|
---|
[219] | 420 | void
|
---|
| 421 | PInPersist::Scan()
|
---|
| 422 | {
|
---|
[241] | 423 | // On cherche la liste des tags, a la fin du fichier
|
---|
[219] | 424 |
|
---|
[802] | 425 | unsigned char ppstype;
|
---|
[241] | 426 | size_t debut;
|
---|
| 427 | #ifdef STREAMPOS_IS_CLASS
|
---|
[219] | 428 | debut = s->tellg().offset();
|
---|
[241] | 429 | #else
|
---|
[219] | 430 | debut = s->tellg();
|
---|
[241] | 431 | #endif
|
---|
[219] | 432 |
|
---|
[241] | 433 | // Find tag entries at end of file
|
---|
| 434 | s->seekg(-(sizeof(int_8)+1), ios::end);
|
---|
[588] | 435 | GetTypeTag(ppstype);
|
---|
[241] | 436 | if (ppstype != PPS_EOF)
|
---|
| 437 | throw FileFormatExc("PInPersist::Scan corrupted file, no eof entry at end of file");
|
---|
[219] | 438 |
|
---|
[241] | 439 | int_8 pos;
|
---|
| 440 | GetRawI8(pos);
|
---|
| 441 | if (pos < 0) { // no tags
|
---|
| 442 | s->seekg(debut);
|
---|
| 443 | return;
|
---|
| 444 | }
|
---|
[219] | 445 |
|
---|
[241] | 446 | char buffer[MAXTAGLEN+1];
|
---|
| 447 | s->seekg(pos);
|
---|
| 448 | while (true) {
|
---|
[588] | 449 | GetTypeTag(ppstype);
|
---|
[241] | 450 | if (ppstype == PPS_EOF) break;
|
---|
| 451 |
|
---|
| 452 | if (ppstype != PPS_TAG)
|
---|
| 453 | throw FileFormatExc("PInPersist::Scan corrupted file, bad tag entry");
|
---|
[219] | 454 |
|
---|
[241] | 455 | GetRawI8(pos);
|
---|
| 456 | int_4 len;
|
---|
| 457 | GetRawI4(len);
|
---|
| 458 | if (len > MAXTAGLEN)
|
---|
| 459 | throw FileFormatExc("PInPersist::Scan corrupted file, tag name too long");
|
---|
| 460 | GetRawBytes(buffer, len);
|
---|
| 461 | buffer[len] = '\0';
|
---|
[219] | 462 |
|
---|
[241] | 463 | tags[buffer] = pos;
|
---|
[219] | 464 | }
|
---|
[241] | 465 | s->seekg(debut);
|
---|
[219] | 466 | }
|
---|
| 467 |
|
---|
| 468 |
|
---|
[256] | 469 | int
|
---|
| 470 | PInPersist::NbTags()
|
---|
| 471 | {
|
---|
| 472 | return tags.size();
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[219] | 475 | bool
|
---|
[241] | 476 | PInPersist::GotoTag(string const& name)
|
---|
[219] | 477 | {
|
---|
[241] | 478 | map<string, int_8>::iterator i = tags.find(name);
|
---|
| 479 | if (i == tags.end())
|
---|
| 480 | return false;
|
---|
| 481 | // throw NotFoundExc("PInPersist::GotoTag tag not found");
|
---|
| 482 | s->seekg((*i).second);
|
---|
[802] | 483 | seqread = false;
|
---|
| 484 | // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
|
---|
[219] | 485 | return(true);
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[256] | 488 | bool
|
---|
| 489 | PInPersist::GotoTagNum(int itag)
|
---|
| 490 | {
|
---|
[582] | 491 | if (itag<0 || itag >= (int)tags.size()) return false;
|
---|
[256] | 492 | map<string, int_8>::iterator i = tags.begin();
|
---|
| 493 | for (int j=0; j<itag; j++) i++;
|
---|
| 494 | s->seekg((*i).second);
|
---|
[802] | 495 | // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
|
---|
[256] | 496 | return(true);
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[582] | 499 | string
|
---|
| 500 | PInPersist::GetTagName(int itag)
|
---|
| 501 | {
|
---|
| 502 | if (itag<0 || itag >= (int)tags.size()) return "";
|
---|
| 503 | map<string, int_8>::iterator i = tags.begin();
|
---|
| 504 | for (int j=0; j<itag; j++) i++;
|
---|
| 505 | return((*i).first);
|
---|
| 506 | }
|
---|
[256] | 507 |
|
---|
[802] | 508 | string
|
---|
| 509 | PInPersist::GetTagClassName(int itag)
|
---|
| 510 | {
|
---|
| 511 | // A faire
|
---|
| 512 | // if (itag<0 || itag >= (int)tags.size()) return "";
|
---|
| 513 | // map<string, int_8>::iterator i = tags.begin();
|
---|
| 514 | // for (int j=0; j<itag; j++) i++;
|
---|
| 515 | // uint_8 cid = (*i).second;
|
---|
| 516 | // return(GetClassName(cid));
|
---|
| 517 | return("");
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[582] | 520 | static vector<string> * ret_tag_names = NULL;
|
---|
| 521 | vector<string> const &
|
---|
| 522 | PInPersist::GetTagNames()
|
---|
| 523 | {
|
---|
| 524 | if (ret_tag_names) delete ret_tag_names;
|
---|
| 525 | ret_tag_names = new vector<string> ;
|
---|
| 526 | map<string, int_8>::iterator i;
|
---|
| 527 | for(i=tags.begin(); i!=tags.end(); i++) ret_tag_names->push_back((*i).first);
|
---|
| 528 | return(*ret_tag_names);
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[219] | 531 | //++
|
---|
| 532 | // void PInPersist::GetByte(char& c)
|
---|
| 533 | // void PInPersist::GetBytes(void* ptr, size_t bytes)
|
---|
| 534 | // void PInPersist::GetR4 (r_4& result)
|
---|
| 535 | // void PInPersist::GetR4s (r_4* tab, size_t n)
|
---|
| 536 | // void PInPersist::GetR8 (r_8& result)
|
---|
| 537 | // void PInPersist::GetR8s (r_8* tab, size_t n)
|
---|
| 538 | // void PInPersist::GetI2 (int_2& result)
|
---|
| 539 | // void PInPersist::GetI2s (int_2* tab, size_t n)
|
---|
| 540 | // void PInPersist::GetU2 (uint_2& result)
|
---|
| 541 | // void PInPersist::GetU2s (uint_2* tab, size_t n)
|
---|
| 542 | // void PInPersist::GetI4 (int_4& result)
|
---|
| 543 | // void PInPersist::GetI4s (int_4* tab, size_t n)
|
---|
| 544 | // void PInPersist::GetU4 (uint_4& result)
|
---|
| 545 | // void PInPersist::GetU4s (uint_4* tab, size_t n)
|
---|
| 546 | // void PInPersist::GetI8 (int_8& result)
|
---|
| 547 | // void PInPersist::GetI8s (int_8* tab, size_t n)
|
---|
| 548 | // void PInPersist::GetU8 (uint_8& result)
|
---|
| 549 | // void PInPersist::GetU8s (uint_8* tab, size_t n)
|
---|
| 550 | // Lecture de données portables depuis le fichier PPersist. Pour chaque type
|
---|
| 551 | // de données, on peut lire une valeur, ou un tableau de valeurs.
|
---|
| 552 | // void PInPersist::GetLine(char* ptr, size_t len)
|
---|
| 553 | // Lecture d'une ligne de texte depuis le fichier PPersist.
|
---|
| 554 | //--
|
---|
| 555 |
|
---|
| 556 |
|
---|
| 557 | static inline void bswap8(void* p)
|
---|
| 558 | {
|
---|
| 559 | uint_8 tmp = *(uint_8*)p;
|
---|
| 560 | *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |
|
---|
| 561 | ((tmp >> (5*8)) & 0x0000FF00) |
|
---|
| 562 | ((tmp >> (3*8)) & 0x00FF0000) |
|
---|
| 563 | ((tmp >> (1*8)) & 0xFF000000) |
|
---|
| 564 | ((tmp & 0xFF000000) << (1*8)) |
|
---|
| 565 | ((tmp & 0x00FF0000) << (3*8)) |
|
---|
| 566 | ((tmp & 0x0000FF00) << (5*8)) |
|
---|
| 567 | ((tmp & 0x000000FF) << (7*8));
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | static inline void bswap4(void* p)
|
---|
| 571 | {
|
---|
| 572 | uint_4 tmp = *(uint_4*)p;
|
---|
| 573 | *(uint_4*)p = ((tmp >> 24) & 0x000000FF) |
|
---|
| 574 | ((tmp >> 8) & 0x0000FF00) |
|
---|
| 575 | ((tmp & 0x0000FF00) << 8) |
|
---|
| 576 | ((tmp & 0x000000FF) << 24);
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | static inline void bswap2(void* p)
|
---|
| 580 | {
|
---|
| 581 | uint_2 tmp = *(uint_2*)p;
|
---|
| 582 | *(uint_2*)p = ((tmp >> 8) & 0x00FF) |
|
---|
| 583 | ((tmp & 0x00FF) << 8);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[588] | 586 | void
|
---|
[802] | 587 | PInPersist::GetTypeTag(unsigned char& c)
|
---|
[588] | 588 | {
|
---|
[802] | 589 | c = PPS_TAG_MARK;
|
---|
| 590 | while (c == PPS_TAG_MARK) GetRawUByte(c);
|
---|
| 591 | // while (c == PPS_TAG_MARK) { Il ne faut plus faire ca !
|
---|
| 592 | // objList.clear(); $CHECK$ Reza 03/2000
|
---|
| 593 | // GetRawByte(c);
|
---|
| 594 | // }
|
---|
[588] | 595 | }
|
---|
[241] | 596 |
|
---|
[588] | 597 |
|
---|
[219] | 598 | void
|
---|
[241] | 599 | PInPersist::GetRawByte(char& c)
|
---|
| 600 | {
|
---|
| 601 | GetRawBytes(&c, 1);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | void
|
---|
[802] | 605 | PInPersist::GetRawUByte(unsigned char& c)
|
---|
| 606 | {
|
---|
| 607 | GetRawBytes(&c, 1);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | void
|
---|
[241] | 611 | PInPersist::GetRawBytes(void* ptr, size_t bytes)
|
---|
| 612 | {
|
---|
| 613 | s->read((char*)ptr, bytes);
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | void
|
---|
| 617 | PInPersist::GetRawI2 (int_2& result)
|
---|
| 618 | {
|
---|
| 619 | GetRawBytes(&result, sizeof(int_2));
|
---|
| 620 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 621 | bswap2(&result);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | void
|
---|
| 625 | PInPersist::GetRawI4 (int_4& result)
|
---|
| 626 | {
|
---|
| 627 | GetRawBytes(&result, sizeof(int_4));
|
---|
| 628 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 629 | bswap4(&result);
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | void
|
---|
| 633 | PInPersist::GetRawI8 (int_8& result)
|
---|
| 634 | {
|
---|
| 635 | GetRawBytes(&result, sizeof(int_8));
|
---|
| 636 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 637 | bswap8(&result);
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | void
|
---|
| 641 | PInPersist::GetRawU8 (uint_8& result)
|
---|
| 642 | {
|
---|
| 643 | GetRawBytes(&result, sizeof(uint_8));
|
---|
| 644 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 645 | bswap8(&result);
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | void
|
---|
[802] | 649 | PInPersist::CheckTag(short datasz, short datatype)
|
---|
| 650 | // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
|
---|
[241] | 651 | {
|
---|
[802] | 652 | unsigned char ppstype;
|
---|
[588] | 653 | GetTypeTag(ppstype);
|
---|
[802] | 654 | if (ppstype != PPS_SIMPLE + datasz + datatype)
|
---|
[256] | 655 | throw FileFormatExc("PInPersist::CheckTag bad type in ppersist file");
|
---|
[241] | 656 | }
|
---|
| 657 |
|
---|
| 658 | void
|
---|
[802] | 659 | PInPersist::CheckArrayTag(short datasz, size_t sz, short datatype)
|
---|
| 660 | // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
|
---|
[241] | 661 | {
|
---|
[802] | 662 | unsigned char ppstype;
|
---|
[588] | 663 | GetTypeTag(ppstype);
|
---|
[241] | 664 | size_t filesz;
|
---|
[802] | 665 | if (sz <= 0x7fffffff) {
|
---|
| 666 | if (ppstype != PPS_SIMPLE_ARRAY4 + datasz + datatype)
|
---|
| 667 | throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file");
|
---|
[241] | 668 | int_4 ff;
|
---|
| 669 | GetRawI4(ff); filesz=ff;
|
---|
| 670 | } else {
|
---|
[802] | 671 | if (ppstype != PPS_SIMPLE_ARRAY8 + datasz + datatype)
|
---|
| 672 | throw FileFormatExc("PInPersist::CheckArrayTag bad type in ppersist file");
|
---|
[241] | 673 | uint_8 ff;
|
---|
| 674 | GetRawU8(ff); filesz=ff;
|
---|
| 675 | }
|
---|
| 676 | if (filesz != sz)
|
---|
[802] | 677 | throw FileFormatExc("PInPersist::CheckArrayTag bad array size in ppersist file");
|
---|
[241] | 678 | }
|
---|
| 679 |
|
---|
| 680 | void
|
---|
| 681 | PInPersist::GetByte(char& c)
|
---|
| 682 | {
|
---|
[802] | 683 | CheckTag(1,PPS_DATATYPE_CHAR);
|
---|
[241] | 684 | GetRawBytes(&c, 1);
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[802] | 687 |
|
---|
[241] | 688 | void
|
---|
| 689 | PInPersist::GetBytes(void* ptr, size_t bytes)
|
---|
| 690 | {
|
---|
[802] | 691 | CheckArrayTag(1, bytes, PPS_DATATYPE_CHAR);
|
---|
[241] | 692 | GetRawBytes(ptr, bytes);
|
---|
| 693 | }
|
---|
| 694 | void
|
---|
[219] | 695 | PInPersist::GetR4 (r_4& result)
|
---|
| 696 | {
|
---|
[802] | 697 | CheckTag(4,PPS_DATATYPE_FLOAT);
|
---|
[241] | 698 | GetRawBytes(&result, sizeof(r_4));
|
---|
[219] | 699 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 700 | bswap4(&result);
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 |
|
---|
| 704 | void
|
---|
| 705 | PInPersist::GetR4s (r_4* tab, size_t n)
|
---|
| 706 | {
|
---|
[802] | 707 | CheckArrayTag(4,n,PPS_DATATYPE_FLOAT);
|
---|
[241] | 708 | GetRawBytes(tab, n*sizeof(r_4));
|
---|
[219] | 709 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 710 |
|
---|
| 711 | for (unsigned int i=0; i<n; i++)
|
---|
| 712 | bswap4(tab+i);
|
---|
| 713 |
|
---|
| 714 | return;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 | void
|
---|
| 718 | PInPersist::GetR8 (r_8& result)
|
---|
| 719 | {
|
---|
[802] | 720 | CheckTag(8,PPS_DATATYPE_FLOAT);
|
---|
[241] | 721 | GetRawBytes(&result, sizeof(r_8));
|
---|
[219] | 722 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 723 | bswap8(&result);
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | void
|
---|
| 727 | PInPersist::GetR8s (r_8* tab, size_t n)
|
---|
| 728 | {
|
---|
[802] | 729 | CheckArrayTag(8,n,PPS_DATATYPE_FLOAT);
|
---|
[241] | 730 | GetRawBytes(tab, n*sizeof(r_8));
|
---|
[219] | 731 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 732 |
|
---|
| 733 | for (unsigned int i=0; i<n; i++)
|
---|
| 734 | bswap8(tab+i);
|
---|
| 735 |
|
---|
| 736 | return;
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | void
|
---|
| 740 | PInPersist::GetI2 (int_2& result)
|
---|
| 741 | {
|
---|
[802] | 742 | CheckTag(2,PPS_DATATYPE_INTEGER);
|
---|
[241] | 743 | GetRawBytes(&result, sizeof(int_2));
|
---|
[219] | 744 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 745 | bswap2(&result);
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | void
|
---|
| 749 | PInPersist::GetI2s (int_2* tab, size_t n)
|
---|
| 750 | {
|
---|
[802] | 751 | CheckArrayTag(2,n,PPS_DATATYPE_INTEGER);
|
---|
[241] | 752 | GetRawBytes(tab, n*sizeof(int_2));
|
---|
[219] | 753 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 754 |
|
---|
| 755 | for (unsigned int i=0; i<n; i++)
|
---|
| 756 | bswap2(tab+i);
|
---|
| 757 |
|
---|
| 758 | return;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | void
|
---|
| 762 | PInPersist::GetU2 (uint_2& result)
|
---|
| 763 | {
|
---|
[802] | 764 | CheckTag(2,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 765 | GetRawBytes(&result, sizeof(uint_2));
|
---|
[219] | 766 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 767 | bswap2(&result);
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | void
|
---|
| 771 | PInPersist::GetU2s (uint_2* tab, size_t n)
|
---|
| 772 | {
|
---|
[802] | 773 | CheckArrayTag(2,n,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 774 | GetRawBytes(tab, n*sizeof(uint_2));
|
---|
[219] | 775 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 776 |
|
---|
| 777 | for (unsigned int i=0; i<n; i++)
|
---|
| 778 | bswap2(tab+i);
|
---|
| 779 |
|
---|
| 780 | return;
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | void
|
---|
| 784 | PInPersist::GetI4 (int_4& result)
|
---|
| 785 | {
|
---|
[802] | 786 | CheckTag(4,PPS_DATATYPE_INTEGER);
|
---|
[241] | 787 | GetRawBytes(&result, sizeof(int_4));
|
---|
[219] | 788 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 789 | bswap4(&result);
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | void
|
---|
| 793 | PInPersist::GetI4s (int_4* tab, size_t n)
|
---|
| 794 | {
|
---|
[802] | 795 | CheckArrayTag(4,n,PPS_DATATYPE_INTEGER);
|
---|
[241] | 796 | GetRawBytes(tab, n*sizeof(int_4));
|
---|
[219] | 797 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 798 |
|
---|
| 799 | for (unsigned int i=0; i<n; i++)
|
---|
| 800 | bswap4(tab+i);
|
---|
| 801 |
|
---|
| 802 | return;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | void
|
---|
| 806 | PInPersist::GetU4 (uint_4& result)
|
---|
| 807 | {
|
---|
[802] | 808 | CheckTag(4,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 809 | GetRawBytes(&result, sizeof(uint_4));
|
---|
[219] | 810 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 811 | bswap4(&result);
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 | void
|
---|
| 815 | PInPersist::GetU4s (uint_4* tab, size_t n)
|
---|
| 816 | {
|
---|
[802] | 817 | CheckArrayTag(4,n,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 818 | GetRawBytes(tab, n*sizeof(uint_4));
|
---|
[219] | 819 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 820 |
|
---|
| 821 | for (unsigned int i=0; i<n; i++)
|
---|
| 822 | bswap4(tab+i);
|
---|
| 823 |
|
---|
| 824 | return;
|
---|
| 825 | }
|
---|
| 826 |
|
---|
| 827 |
|
---|
| 828 | void
|
---|
| 829 | PInPersist::GetI8 (int_8& result)
|
---|
| 830 | {
|
---|
[802] | 831 | CheckTag(8,PPS_DATATYPE_INTEGER);
|
---|
[241] | 832 | GetRawBytes(&result, sizeof(int_8));
|
---|
[219] | 833 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 834 | bswap8(&result);
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | void
|
---|
| 838 | PInPersist::GetI8s (int_8* tab, size_t n)
|
---|
| 839 | {
|
---|
[802] | 840 | CheckArrayTag(8,n,PPS_DATATYPE_INTEGER);
|
---|
[241] | 841 | GetRawBytes(tab, n*sizeof(int_8));
|
---|
[219] | 842 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 843 |
|
---|
| 844 | for (unsigned int i=0; i<n; i++)
|
---|
| 845 | bswap8(tab+i);
|
---|
| 846 |
|
---|
| 847 | return;
|
---|
| 848 | }
|
---|
| 849 |
|
---|
| 850 | void
|
---|
| 851 | PInPersist::GetU8 (uint_8& result)
|
---|
| 852 | {
|
---|
[802] | 853 | CheckTag(8,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 854 | GetRawBytes(&result, sizeof(uint_8));
|
---|
[219] | 855 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 856 | bswap8(&result);
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | void
|
---|
| 860 | PInPersist::GetU8s (uint_8* tab, size_t n)
|
---|
| 861 | {
|
---|
[802] | 862 | CheckArrayTag(8,n,PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 863 | GetRawBytes(tab, n*sizeof(uint_8));
|
---|
[219] | 864 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 865 |
|
---|
| 866 | for (unsigned int i=0; i<n; i++)
|
---|
| 867 | bswap8(tab+i);
|
---|
| 868 |
|
---|
| 869 | return;
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 |
|
---|
| 873 | void
|
---|
| 874 | PInPersist::GetLine(char* ptr, size_t len)
|
---|
| 875 | {
|
---|
[802] | 876 | string str;
|
---|
| 877 | GetStr(str);
|
---|
| 878 | strncpy(ptr, str.c_str(), len);
|
---|
| 879 | ptr[len] = '\0';
|
---|
[219] | 880 | }
|
---|
| 881 |
|
---|
[241] | 882 | void
|
---|
| 883 | PInPersist::GetStr(string& str)
|
---|
| 884 | {
|
---|
[802] | 885 | unsigned char ppstype;
|
---|
[588] | 886 | GetTypeTag(ppstype);
|
---|
[241] | 887 | if (ppstype != PPS_STRING)
|
---|
[802] | 888 | throw FileFormatExc("PInPersist::GetStr bad type in ppersist file");
|
---|
| 889 | int_4 len;
|
---|
| 890 | GetRawI4(len);
|
---|
[582] | 891 | char * buff = new char[len+1];
|
---|
[241] | 892 | GetRawBytes(buff, len);
|
---|
| 893 | buff[len] = '\0';
|
---|
| 894 | str = buff;
|
---|
| 895 | delete[] buff;
|
---|
| 896 | }
|
---|
[219] | 897 |
|
---|
[802] | 898 |
|
---|
[241] | 899 | PPersist*
|
---|
| 900 | PInPersist::ReadObject()
|
---|
| 901 | {
|
---|
[802] | 902 | return(GetPPObject());
|
---|
| 903 | }
|
---|
| 904 |
|
---|
| 905 | void
|
---|
| 906 | PInPersist::GetObject(AnyDataObj & o)
|
---|
| 907 | {
|
---|
| 908 | GetPPObject(&o);
|
---|
| 909 | return;
|
---|
| 910 | }
|
---|
| 911 |
|
---|
| 912 | void
|
---|
| 913 | PInPersist::GetObject(AnyDataObj & o, string tagname)
|
---|
| 914 | {
|
---|
| 915 | GotoTag(tagname);
|
---|
| 916 | GetPPObject(&o);
|
---|
| 917 | return;
|
---|
| 918 | }
|
---|
| 919 |
|
---|
| 920 | PPersist*
|
---|
| 921 | PInPersist::GetPPObject(AnyDataObj * po)
|
---|
| 922 | {
|
---|
[241] | 923 | // Get tag
|
---|
[802] | 924 | unsigned char ppstype;
|
---|
[588] | 925 | GetTypeTag(ppstype);
|
---|
[241] | 926 | if (ppstype != PPS_OBJECT && ppstype != PPS_REFERENCE && ppstype != PPS_NULL) {
|
---|
[256] | 927 | throw FileFormatExc("PInPersist::ReadObject : not an object in flow");
|
---|
[241] | 928 | }
|
---|
| 929 |
|
---|
| 930 | if (ppstype == PPS_NULL) {
|
---|
| 931 | return NULL;
|
---|
| 932 | } else if (ppstype == PPS_OBJECT) {
|
---|
| 933 | // Get class id
|
---|
| 934 | uint_8 classId;
|
---|
| 935 | GetRawU8(classId);
|
---|
[802] | 936 | uint_8 oid,oid2;
|
---|
| 937 | GetRawU8(oid);
|
---|
[241] | 938 |
|
---|
| 939 | // Get factory method
|
---|
| 940 | ClassCreatorFunc f = FindCreatorFunc(classId);
|
---|
| 941 | if (!f) {
|
---|
| 942 | throw NotFoundExc("PInPersist::ReadObject class not registered");
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | // Create object
|
---|
| 946 | PPersist* object = f();
|
---|
[802] | 947 | // If a DataObject was specified , we assign it to the PPersistObject
|
---|
| 948 | if (po != NULL) object->SetDataObj(*po);
|
---|
| 949 |
|
---|
[241] | 950 | object->ReadSelf(*this);
|
---|
[802] | 951 | unsigned char ppstag;
|
---|
| 952 | // Read the ENDOBJECT
|
---|
| 953 | GetRawUByte(ppstag);
|
---|
| 954 | if (ppstag != PPS_ENDOBJECT)
|
---|
| 955 | throw FileFormatExc("PInPersist::ReadObject No PPS_ENDOBJECT tag");
|
---|
| 956 | GetRawU8(oid2);
|
---|
| 957 | if (oid2 != oid)
|
---|
| 958 | throw FileFormatExc("PInPersist::ReadObject Inconsistent PPS-OId at PPS_ENDOBJECT ");
|
---|
| 959 |
|
---|
| 960 | KeepOId(oid, *object);
|
---|
[241] | 961 | return object;
|
---|
[802] | 962 | }
|
---|
| 963 | else if (ppstype == PPS_REFERENCE)
|
---|
| 964 | return ReadReference();
|
---|
| 965 |
|
---|
| 966 | else throw FileFormatExc("PInPersist::ReadObject invalide Tag Type !");
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 |
|
---|
| 970 | void
|
---|
| 971 | PInPersist::AnalyseTags(int lev)
|
---|
| 972 | {
|
---|
| 973 | unsigned char ppstag=0;
|
---|
| 974 | unsigned char ppst1,ppst2,ppst3;
|
---|
| 975 | uint_8 cpos,fsize;
|
---|
| 976 | uint_8 ui8,cid,oid;
|
---|
| 977 | int_4 i4;
|
---|
| 978 | int_8 i8;
|
---|
| 979 | char * buff;
|
---|
| 980 | string str;
|
---|
| 981 |
|
---|
| 982 | cout << "\n ---------------------------------------------------------- " << endl;
|
---|
| 983 | cout << " PInPersist::AnalyseTags(Level= " << lev << ")" << endl;
|
---|
| 984 |
|
---|
| 985 |
|
---|
| 986 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 987 | cpos = s->tellg().offset();
|
---|
| 988 | #else
|
---|
| 989 | cpos = s->tellg();
|
---|
| 990 | #endif
|
---|
| 991 | s->seekg(0,ios::end);
|
---|
| 992 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 993 | fsize = s->tellg().offset();
|
---|
| 994 | #else
|
---|
| 995 | fsize = s->tellg();
|
---|
| 996 | #endif
|
---|
| 997 | s->seekg(cpos,ios::beg);
|
---|
| 998 |
|
---|
| 999 | cout << " Version= " << Version() << " FileSize= " << fsize
|
---|
| 1000 | << " Creation Date= " << CreationDateStr() << endl;
|
---|
| 1001 |
|
---|
| 1002 | uint_8 totntags = 0;
|
---|
| 1003 | bool eofok = false;
|
---|
| 1004 |
|
---|
| 1005 | while ( (ppstag != PPS_EOF) && (cpos < fsize) ) {
|
---|
| 1006 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 1007 | cpos = s->tellg().offset();
|
---|
| 1008 | #else
|
---|
| 1009 | cpos = s->tellg();
|
---|
| 1010 | #endif
|
---|
| 1011 | GetRawUByte(ppstag);
|
---|
| 1012 | totntags++;
|
---|
| 1013 |
|
---|
| 1014 | ppst1 = ppstag&0x0f; // bits 0123
|
---|
| 1015 | ppst2 = ppstag&0x30; // bits 45
|
---|
| 1016 | ppst3 = ppstag&0xc0; // bits 67
|
---|
| 1017 | if ((ppst2 == 0) && (ppst3 == 0) ) {
|
---|
| 1018 | switch (ppst1) {
|
---|
| 1019 |
|
---|
| 1020 | case PPS_NULL :
|
---|
| 1021 | if (lev > 1) cout << "<PPS_NULL> tag at position " << hex << cpos << dec << endl;
|
---|
| 1022 | break;
|
---|
| 1023 |
|
---|
| 1024 | case PPS_STRING :
|
---|
| 1025 | GetRawI4(i4);
|
---|
| 1026 | if (lev > 1) cout << "<PPS_STRING> tag at position " << hex << cpos << dec
|
---|
| 1027 | << " Length=" << i4 << endl;
|
---|
| 1028 | s->seekg(i4,ios::cur);
|
---|
| 1029 | break;
|
---|
| 1030 |
|
---|
| 1031 | case PPS_OBJECT :
|
---|
| 1032 | GetRawU8(cid);
|
---|
| 1033 | GetRawU8(oid);
|
---|
| 1034 | cout << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid
|
---|
| 1035 | << " ObjectId= " << oid << dec << endl;
|
---|
| 1036 | break;
|
---|
| 1037 |
|
---|
| 1038 | case PPS_REFERENCE :
|
---|
| 1039 | GetRawU8(oid);
|
---|
| 1040 | GetRawI8(i8);
|
---|
| 1041 | cout << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= "
|
---|
| 1042 | << oid << " OrigPos=" << i8 << dec << endl;
|
---|
| 1043 | break;
|
---|
| 1044 |
|
---|
| 1045 | case PPS_TAG_MARK :
|
---|
| 1046 | cout << "<PPS_TAG_MARK> tag at position " << hex << cpos << dec << endl;
|
---|
| 1047 | break;
|
---|
| 1048 |
|
---|
| 1049 | case PPS_ENDOBJECT :
|
---|
| 1050 | GetRawU8(oid);
|
---|
| 1051 | cout << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= "
|
---|
| 1052 | << oid << dec << endl;
|
---|
| 1053 | break;
|
---|
| 1054 |
|
---|
| 1055 | case PPS_TAG :
|
---|
| 1056 | GetRawI8(i8);
|
---|
| 1057 | GetRawI4(i4);
|
---|
| 1058 | buff = new char[i4+1];
|
---|
| 1059 | GetRawBytes(buff, i4);
|
---|
| 1060 | buff[i4] = '\0'; str = buff;
|
---|
| 1061 | delete[] buff;
|
---|
| 1062 | cout << "<PPS_TAG> tag at position " << hex << cpos << dec
|
---|
| 1063 | << " Name= " << str << endl;
|
---|
| 1064 | break;
|
---|
| 1065 |
|
---|
| 1066 | case PPS_EOF :
|
---|
| 1067 | GetRawI8(i8);
|
---|
| 1068 | cout << "<PPS_EOF> tag at position " << hex << cpos
|
---|
| 1069 | << " TagPos=" << i8 << dec << endl;
|
---|
| 1070 | eofok = true;
|
---|
| 1071 | break;
|
---|
| 1072 |
|
---|
| 1073 | default :
|
---|
| 1074 | cerr << " ERROR : Unexpected tag value " << hex << ppstag
|
---|
| 1075 | << " At position" << cpos << dec << endl;
|
---|
| 1076 | throw FileFormatExc("PInPersist::AnalyseTags() - Unexpected tag value !");
|
---|
| 1077 | }
|
---|
[241] | 1078 | }
|
---|
[802] | 1079 | else {
|
---|
| 1080 | string dtype = "???? x";
|
---|
| 1081 | if (ppst3 == PPS_DATATYPE_CHAR) dtype = "CHAR x";
|
---|
| 1082 | else if (ppst3 == PPS_DATATYPE_FLOAT) dtype = "FLOAT x";
|
---|
| 1083 | else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x";
|
---|
| 1084 | else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x";
|
---|
| 1085 | int_4 dsize = ppst1;
|
---|
| 1086 | char sb[16];
|
---|
| 1087 | sprintf(sb, "%d", dsize);
|
---|
| 1088 | dtype += sb;
|
---|
| 1089 |
|
---|
| 1090 | switch (ppst2) {
|
---|
| 1091 |
|
---|
| 1092 | case PPS_SIMPLE :
|
---|
| 1093 | if (lev > 2) cout << "<PPS_SIMPLE> tag at position " << hex << cpos << dec
|
---|
| 1094 | << " DataType=" << dtype << endl;
|
---|
| 1095 | s->seekg(dsize, ios::cur);
|
---|
| 1096 | break;
|
---|
| 1097 |
|
---|
| 1098 | case PPS_SIMPLE_ARRAY4 :
|
---|
| 1099 | GetRawI4(i4);
|
---|
| 1100 | if (lev > 0) cout << "<PPS_SIMPLE_ARRAY4> tag at position " << hex << cpos << dec
|
---|
| 1101 | << " DataType=" << dtype << " NElts= " << i4 << endl;
|
---|
| 1102 | s->seekg((uint_8)dsize*(uint_8)i4, ios::cur);
|
---|
| 1103 | break;
|
---|
| 1104 |
|
---|
| 1105 | case PPS_SIMPLE_ARRAY8 :
|
---|
| 1106 | GetRawU8(ui8);
|
---|
| 1107 | if (lev > 0) cout << "<PPS_SIMPLE_ARRAY8> tag at position " << hex << cpos << dec
|
---|
| 1108 | << " DataType=" << dtype << " NElts= " << ui8 << endl;
|
---|
| 1109 | s->seekg((uint_8)dsize*ui8, ios::cur);
|
---|
| 1110 | break;
|
---|
| 1111 | }
|
---|
| 1112 | }
|
---|
[241] | 1113 | }
|
---|
[802] | 1114 | if (!eofok)
|
---|
| 1115 | throw FileFormatExc("PInPersist::AnalyseTags() - Not found <PPS_EOF> tag ");
|
---|
| 1116 |
|
---|
| 1117 | cout << " PInPersist::AnalyseTags() - End - Total Number of Tags= " << totntags << endl;
|
---|
| 1118 | cout << " ---------------------------------------------------------- \n" << endl;
|
---|
| 1119 | return;
|
---|
[241] | 1120 | }
|
---|
| 1121 |
|
---|
[802] | 1122 | void
|
---|
| 1123 | PInPersist::ReadReference(PPersist & ppo)
|
---|
[241] | 1124 | {
|
---|
[802] | 1125 | PPersist * pr = ReadReference();
|
---|
| 1126 | ppo.ShareDataReference(*pr);
|
---|
[241] | 1127 | }
|
---|
| 1128 |
|
---|
[802] | 1129 |
|
---|
| 1130 | PPersist *
|
---|
| 1131 | PInPersist::ReadReference()
|
---|
| 1132 | {
|
---|
| 1133 | uint_8 oid;
|
---|
| 1134 | int_8 pos;
|
---|
| 1135 | GetRawU8(oid);
|
---|
| 1136 | GetRawI8(pos);
|
---|
| 1137 | // cerr << " DBG - PInPersist::ReadReference-A " << oid << " Pos= " << pos << endl;
|
---|
| 1138 | map<uint_8, PPersist *>::iterator i = objList.find(oid);
|
---|
| 1139 | if (i != objList.end()) return (*i).second;
|
---|
| 1140 | else { // We may have skeeped it !
|
---|
| 1141 | // Let's try to read it
|
---|
| 1142 | int_8 cpos;
|
---|
| 1143 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 1144 | cpos = s->tellg().offset();
|
---|
| 1145 | #else
|
---|
| 1146 | cpos = s->tellg();
|
---|
| 1147 | #endif
|
---|
| 1148 | s->seekg(pos);
|
---|
| 1149 | PPersist* ppo = ReadObject();
|
---|
| 1150 | s->seekg(cpos);
|
---|
| 1151 | delete ppo;
|
---|
| 1152 | // cerr << " DBG - PInPersist::ReadReference-B ... " << endl;
|
---|
| 1153 |
|
---|
| 1154 | map<uint_8, PPersist *>::iterator i2 = objList.find(oid);
|
---|
| 1155 | if (i2 == objList.end())
|
---|
| 1156 | throw FileFormatExc("PInPersist::ReadReference() Not found PPS_OId ");
|
---|
| 1157 | return (*i2).second;
|
---|
| 1158 | }
|
---|
| 1159 | }
|
---|
| 1160 |
|
---|
| 1161 |
|
---|
| 1162 | void
|
---|
| 1163 | PInPersist::KeepOId(uint_8 oid, PPersist & ppo)
|
---|
| 1164 | {
|
---|
| 1165 | if ((oid&0x1) == 0) return; // This is not an object which can be referenced
|
---|
| 1166 | // cerr << " DBG - PInPersist::KeepOId() " << oid << endl;
|
---|
| 1167 | if ((objList.size() > 0) && (objList.find(oid) != objList.end()) ) {
|
---|
| 1168 | // Ceci ne devrait arriver que si on lit dans le desordre (avec GotoTag)
|
---|
| 1169 | // et pas avec une lecture sequentielle ... Reza 03/2000
|
---|
| 1170 | // cerr << "PInPersist::KeepOId()/Warning - already present PPS_ObjectId ! " << oid << endl;
|
---|
| 1171 | if (seqread) throw FileFormatExc("PInPersist::KeepOId() already present PPS_ObjectId ");
|
---|
| 1172 | PPersist *pp = (*objList.find(oid)).second;
|
---|
| 1173 | ppo.ShareDataReference(*pp);
|
---|
| 1174 | }
|
---|
| 1175 | else {
|
---|
| 1176 | PPersist * npp = ppo.CloneSharedReference();
|
---|
| 1177 | if (npp == NULL) throw PError("PInPersist::KeepOId() NULL returned by PPersist.Clone() ! ");
|
---|
| 1178 | objList[oid] = npp;
|
---|
| 1179 | }
|
---|
| 1180 | return;
|
---|
| 1181 | }
|
---|
| 1182 |
|
---|
[219] | 1183 | //++
|
---|
| 1184 | // Class POutPersist
|
---|
| 1185 | // Lib Outils++
|
---|
| 1186 | // include ppersist.h
|
---|
| 1187 | //
|
---|
| 1188 | // Fichier d'objets persistants, en écriture.
|
---|
| 1189 | //--
|
---|
| 1190 |
|
---|
| 1191 |
|
---|
| 1192 | //++
|
---|
| 1193 | // POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE)
|
---|
| 1194 | //
|
---|
| 1195 | // Crée un nouveau fichier ppersist. Par défaut, il est petit=boutien
|
---|
| 1196 | // sur machines petit-boutiennes, et gros-boutien sur machines
|
---|
| 1197 | // gros-boutiennes. On peut explicitement spécifier PPersist::PPS_LITTLE_ENDIAN
|
---|
| 1198 | // ou PPersist::PPS_BIG_ENDIAN.
|
---|
| 1199 | //--
|
---|
| 1200 | POutPersist::POutPersist(string const& flnm, int endianness)
|
---|
| 1201 | {
|
---|
| 1202 | if (endianness == -1)
|
---|
| 1203 | bigEndian = IS_BIG_ENDIAN;
|
---|
| 1204 | else
|
---|
| 1205 | bigEndian = endianness;
|
---|
| 1206 |
|
---|
[802] | 1207 | // PPS (POutPersist stream) Object Id initialisation
|
---|
| 1208 | pps_OId = 0;
|
---|
[241] | 1209 | // Output stream creation
|
---|
| 1210 | s = new ofstream(flnm.c_str(),ios::out | IOS_BIN);
|
---|
| 1211 |
|
---|
| 1212 | // Header
|
---|
[802] | 1213 | PutRawBytes("SOS-SOPHYA-PPersistFile V2 ",32);
|
---|
[241] | 1214 | PutRawBytes(bigEndian
|
---|
| 1215 | ? "BIG-ENDIAN "
|
---|
| 1216 | : "LITTLE-ENDIAN ",32);
|
---|
| 1217 |
|
---|
| 1218 | // ---- GMT creation date of the file
|
---|
| 1219 | time_t tm = time(NULL);
|
---|
| 1220 | char datestring[33];
|
---|
[742] | 1221 | int l=strftime(datestring,32,"%d/%m/%Y %H:%M:%S GMT",gmtime(&tm));
|
---|
[241] | 1222 | for(int i=l; i<32; i++) datestring[i] = ' ';
|
---|
| 1223 | datestring[32] = '\0';
|
---|
| 1224 | PutRawBytes(datestring, 32);
|
---|
[802] | 1225 | filename = flnm;
|
---|
[219] | 1226 | }
|
---|
| 1227 |
|
---|
| 1228 | POutPersist::~POutPersist()
|
---|
| 1229 | {
|
---|
[241] | 1230 | if (tags.size() == 0) {
|
---|
[802] | 1231 | PutRawUByte(PPS_EOF);
|
---|
[241] | 1232 | PutRawI8(-1);
|
---|
| 1233 | } else {
|
---|
| 1234 | int_8 tagPos;
|
---|
| 1235 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 1236 | tagPos = s->tellp().offset();
|
---|
| 1237 | #else
|
---|
| 1238 | tagPos = s->tellp();
|
---|
| 1239 | #endif
|
---|
| 1240 | for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
|
---|
| 1241 | string name = (*i).first;
|
---|
| 1242 | int_8 pos = (*i).second;
|
---|
[802] | 1243 | PutRawUByte(PPS_TAG); // This is a tag
|
---|
[241] | 1244 | PutRawI8(pos); // position of previous tag
|
---|
| 1245 | PutRawI4(name.length()); // length of the name
|
---|
| 1246 | PutRawBytes(name.c_str(), name.length()); // name, without final "0".
|
---|
| 1247 | }
|
---|
[802] | 1248 | PutRawUByte(PPS_EOF);
|
---|
[241] | 1249 | PutRawI8(tagPos);
|
---|
| 1250 | }
|
---|
| 1251 |
|
---|
| 1252 | delete s; // Close the stream
|
---|
[219] | 1253 | }
|
---|
| 1254 |
|
---|
| 1255 |
|
---|
[241] | 1256 | void
|
---|
| 1257 | POutPersist::WriteTag(string const& name)
|
---|
[219] | 1258 | {
|
---|
[241] | 1259 | if (name.length() > MAXTAGLEN)
|
---|
| 1260 | throw ParmError("POutPersist::WriteTag tag name too long");
|
---|
[219] | 1261 |
|
---|
[241] | 1262 | if (tags.find(name) != tags.end())
|
---|
| 1263 | throw DuplicateIdExc("POutPersist::WriteTag duplicate tag name");
|
---|
| 1264 |
|
---|
| 1265 | // Get current file position
|
---|
| 1266 | int_8 tagPos;
|
---|
| 1267 |
|
---|
[219] | 1268 | #ifdef STREAMPOS_IS_CLASS
|
---|
[241] | 1269 | tagPos = s->tellp().offset();
|
---|
[219] | 1270 | #else
|
---|
[241] | 1271 | tagPos = s->tellp();
|
---|
[219] | 1272 | #endif
|
---|
[241] | 1273 |
|
---|
| 1274 | tags[name] = tagPos;
|
---|
[802] | 1275 | PutRawUByte(PPS_TAG_MARK); // This is a tag
|
---|
| 1276 | // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000
|
---|
[219] | 1277 | }
|
---|
| 1278 |
|
---|
| 1279 | //++
|
---|
| 1280 | // void POutPersist::PutByte(char& c)
|
---|
| 1281 | // void POutPersist::PutBytes(void const* ptr, size_t bytes)
|
---|
| 1282 | // void POutPersist::PutR4 (r_4 result)
|
---|
| 1283 | // void POutPersist::PutR4s (r_4 const* tab, size_t n)
|
---|
| 1284 | // void POutPersist::PutR8 (r_8 result)
|
---|
| 1285 | // void POutPersist::PutR8s (r_8 const* tab, size_t n)
|
---|
| 1286 | // void POutPersist::PutI2 (int_2 result)
|
---|
| 1287 | // void POutPersist::PutI2s (int_2 const* tab, size_t n)
|
---|
| 1288 | // void POutPersist::PutU2 (uint_2 result)
|
---|
| 1289 | // void POutPersist::PutU2s (uint_2 const* tab, size_t n)
|
---|
| 1290 | // void POutPersist::PutI4 (int_4 result)
|
---|
| 1291 | // void POutPersist::PutI4s (int_4 const* tab, size_t n)
|
---|
| 1292 | // void POutPersist::PutU4 (uint_4 result)
|
---|
| 1293 | // void POutPersist::PutU4s (uint_4 const* tab, size_t n)
|
---|
| 1294 | // void POutPersist::PutI8 (int_8 result)
|
---|
| 1295 | // void POutPersist::PutI8s (int_8 const* tab, size_t n)
|
---|
| 1296 | // void POutPersist::PutU8 (uint_8 result)
|
---|
| 1297 | // void POutPersist::PutU8s (uint_8 const* tab, size_t n)
|
---|
[241] | 1298 | // void POutPersist::PutStr (string const&)
|
---|
[219] | 1299 | // Ecriture de données portables.. Pour chaque type
|
---|
| 1300 | // de données, on peut écrire une valeur, ou un tableau de valeurs.
|
---|
| 1301 | // void POutPersist::PutLine(char const* ptr, size_t len)
|
---|
| 1302 | // Ecriture d'une ligne de texte dans le fichier PPersist.
|
---|
| 1303 | //--
|
---|
| 1304 |
|
---|
| 1305 |
|
---|
| 1306 |
|
---|
| 1307 |
|
---|
| 1308 | void
|
---|
[241] | 1309 | POutPersist::PutRawBytes(void const* ptr, size_t bytes)
|
---|
| 1310 | {
|
---|
| 1311 | s->write((char const*)ptr, bytes);
|
---|
| 1312 | }
|
---|
| 1313 |
|
---|
| 1314 | void
|
---|
| 1315 | POutPersist::PutRawByte(char c)
|
---|
| 1316 | {
|
---|
| 1317 | PutRawBytes(&c, 1);
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | void
|
---|
[802] | 1321 | POutPersist::PutRawUByte(unsigned char c)
|
---|
| 1322 | {
|
---|
| 1323 | PutRawBytes(&c, 1);
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | void
|
---|
[241] | 1327 | POutPersist::PutRawI2 (int_2 val)
|
---|
| 1328 | {
|
---|
| 1329 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1330 | bswap2(&val);
|
---|
| 1331 |
|
---|
| 1332 | PutRawBytes(&val, sizeof(int_2));
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
| 1335 | void
|
---|
| 1336 | POutPersist::PutRawI4 (int_4 val)
|
---|
| 1337 | {
|
---|
| 1338 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1339 | bswap4(&val);
|
---|
| 1340 |
|
---|
| 1341 | PutRawBytes(&val, sizeof(int_4));
|
---|
| 1342 | }
|
---|
| 1343 |
|
---|
| 1344 | void
|
---|
| 1345 | POutPersist::PutRawI8 (int_8 val)
|
---|
| 1346 | {
|
---|
| 1347 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1348 | bswap8(&val);
|
---|
| 1349 |
|
---|
| 1350 | PutRawBytes(&val, sizeof(int_8));
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
| 1353 | void
|
---|
| 1354 | POutPersist::PutRawU8 (uint_8 val)
|
---|
| 1355 | {
|
---|
| 1356 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1357 | bswap8(&val);
|
---|
| 1358 |
|
---|
| 1359 | PutRawBytes(&val, sizeof(uint_8));
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 | void
|
---|
[802] | 1363 | POutPersist::PutArrayTag(short datasz, size_t sz, short datatype)
|
---|
| 1364 | // datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
|
---|
[241] | 1365 | {
|
---|
[802] | 1366 | if (sz <= 0x7fffffff) {
|
---|
| 1367 | PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype);
|
---|
[241] | 1368 | PutRawI4(sz);
|
---|
| 1369 | } else {
|
---|
[802] | 1370 | PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype);
|
---|
[241] | 1371 | PutRawU8(sz);
|
---|
| 1372 | }
|
---|
| 1373 | }
|
---|
| 1374 |
|
---|
| 1375 | void
|
---|
[219] | 1376 | POutPersist::PutByte(char c)
|
---|
| 1377 | {
|
---|
[802] | 1378 | PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR);
|
---|
[241] | 1379 | PutRawBytes(&c, 1);
|
---|
[219] | 1380 | }
|
---|
| 1381 |
|
---|
[241] | 1382 |
|
---|
| 1383 |
|
---|
[219] | 1384 | void
|
---|
| 1385 | POutPersist::PutBytes(void const* ptr, size_t bytes)
|
---|
| 1386 | {
|
---|
[802] | 1387 | PutArrayTag(1, bytes, PPS_DATATYPE_CHAR);
|
---|
[241] | 1388 | PutRawBytes(ptr, bytes);
|
---|
[219] | 1389 | }
|
---|
| 1390 |
|
---|
| 1391 | void
|
---|
| 1392 | POutPersist::PutR4 (r_4 val)
|
---|
| 1393 | {
|
---|
[802] | 1394 | PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT);
|
---|
[241] | 1395 |
|
---|
[219] | 1396 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1397 | bswap4(&val);
|
---|
| 1398 |
|
---|
[241] | 1399 | PutRawBytes(&val, sizeof(r_4));
|
---|
[219] | 1400 | }
|
---|
| 1401 |
|
---|
| 1402 | void
|
---|
| 1403 | POutPersist::PutR4s (r_4 const* tab, size_t n)
|
---|
| 1404 | {
|
---|
[802] | 1405 | PutArrayTag(4, n, PPS_DATATYPE_FLOAT);
|
---|
[241] | 1406 |
|
---|
| 1407 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1408 | PutRawBytes(tab, n*sizeof(r_4));
|
---|
| 1409 | } else {
|
---|
| 1410 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1411 | r_4 val = tab[i];
|
---|
| 1412 | bswap4(&val);
|
---|
| 1413 | PutRawBytes(&val, sizeof(r_4));
|
---|
| 1414 | }
|
---|
| 1415 | }
|
---|
[219] | 1416 | }
|
---|
| 1417 |
|
---|
| 1418 | void
|
---|
| 1419 | POutPersist::PutR8 (r_8 val)
|
---|
| 1420 | {
|
---|
[802] | 1421 | PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT);
|
---|
[241] | 1422 |
|
---|
[219] | 1423 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1424 | bswap8(&val);
|
---|
| 1425 |
|
---|
[241] | 1426 | PutRawBytes(&val, sizeof(r_8));
|
---|
[219] | 1427 | }
|
---|
| 1428 |
|
---|
| 1429 | void
|
---|
| 1430 | POutPersist::PutR8s (r_8 const* tab, size_t n)
|
---|
| 1431 | {
|
---|
[802] | 1432 | PutArrayTag(8, n, PPS_DATATYPE_FLOAT);
|
---|
[241] | 1433 |
|
---|
| 1434 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1435 | PutRawBytes(tab, n*sizeof(r_8));
|
---|
| 1436 | } else {
|
---|
| 1437 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1438 | r_8 val = tab[i];
|
---|
| 1439 | bswap8(&val);
|
---|
| 1440 | PutRawBytes(&val, sizeof(r_8));
|
---|
| 1441 | }
|
---|
| 1442 | }
|
---|
[219] | 1443 | }
|
---|
| 1444 |
|
---|
| 1445 | void
|
---|
| 1446 | POutPersist::PutI2 (int_2 val)
|
---|
| 1447 | {
|
---|
[802] | 1448 | PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER);
|
---|
[241] | 1449 |
|
---|
[219] | 1450 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1451 | bswap2(&val);
|
---|
| 1452 |
|
---|
[241] | 1453 | PutRawBytes(&val, sizeof(int_2));
|
---|
[219] | 1454 | }
|
---|
| 1455 |
|
---|
| 1456 | void
|
---|
| 1457 | POutPersist::PutI2s (int_2 const* tab, size_t n)
|
---|
| 1458 | {
|
---|
[802] | 1459 | PutArrayTag(2, n, PPS_DATATYPE_INTEGER);
|
---|
[241] | 1460 |
|
---|
| 1461 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1462 | PutRawBytes(tab, n*sizeof(int_2));
|
---|
| 1463 | } else {
|
---|
| 1464 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1465 | int_2 val = tab[i];
|
---|
| 1466 | bswap2(&val);
|
---|
| 1467 | PutRawBytes(&val, sizeof(int_2));
|
---|
| 1468 | }
|
---|
| 1469 | }
|
---|
[219] | 1470 | }
|
---|
| 1471 |
|
---|
| 1472 | void
|
---|
| 1473 | POutPersist::PutU2 (uint_2 val)
|
---|
| 1474 | {
|
---|
[802] | 1475 | PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1476 |
|
---|
[219] | 1477 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1478 | bswap2(&val);
|
---|
| 1479 |
|
---|
[241] | 1480 | PutRawBytes(&val, sizeof(uint_2));
|
---|
[219] | 1481 | }
|
---|
| 1482 |
|
---|
| 1483 | void
|
---|
| 1484 | POutPersist::PutU2s (uint_2 const* tab, size_t n)
|
---|
| 1485 | {
|
---|
[802] | 1486 | PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1487 |
|
---|
| 1488 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1489 | PutRawBytes(tab, n*sizeof(uint_2));
|
---|
| 1490 | } else {
|
---|
| 1491 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1492 | uint_2 val = tab[i];
|
---|
| 1493 | bswap2(&val);
|
---|
| 1494 | PutRawBytes(&val, sizeof(uint_2));
|
---|
| 1495 | }
|
---|
| 1496 | }
|
---|
[219] | 1497 | }
|
---|
| 1498 |
|
---|
| 1499 | void
|
---|
| 1500 | POutPersist::PutI4 (int_4 val)
|
---|
| 1501 | {
|
---|
[802] | 1502 | PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER);
|
---|
[241] | 1503 |
|
---|
[219] | 1504 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1505 | bswap4(&val);
|
---|
| 1506 |
|
---|
[241] | 1507 | PutRawBytes(&val, sizeof(int_4));
|
---|
[219] | 1508 | }
|
---|
| 1509 |
|
---|
| 1510 | void
|
---|
| 1511 | POutPersist::PutI4s (int_4 const* tab, size_t n)
|
---|
| 1512 | {
|
---|
[802] | 1513 | PutArrayTag(4, n, PPS_DATATYPE_INTEGER);
|
---|
[241] | 1514 |
|
---|
| 1515 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1516 | PutRawBytes(tab, n*sizeof(int_4));
|
---|
| 1517 | } else {
|
---|
| 1518 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1519 | int_4 val = tab[i];
|
---|
| 1520 | bswap4(&val);
|
---|
| 1521 | PutRawBytes(&val, sizeof(int_4));
|
---|
| 1522 | }
|
---|
| 1523 | }
|
---|
[219] | 1524 | }
|
---|
| 1525 |
|
---|
| 1526 | void
|
---|
| 1527 | POutPersist::PutU4 (uint_4 val)
|
---|
| 1528 | {
|
---|
[802] | 1529 | PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1530 |
|
---|
[219] | 1531 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1532 | bswap4(&val);
|
---|
| 1533 |
|
---|
[241] | 1534 | PutRawBytes(&val, sizeof(uint_4));
|
---|
[219] | 1535 | }
|
---|
| 1536 |
|
---|
| 1537 | void
|
---|
| 1538 | POutPersist::PutU4s (uint_4 const* tab, size_t n)
|
---|
| 1539 | {
|
---|
[802] | 1540 | PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1541 |
|
---|
| 1542 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1543 | PutRawBytes(tab, n*sizeof(uint_4));
|
---|
| 1544 | } else {
|
---|
| 1545 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1546 | uint_4 val = tab[i];
|
---|
| 1547 | bswap4(&val);
|
---|
| 1548 | PutRawBytes(&val, sizeof(uint_4));
|
---|
| 1549 | }
|
---|
| 1550 | }
|
---|
[219] | 1551 | }
|
---|
| 1552 |
|
---|
| 1553 | void
|
---|
| 1554 | POutPersist::PutI8 (int_8 val)
|
---|
| 1555 | {
|
---|
[802] | 1556 | PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER);
|
---|
[241] | 1557 |
|
---|
[219] | 1558 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1559 | bswap8(&val);
|
---|
| 1560 |
|
---|
[241] | 1561 | PutRawBytes(&val, sizeof(int_8));
|
---|
[219] | 1562 | }
|
---|
| 1563 |
|
---|
| 1564 | void
|
---|
| 1565 | POutPersist::PutI8s (int_8 const* tab, size_t n)
|
---|
| 1566 | {
|
---|
[802] | 1567 | PutArrayTag(8, n, PPS_DATATYPE_INTEGER);
|
---|
[241] | 1568 |
|
---|
| 1569 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1570 | PutRawBytes(tab, n*sizeof(int_8));
|
---|
| 1571 | } else {
|
---|
| 1572 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1573 | int_8 val = tab[i];
|
---|
| 1574 | bswap8(&val);
|
---|
| 1575 | PutRawBytes(&val, sizeof(int_8));
|
---|
| 1576 | }
|
---|
| 1577 | }
|
---|
[219] | 1578 | }
|
---|
| 1579 |
|
---|
| 1580 | void
|
---|
| 1581 | POutPersist::PutU8 (uint_8 val)
|
---|
| 1582 | {
|
---|
[802] | 1583 | PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1584 |
|
---|
[219] | 1585 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1586 | bswap8(&val);
|
---|
| 1587 |
|
---|
[241] | 1588 | PutRawBytes(&val, sizeof(uint_8));
|
---|
[219] | 1589 | }
|
---|
| 1590 |
|
---|
| 1591 | void
|
---|
| 1592 | POutPersist::PutU8s (uint_8 const* tab, size_t n)
|
---|
| 1593 | {
|
---|
[802] | 1594 | PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED);
|
---|
[241] | 1595 |
|
---|
| 1596 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1597 | PutRawBytes(tab, n*sizeof(uint_8));
|
---|
| 1598 | } else {
|
---|
| 1599 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1600 | uint_8 val = tab[i];
|
---|
| 1601 | bswap8(&val);
|
---|
| 1602 | PutRawBytes(&val, sizeof(uint_8));
|
---|
| 1603 | }
|
---|
| 1604 | }
|
---|
[219] | 1605 | }
|
---|
| 1606 |
|
---|
| 1607 | void
|
---|
[241] | 1608 | POutPersist::PutStr(string const& str)
|
---|
| 1609 | {
|
---|
[802] | 1610 | PutRawUByte(PPS_STRING);
|
---|
| 1611 | PutRawI4(str.length());
|
---|
[241] | 1612 | PutRawBytes(str.c_str(), str.length());
|
---|
| 1613 | }
|
---|
| 1614 |
|
---|
| 1615 | void
|
---|
[219] | 1616 | POutPersist::PutLine(char const* ptr, size_t len)
|
---|
| 1617 | {
|
---|
[802] | 1618 | string str = ptr;
|
---|
| 1619 | PutStr(str);
|
---|
| 1620 | }
|
---|
[241] | 1621 |
|
---|
[802] | 1622 |
|
---|
| 1623 | void
|
---|
| 1624 | POutPersist::PutObject(AnyDataObj & o)
|
---|
| 1625 | {
|
---|
| 1626 | ClassCreatorFunc f = FindCreatorFunc(getDataObjClassId(o));
|
---|
| 1627 | if (!f)
|
---|
| 1628 | throw NotFoundExc("PInPersist::PutObject() class not registered");
|
---|
| 1629 | PPersist* ppo = f();
|
---|
| 1630 | ppo->SetDataObj(o);
|
---|
| 1631 | PutPPObject(ppo);
|
---|
[219] | 1632 | }
|
---|
| 1633 |
|
---|
[241] | 1634 | void
|
---|
[802] | 1635 | POutPersist::PutObject(AnyDataObj & o, string tagname)
|
---|
[241] | 1636 | {
|
---|
[802] | 1637 | WriteTag(tagname);
|
---|
| 1638 | PutObject(o);
|
---|
| 1639 | }
|
---|
[219] | 1640 |
|
---|
[802] | 1641 |
|
---|
| 1642 | void
|
---|
| 1643 | POutPersist::PutPPObject(PPersist const* obj)
|
---|
| 1644 | {
|
---|
| 1645 | if (serializeNullAndRepeat(obj)) return; // NULL object or already written in stream
|
---|
| 1646 |
|
---|
| 1647 | // We have to write the object
|
---|
| 1648 | uint_8 oid = assignObjectId(obj); // We assing a PPS Object Id
|
---|
| 1649 | PutRawUByte(PPS_OBJECT); // We write the Object Tag
|
---|
| 1650 | PutRawU8(getPPClassId(*obj)); // Writing the PPersist ClassId
|
---|
| 1651 | PutRawU8(oid); // Write the PPS Object Id
|
---|
[241] | 1652 | obj->WriteSelf(*this);
|
---|
[802] | 1653 | PutRawUByte(PPS_ENDOBJECT); // We write the End-Of-Object Tag
|
---|
| 1654 | PutRawU8(oid); // and again its PPS Object Id
|
---|
[241] | 1655 | }
|
---|
[219] | 1656 |
|
---|
[241] | 1657 | bool
|
---|
| 1658 | POutPersist::serializeNullAndRepeat(PPersist const* x)
|
---|
[219] | 1659 | {
|
---|
[241] | 1660 | if (x == NULL) {
|
---|
[802] | 1661 | PutRawUByte(PPS_NULL);
|
---|
[241] | 1662 | return true;
|
---|
| 1663 | }
|
---|
[219] | 1664 |
|
---|
[802] | 1665 | int_8 pos;
|
---|
| 1666 | uint_8 id = findObjectId(x, pos);
|
---|
| 1667 | if (id > 0) {
|
---|
| 1668 | PutRawUByte(PPS_REFERENCE);
|
---|
| 1669 | PutRawU8(id); // Writing the corresponding object Id
|
---|
| 1670 | PutRawI8(pos); // The original object position
|
---|
[241] | 1671 | return true;
|
---|
| 1672 | }
|
---|
[219] | 1673 |
|
---|
[802] | 1674 | return false; // Object have to be written in stream ...
|
---|
[219] | 1675 | }
|
---|
| 1676 |
|
---|
[802] | 1677 | uint_8
|
---|
[241] | 1678 | POutPersist::assignObjectId(PPersist const* x)
|
---|
[219] | 1679 | {
|
---|
[802] | 1680 | pps_OId += 16; // We keep the three first bytes for future usage
|
---|
| 1681 | // Bit 1 non zero -> Object can be referenced
|
---|
| 1682 | uint_8 id = pps_OId;
|
---|
| 1683 | uint_8 mid = x->getMemOId();
|
---|
| 1684 | if (mid > 0) {
|
---|
| 1685 | int_8 pos;
|
---|
| 1686 | if (findObjectId(x,pos) > 0)
|
---|
| 1687 | throw PError("POutPersist::assignObjectId() Error - Already serialized object ! ");
|
---|
| 1688 | id += 1; // Object Id starting at zero
|
---|
| 1689 | objreftag rt;
|
---|
| 1690 | rt.ppsoid = id;
|
---|
| 1691 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 1692 | rt.ppspos = s->tellp().offset();
|
---|
| 1693 | #else
|
---|
| 1694 | rt.ppspos = s->tellp();
|
---|
| 1695 | #endif
|
---|
| 1696 | objList[mid] = rt;
|
---|
| 1697 | }
|
---|
[241] | 1698 | return id;
|
---|
[219] | 1699 | }
|
---|
| 1700 |
|
---|
[802] | 1701 | uint_8
|
---|
| 1702 | POutPersist::findObjectId(PPersist const* x, int_8 & pos)
|
---|
[219] | 1703 | {
|
---|
[802] | 1704 | pos = -1;
|
---|
| 1705 | uint_8 mid = x->getMemOId();
|
---|
| 1706 | if (mid == 0) return(0);
|
---|
| 1707 | ObjList::iterator i = objList.find(mid);
|
---|
| 1708 | if (i == objList.end()) return 0;
|
---|
| 1709 | pos = (*i).second.ppspos;
|
---|
| 1710 | return (*i).second.ppsoid;
|
---|
[219] | 1711 | }
|
---|
| 1712 |
|
---|
[241] | 1713 |
|
---|