[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 "peidainit.h"
|
---|
| 7 | #include "ppersist.h"
|
---|
| 8 | #include <fstream.h>
|
---|
[241] | 9 | #include <typeinfo>
|
---|
[219] | 10 |
|
---|
| 11 |
|
---|
| 12 | #ifdef __mac__
|
---|
| 13 | #include "unixmac.h"
|
---|
| 14 | #include <SIOUX.h>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
[241] | 17 | using namespace PlanckDPC;
|
---|
[219] | 18 |
|
---|
[241] | 19 | #define MAXTAGLEN 255
|
---|
| 20 |
|
---|
[219] | 21 | //++
|
---|
[241] | 22 | // Class PIOPersist
|
---|
[219] | 23 | // Lib Outils++
|
---|
| 24 | // include ppersist.h
|
---|
| 25 | //
|
---|
[241] | 26 | // Root class for persistant files. Handles the registration of
|
---|
| 27 | // persistant classes
|
---|
[219] | 28 | //--
|
---|
| 29 |
|
---|
| 30 | //++
|
---|
[241] | 31 | // Links See
|
---|
[219] | 32 | // PPersist
|
---|
[241] | 33 | // PInPersist
|
---|
| 34 | // POutPersist
|
---|
[219] | 35 | //--
|
---|
| 36 |
|
---|
| 37 |
|
---|
[241] | 38 | MD5_CTX PIOPersist::ctx;
|
---|
| 39 | PIOPersist::ClassList PIOPersist::classList;
|
---|
| 40 |
|
---|
| 41 |
|
---|
[219] | 42 | //++
|
---|
| 43 | void
|
---|
[241] | 44 | PIOPersist::RegisterClass(uint_8 classId, ClassCreatorFunc f)
|
---|
[219] | 45 | //
|
---|
[241] | 46 | // Register a new persistant class.
|
---|
| 47 | // The classId is usually a hash of the class name, and this
|
---|
| 48 | // method is called only through the PPersistRegistrar template
|
---|
| 49 | // class, with the PPRegister(className) macro.
|
---|
[219] | 50 | //
|
---|
| 51 | //--
|
---|
| 52 | {
|
---|
[241] | 53 | if (classList.size() && (classList.find(classId) != classList.end())) {
|
---|
[219] | 54 | cerr << "RegisterClass : Error, " << hex << classId << dec
|
---|
| 55 | << " already registered." << endl;
|
---|
[241] | 56 | throw(DuplicateIdExc("PIOPersist::RegisterClass"));
|
---|
[219] | 57 | }
|
---|
| 58 |
|
---|
[241] | 59 | classList[classId] = f;
|
---|
[219] | 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
[241] | 63 | PIOPersist::ClassCreatorFunc
|
---|
| 64 | PIOPersist::FindCreatorFunc(uint_8 classId)
|
---|
[219] | 65 | {
|
---|
[241] | 66 | ClassList::iterator i = classList.find(classId);
|
---|
| 67 | if (i == classList.end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc"));
|
---|
| 68 | return (*i).second;
|
---|
[219] | 69 | }
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | //++
|
---|
| 73 | // Class PPersist
|
---|
| 74 | // Lib Outils++
|
---|
| 75 | // include ppersist.h
|
---|
| 76 | //
|
---|
| 77 | // Classe de base pour des objets persistants. Pour créer un objet
|
---|
| 78 | // persistant :
|
---|
| 79 | // - Hériter de PPersist.
|
---|
| 80 | // - Définir un numéro d'identification de la classe, unique dans Peida
|
---|
| 81 | // - Implémenter "ClassId()"
|
---|
| 82 | // - Implémenter "WriteSelf" et "ReadSelf", qui doivent écrire toutes les variables
|
---|
| 83 | // membres que l'on souhaite écrire, et les relire dans le même ordre.
|
---|
| 84 | // Pour écrire une référence à un objet : l'objet doit être un PPersist,
|
---|
| 85 | // et il suffit d'appeler "Write" sur cet objet, et "PPersistMgr::ReadObject".
|
---|
| 86 | // Si plusieurs objets font référence au même, pour éviter de l'écrire plusieurs
|
---|
| 87 | // fois, il faut que cet objet soit un PShPersist.
|
---|
| 88 | // - Pour que le fichier soit portable, écrire et lire les variables membres en utilisant
|
---|
| 89 | // les fonctions PutXX/GetXX de PInPersist/POutPersist.
|
---|
| 90 | //
|
---|
| 91 | // Attention: les méthodes à redéfinir sont WriteSelf et ReadSelf, mais il ne faut jamais
|
---|
| 92 | // les appeler directement. Seuls Write et Read peuvent être appelées par l'utilisateur.
|
---|
| 93 | //--
|
---|
| 94 |
|
---|
| 95 | //++
|
---|
[241] | 96 | // Links See
|
---|
[219] | 97 | // PInPersist
|
---|
| 98 | // POutPersist
|
---|
[241] | 99 | // PIOPersist
|
---|
[219] | 100 | //--
|
---|
| 101 |
|
---|
| 102 | //++
|
---|
| 103 | void
|
---|
| 104 | PPersist::Write(string const& fn) const
|
---|
| 105 | //
|
---|
| 106 | // Ecrit l'objet dans un nouveau fichier ppersist "fn".
|
---|
| 107 | //--
|
---|
| 108 | {
|
---|
| 109 | POutPersist of(fn);
|
---|
| 110 | Write(of);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | //++
|
---|
| 114 | void
|
---|
| 115 | PPersist::Read(string const& fn)
|
---|
| 116 | //
|
---|
| 117 | // Relit l'objet dans le fichier ppersist "fn". Il faut connaître a priori
|
---|
| 118 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
| 119 | // objet, utiliser PPersistMgr::ReadObject.
|
---|
| 120 | //--
|
---|
| 121 | {
|
---|
| 122 | PInPersist inf(fn);
|
---|
| 123 | Read(inf);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | //++
|
---|
| 127 | void
|
---|
| 128 | PPersist::Write(POutPersist& s) const
|
---|
| 129 | //
|
---|
| 130 | // Ecrit l'objet dans le fichier PPersist.
|
---|
| 131 | //--
|
---|
| 132 | {
|
---|
[241] | 133 | s.PutObject(this);
|
---|
[219] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | //++
|
---|
| 138 | void
|
---|
| 139 | PPersist::Read(PInPersist& s)
|
---|
| 140 | //
|
---|
| 141 | // Relit l'objet dans le fichier ppersist. Il faut connaître a priori
|
---|
| 142 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
[241] | 143 | // objet, utiliser PInPersist::ReadObject.
|
---|
| 144 | // Il faut qu'on soit un objet ecrit
|
---|
[219] | 145 | //--
|
---|
| 146 | {
|
---|
[241] | 147 | // We should be the exact type
|
---|
| 148 | // Check tag value
|
---|
| 149 | char ppstype;
|
---|
| 150 | s.GetRawByte(ppstype);
|
---|
| 151 | if (ppstype != PInPersist::PPS_OBJECT) {
|
---|
| 152 | throw TypeMismatchExc("PPersist::Read : not an object in flow");
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | // Check class id
|
---|
| 156 | uint_8 classId;
|
---|
| 157 | s.GetRawU8(classId);
|
---|
| 158 | if (classId != PIOPersist::Hash(typeid(*this).name())) {
|
---|
| 159 | throw TypeMismatchExc("PPersist::Read : not the same object type");
|
---|
| 160 | }
|
---|
[219] | 161 |
|
---|
[241] | 162 | ReadSelf(s);
|
---|
[219] | 163 | }
|
---|
| 164 |
|
---|
| 165 | //++
|
---|
[241] | 166 | void
|
---|
| 167 | PPersist::Write(POutPersist& s, string const& tag) const
|
---|
[219] | 168 | //
|
---|
[241] | 169 | // Ecrit l'objet dans le fichier PPersist avec un tag
|
---|
[219] | 170 | //--
|
---|
| 171 | {
|
---|
[241] | 172 | s.WriteTag(tag);
|
---|
| 173 | s.PutObject(this);
|
---|
[219] | 174 | }
|
---|
| 175 |
|
---|
| 176 | //++
|
---|
| 177 | void
|
---|
[241] | 178 | PPersist::ReadAtTag(PInPersist& s, string const& tag)
|
---|
[219] | 179 | //
|
---|
| 180 | // Lit l'objet à la position du tag numéro "tagid".
|
---|
| 181 | //--
|
---|
| 182 | {
|
---|
[241] | 183 | if (!s.GotoTag(tag))
|
---|
| 184 | throw NotFoundExc("PPersist::ReadAtTag tag not found");
|
---|
| 185 | Read(s);
|
---|
[219] | 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | //++
|
---|
| 190 | // virtual void PPersist::ReadSelf(PInPersist&)=0
|
---|
| 191 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Read
|
---|
| 192 | // et PPersistMgr::ReadObject. Il faut relire les variables membres,
|
---|
| 193 | // dans l'ordre où elles ont été écrites par WriteSelf.
|
---|
| 194 | // virtual void PPersist::WriteSelf(POutPersist&) const=0
|
---|
| 195 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Write.
|
---|
| 196 | // Il faut écrire les variables membres,
|
---|
| 197 | // dans l'ordre où elles seront relues par ReadSelf.
|
---|
| 198 | //--
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | //++
|
---|
| 203 | // Class PInPersist
|
---|
| 204 | // Lib Outils++
|
---|
| 205 | // include ppersist.h
|
---|
| 206 | //
|
---|
| 207 | // Fichier d'objets persistants, en lecture.
|
---|
| 208 | //--
|
---|
| 209 |
|
---|
| 210 | //++
|
---|
| 211 | PInPersist::PInPersist(string const& flnm, bool scan)
|
---|
| 212 | //
|
---|
| 213 | // Constructeur. Ouvre le fichier.
|
---|
| 214 | //--
|
---|
| 215 | {
|
---|
[241] | 216 | s = new ifstream(flnm.c_str(),ios::in | IOS_BIN);
|
---|
| 217 |
|
---|
| 218 | // Read and check header
|
---|
| 219 |
|
---|
[219] | 220 | char rbuf[36];
|
---|
[241] | 221 | GetRawBytes(rbuf, 32);
|
---|
| 222 | if (strncmp(rbuf,"PlanckDPC-PPersistFile", 22) != 0) {
|
---|
| 223 | throw FileFormatExc("PInPersist::PInPersist bad header");
|
---|
[219] | 224 | }
|
---|
[241] | 225 | version = atoi(rbuf+24);
|
---|
| 226 |
|
---|
| 227 | // read endianness
|
---|
| 228 | GetRawBytes(rbuf, 32);
|
---|
| 229 | if (strncmp(rbuf,"BIG-ENDIAN",10) == 0)
|
---|
| 230 | bigEndian = true;
|
---|
| 231 | else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0)
|
---|
| 232 | bigEndian = false;
|
---|
| 233 | else {
|
---|
| 234 | throw FileFormatExc("PInPersist::PInPersist bad header - endianness");
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | // read creation date
|
---|
| 238 | GetRawBytes(rbuf, 32);
|
---|
| 239 | rbuf[32] = '\0';
|
---|
| 240 | struct tm tm;
|
---|
| 241 | strptime(rbuf,"%d/%m/%Y %T GMT",&tm);
|
---|
| 242 | creationdate = mktime(&tm);
|
---|
| 243 |
|
---|
| 244 | if (scan) Scan();
|
---|
[219] | 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | PInPersist::~PInPersist()
|
---|
| 250 | {
|
---|
| 251 | delete s;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | void
|
---|
| 256 | PInPersist::Scan()
|
---|
| 257 | {
|
---|
[241] | 258 | // On cherche la liste des tags, a la fin du fichier
|
---|
[219] | 259 |
|
---|
[241] | 260 | char ppstype;
|
---|
| 261 | size_t debut;
|
---|
| 262 | #ifdef STREAMPOS_IS_CLASS
|
---|
[219] | 263 | debut = s->tellg().offset();
|
---|
[241] | 264 | #else
|
---|
[219] | 265 | debut = s->tellg();
|
---|
[241] | 266 | #endif
|
---|
[219] | 267 |
|
---|
[241] | 268 | // Find tag entries at end of file
|
---|
| 269 | s->seekg(-(sizeof(int_8)+1), ios::end);
|
---|
| 270 | GetRawByte(ppstype);
|
---|
| 271 | if (ppstype != PPS_EOF)
|
---|
| 272 | throw FileFormatExc("PInPersist::Scan corrupted file, no eof entry at end of file");
|
---|
[219] | 273 |
|
---|
[241] | 274 | int_8 pos;
|
---|
| 275 | GetRawI8(pos);
|
---|
| 276 | if (pos < 0) { // no tags
|
---|
| 277 | s->seekg(debut);
|
---|
| 278 | return;
|
---|
| 279 | }
|
---|
[219] | 280 |
|
---|
[241] | 281 | char buffer[MAXTAGLEN+1];
|
---|
| 282 | s->seekg(pos);
|
---|
| 283 | while (true) {
|
---|
| 284 | GetRawByte(ppstype);
|
---|
| 285 | if (ppstype == PPS_EOF) break;
|
---|
| 286 |
|
---|
| 287 | if (ppstype != PPS_TAG)
|
---|
| 288 | throw FileFormatExc("PInPersist::Scan corrupted file, bad tag entry");
|
---|
[219] | 289 |
|
---|
[241] | 290 | GetRawI8(pos);
|
---|
| 291 | int_4 len;
|
---|
| 292 | GetRawI4(len);
|
---|
| 293 | if (len > MAXTAGLEN)
|
---|
| 294 | throw FileFormatExc("PInPersist::Scan corrupted file, tag name too long");
|
---|
| 295 | GetRawBytes(buffer, len);
|
---|
| 296 | buffer[len] = '\0';
|
---|
[219] | 297 |
|
---|
[241] | 298 | tags[buffer] = pos;
|
---|
[219] | 299 | }
|
---|
[241] | 300 | s->seekg(debut);
|
---|
[219] | 301 | }
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | bool
|
---|
[241] | 305 | PInPersist::GotoTag(string const& name)
|
---|
[219] | 306 | {
|
---|
[241] | 307 | map<string, int_8>::iterator i = tags.find(name);
|
---|
| 308 | if (i == tags.end())
|
---|
| 309 | return false;
|
---|
| 310 | // throw NotFoundExc("PInPersist::GotoTag tag not found");
|
---|
| 311 | s->seekg((*i).second);
|
---|
[219] | 312 | return(true);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | //++
|
---|
| 316 | // void PInPersist::GetByte(char& c)
|
---|
| 317 | // void PInPersist::GetBytes(void* ptr, size_t bytes)
|
---|
| 318 | // void PInPersist::GetR4 (r_4& result)
|
---|
| 319 | // void PInPersist::GetR4s (r_4* tab, size_t n)
|
---|
| 320 | // void PInPersist::GetR8 (r_8& result)
|
---|
| 321 | // void PInPersist::GetR8s (r_8* tab, size_t n)
|
---|
| 322 | // void PInPersist::GetI2 (int_2& result)
|
---|
| 323 | // void PInPersist::GetI2s (int_2* tab, size_t n)
|
---|
| 324 | // void PInPersist::GetU2 (uint_2& result)
|
---|
| 325 | // void PInPersist::GetU2s (uint_2* tab, size_t n)
|
---|
| 326 | // void PInPersist::GetI4 (int_4& result)
|
---|
| 327 | // void PInPersist::GetI4s (int_4* tab, size_t n)
|
---|
| 328 | // void PInPersist::GetU4 (uint_4& result)
|
---|
| 329 | // void PInPersist::GetU4s (uint_4* tab, size_t n)
|
---|
| 330 | // void PInPersist::GetI8 (int_8& result)
|
---|
| 331 | // void PInPersist::GetI8s (int_8* tab, size_t n)
|
---|
| 332 | // void PInPersist::GetU8 (uint_8& result)
|
---|
| 333 | // void PInPersist::GetU8s (uint_8* tab, size_t n)
|
---|
| 334 | // Lecture de données portables depuis le fichier PPersist. Pour chaque type
|
---|
| 335 | // de données, on peut lire une valeur, ou un tableau de valeurs.
|
---|
| 336 | // void PInPersist::GetLine(char* ptr, size_t len)
|
---|
| 337 | // Lecture d'une ligne de texte depuis le fichier PPersist.
|
---|
| 338 | //--
|
---|
| 339 |
|
---|
| 340 |
|
---|
| 341 | static inline void bswap8(void* p)
|
---|
| 342 | {
|
---|
| 343 | uint_8 tmp = *(uint_8*)p;
|
---|
| 344 | *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |
|
---|
| 345 | ((tmp >> (5*8)) & 0x0000FF00) |
|
---|
| 346 | ((tmp >> (3*8)) & 0x00FF0000) |
|
---|
| 347 | ((tmp >> (1*8)) & 0xFF000000) |
|
---|
| 348 | ((tmp & 0xFF000000) << (1*8)) |
|
---|
| 349 | ((tmp & 0x00FF0000) << (3*8)) |
|
---|
| 350 | ((tmp & 0x0000FF00) << (5*8)) |
|
---|
| 351 | ((tmp & 0x000000FF) << (7*8));
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | static inline void bswap4(void* p)
|
---|
| 355 | {
|
---|
| 356 | uint_4 tmp = *(uint_4*)p;
|
---|
| 357 | *(uint_4*)p = ((tmp >> 24) & 0x000000FF) |
|
---|
| 358 | ((tmp >> 8) & 0x0000FF00) |
|
---|
| 359 | ((tmp & 0x0000FF00) << 8) |
|
---|
| 360 | ((tmp & 0x000000FF) << 24);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | static inline void bswap2(void* p)
|
---|
| 364 | {
|
---|
| 365 | uint_2 tmp = *(uint_2*)p;
|
---|
| 366 | *(uint_2*)p = ((tmp >> 8) & 0x00FF) |
|
---|
| 367 | ((tmp & 0x00FF) << 8);
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[241] | 370 |
|
---|
[219] | 371 | void
|
---|
[241] | 372 | PInPersist::GetRawByte(char& c)
|
---|
| 373 | {
|
---|
| 374 | GetRawBytes(&c, 1);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | void
|
---|
| 378 | PInPersist::GetRawBytes(void* ptr, size_t bytes)
|
---|
| 379 | {
|
---|
| 380 | s->read((char*)ptr, bytes);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | void
|
---|
| 384 | PInPersist::GetRawI2 (int_2& result)
|
---|
| 385 | {
|
---|
| 386 | GetRawBytes(&result, sizeof(int_2));
|
---|
| 387 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 388 | bswap2(&result);
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | void
|
---|
| 392 | PInPersist::GetRawI4 (int_4& result)
|
---|
| 393 | {
|
---|
| 394 | GetRawBytes(&result, sizeof(int_4));
|
---|
| 395 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 396 | bswap4(&result);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | void
|
---|
| 400 | PInPersist::GetRawI8 (int_8& result)
|
---|
| 401 | {
|
---|
| 402 | GetRawBytes(&result, sizeof(int_8));
|
---|
| 403 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 404 | bswap8(&result);
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | void
|
---|
| 408 | PInPersist::GetRawU8 (uint_8& result)
|
---|
| 409 | {
|
---|
| 410 | GetRawBytes(&result, sizeof(uint_8));
|
---|
| 411 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 412 | bswap8(&result);
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | void
|
---|
| 416 | PInPersist::CheckTag(short datasz)
|
---|
| 417 | {
|
---|
| 418 | char ppstype;
|
---|
| 419 | GetRawByte(ppstype);
|
---|
| 420 | if (ppstype != PPS_SIMPLE + datasz)
|
---|
| 421 | throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file");
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | void
|
---|
| 425 | PInPersist::CheckArrayTag(short datasz, size_t sz)
|
---|
| 426 | {
|
---|
| 427 | char ppstype;
|
---|
| 428 | GetRawByte(ppstype);
|
---|
| 429 | size_t filesz;
|
---|
| 430 | if (sz <= 0x7fff) {
|
---|
| 431 | if (ppstype != PPS_SIMPLE_ARRAY + datasz)
|
---|
| 432 | throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file");
|
---|
| 433 | int_2 ff;
|
---|
| 434 | GetRawI2(ff); filesz=ff;
|
---|
| 435 | } else if (sz <= 0x7fffffff) {
|
---|
| 436 | if (ppstype != PPS_SIMPLE_ARRAY4 + datasz)
|
---|
| 437 | throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file");
|
---|
| 438 | int_4 ff;
|
---|
| 439 | GetRawI4(ff); filesz=ff;
|
---|
| 440 | } else {
|
---|
| 441 | if (ppstype != PPS_SIMPLE_ARRAY8 + datasz)
|
---|
| 442 | throw TypeMismatchExc("PInPersist::CheckTag bad type in ppersist file");
|
---|
| 443 | uint_8 ff;
|
---|
| 444 | GetRawU8(ff); filesz=ff;
|
---|
| 445 | }
|
---|
| 446 | if (filesz != sz)
|
---|
| 447 | throw TypeMismatchExc("PInPersist::CheckTag bad array size in ppersist file");
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | void
|
---|
| 451 | PInPersist::GetByte(char& c)
|
---|
| 452 | {
|
---|
| 453 | CheckTag(1);
|
---|
| 454 | GetRawBytes(&c, 1);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | void
|
---|
| 458 | PInPersist::GetBytes(void* ptr, size_t bytes)
|
---|
| 459 | {
|
---|
| 460 | CheckArrayTag(1, bytes);
|
---|
| 461 | GetRawBytes(ptr, bytes);
|
---|
| 462 | }
|
---|
| 463 | void
|
---|
[219] | 464 | PInPersist::GetR4 (r_4& result)
|
---|
| 465 | {
|
---|
[241] | 466 | CheckTag(4);
|
---|
| 467 | GetRawBytes(&result, sizeof(r_4));
|
---|
[219] | 468 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 469 | bswap4(&result);
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 |
|
---|
| 473 | void
|
---|
| 474 | PInPersist::GetR4s (r_4* tab, size_t n)
|
---|
| 475 | {
|
---|
[241] | 476 | CheckArrayTag(4,n);
|
---|
| 477 | GetRawBytes(tab, n*sizeof(r_4));
|
---|
[219] | 478 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 479 |
|
---|
| 480 | for (unsigned int i=0; i<n; i++)
|
---|
| 481 | bswap4(tab+i);
|
---|
| 482 |
|
---|
| 483 | return;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | void
|
---|
| 487 | PInPersist::GetR8 (r_8& result)
|
---|
| 488 | {
|
---|
[241] | 489 | CheckTag(8);
|
---|
| 490 | GetRawBytes(&result, sizeof(r_8));
|
---|
[219] | 491 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 492 | bswap8(&result);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | void
|
---|
| 496 | PInPersist::GetR8s (r_8* tab, size_t n)
|
---|
| 497 | {
|
---|
[241] | 498 | CheckArrayTag(8,n);
|
---|
| 499 | GetRawBytes(tab, n*sizeof(r_8));
|
---|
[219] | 500 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 501 |
|
---|
| 502 | for (unsigned int i=0; i<n; i++)
|
---|
| 503 | bswap8(tab+i);
|
---|
| 504 |
|
---|
| 505 | return;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | void
|
---|
| 509 | PInPersist::GetI2 (int_2& result)
|
---|
| 510 | {
|
---|
[241] | 511 | CheckTag(2);
|
---|
| 512 | GetRawBytes(&result, sizeof(int_2));
|
---|
[219] | 513 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 514 | bswap2(&result);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | void
|
---|
| 518 | PInPersist::GetI2s (int_2* tab, size_t n)
|
---|
| 519 | {
|
---|
[241] | 520 | CheckArrayTag(2,n);
|
---|
| 521 | GetRawBytes(tab, n*sizeof(int_2));
|
---|
[219] | 522 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 523 |
|
---|
| 524 | for (unsigned int i=0; i<n; i++)
|
---|
| 525 | bswap2(tab+i);
|
---|
| 526 |
|
---|
| 527 | return;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | void
|
---|
| 531 | PInPersist::GetU2 (uint_2& result)
|
---|
| 532 | {
|
---|
[241] | 533 | CheckTag(2);
|
---|
| 534 | GetRawBytes(&result, sizeof(uint_2));
|
---|
[219] | 535 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 536 | bswap2(&result);
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | void
|
---|
| 540 | PInPersist::GetU2s (uint_2* tab, size_t n)
|
---|
| 541 | {
|
---|
[241] | 542 | CheckArrayTag(2,n);
|
---|
| 543 | GetRawBytes(tab, n*sizeof(uint_2));
|
---|
[219] | 544 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 545 |
|
---|
| 546 | for (unsigned int i=0; i<n; i++)
|
---|
| 547 | bswap2(tab+i);
|
---|
| 548 |
|
---|
| 549 | return;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | void
|
---|
| 553 | PInPersist::GetI4 (int_4& result)
|
---|
| 554 | {
|
---|
[241] | 555 | CheckTag(4);
|
---|
| 556 | GetRawBytes(&result, sizeof(int_4));
|
---|
[219] | 557 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 558 | bswap4(&result);
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | void
|
---|
| 562 | PInPersist::GetI4s (int_4* tab, size_t n)
|
---|
| 563 | {
|
---|
[241] | 564 | CheckArrayTag(4,n);
|
---|
| 565 | GetRawBytes(tab, n*sizeof(int_4));
|
---|
[219] | 566 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 567 |
|
---|
| 568 | for (unsigned int i=0; i<n; i++)
|
---|
| 569 | bswap4(tab+i);
|
---|
| 570 |
|
---|
| 571 | return;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | void
|
---|
| 575 | PInPersist::GetU4 (uint_4& result)
|
---|
| 576 | {
|
---|
[241] | 577 | CheckTag(4);
|
---|
| 578 | GetRawBytes(&result, sizeof(uint_4));
|
---|
[219] | 579 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 580 | bswap4(&result);
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | void
|
---|
| 584 | PInPersist::GetU4s (uint_4* tab, size_t n)
|
---|
| 585 | {
|
---|
[241] | 586 | CheckArrayTag(4,n);
|
---|
| 587 | GetRawBytes(tab, n*sizeof(uint_4));
|
---|
[219] | 588 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 589 |
|
---|
| 590 | for (unsigned int i=0; i<n; i++)
|
---|
| 591 | bswap4(tab+i);
|
---|
| 592 |
|
---|
| 593 | return;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 |
|
---|
| 597 | void
|
---|
| 598 | PInPersist::GetI8 (int_8& result)
|
---|
| 599 | {
|
---|
[241] | 600 | CheckTag(8);
|
---|
| 601 | GetRawBytes(&result, sizeof(int_8));
|
---|
[219] | 602 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 603 | bswap8(&result);
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | void
|
---|
| 607 | PInPersist::GetI8s (int_8* tab, size_t n)
|
---|
| 608 | {
|
---|
[241] | 609 | CheckArrayTag(8,n);
|
---|
| 610 | GetRawBytes(tab, n*sizeof(int_8));
|
---|
[219] | 611 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 612 |
|
---|
| 613 | for (unsigned int i=0; i<n; i++)
|
---|
| 614 | bswap8(tab+i);
|
---|
| 615 |
|
---|
| 616 | return;
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | void
|
---|
| 620 | PInPersist::GetU8 (uint_8& result)
|
---|
| 621 | {
|
---|
[241] | 622 | CheckTag(8);
|
---|
| 623 | GetRawBytes(&result, sizeof(uint_8));
|
---|
[219] | 624 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 625 | bswap8(&result);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | void
|
---|
| 629 | PInPersist::GetU8s (uint_8* tab, size_t n)
|
---|
| 630 | {
|
---|
[241] | 631 | CheckArrayTag(8,n);
|
---|
| 632 | GetRawBytes(tab, n*sizeof(uint_8));
|
---|
[219] | 633 | if (bigEndian == IS_BIG_ENDIAN) return;
|
---|
| 634 |
|
---|
| 635 | for (unsigned int i=0; i<n; i++)
|
---|
| 636 | bswap8(tab+i);
|
---|
| 637 |
|
---|
| 638 | return;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 |
|
---|
| 642 | void
|
---|
| 643 | PInPersist::GetLine(char* ptr, size_t len)
|
---|
| 644 | {
|
---|
[241] | 645 | char ppstype;
|
---|
| 646 | GetRawByte(ppstype);
|
---|
| 647 | if (ppstype != PPS_LINE)
|
---|
| 648 | throw TypeMismatchExc("PInPersist::GetLine bad type in ppersist file");
|
---|
| 649 | s->getline(ptr, len, '\n');
|
---|
[219] | 650 | }
|
---|
| 651 |
|
---|
[241] | 652 | void
|
---|
| 653 | PInPersist::GetStr(string& str)
|
---|
| 654 | {
|
---|
| 655 | char ppstype;
|
---|
| 656 | GetRawByte(ppstype);
|
---|
| 657 | if (ppstype != PPS_STRING)
|
---|
| 658 | throw TypeMismatchExc("PInPersist::GetLine bad type in ppersist file");
|
---|
| 659 | int_2 len;
|
---|
| 660 | GetRawI2(len);
|
---|
| 661 | char * buff = new char(len+1);
|
---|
| 662 | GetRawBytes(buff, len);
|
---|
| 663 | buff[len] = '\0';
|
---|
| 664 | str = buff;
|
---|
| 665 | delete[] buff;
|
---|
| 666 | }
|
---|
[219] | 667 |
|
---|
[241] | 668 | PPersist*
|
---|
| 669 | PInPersist::ReadObject()
|
---|
| 670 | {
|
---|
| 671 | // Get tag
|
---|
| 672 | char ppstype;
|
---|
| 673 | GetRawByte(ppstype);
|
---|
| 674 | if (ppstype != PPS_OBJECT && ppstype != PPS_REFERENCE && ppstype != PPS_NULL) {
|
---|
| 675 | throw TypeMismatchExc("PInPersist::ReadObject : not an object in flow");
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | if (ppstype == PPS_NULL) {
|
---|
| 679 | return NULL;
|
---|
| 680 | } else if (ppstype == PPS_OBJECT) {
|
---|
| 681 | // Get class id
|
---|
| 682 | uint_8 classId;
|
---|
| 683 | GetRawU8(classId);
|
---|
| 684 |
|
---|
| 685 | // Get factory method
|
---|
| 686 | ClassCreatorFunc f = FindCreatorFunc(classId);
|
---|
| 687 | if (!f) {
|
---|
| 688 | throw NotFoundExc("PInPersist::ReadObject class not registered");
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | // Create object
|
---|
| 692 | PPersist* object = f();
|
---|
| 693 | object->ReadSelf(*this);
|
---|
| 694 | assignObjectId(object);
|
---|
| 695 | return object;
|
---|
| 696 | } else {
|
---|
| 697 | // Get object id
|
---|
| 698 | int_4 id;
|
---|
| 699 | GetRawI4(id);
|
---|
| 700 | if (id <0 || id>=objList.size()) {
|
---|
| 701 | throw FileFormatExc("PInPersist::ReadObject invalid object id for reference");
|
---|
| 702 | }
|
---|
| 703 | return objList[id];
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | int_4
|
---|
| 708 | PInPersist::assignObjectId(PPersist* x)
|
---|
| 709 | {
|
---|
| 710 | objList.push_back(x);
|
---|
| 711 | return objList.size()-1;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[219] | 714 | //++
|
---|
| 715 | // Class POutPersist
|
---|
| 716 | // Lib Outils++
|
---|
| 717 | // include ppersist.h
|
---|
| 718 | //
|
---|
| 719 | // Fichier d'objets persistants, en écriture.
|
---|
| 720 | //--
|
---|
| 721 |
|
---|
| 722 |
|
---|
| 723 | //++
|
---|
| 724 | // POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE)
|
---|
| 725 | //
|
---|
| 726 | // Crée un nouveau fichier ppersist. Par défaut, il est petit=boutien
|
---|
| 727 | // sur machines petit-boutiennes, et gros-boutien sur machines
|
---|
| 728 | // gros-boutiennes. On peut explicitement spécifier PPersist::PPS_LITTLE_ENDIAN
|
---|
| 729 | // ou PPersist::PPS_BIG_ENDIAN.
|
---|
| 730 | //--
|
---|
| 731 | POutPersist::POutPersist(string const& flnm, int endianness)
|
---|
| 732 | {
|
---|
| 733 | if (endianness == -1)
|
---|
| 734 | bigEndian = IS_BIG_ENDIAN;
|
---|
| 735 | else
|
---|
| 736 | bigEndian = endianness;
|
---|
| 737 |
|
---|
[241] | 738 | // Output stream creation
|
---|
| 739 | s = new ofstream(flnm.c_str(),ios::out | IOS_BIN);
|
---|
| 740 |
|
---|
| 741 | // Header
|
---|
| 742 | PutRawBytes("PlanckDPC-PPersistFile V1 ",32);
|
---|
| 743 | PutRawBytes(bigEndian
|
---|
| 744 | ? "BIG-ENDIAN "
|
---|
| 745 | : "LITTLE-ENDIAN ",32);
|
---|
| 746 |
|
---|
| 747 | // ---- GMT creation date of the file
|
---|
| 748 | time_t tm = time(NULL);
|
---|
| 749 | char datestring[33];
|
---|
| 750 | int l=strftime(datestring,32,"%d/%m/%Y %T GMT",gmtime(&tm));
|
---|
| 751 | for(int i=l; i<32; i++) datestring[i] = ' ';
|
---|
| 752 | datestring[32] = '\0';
|
---|
| 753 | PutRawBytes(datestring, 32);
|
---|
[219] | 754 | }
|
---|
| 755 |
|
---|
| 756 | POutPersist::~POutPersist()
|
---|
| 757 | {
|
---|
[241] | 758 | if (tags.size() == 0) {
|
---|
| 759 | PutRawByte(PPS_EOF);
|
---|
| 760 | PutRawI8(-1);
|
---|
| 761 | } else {
|
---|
| 762 | int_8 tagPos;
|
---|
| 763 | #ifdef STREAMPOS_IS_CLASS
|
---|
| 764 | tagPos = s->tellp().offset();
|
---|
| 765 | #else
|
---|
| 766 | tagPos = s->tellp();
|
---|
| 767 | #endif
|
---|
| 768 | for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
|
---|
| 769 | string name = (*i).first;
|
---|
| 770 | int_8 pos = (*i).second;
|
---|
| 771 | PutRawByte(PPS_TAG); // This is a tag
|
---|
| 772 | PutRawI8(pos); // position of previous tag
|
---|
| 773 | PutRawI4(name.length()); // length of the name
|
---|
| 774 | PutRawBytes(name.c_str(), name.length()); // name, without final "0".
|
---|
| 775 | }
|
---|
| 776 | PutRawByte(PPS_EOF);
|
---|
| 777 | PutRawI8(tagPos);
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | delete s; // Close the stream
|
---|
[219] | 781 | }
|
---|
| 782 |
|
---|
| 783 |
|
---|
[241] | 784 | void
|
---|
| 785 | POutPersist::WriteTag(string const& name)
|
---|
[219] | 786 | {
|
---|
[241] | 787 | if (name.length() > MAXTAGLEN)
|
---|
| 788 | throw ParmError("POutPersist::WriteTag tag name too long");
|
---|
[219] | 789 |
|
---|
[241] | 790 | if (tags.find(name) != tags.end())
|
---|
| 791 | throw DuplicateIdExc("POutPersist::WriteTag duplicate tag name");
|
---|
| 792 |
|
---|
| 793 | // Get current file position
|
---|
| 794 | int_8 tagPos;
|
---|
| 795 |
|
---|
[219] | 796 | #ifdef STREAMPOS_IS_CLASS
|
---|
[241] | 797 | tagPos = s->tellp().offset();
|
---|
[219] | 798 | #else
|
---|
[241] | 799 | tagPos = s->tellp();
|
---|
[219] | 800 | #endif
|
---|
[241] | 801 |
|
---|
| 802 | tags[name] = tagPos;
|
---|
[219] | 803 | }
|
---|
| 804 |
|
---|
| 805 | //++
|
---|
| 806 | // void POutPersist::PutByte(char& c)
|
---|
| 807 | // void POutPersist::PutBytes(void const* ptr, size_t bytes)
|
---|
| 808 | // void POutPersist::PutR4 (r_4 result)
|
---|
| 809 | // void POutPersist::PutR4s (r_4 const* tab, size_t n)
|
---|
| 810 | // void POutPersist::PutR8 (r_8 result)
|
---|
| 811 | // void POutPersist::PutR8s (r_8 const* tab, size_t n)
|
---|
| 812 | // void POutPersist::PutI2 (int_2 result)
|
---|
| 813 | // void POutPersist::PutI2s (int_2 const* tab, size_t n)
|
---|
| 814 | // void POutPersist::PutU2 (uint_2 result)
|
---|
| 815 | // void POutPersist::PutU2s (uint_2 const* tab, size_t n)
|
---|
| 816 | // void POutPersist::PutI4 (int_4 result)
|
---|
| 817 | // void POutPersist::PutI4s (int_4 const* tab, size_t n)
|
---|
| 818 | // void POutPersist::PutU4 (uint_4 result)
|
---|
| 819 | // void POutPersist::PutU4s (uint_4 const* tab, size_t n)
|
---|
| 820 | // void POutPersist::PutI8 (int_8 result)
|
---|
| 821 | // void POutPersist::PutI8s (int_8 const* tab, size_t n)
|
---|
| 822 | // void POutPersist::PutU8 (uint_8 result)
|
---|
| 823 | // void POutPersist::PutU8s (uint_8 const* tab, size_t n)
|
---|
[241] | 824 | // void POutPersist::PutStr (string const&)
|
---|
[219] | 825 | // Ecriture de données portables.. Pour chaque type
|
---|
| 826 | // de données, on peut écrire une valeur, ou un tableau de valeurs.
|
---|
| 827 | // void POutPersist::PutLine(char const* ptr, size_t len)
|
---|
| 828 | // Ecriture d'une ligne de texte dans le fichier PPersist.
|
---|
| 829 | //--
|
---|
| 830 |
|
---|
| 831 |
|
---|
| 832 |
|
---|
| 833 |
|
---|
| 834 | void
|
---|
[241] | 835 | POutPersist::PutRawBytes(void const* ptr, size_t bytes)
|
---|
| 836 | {
|
---|
| 837 | s->write((char const*)ptr, bytes);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | void
|
---|
| 841 | POutPersist::PutRawByte(char c)
|
---|
| 842 | {
|
---|
| 843 | PutRawBytes(&c, 1);
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | void
|
---|
| 847 | POutPersist::PutRawI2 (int_2 val)
|
---|
| 848 | {
|
---|
| 849 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 850 | bswap2(&val);
|
---|
| 851 |
|
---|
| 852 | PutRawBytes(&val, sizeof(int_2));
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | void
|
---|
| 856 | POutPersist::PutRawI4 (int_4 val)
|
---|
| 857 | {
|
---|
| 858 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 859 | bswap4(&val);
|
---|
| 860 |
|
---|
| 861 | PutRawBytes(&val, sizeof(int_4));
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | void
|
---|
| 865 | POutPersist::PutRawI8 (int_8 val)
|
---|
| 866 | {
|
---|
| 867 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 868 | bswap8(&val);
|
---|
| 869 |
|
---|
| 870 | PutRawBytes(&val, sizeof(int_8));
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 | void
|
---|
| 874 | POutPersist::PutRawU8 (uint_8 val)
|
---|
| 875 | {
|
---|
| 876 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 877 | bswap8(&val);
|
---|
| 878 |
|
---|
| 879 | PutRawBytes(&val, sizeof(uint_8));
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | void
|
---|
| 883 | POutPersist::PutArrayTag(short datasz, size_t sz)
|
---|
| 884 | {
|
---|
| 885 | if (sz <= 0x7fff) {
|
---|
| 886 | PutRawByte(PPS_SIMPLE_ARRAY + datasz);
|
---|
| 887 | PutRawI2(sz);
|
---|
| 888 | } else if (sz <= 0x7fffffff) {
|
---|
| 889 | PutRawByte(PPS_SIMPLE_ARRAY4 + datasz);
|
---|
| 890 | PutRawI4(sz);
|
---|
| 891 | } else {
|
---|
| 892 | PutRawByte(PPS_SIMPLE_ARRAY8 + datasz);
|
---|
| 893 | PutRawU8(sz);
|
---|
| 894 | }
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | void
|
---|
[219] | 898 | POutPersist::PutByte(char c)
|
---|
| 899 | {
|
---|
[241] | 900 | PutRawByte(PPS_SIMPLE + 1);
|
---|
| 901 | PutRawBytes(&c, 1);
|
---|
[219] | 902 | }
|
---|
| 903 |
|
---|
[241] | 904 |
|
---|
| 905 |
|
---|
[219] | 906 | void
|
---|
| 907 | POutPersist::PutBytes(void const* ptr, size_t bytes)
|
---|
| 908 | {
|
---|
[241] | 909 | PutArrayTag(1, bytes);
|
---|
| 910 | PutRawBytes(ptr, bytes);
|
---|
[219] | 911 | }
|
---|
| 912 |
|
---|
| 913 | void
|
---|
| 914 | POutPersist::PutR4 (r_4 val)
|
---|
| 915 | {
|
---|
[241] | 916 | PutRawByte(PPS_SIMPLE + 4);
|
---|
| 917 |
|
---|
[219] | 918 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 919 | bswap4(&val);
|
---|
| 920 |
|
---|
[241] | 921 | PutRawBytes(&val, sizeof(r_4));
|
---|
[219] | 922 | }
|
---|
| 923 |
|
---|
| 924 | void
|
---|
| 925 | POutPersist::PutR4s (r_4 const* tab, size_t n)
|
---|
| 926 | {
|
---|
[241] | 927 | PutArrayTag(4, n);
|
---|
| 928 |
|
---|
| 929 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 930 | PutRawBytes(tab, n*sizeof(r_4));
|
---|
| 931 | } else {
|
---|
| 932 | for (unsigned int i=0; i<n; i++) {
|
---|
| 933 | r_4 val = tab[i];
|
---|
| 934 | bswap4(&val);
|
---|
| 935 | PutRawBytes(&val, sizeof(r_4));
|
---|
| 936 | }
|
---|
| 937 | }
|
---|
[219] | 938 | }
|
---|
| 939 |
|
---|
| 940 | void
|
---|
| 941 | POutPersist::PutR8 (r_8 val)
|
---|
| 942 | {
|
---|
[241] | 943 | PutRawByte(PPS_SIMPLE + 8);
|
---|
| 944 |
|
---|
[219] | 945 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 946 | bswap8(&val);
|
---|
| 947 |
|
---|
[241] | 948 | PutRawBytes(&val, sizeof(r_8));
|
---|
[219] | 949 | }
|
---|
| 950 |
|
---|
| 951 | void
|
---|
| 952 | POutPersist::PutR8s (r_8 const* tab, size_t n)
|
---|
| 953 | {
|
---|
[241] | 954 | PutArrayTag(8, n);
|
---|
| 955 |
|
---|
| 956 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 957 | PutRawBytes(tab, n*sizeof(r_8));
|
---|
| 958 | } else {
|
---|
| 959 | for (unsigned int i=0; i<n; i++) {
|
---|
| 960 | r_8 val = tab[i];
|
---|
| 961 | bswap8(&val);
|
---|
| 962 | PutRawBytes(&val, sizeof(r_8));
|
---|
| 963 | }
|
---|
| 964 | }
|
---|
[219] | 965 | }
|
---|
| 966 |
|
---|
| 967 | void
|
---|
| 968 | POutPersist::PutI2 (int_2 val)
|
---|
| 969 | {
|
---|
[241] | 970 | PutRawByte(PPS_SIMPLE + 2);
|
---|
| 971 |
|
---|
[219] | 972 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 973 | bswap2(&val);
|
---|
| 974 |
|
---|
[241] | 975 | PutRawBytes(&val, sizeof(int_2));
|
---|
[219] | 976 | }
|
---|
| 977 |
|
---|
| 978 | void
|
---|
| 979 | POutPersist::PutI2s (int_2 const* tab, size_t n)
|
---|
| 980 | {
|
---|
[241] | 981 | PutArrayTag(2, n);
|
---|
| 982 |
|
---|
| 983 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 984 | PutRawBytes(tab, n*sizeof(int_2));
|
---|
| 985 | } else {
|
---|
| 986 | for (unsigned int i=0; i<n; i++) {
|
---|
| 987 | int_2 val = tab[i];
|
---|
| 988 | bswap2(&val);
|
---|
| 989 | PutRawBytes(&val, sizeof(int_2));
|
---|
| 990 | }
|
---|
| 991 | }
|
---|
[219] | 992 | }
|
---|
| 993 |
|
---|
| 994 | void
|
---|
| 995 | POutPersist::PutU2 (uint_2 val)
|
---|
| 996 | {
|
---|
[241] | 997 | PutRawByte(PPS_SIMPLE + 2);
|
---|
| 998 |
|
---|
[219] | 999 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1000 | bswap2(&val);
|
---|
| 1001 |
|
---|
[241] | 1002 | PutRawBytes(&val, sizeof(uint_2));
|
---|
[219] | 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | void
|
---|
| 1006 | POutPersist::PutU2s (uint_2 const* tab, size_t n)
|
---|
| 1007 | {
|
---|
[241] | 1008 | PutArrayTag(2, n);
|
---|
| 1009 |
|
---|
| 1010 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1011 | PutRawBytes(tab, n*sizeof(uint_2));
|
---|
| 1012 | } else {
|
---|
| 1013 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1014 | uint_2 val = tab[i];
|
---|
| 1015 | bswap2(&val);
|
---|
| 1016 | PutRawBytes(&val, sizeof(uint_2));
|
---|
| 1017 | }
|
---|
| 1018 | }
|
---|
[219] | 1019 | }
|
---|
| 1020 |
|
---|
| 1021 | void
|
---|
| 1022 | POutPersist::PutI4 (int_4 val)
|
---|
| 1023 | {
|
---|
[241] | 1024 | PutRawByte(PPS_SIMPLE + 4);
|
---|
| 1025 |
|
---|
[219] | 1026 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1027 | bswap4(&val);
|
---|
| 1028 |
|
---|
[241] | 1029 | PutRawBytes(&val, sizeof(int_4));
|
---|
[219] | 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | void
|
---|
| 1033 | POutPersist::PutI4s (int_4 const* tab, size_t n)
|
---|
| 1034 | {
|
---|
[241] | 1035 | PutArrayTag(4, n);
|
---|
| 1036 |
|
---|
| 1037 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1038 | PutRawBytes(tab, n*sizeof(int_4));
|
---|
| 1039 | } else {
|
---|
| 1040 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1041 | int_4 val = tab[i];
|
---|
| 1042 | bswap4(&val);
|
---|
| 1043 | PutRawBytes(&val, sizeof(int_4));
|
---|
| 1044 | }
|
---|
| 1045 | }
|
---|
[219] | 1046 | }
|
---|
| 1047 |
|
---|
| 1048 | void
|
---|
| 1049 | POutPersist::PutU4 (uint_4 val)
|
---|
| 1050 | {
|
---|
[241] | 1051 | PutRawByte(PPS_SIMPLE + 4);
|
---|
| 1052 |
|
---|
[219] | 1053 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1054 | bswap4(&val);
|
---|
| 1055 |
|
---|
[241] | 1056 | PutRawBytes(&val, sizeof(uint_4));
|
---|
[219] | 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | void
|
---|
| 1060 | POutPersist::PutU4s (uint_4 const* tab, size_t n)
|
---|
| 1061 | {
|
---|
[241] | 1062 | PutArrayTag(4, n);
|
---|
| 1063 |
|
---|
| 1064 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1065 | PutRawBytes(tab, n*sizeof(uint_4));
|
---|
| 1066 | } else {
|
---|
| 1067 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1068 | uint_4 val = tab[i];
|
---|
| 1069 | bswap4(&val);
|
---|
| 1070 | PutRawBytes(&val, sizeof(uint_4));
|
---|
| 1071 | }
|
---|
| 1072 | }
|
---|
[219] | 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | void
|
---|
| 1076 | POutPersist::PutI8 (int_8 val)
|
---|
| 1077 | {
|
---|
[241] | 1078 | PutRawByte(PPS_SIMPLE + 8);
|
---|
| 1079 |
|
---|
[219] | 1080 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1081 | bswap8(&val);
|
---|
| 1082 |
|
---|
[241] | 1083 | PutRawBytes(&val, sizeof(int_8));
|
---|
[219] | 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | void
|
---|
| 1087 | POutPersist::PutI8s (int_8 const* tab, size_t n)
|
---|
| 1088 | {
|
---|
[241] | 1089 | PutArrayTag(8, n);
|
---|
| 1090 |
|
---|
| 1091 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1092 | PutRawBytes(tab, n*sizeof(int_8));
|
---|
| 1093 | } else {
|
---|
| 1094 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1095 | int_8 val = tab[i];
|
---|
| 1096 | bswap8(&val);
|
---|
| 1097 | PutRawBytes(&val, sizeof(int_8));
|
---|
| 1098 | }
|
---|
| 1099 | }
|
---|
[219] | 1100 | }
|
---|
| 1101 |
|
---|
| 1102 | void
|
---|
| 1103 | POutPersist::PutU8 (uint_8 val)
|
---|
| 1104 | {
|
---|
[241] | 1105 | PutRawByte(PPS_SIMPLE + 8);
|
---|
| 1106 |
|
---|
[219] | 1107 | if (bigEndian != IS_BIG_ENDIAN)
|
---|
| 1108 | bswap8(&val);
|
---|
| 1109 |
|
---|
[241] | 1110 | PutRawBytes(&val, sizeof(uint_8));
|
---|
[219] | 1111 | }
|
---|
| 1112 |
|
---|
| 1113 | void
|
---|
| 1114 | POutPersist::PutU8s (uint_8 const* tab, size_t n)
|
---|
| 1115 | {
|
---|
[241] | 1116 | PutArrayTag(8, n);
|
---|
| 1117 |
|
---|
| 1118 | if (bigEndian == IS_BIG_ENDIAN) {
|
---|
| 1119 | PutRawBytes(tab, n*sizeof(uint_8));
|
---|
| 1120 | } else {
|
---|
| 1121 | for (unsigned int i=0; i<n; i++) {
|
---|
| 1122 | uint_8 val = tab[i];
|
---|
| 1123 | bswap8(&val);
|
---|
| 1124 | PutRawBytes(&val, sizeof(uint_8));
|
---|
| 1125 | }
|
---|
| 1126 | }
|
---|
[219] | 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | void
|
---|
[241] | 1130 | POutPersist::PutStr(string const& str)
|
---|
| 1131 | {
|
---|
| 1132 | PutRawByte(PPS_STRING);
|
---|
| 1133 | PutRawI2(str.length());
|
---|
| 1134 | PutRawBytes(str.c_str(), str.length());
|
---|
| 1135 | }
|
---|
| 1136 |
|
---|
| 1137 | void
|
---|
[219] | 1138 | POutPersist::PutLine(char const* ptr, size_t len)
|
---|
| 1139 | {
|
---|
[241] | 1140 | PutRawByte(PPS_LINE);
|
---|
| 1141 |
|
---|
[219] | 1142 | if (len == 0) len = strlen(ptr);
|
---|
[241] | 1143 | PutRawBytes(ptr, len);
|
---|
| 1144 | PutRawByte('\n');
|
---|
[219] | 1145 | }
|
---|
| 1146 |
|
---|
[241] | 1147 | void
|
---|
| 1148 | POutPersist::PutObject(PPersist const* obj)
|
---|
| 1149 | {
|
---|
| 1150 | if (serializeNullAndRepeat(obj)) return;
|
---|
[219] | 1151 |
|
---|
[241] | 1152 | PutRawByte(PPS_OBJECT);
|
---|
| 1153 | PutRawU8(PIOPersist::Hash(typeid(*obj).name()));
|
---|
| 1154 | assignObjectId(obj);
|
---|
| 1155 | obj->WriteSelf(*this);
|
---|
| 1156 | }
|
---|
[219] | 1157 |
|
---|
[241] | 1158 | bool
|
---|
| 1159 | POutPersist::serializeNullAndRepeat(PPersist const* x)
|
---|
[219] | 1160 | {
|
---|
[241] | 1161 | if (x == NULL) {
|
---|
| 1162 | PutRawByte(PPS_NULL);
|
---|
| 1163 | return true;
|
---|
| 1164 | }
|
---|
[219] | 1165 |
|
---|
[241] | 1166 | int_4 id = findObjectId(x);
|
---|
| 1167 | if (id >= 0) {
|
---|
| 1168 | PutRawByte(PPS_REFERENCE);
|
---|
| 1169 | PutRawI4(id);
|
---|
| 1170 | return true;
|
---|
| 1171 | }
|
---|
[219] | 1172 |
|
---|
[241] | 1173 | return false;
|
---|
[219] | 1174 | }
|
---|
| 1175 |
|
---|
[241] | 1176 | int_4
|
---|
| 1177 | POutPersist::assignObjectId(PPersist const* x)
|
---|
[219] | 1178 | {
|
---|
[241] | 1179 | int_4 id = objList.size();
|
---|
| 1180 | objList[x] = id;
|
---|
| 1181 | return id;
|
---|
[219] | 1182 | }
|
---|
| 1183 |
|
---|
[241] | 1184 | int_4
|
---|
| 1185 | POutPersist::findObjectId(PPersist const* x)
|
---|
[219] | 1186 | {
|
---|
[241] | 1187 | ObjList::iterator i = objList.find(x);
|
---|
| 1188 | if (i == objList.end()) return -1;
|
---|
| 1189 | return (*i).second;
|
---|
[219] | 1190 | }
|
---|
| 1191 |
|
---|
[241] | 1192 |
|
---|