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