source: Sophya/trunk/SophyaLib/BaseTools/ppfbinstream.cc@ 4024

Last change on this file since 4024 was 3750, checked in by ansari, 16 years ago

Prise en charge de float 128 bits (r_16, complex<r_16>) par les NDataBlock<T> et PPersist, controlee par le flag de compilation SO_LDBLE128 defini ds machdefs.h , Reza 03/03/2010

File size: 48.1 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <sys/types.h>
7#include <time.h>
8#include "ppfbinstream.h"
9#include "pexceptions.h"
10#include <iostream>
11
12
13// strptime n'est pas defini sous Linux avec g++ avant gcc 3.x - Reza Mars 2000
14#if defined(Linux) && defined(__GNUG__) && (__GNUC__ < 3)
15extern "C" {
16char *strptime(const char *buf, const char *format, struct tm *tm);
17}
18#endif
19
20#define MAXTAGLEN_V2 255
21
22
23static inline void bswap16(void* p)
24{
25 uint_8* p8 = (uint_8*)p;
26 uint_8 tmp1 = *(p8);
27 uint_8 tmp = *(p8+1);
28 *(p8) = ((tmp >> (7*8)) & 0x000000FF) |
29 ((tmp >> (5*8)) & 0x0000FF00) |
30 ((tmp >> (3*8)) & 0x00FF0000) |
31 ((tmp >> (1*8)) & 0xFF000000) |
32 ((tmp & 0xFF000000) << (1*8)) |
33 ((tmp & 0x00FF0000) << (3*8)) |
34 ((tmp & 0x0000FF00) << (5*8)) |
35 ((tmp & 0x000000FF) << (7*8));
36 *(p8+1) = ((tmp1 >> (7*8)) & 0x000000FF) |
37 ((tmp1 >> (5*8)) & 0x0000FF00) |
38 ((tmp1 >> (3*8)) & 0x00FF0000) |
39 ((tmp1 >> (1*8)) & 0xFF000000) |
40 ((tmp1 & 0xFF000000) << (1*8)) |
41 ((tmp1 & 0x00FF0000) << (3*8)) |
42 ((tmp1 & 0x0000FF00) << (5*8)) |
43 ((tmp1 & 0x000000FF) << (7*8));
44}
45
46static inline void bswap8(void* p)
47{
48 uint_8 tmp = *(uint_8*)p;
49 *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |
50 ((tmp >> (5*8)) & 0x0000FF00) |
51 ((tmp >> (3*8)) & 0x00FF0000) |
52 ((tmp >> (1*8)) & 0xFF000000) |
53 ((tmp & 0xFF000000) << (1*8)) |
54 ((tmp & 0x00FF0000) << (3*8)) |
55 ((tmp & 0x0000FF00) << (5*8)) |
56 ((tmp & 0x000000FF) << (7*8));
57}
58
59static inline void bswap4(void* p)
60{
61 uint_4 tmp = *(uint_4*)p;
62 *(uint_4*)p = ((tmp >> 24) & 0x000000FF) |
63 ((tmp >> 8) & 0x0000FF00) |
64 ((tmp & 0x0000FF00) << 8) |
65 ((tmp & 0x000000FF) << 24);
66}
67
68static inline void bswap2(void* p)
69{
70 uint_2 tmp = *(uint_2*)p;
71 *(uint_2*)p = ((tmp >> 8) & 0x00FF) |
72 ((tmp & 0x00FF) << 8);
73}
74
75// Convertit la taille de donnees (2 (int_2), 4 (int_4, r_4), 8 (r_8) ...) en taille codee dans le tag
76static inline unsigned char datasize2tagsize( unsigned char sz)
77{
78 // Taille permises 1,2,4,8,16
79 // Pour les tailles <= 8, c'est directement la taille
80 if (sz<=8) return sz;
81 else {
82 if (sz==16) return 10;
83 else return 0; // signale une erreur
84 }
85}
86// Convertit la taille codee dans le tag en taille de donnees (2 (int_2), 4 (int_4, r_4), 8 (r_8) ...) en
87static inline unsigned char tagsize2datasize( unsigned char sz)
88{
89 // Taille permises 1,2,4,8,16
90 // Pour les tailles <= 8, c'est directement la taille
91 if (sz<=8) return sz;
92 else {
93 if (sz==10) return 10;
94 else return 0; // signale une erreur
95 }
96}
97
98//-------------------------------------------------------------------------
99//---------------------- Classe PPFBinaryIOStream ------------------------
100//-------------------------------------------------------------------------
101
102/*!
103 \class SOPHYA::PPFBinaryIOStream
104 \ingroup BaseTools
105 Base class for SOPHYA PPF input / output stream
106*/
107
108
109PPFBinaryIOStream::PPFBinaryIOStream()
110{
111 version = 0; // PPersist(In/Out) version
112 // creationdate = time(NULL); // Date de creation du fichier
113 _nbpostag = 0; // Nb de tag de positionnement
114 _nbobjs = 0; // Nb total d'objets
115 _nbtlobjs = 0; // Nb d'objets de niveau 1
116 _nbrefs = 0; // Nb de reference d'objets
117 _maxnestlevel = 0; // Niveau maximum d'objets emboites
118}
119
120PPFBinaryIOStream::~PPFBinaryIOStream()
121{
122}
123
124//! Return the creation date/time of the associated stream as a string
125string
126PPFBinaryIOStream::CreationDateStr()
127{
128 time_t cdt = CreationDate();
129 string cdate = ctime(&cdt);
130 return(cdate);
131}
132
133//! Return a string with information about the stream (creation-date, version ...)
134string
135PPFBinaryIOStream::InfoString()
136{
137 string rs;
138 char buff[256];
139 sprintf(buff,"PPFStream Version= %d CreationDate= ", Version());
140 rs += buff;
141 rs += CreationDateStr();
142 sprintf(buff,"\n NbObjs= %ld NbTopLevObjs= %ld NbRefs= %ld MaxNest= %d ",
143 (long)NbObjects(), (long)NbTopLevelObjects(),
144 (long)NbReferences(), MaxNestedObjsLevel());
145 rs += buff;
146 sprintf(buff,"\n NbPosTag= %ld NbNameTag= %ld ", (long)NbPosTags(),
147 (long)NbNameTags());
148 rs += buff;
149 return rs;
150}
151
152
153//! Return the name tag number \b itag
154string
155PPFBinaryIOStream::GetTagName(int itag)
156{
157 if (itag<0 || itag >= (int)tags.size()) return "";
158 map<string, int_8>::iterator i = tags.begin();
159 for (int j=0; j<itag; j++) i++;
160 return((*i).first);
161}
162
163
164//! Return a reference to a vector<string> containing all name-tags associated with the stream
165static vector<string> * ret_tag_names = NULL;
166vector<string> const &
167PPFBinaryIOStream::GetNameTags()
168{
169if (ret_tag_names) delete ret_tag_names;
170ret_tag_names = new vector<string> ;
171map<string, int_8>::iterator i;
172for(i=tags.begin(); i!=tags.end(); i++) ret_tag_names->push_back((*i).first);
173return(*ret_tag_names);
174}
175
176//-------------------------------------------------------------------------
177//-------------------- Classe PPFBinaryInputStream -----------------------
178//-------------------------------------------------------------------------
179
180/*!
181 \class SOPHYA::PPFBinaryInputStream
182 \ingroup BaseTools
183 PPF Input stream implementing read operations
184*/
185
186/*! Constructor from a RawInOutStream pointer
187 \param is : pointer to RawInOutStream
188 \param ad : if true, the RawInOutStream \b is is deleted by the destructor
189 \param scan : if true, try to read the name-tags table from the end of the input stream
190*/
191PPFBinaryInputStream::PPFBinaryInputStream(RawInOutStream * is, bool ad, bool scan)
192{
193 s = is;
194 _ads = ad;
195 Init(scan);
196}
197
198/*! Constructor
199 \param flnm : input file name
200 \param scan : if true, try to read the name-tags table from the end of the input stream
201*/
202PPFBinaryInputStream::PPFBinaryInputStream(string const& flnm, bool scan)
203//
204// Constructeur. Ouvre le fichier.
205//--
206{
207 s = new RawInFileStream(flnm.c_str());
208 _ads = true;
209 Init(scan);
210}
211
212PPFBinaryInputStream::~PPFBinaryInputStream()
213{
214 if (_ads && (s!=NULL) ) delete s;
215}
216
217void
218PPFBinaryInputStream::Init(bool scan)
219{
220 // Read and check header
221
222 char rbuf[36];
223 GetRawBytes(rbuf, 32);
224 if (strncmp(rbuf,"SOS-SOPHYA-PPersistFile", 23) != 0) {
225 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream bad header");
226 }
227 rbuf[32] = '\0';
228 version = atoi(rbuf+25);
229 if (version < 2) {
230 cerr << "PPFBinaryInputStream::PPFBinaryInputStream(" << FileName() << ") Version(=" << version
231 << ") < 2 not supported !" << endl;
232 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream() - Unsupported (Old) Version");
233 }
234 // read endianness
235 GetRawBytes(rbuf, 32);
236 if (strncmp(rbuf,"BIG-ENDIAN",10) == 0)
237 bigEndian = true;
238 else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0)
239 bigEndian = false;
240 else {
241 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream bad header - endianness");
242 }
243
244 // read creation date
245 GetRawBytes(rbuf, 32);
246 rbuf[32] = '\0';
247 struct tm tm;
248 strptime(rbuf,"%d/%m/%Y %H:%M:%S GMT",&tm);
249 /* #else
250 sscanf(rbuf,"%2d/%2d/%4d %2d:%2d:%2d GMT",&tm.tm_mday,&tm.tm_mon,&tm.tm_year,
251 &tm.tm_hour,&tm.tm_min,&tm.tm_sec);
252 tm.tm_mon --; tm.tm_year -= 1900;
253 #endif */
254
255 creationdate = mktime(&tm);
256 seqread = true; // To flag non sequential reads
257 if (scan && s->isSeekable()) {
258 if (Version() >= 3) ReadNameTagTable();
259 else ReadNameTagTableV2();
260 }
261}
262
263
264void
265PPFBinaryInputStream::ReadNameTagTable()
266{
267 unsigned char ppstype;
268 int_8 debut;
269 debut = s->tellg();
270
271 s->seekg(-(sizeof(int_8)+1), ios::end);
272 // Lecture position NameTagTable et tag EOF
273 int_8 pos;
274 GetRawI8(pos);
275 GetRawUByte(ppstype);
276 if (ppstype != PPS_EOF)
277 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable() Corrupted file, no EOF tag at end of file");
278
279 // On se positionne au debut du NameTagTable
280 s->seekg(pos);
281 GetRawUByte(ppstype);
282 if (ppstype != PPS_NAMETAG_TABLE)
283 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable() Corrupted file PPS_NAMETAG_TABLE not found");
284 // Lecture nb de PosTag et nb d'objets dans le flot
285 int_8 stats[8] = {0,0,0,0,0,0,0,0};
286 GetI8s(stats, 8);
287 _nbpostag = stats[0];
288 _nbobjs = stats[1];
289 _nbtlobjs = stats[2];
290 _nbrefs = stats[3];
291 _maxnestlevel = stats[4];
292
293 uint_8 ttsz,it;
294 // Lecture nombre de NameTag
295 GetRawU8(ttsz);
296 if (ttsz > 0) {
297 for(it=0; it<ttsz; it++) {
298 int_8 tpos;
299 string tname;
300 GetRawI8(tpos);
301 GetStr(tname);
302 tags[tname] = tpos;
303 }
304 }
305 // On revient au debut du float, juste apres l'entete
306 s->seekg(debut);
307}
308
309void
310PPFBinaryInputStream::ReadNameTagTableV2()
311{
312 // On cherche la liste des tags, a la fin du fichier
313
314 unsigned char ppstype;
315 int_8 debut;
316 debut = s->tellg();
317
318 s->seekg(-(sizeof(int_8)+1), ios::end);
319
320 GetRawUByte(ppstype);
321 if (ppstype != PPS_EOF)
322 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, no eof entry at end of file");
323
324 int_8 pos;
325 GetRawI8(pos);
326 if (pos < 0) { // no tags
327 s->seekg(debut);
328 return;
329 }
330
331 char buffer[MAXTAGLEN_V2+1];
332 s->seekg(pos);
333 while (true) {
334 GetRawUByte(ppstype);
335 if (ppstype == PPS_EOF) break;
336
337 if (ppstype != PPS_NAMETAG_TABLE)
338 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, bad tag entry");
339
340 GetRawI8(pos);
341 int_4 len;
342 GetRawI4(len);
343 if (len > MAXTAGLEN_V2)
344 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, tag name too long");
345 GetRawBytes(buffer, len);
346 buffer[len] = '\0';
347
348 tags[buffer] = pos;
349 }
350 s->seekg(debut);
351}
352
353
354bool
355PPFBinaryInputStream::GotoPositionTag(int_8 pos)
356{
357 s->seekg(pos);
358 unsigned char ppstag;
359 GetRawUByte(ppstag);
360 if (ppstag != PPS_POSTAG_MARK)
361 throw FileFormatExc("PPFBinaryInputStream::GotoPositionTag() - PPS_POSTAG_MARK not found!");
362 int_8 tpos;
363 GetRawI8(tpos);
364 if (tpos != pos)
365 throw FileFormatExc("PPFBinaryInputStream::GotoPositionTag() - Wrong tag position!");
366 return true;
367}
368
369bool
370PPFBinaryInputStream::GotoNameTag(string const& name)
371{
372 map<string, int_8>::iterator i = tags.find(name);
373 if (i == tags.end())
374 return false;
375 // throw NotFoundExc("PPFBinaryInputStream::GotoNameTag tag not found");
376 s->seekg((*i).second);
377 seqread = false;
378 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
379 return(true);
380}
381
382
383bool
384PPFBinaryInputStream::GotoNameTagNum(int itag)
385{
386 if (itag<0 || itag >= (int)tags.size()) return false;
387 map<string, int_8>::iterator i = tags.begin();
388 for (int j=0; j<itag; j++) i++;
389 s->seekg((*i).second);
390 seqread = false;
391 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
392 return(true);
393}
394
395
396bool
397PPFBinaryInputStream::SkipToNextObject()
398{
399 if (! s->isSeekable()) return false;
400 unsigned char ppstag=0;
401 // int kss;
402 while ((ppstag != PPS_OBJECT) && (ppstag != PPS_REFERENCE) && (ppstag != PPS_EOF)) {
403 // kss++;
404 GetRawUByte(ppstag);
405 // cout << " DBG--SkipToNextObject(" << kss << ")" << (int)ppstag << ","
406 // << (int)PPS_OBJECT << " fpos=" << s->tellg() << endl;
407 if ((ppstag != PPS_OBJECT) && (ppstag != PPS_REFERENCE) && (ppstag != PPS_EOF))
408 SkipItem(false, ppstag);
409 }
410 s->seekg(-1, ios::cur);
411 if (ppstag == PPS_OBJECT) return true;
412 else return false;
413
414}
415
416bool
417PPFBinaryInputStream::SkipNextItem()
418{
419 if (! s->isSeekable()) return false;
420 SkipItem();
421 return true;
422}
423
424char
425PPFBinaryInputStream::NextItemTag(short & datasz, size_t & asz)
426{
427 int_8 cpos;
428 cpos = s->tellg();
429
430 unsigned char ppstag=0;
431 unsigned char ppst1,ppst2,ppst3,ppst30;
432 GetRawUByte(ppstag);
433 ppst1 = ppstag&0x0f; // bits 0123
434 ppst2 = ppstag&0x30; // bits 45
435 ppst3 = ppstag&0xc0; // bits 67
436 ppst30 = ppstag&0xc1; // bits 0 67
437
438 datasz = 0;
439 asz = 0;
440
441 int_4 i4;
442 uint_8 ui8;
443
444 if ((ppst2 == 0) && (ppst3 == 0) ) {
445 switch (ppst1) {
446 case PPS_STRING :
447 GetRawI4(i4);
448 datasz = 1;
449 asz = i4;
450 break;
451
452 case PPS_NULL :
453 case PPS_OBJECT :
454 case PPS_REFERENCE :
455 case PPS_NAMETAG_MARK :
456 case PPS_POSTAG_MARK :
457 case PPS_ENDOBJECT :
458 case PPS_POSTAG_TABLE :
459 case PPS_NAMETAG_TABLE :
460 case PPS_EOF :
461 datasz = 0;
462 asz = 0;
463 break;
464
465 default :
466 throw FileFormatExc("PPFBinaryInputStream::NextItemTag() - Unexpected tag value !");
467 break;
468 }
469 }
470 else {
471 int_4 dsize = ppst1;
472 if (ppst30 == PPS_DATATYPE_COMPLEX) {
473 dsize--;
474 }
475 switch (ppst2) {
476 case PPS_SIMPLE :
477 datasz = dsize;
478 asz = 1;
479 break;
480
481 case PPS_SIMPLE_ARRAY4 :
482 GetRawI4(i4);
483 datasz = dsize;
484 asz = i4;
485 break;
486
487 case PPS_SIMPLE_ARRAY8 :
488 GetRawU8(ui8);
489 datasz = dsize;
490 asz = ui8;
491 break;
492 }
493 }
494
495 s->seekg(cpos,ios::beg);
496 return(ppstag);
497}
498
499void
500PPFBinaryInputStream::SkipItem(bool fgrdt, unsigned char itag)
501{
502 unsigned char ppstag=0;
503 unsigned char ppst1,ppst2,ppst3,ppst30;
504 uint_8 ui8;
505 int_4 i4;
506 int_8 i8;
507
508 if (fgrdt) GetRawUByte(ppstag);
509 else ppstag = itag;
510
511 ppst1 = ppstag&0x0f; // bits 0123
512 ppst2 = ppstag&0x30; // bits 45
513 ppst3 = ppstag&0xc0; // bits 67
514 ppst30 = ppstag&0xc1; // bits 0 67
515 if ((ppst2 == 0) && (ppst3 == 0) ) {
516 switch (ppst1) {
517 case PPS_NULL :
518 case PPS_NAMETAG_MARK :
519 break;
520
521 case PPS_STRING :
522 GetRawI4(i4);
523 s->seekg(i4,ios::cur);
524 break;
525
526 case PPS_OBJECT :
527 GetRawU8(ui8);
528 GetRawU8(ui8);
529 break;
530
531 case PPS_REFERENCE :
532 GetRawU8(ui8);
533 GetRawI8(i8);
534 break;
535
536 case PPS_POSTAG_MARK :
537 GetRawI8(i8);
538 break;
539
540 case PPS_ENDOBJECT :
541 GetRawU8(ui8);
542 break;
543
544 case PPS_POSTAG_TABLE :
545 GetRawI4(i4);
546 // for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);
547 s->seekg((int_8)i4*8,ios::cur);
548 break;
549
550 case PPS_NAMETAG_TABLE :
551 if (Version() < 3) {
552 GetRawI8(i8);
553 GetRawI4(i4);
554 s->seekg(i4,ios::cur);
555 }
556 else {
557 GetI8(i8); // nb pos tag
558 GetI8(i8); // nb d'objets
559 GetI8(i8); // nb objets toplevel
560 GetU8(ui8); // nb de nametag
561 for(uint_8 kt=0; kt<ui8; kt++) {
562 GetRawI4(i4);
563 s->seekg(i4,ios::cur);
564 }
565 GetI8(i8); // position debut NameTagTable
566 }
567 break;
568
569
570 case PPS_EOF :
571 if (Version() < 3) GetRawI8(i8);
572 break;
573
574 default :
575 cerr << "PPFBinaryInputStream::SkipItem() ERROR : Unexpected tag value "
576 << hex << ppstag << " At position" << s->tellg() << dec << endl;
577 throw FileFormatExc("PPFBinaryInputStream::SkipItem() - Unexpected tag value !");
578 }
579 }
580 else {
581 string dtype = "???? x";
582 int_4 dsize = ppst1;
583 int_8 dsizeskip = dsize;
584 if (ppst30 == PPS_DATATYPE_COMPLEX) {
585 dsize--;
586 dsizeskip = 2*dsize;
587 }
588
589 switch (ppst2) {
590 case PPS_SIMPLE :
591 s->seekg(dsizeskip, ios::cur);
592 break;
593
594 case PPS_SIMPLE_ARRAY4 :
595 GetRawI4(i4);
596 s->seekg(dsizeskip*(int_8)i4, ios::cur);
597 break;
598
599 case PPS_SIMPLE_ARRAY8 :
600 GetRawU8(ui8);
601 s->seekg(dsizeskip*ui8, ios::cur);
602 break;
603 }
604 }
605return;
606}
607
608//++
609// void PPFBinaryInputStream::GetByte(char& c)
610// void PPFBinaryInputStream::GetBytes(void* ptr, size_t bytes)
611// void PPFBinaryInputStream::GetR4 (r_4& result)
612// void PPFBinaryInputStream::GetR4s (r_4* tab, size_t n)
613// void PPFBinaryInputStream::GetR8 (r_8& result)
614// void PPFBinaryInputStream::GetR8s (r_8* tab, size_t n)
615// void PPFBinaryInputStream::GetI2 (int_2& result)
616// void PPFBinaryInputStream::GetI2s (int_2* tab, size_t n)
617// void PPFBinaryInputStream::GetU2 (uint_2& result)
618// void PPFBinaryInputStream::GetU2s (uint_2* tab, size_t n)
619// void PPFBinaryInputStream::GetI4 (int_4& result)
620// void PPFBinaryInputStream::GetI4s (int_4* tab, size_t n)
621// void PPFBinaryInputStream::GetU4 (uint_4& result)
622// void PPFBinaryInputStream::GetU4s (uint_4* tab, size_t n)
623// void PPFBinaryInputStream::GetI8 (int_8& result)
624// void PPFBinaryInputStream::GetI8s (int_8* tab, size_t n)
625// void PPFBinaryInputStream::GetU8 (uint_8& result)
626// void PPFBinaryInputStream::GetU8s (uint_8* tab, size_t n)
627// Lecture de données portables depuis le fichier PPersist. Pour chaque type
628// de données, on peut lire une valeur, ou un tableau de valeurs.
629// void PPFBinaryInputStream::GetLine(char* ptr, size_t len)
630// Lecture d'une ligne de texte depuis le fichier PPersist.
631//--
632
633
634void
635PPFBinaryInputStream::GetTypeTag(unsigned char& c)
636{
637 int_8 tpos;
638 c = PPS_NAMETAG_MARK;
639 while ( (c == PPS_NAMETAG_MARK) || (c == PPS_POSTAG_MARK) ) {
640 GetRawUByte(c);
641 if (c == PPS_POSTAG_MARK) GetRawI8(tpos);
642 }
643 // while (c == PPS_NAMETAG_MARK) { Il ne faut plus faire ca !
644 // objList.clear(); $CHECK$ Reza 03/2000
645 // GetRawByte(c);
646 // }
647}
648
649
650void
651PPFBinaryInputStream::GetRawByte(char& c)
652{
653 GetRawBytes(&c, 1);
654}
655
656void
657PPFBinaryInputStream::GetRawUByte(unsigned char& c)
658{
659 GetRawBytes(&c, 1);
660}
661
662void
663PPFBinaryInputStream::GetRawBytes(void* ptr, size_t bytes)
664{
665 s->read((char*)ptr, bytes);
666}
667
668void
669PPFBinaryInputStream::GetRawI2 (int_2& result)
670{
671 GetRawBytes(&result, sizeof(int_2));
672 if (bigEndian != IS_BIG_ENDIAN)
673 bswap2(&result);
674}
675
676void
677PPFBinaryInputStream::GetRawI4 (int_4& result)
678{
679 GetRawBytes(&result, sizeof(int_4));
680 if (bigEndian != IS_BIG_ENDIAN)
681 bswap4(&result);
682}
683
684void
685PPFBinaryInputStream::GetRawI8 (int_8& result)
686{
687 GetRawBytes(&result, sizeof(int_8));
688 if (bigEndian != IS_BIG_ENDIAN)
689 bswap8(&result);
690}
691
692void
693PPFBinaryInputStream::GetRawU8 (uint_8& result)
694{
695 GetRawBytes(&result, sizeof(uint_8));
696 if (bigEndian != IS_BIG_ENDIAN)
697 bswap8(&result);
698}
699
700void
701PPFBinaryInputStream::CheckTag(unsigned char datasz, unsigned char datatype)
702// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
703{
704 unsigned char ppstype;
705 GetTypeTag(ppstype);
706 if (ppstype != PPS_SIMPLE + datasize2tagsize(datasz) + datatype)
707 throw FileFormatExc("PPFBinaryInputStream::CheckTag bad type in ppersist file");
708}
709
710void
711PPFBinaryInputStream::CheckArrayTag(unsigned char datasz, size_t sz, unsigned char datatype)
712// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
713{
714 unsigned char ppstype;
715 GetTypeTag(ppstype);
716 size_t filesz;
717 if (sz <= 0x7fffffff) {
718 if (ppstype != PPS_SIMPLE_ARRAY4 + datasize2tagsize(datasz) + datatype)
719 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad type in ppersist file");
720 int_4 ff;
721 GetRawI4(ff); filesz=ff;
722 } else {
723 if (ppstype != PPS_SIMPLE_ARRAY8 + datasize2tagsize(datasz) + datatype)
724 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad type in ppersist file");
725 uint_8 ff;
726 GetRawU8(ff); filesz=ff;
727 }
728 if (filesz != sz)
729 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad array size in ppersist file");
730}
731
732void
733PPFBinaryInputStream::GetByte(char& c)
734{
735 CheckTag(1,PPS_DATATYPE_CHAR);
736 GetRawBytes(&c, 1);
737}
738
739void
740PPFBinaryInputStream::GetBytes(void* ptr, size_t bytes)
741{
742 CheckArrayTag(1, bytes, PPS_DATATYPE_CHAR);
743 GetRawBytes(ptr, bytes);
744}
745
746void
747PPFBinaryInputStream::GetR4 (r_4& result)
748{
749 CheckTag(4,PPS_DATATYPE_FLOAT);
750 GetRawBytes(&result, sizeof(r_4));
751 if (bigEndian != IS_BIG_ENDIAN)
752 bswap4(&result);
753}
754
755
756void
757PPFBinaryInputStream::GetR4s (r_4* tab, size_t n)
758{
759 CheckArrayTag(4,n,PPS_DATATYPE_FLOAT);
760 GetRawBytes(tab, n*sizeof(r_4));
761 if (bigEndian == IS_BIG_ENDIAN) return;
762
763 for (size_t i=0; i<n; i++) bswap4(tab+i);
764
765 return;
766}
767
768void
769PPFBinaryInputStream::GetR8 (r_8& result)
770{
771 CheckTag(8,PPS_DATATYPE_FLOAT);
772 GetRawBytes(&result, sizeof(r_8));
773 if (bigEndian != IS_BIG_ENDIAN)
774 bswap8(&result);
775}
776
777void
778PPFBinaryInputStream::GetR8s (r_8* tab, size_t n)
779{
780 CheckArrayTag(8,n,PPS_DATATYPE_FLOAT);
781 GetRawBytes(tab, n*sizeof(r_8));
782 if (bigEndian == IS_BIG_ENDIAN) return;
783
784 for (size_t i=0; i<n; i++) bswap8(tab+i);
785
786 return;
787}
788
789void
790PPFBinaryInputStream::GetI1 (int_1& result)
791{
792 CheckTag(1,PPS_DATATYPE_INTEGER);
793 GetRawBytes(&result, sizeof(int_1));
794}
795
796void
797PPFBinaryInputStream::GetI1s (int_1* tab, size_t n)
798{
799 CheckArrayTag(1,n,PPS_DATATYPE_INTEGER);
800 GetRawBytes(tab, n*sizeof(int_1));
801}
802
803void
804PPFBinaryInputStream::GetU1 (uint_1& result)
805{
806 CheckTag(1,PPS_DATATYPE_UNSIGNED);
807 GetRawBytes(&result, sizeof(uint_1));
808}
809
810void
811PPFBinaryInputStream::GetU1s (uint_1* tab, size_t n)
812{
813 CheckArrayTag(1,n,PPS_DATATYPE_UNSIGNED);
814 GetRawBytes(tab, n*sizeof(uint_1));
815}
816
817void
818PPFBinaryInputStream::GetI2 (int_2& result)
819{
820 CheckTag(2,PPS_DATATYPE_INTEGER);
821 GetRawBytes(&result, sizeof(int_2));
822 if (bigEndian != IS_BIG_ENDIAN)
823 bswap2(&result);
824}
825
826void
827PPFBinaryInputStream::GetI2s (int_2* tab, size_t n)
828{
829 CheckArrayTag(2,n,PPS_DATATYPE_INTEGER);
830 GetRawBytes(tab, n*sizeof(int_2));
831 if (bigEndian == IS_BIG_ENDIAN) return;
832
833 for (size_t i=0; i<n; i++) bswap2(tab+i);
834
835 return;
836}
837
838void
839PPFBinaryInputStream::GetU2 (uint_2& result)
840{
841 CheckTag(2,PPS_DATATYPE_UNSIGNED);
842 GetRawBytes(&result, sizeof(uint_2));
843 if (bigEndian != IS_BIG_ENDIAN)
844 bswap2(&result);
845}
846
847void
848PPFBinaryInputStream::GetU2s (uint_2* tab, size_t n)
849{
850 CheckArrayTag(2,n,PPS_DATATYPE_UNSIGNED);
851 GetRawBytes(tab, n*sizeof(uint_2));
852 if (bigEndian == IS_BIG_ENDIAN) return;
853
854 for (size_t i=0; i<n; i++) bswap2(tab+i);
855
856 return;
857}
858
859void
860PPFBinaryInputStream::GetI4 (int_4& result)
861{
862 CheckTag(4,PPS_DATATYPE_INTEGER);
863 GetRawBytes(&result, sizeof(int_4));
864 if (bigEndian != IS_BIG_ENDIAN)
865 bswap4(&result);
866}
867
868void
869PPFBinaryInputStream::GetI4s (int_4* tab, size_t n)
870{
871 CheckArrayTag(4,n,PPS_DATATYPE_INTEGER);
872 GetRawBytes(tab, n*sizeof(int_4));
873 if (bigEndian == IS_BIG_ENDIAN) return;
874
875 for (size_t i=0; i<n; i++) bswap4(tab+i);
876
877 return;
878}
879
880void
881PPFBinaryInputStream::GetU4 (uint_4& result)
882{
883 CheckTag(4,PPS_DATATYPE_UNSIGNED);
884 GetRawBytes(&result, sizeof(uint_4));
885 if (bigEndian != IS_BIG_ENDIAN)
886 bswap4(&result);
887}
888
889void
890PPFBinaryInputStream::GetU4s (uint_4* tab, size_t n)
891{
892 CheckArrayTag(4,n,PPS_DATATYPE_UNSIGNED);
893 GetRawBytes(tab, n*sizeof(uint_4));
894 if (bigEndian == IS_BIG_ENDIAN) return;
895
896 for (size_t i=0; i<n; i++) bswap4(tab+i);
897
898 return;
899}
900
901
902void
903PPFBinaryInputStream::GetI8 (int_8& result)
904{
905 CheckTag(8,PPS_DATATYPE_INTEGER);
906 GetRawBytes(&result, sizeof(int_8));
907 if (bigEndian != IS_BIG_ENDIAN)
908 bswap8(&result);
909}
910
911void
912PPFBinaryInputStream::GetI8s (int_8* tab, size_t n)
913{
914 CheckArrayTag(8,n,PPS_DATATYPE_INTEGER);
915 GetRawBytes(tab, n*sizeof(int_8));
916 if (bigEndian == IS_BIG_ENDIAN) return;
917
918 for (size_t i=0; i<n; i++) bswap8(tab+i);
919
920 return;
921}
922
923void
924PPFBinaryInputStream::GetU8 (uint_8& result)
925{
926 CheckTag(8,PPS_DATATYPE_UNSIGNED);
927 GetRawBytes(&result, sizeof(uint_8));
928 if (bigEndian != IS_BIG_ENDIAN)
929 bswap8(&result);
930}
931
932void
933PPFBinaryInputStream::GetU8s (uint_8* tab, size_t n)
934{
935 CheckArrayTag(8,n,PPS_DATATYPE_UNSIGNED);
936 GetRawBytes(tab, n*sizeof(uint_8));
937 if (bigEndian == IS_BIG_ENDIAN) return;
938
939 for (size_t i=0; i<n; i++) bswap8(tab+i);
940
941 return;
942}
943
944
945void
946PPFBinaryInputStream::GetLine(char* ptr, size_t len)
947{
948 string str;
949 GetStr(str);
950 strncpy(ptr, str.c_str(), len);
951 ptr[len] = '\0';
952}
953
954void
955PPFBinaryInputStream::GetStr(string& str)
956{
957 unsigned char ppstype;
958 GetTypeTag(ppstype);
959 if (ppstype != PPS_STRING)
960 throw FileFormatExc("PPFBinaryInputStream::GetStr bad type in ppersist file");
961 int_4 len;
962 GetRawI4(len);
963 char * buff = new char[len+1];
964 GetRawBytes(buff, len);
965 buff[len] = '\0';
966 str = buff;
967 delete[] buff;
968}
969
970void
971PPFBinaryInputStream::GetZ4 (complex<r_4>& result)
972{
973 CheckTag(4,PPS_DATATYPE_COMPLEX);
974 r_4 reim[2];
975 GetRawBytes(reim, 2*sizeof(r_4));
976 if (bigEndian != IS_BIG_ENDIAN) {
977 bswap4(reim);
978 bswap4(reim+1);
979 }
980 result = complex<r_4>(reim[0], reim[1]);
981}
982
983void
984PPFBinaryInputStream::GetZ4s (complex<r_4>* tab, size_t n)
985{
986 CheckArrayTag(4,n,PPS_DATATYPE_COMPLEX);
987 GetRawBytes(tab, n*2*sizeof(r_4));
988 if (bigEndian == IS_BIG_ENDIAN) return;
989
990 r_4 * p = (r_4 *)tab;
991 for (size_t i=0; i<n; i++) {
992 bswap4(p); p++;
993 bswap4(p); p++;
994 }
995 return;
996}
997
998void
999PPFBinaryInputStream::GetZ8 (complex<r_8>& result)
1000{
1001 CheckTag(8,PPS_DATATYPE_COMPLEX);
1002 r_8 reim[2];
1003 GetRawBytes(reim, 2*sizeof(r_8));
1004 if (bigEndian != IS_BIG_ENDIAN) {
1005 bswap8(reim);
1006 bswap8(reim+1);
1007 }
1008 result = complex<r_8>(reim[0], reim[1]);
1009}
1010
1011void
1012PPFBinaryInputStream::GetZ8s (complex<r_8>* tab, size_t n)
1013{
1014 CheckArrayTag(8,n,PPS_DATATYPE_COMPLEX);
1015 GetRawBytes(tab, n*2*sizeof(r_8));
1016 if (bigEndian == IS_BIG_ENDIAN) return;
1017
1018 r_8 * p = (r_8 *)tab;
1019 for (size_t i=0; i<n; i++) {
1020 bswap8(p); p++;
1021 bswap8(p); p++;
1022 }
1023 return;
1024}
1025
1026// Support pour les long double (128 bits floating point numbers)
1027#ifdef SO_LDBLE128
1028void
1029PPFBinaryInputStream::GetR16 (r_16& result)
1030{
1031 CheckTag(16,PPS_DATATYPE_FLOAT);
1032 GetRawBytes(&result, sizeof(r_16));
1033 if (bigEndian != IS_BIG_ENDIAN)
1034 bswap16(&result);
1035}
1036
1037void
1038PPFBinaryInputStream::GetR16s (r_16* tab, size_t n)
1039{
1040 CheckArrayTag(16,n,PPS_DATATYPE_FLOAT);
1041 GetRawBytes(tab, n*sizeof(r_16));
1042 if (bigEndian == IS_BIG_ENDIAN) return;
1043
1044 for (size_t i=0; i<n; i++) bswap16(tab+i);
1045
1046 return;
1047}
1048
1049void
1050PPFBinaryInputStream::GetZ16 (complex<r_16>& result)
1051{
1052 CheckTag(16,PPS_DATATYPE_COMPLEX);
1053 r_16 reim[2];
1054 GetRawBytes(reim, 2*sizeof(r_16));
1055 if (bigEndian != IS_BIG_ENDIAN) {
1056 bswap16(reim);
1057 bswap16(reim+1);
1058 }
1059 result = complex<r_16>(reim[0], reim[1]);
1060}
1061
1062void
1063PPFBinaryInputStream::GetZ16s (complex<r_16>* tab, size_t n)
1064{
1065 CheckArrayTag(16,n,PPS_DATATYPE_COMPLEX);
1066 GetRawBytes(tab, n*2*sizeof(r_16));
1067 if (bigEndian == IS_BIG_ENDIAN) return;
1068
1069 r_16 * p = (r_16 *)tab;
1070 for (size_t i=0; i<n; i++) {
1071 bswap16(p); p++;
1072 bswap16(p); p++;
1073 }
1074 return;
1075}
1076#endif
1077
1078void
1079PPFBinaryInputStream::GetPosTagTable(int_8* ptab, size_t sz)
1080{
1081 unsigned char ppstype;
1082 GetTypeTag(ppstype);
1083 if (ppstype != PPS_POSTAG_TABLE)
1084 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable bad type in ppersist stream");
1085 int_4 tsz;
1086 GetRawI4(tsz);
1087 if (tsz != (int_4)sz)
1088 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable Size mismatch ");
1089 for(int kk=0; kk<tsz; kk++)
1090 GetRawI8(ptab[kk]);
1091 return;
1092}
1093
1094void
1095PPFBinaryInputStream::GetPosTagTable(vector<int_8>& ptab)
1096{
1097 unsigned char ppstype;
1098 GetTypeTag(ppstype);
1099 if (ppstype != PPS_POSTAG_TABLE)
1100 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable bad type in ppersist stream");
1101 ptab.clear();
1102 int_4 tsz;
1103 GetRawI4(tsz);
1104 int_8 tpos;
1105 for(int kk=0; kk<tsz; kk++) {
1106 GetRawI8(tpos);
1107 ptab.push_back(tpos);
1108 }
1109 return;
1110}
1111
1112/*! Scans the streams and prints in text format to standard output (cout) the PPF
1113 stream content.
1114 \param lev : print level (level of details) = 0,1,2,3
1115*/
1116void
1117PPFBinaryInputStream::AnalyseTags(int lev)
1118{
1119 unsigned char ppstag=0;
1120 unsigned char ppst1,ppst2,ppst3,ppst30;
1121 int_8 cpos,fsize;
1122 uint_8 ui8,cid,oid;
1123 int_4 i4;
1124 int_8 i8;
1125 char * buff;
1126 string str;
1127 // Pour indenter lors de l'impression
1128 #define _MXINDENT_ 10
1129 const char * indents[_MXINDENT_+1] = {""," ", " ", " ", " ", " ",
1130 " ", " ", " ",
1131 " ", " "};
1132 int idxindent = 0;
1133 int idxind = 0;
1134 cout << "\n ---------------------------------------------------------- " << endl;
1135 cout << " PPFBinaryInputStream::AnalyseTags(Level= " << lev << ")" << endl;
1136
1137
1138 cpos = s->tellg();
1139 s->seekg(0,ios::end);
1140 fsize = s->tellg();
1141 s->seekg(cpos,ios::beg);
1142
1143 cout << " FileName= " << FileName() << endl;
1144 cout << " Version= " << Version() << " FileSize= " << fsize
1145 << " Creation Date= " << CreationDateStr() << endl;
1146 cout << " NbPosTag=" << NbPosTags() << " NbNameTag=" << tags.size()
1147 << " NbObjs=" << NbObjects() << " NbTopLevObjs=" << NbTopLevelObjects()
1148 << " NbRefs=" << NbReferences() << " MaxNest=" << MaxNestedObjsLevel()
1149 << endl << endl;
1150
1151 uint_8 totntags = 0;
1152 bool eofok = false;
1153
1154 while ( (ppstag != PPS_EOF) && (cpos < fsize) ) {
1155 cpos = s->tellg();
1156 GetRawUByte(ppstag);
1157 totntags++;
1158
1159 ppst1 = ppstag&0x0f; // bits 0123
1160 ppst2 = ppstag&0x30; // bits 45
1161 ppst3 = ppstag&0xc0; // bits 67
1162 ppst30 = ppstag&0xc1; // bits 0 67
1163 if ((ppst2 == 0) && (ppst3 == 0) ) {
1164 switch (ppst1) {
1165
1166 case PPS_NULL :
1167 if (lev > 1) cout << indents[idxindent]
1168 << "<PPS_NULL> tag at position " << hex << cpos << dec << endl;
1169 break;
1170
1171 case PPS_STRING :
1172 GetRawI4(i4);
1173 if (lev > 1) cout << indents[idxindent]
1174 << "<PPS_STRING> tag at position " << hex << cpos << dec
1175 << " Length=" << i4 << endl;
1176 s->seekg(i4,ios::cur);
1177 break;
1178
1179 case PPS_OBJECT :
1180 GetRawU8(cid);
1181 GetRawU8(oid);
1182 cout << indents[idxindent]
1183 << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid
1184 << " ObjectId= " << oid << dec << endl;
1185 idxind++;
1186 idxindent = (idxind <= _MXINDENT_) ? idxind : _MXINDENT_;
1187 break;
1188
1189 case PPS_REFERENCE :
1190 GetRawU8(oid);
1191 GetRawI8(i8);
1192 cout << indents[idxindent]
1193 << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= "
1194 << oid << " OrigPos=" << i8 << dec << endl;
1195 break;
1196
1197 case PPS_NAMETAG_MARK :
1198 cout << indents[idxindent]
1199 << "<PPS_NAMETAG_MARK> tag at position " << hex << cpos << dec << endl;
1200 break;
1201
1202 case PPS_POSTAG_MARK :
1203 GetRawI8(i8);
1204 cout << indents[idxindent]
1205 << "<PPS_POSTAG_MARK> tag at position " << hex << cpos
1206 << " TPos=" << i8 << dec << endl;
1207 break;
1208
1209 case PPS_ENDOBJECT :
1210 GetRawU8(oid);
1211 idxind--;
1212 idxindent = (idxind >= 0) ? idxind : 0;
1213 cout << indents[idxindent]
1214 << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= "
1215 << oid << dec << endl;
1216 break;
1217
1218 case PPS_POSTAG_TABLE :
1219 GetRawI4(i4);
1220 for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);
1221 cout << indents[idxindent]
1222 << "<PPS_POSTAG_TABLE> tag at position " << hex << cpos << dec << endl;
1223 break;
1224
1225 case PPS_NAMETAG_TABLE :
1226 if (Version() < 3) {
1227 GetRawI8(i8);
1228 GetRawI4(i4);
1229 buff = new char[i4+1];
1230 GetRawBytes(buff, i4);
1231 buff[i4] = '\0'; str = buff;
1232 delete[] buff;
1233 cout << indents[idxindent]
1234 << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec
1235 << " Name= " << str << endl;
1236 }
1237 else {
1238 cout << indents[idxindent]
1239 << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec << endl;
1240 int_8 stats[8];
1241 GetI8s(stats,8);
1242 GetRawU8(ui8); // nb de nametag
1243 for(uint_8 kt=0; kt<ui8; kt++) {
1244 string tname;
1245 GetRawI8(i8);
1246 GetStr(tname);
1247 if (lev > 0)
1248 cout << "<PPS_NAMETAG_ENTRY> NameTag=" << tname
1249 << " NameTagMark Position=" << hex << i8 << dec << endl;
1250 }
1251 GetRawI8(i8); // position debut NameTagTable
1252 }
1253 break;
1254
1255 case PPS_EOF :
1256 if (Version() < 3) GetRawI8(i8);
1257 cout << indents[idxindent]
1258 << "<PPS_EOF> tag at position " << hex << cpos
1259 << " TagPos=" << i8 << dec << endl;
1260 eofok = true;
1261 break;
1262
1263 default :
1264 cerr << " ERROR : Unexpected tag value " << hex << ppstag
1265 << " At position" << cpos << dec << endl;
1266 throw FileFormatExc("PPFBinaryInputStream::AnalyseTags() - Unexpected tag value !");
1267 }
1268 }
1269 else {
1270 string dtype = "???? x";
1271 if (ppst30 == PPS_DATATYPE_COMPLEX) dtype = "COMPLEX x";
1272 else if (ppst3 == PPS_DATATYPE_CHAR) dtype = "CHAR x";
1273 else if (ppst3 == PPS_DATATYPE_FLOAT) dtype = "FLOAT x";
1274 else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x";
1275 else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x";
1276 int_4 dsize = (int_4)tagsize2datasize(ppst1);
1277 int_8 dsizeskip = dsize;
1278 if (ppst30 == PPS_DATATYPE_COMPLEX) {
1279 dsize--;
1280 dsizeskip = 2*dsize;
1281 }
1282 char sb[16];
1283 sprintf(sb, "%d", dsize);
1284 dtype += sb;
1285
1286 switch (ppst2) {
1287
1288 case PPS_SIMPLE :
1289 if (lev > 2) cout << indents[idxindent]
1290 << "<PPS_SIMPLE> tag at position " << hex << cpos << dec
1291 << " DataType=" << dtype << endl;
1292 s->seekg(dsizeskip, ios::cur);
1293 break;
1294
1295 case PPS_SIMPLE_ARRAY4 :
1296 GetRawI4(i4);
1297 if (lev > 0) cout << indents[idxindent]
1298 << "<PPS_SIMPLE_ARRAY4> tag at position " << hex << cpos << dec
1299 << " DataType=" << dtype << " NElts= " << i4 << endl;
1300 s->seekg(dsizeskip*(int_8)i4, ios::cur);
1301 break;
1302
1303 case PPS_SIMPLE_ARRAY8 :
1304 GetRawU8(ui8);
1305 if (lev > 0) cout << indents[idxindent]
1306 << "<PPS_SIMPLE_ARRAY8> tag at position " << hex << cpos << dec
1307 << " DataType=" << dtype << " NElts= " << ui8 << endl;
1308 s->seekg(dsizeskip*ui8, ios::cur);
1309 break;
1310 }
1311 }
1312 }
1313 if (!eofok)
1314 throw FileFormatExc("PPFBinaryInputStream::AnalyseTags() - Not found <PPS_EOF> tag ");
1315
1316 cout << " PPFBinaryInputStream::AnalyseTags() - End - Total Number of Tags= " << totntags << endl;
1317 cout << " ---------------------------------------------------------- \n" << endl;
1318 return;
1319}
1320
1321
1322//-------------------------------------------------------------------------
1323//------------------- Classe PPFBinaryOutputStream -----------------------
1324//-------------------------------------------------------------------------
1325
1326
1327/*!
1328 \class SOPHYA::PPFBinaryInputStream
1329 \ingroup BaseTools
1330 PPF output stream implementing write operations. The byte-ordering (endianess)
1331 of the output stream can be selected at creation. By default, the native endianness
1332 of the machine is used.
1333*/
1334
1335/*! Constructor from a RawInOutStream pointer
1336 \param os : pointer to RawInOutStream
1337 \param ad : if true, the RawInOutStream \b os is deleted by the destructor
1338 \param endianness : Endianness selector PPS_NATIVE PPS_LITTLE_ENDIAN PPS_BIG_ENDIAN
1339*/
1340PPFBinaryOutputStream::PPFBinaryOutputStream(RawInOutStream* os, bool ad, int endianness)
1341{
1342 s = os;
1343 _ads = ad;
1344 Init(endianness);
1345}
1346
1347/*! Constructor
1348 \param flnm : output file name
1349 \param endianness : Endianness selector PPS_NATIVE PPS_LITTLE_ENDIAN PPS_BIG_ENDIAN
1350*/
1351PPFBinaryOutputStream::PPFBinaryOutputStream(string const& flnm, int endianness)
1352{
1353 // Output stream creation
1354 s = new RawOutFileStream(flnm.c_str());
1355 _ads = true;
1356 Init(endianness);
1357}
1358
1359PPFBinaryOutputStream::~PPFBinaryOutputStream()
1360{
1361 WriteNameTagTable();
1362 if (_ads && (s != NULL)) delete s; // Close the stream
1363}
1364
1365void
1366PPFBinaryOutputStream::Init(int endianness)
1367{
1368 if (endianness == -1)
1369 bigEndian = IS_BIG_ENDIAN;
1370 else
1371 bigEndian = endianness;
1372
1373 version = 3;
1374 // Header
1375 PutRawBytes("SOS-SOPHYA-PPersistFile V4 ",32);
1376 PutRawBytes(bigEndian
1377 ? "BIG-ENDIAN "
1378 : "LITTLE-ENDIAN ",32);
1379
1380// ---- GMT creation date of the file
1381 time_t tm = time(NULL);
1382 creationdate = tm;
1383 char datestring[33];
1384 int l=strftime(datestring,32,"%d/%m/%Y %H:%M:%S GMT",gmtime(&tm));
1385 for(int i=l; i<32; i++) datestring[i] = ' ';
1386 datestring[32] = '\0';
1387 PutRawBytes(datestring, 32);
1388}
1389
1390void
1391PPFBinaryOutputStream::WriteNameTagTable()
1392{
1393 int_8 tagPos;
1394 tagPos = s->tellp();
1395 PutRawUByte(PPS_NAMETAG_TABLE); // NameTagTable tag
1396 // Ecriture nb de PosTag et nb d'objets dans le flot
1397 int_8 stats[8] = {0,0,0,0,0,0,0,0};
1398 stats[0] = _nbpostag;
1399 stats[1] = _nbobjs;
1400 stats[2] = _nbtlobjs;
1401 stats[3] = _nbrefs;
1402 stats[4] = _maxnestlevel;
1403 PutI8s(stats, 8);
1404 // Ecriture nb de tag et les tags
1405 PutRawU8((uint_8)tags.size()); // Number of tags
1406 if (tags.size() > 0) {
1407 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1408 int_8 pos = (*i).second;
1409 PutRawI8(pos);
1410 PutStr((*i).first);
1411 }
1412 }
1413 PutRawI8(tagPos);
1414 PutRawUByte(PPS_EOF);
1415 return;
1416}
1417
1418void
1419PPFBinaryOutputStream::WriteNameTagTableV2()
1420{
1421 if (tags.size() == 0) {
1422 PutRawUByte(PPS_EOF);
1423 PutRawI8(-1);
1424 } else {
1425 int_8 tagPos;
1426 tagPos = s->tellp();
1427 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1428 string name = (*i).first;
1429 int_8 pos = (*i).second;
1430 PutRawUByte(PPS_NAMETAG_TABLE); // This is a tag
1431 PutRawI8(pos); // position of previous tag
1432 PutRawI4(name.length()); // length of the name
1433 PutRawBytes(name.c_str(), name.length()); // name, without final "0".
1434 }
1435 PutRawUByte(PPS_EOF);
1436 PutRawI8(tagPos);
1437 }
1438
1439}
1440
1441
1442int_8
1443PPFBinaryOutputStream::WritePositionTag()
1444{
1445 int_8 tagpos;
1446 tagpos = s->tellp();
1447 PutRawByte(PPS_POSTAG_MARK);
1448 PutRawI8(tagpos);
1449 _nbpostag++; // Compteur de nombre de tags
1450 return(tagpos);
1451}
1452
1453void
1454PPFBinaryOutputStream::WriteNameTag(string const& name)
1455{
1456 // if (name.length() > MAXTAGLEN_V2)
1457 // throw ParmError("PPFBinaryOutputStream::WriteNameTag tag name too long");
1458
1459 if (tags.find(name) != tags.end())
1460 throw DuplicateIdExc("PPFBinaryOutputStream::WriteNameTag duplicate tag name");
1461
1462 // Get current file position
1463 int_8 tagPos;
1464 tagPos = s->tellp();
1465
1466 tags[name] = tagPos;
1467 PutRawUByte(PPS_NAMETAG_MARK); // This is a tag
1468 // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000
1469}
1470
1471//++
1472// void PPFBinaryOutputStream::PutByte(char& c)
1473// void PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1474// void PPFBinaryOutputStream::PutR4 (r_4 result)
1475// void PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1476// void PPFBinaryOutputStream::PutR8 (r_8 result)
1477// void PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1478// void PPFBinaryOutputStream::PutI2 (int_2 result)
1479// void PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1480// void PPFBinaryOutputStream::PutU2 (uint_2 result)
1481// void PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1482// void PPFBinaryOutputStream::PutI4 (int_4 result)
1483// void PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1484// void PPFBinaryOutputStream::PutU4 (uint_4 result)
1485// void PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1486// void PPFBinaryOutputStream::PutI8 (int_8 result)
1487// void PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1488// void PPFBinaryOutputStream::PutU8 (uint_8 result)
1489// void PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1490// void PPFBinaryOutputStream::PutStr (string const&)
1491// Ecriture de données portables.. Pour chaque type
1492// de données, on peut écrire une valeur, ou un tableau de valeurs.
1493// void PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1494// Ecriture d'une ligne de texte dans le fichier PPersist.
1495//--
1496
1497
1498
1499
1500void
1501PPFBinaryOutputStream::PutRawBytes(void const* ptr, size_t bytes)
1502{
1503 s->write((char const*)ptr, bytes);
1504}
1505
1506void
1507PPFBinaryOutputStream::PutRawByte(char c)
1508{
1509 PutRawBytes(&c, 1);
1510}
1511
1512void
1513PPFBinaryOutputStream::PutRawUByte(unsigned char c)
1514{
1515 PutRawBytes(&c, 1);
1516}
1517
1518void
1519PPFBinaryOutputStream::PutRawI2 (int_2 val)
1520{
1521 if (bigEndian != IS_BIG_ENDIAN)
1522 bswap2(&val);
1523
1524 PutRawBytes(&val, sizeof(int_2));
1525}
1526
1527void
1528PPFBinaryOutputStream::PutRawI4 (int_4 val)
1529{
1530 if (bigEndian != IS_BIG_ENDIAN)
1531 bswap4(&val);
1532
1533 PutRawBytes(&val, sizeof(int_4));
1534}
1535
1536void
1537PPFBinaryOutputStream::PutRawI8 (int_8 val)
1538{
1539 if (bigEndian != IS_BIG_ENDIAN)
1540 bswap8(&val);
1541
1542 PutRawBytes(&val, sizeof(int_8));
1543}
1544
1545void
1546PPFBinaryOutputStream::PutRawU8 (uint_8 val)
1547{
1548 if (bigEndian != IS_BIG_ENDIAN)
1549 bswap8(&val);
1550
1551 PutRawBytes(&val, sizeof(uint_8));
1552}
1553
1554void
1555PPFBinaryOutputStream::PutArrayTag(unsigned char datasz, size_t sz, unsigned char datatype)
1556// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
1557{
1558 if (sz <= 0x7fffffff) {
1559 PutRawUByte(PPS_SIMPLE_ARRAY4 + datasize2tagsize(datasz) + datatype);
1560 PutRawI4(sz);
1561 } else {
1562 PutRawUByte(PPS_SIMPLE_ARRAY8 + datasize2tagsize(datasz) + datatype);
1563 PutRawU8(sz);
1564 }
1565}
1566
1567void
1568PPFBinaryOutputStream::PutByte(char c)
1569{
1570 PutRawByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_CHAR);
1571 PutRawBytes(&c, 1);
1572}
1573
1574
1575
1576void
1577PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1578{
1579 PutArrayTag(1, bytes, PPS_DATATYPE_CHAR);
1580 PutRawBytes(ptr, bytes);
1581}
1582
1583void
1584PPFBinaryOutputStream::PutR4 (r_4 val)
1585{
1586 PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_FLOAT);
1587
1588 if (bigEndian != IS_BIG_ENDIAN)
1589 bswap4(&val);
1590
1591 PutRawBytes(&val, sizeof(r_4));
1592}
1593
1594void
1595PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1596{
1597 PutArrayTag(4, n, PPS_DATATYPE_FLOAT);
1598
1599 if (bigEndian == IS_BIG_ENDIAN) {
1600 PutRawBytes(tab, n*sizeof(r_4));
1601 } else {
1602 for (size_t i=0; i<n; i++) {
1603 r_4 val = tab[i];
1604 bswap4(&val);
1605 PutRawBytes(&val, sizeof(r_4));
1606 }
1607 }
1608}
1609
1610void
1611PPFBinaryOutputStream::PutR8 (r_8 val)
1612{
1613 PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_FLOAT);
1614
1615 if (bigEndian != IS_BIG_ENDIAN)
1616 bswap8(&val);
1617
1618 PutRawBytes(&val, sizeof(r_8));
1619}
1620
1621void
1622PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1623{
1624 PutArrayTag(8, n, PPS_DATATYPE_FLOAT);
1625
1626 if (bigEndian == IS_BIG_ENDIAN) {
1627 PutRawBytes(tab, n*sizeof(r_8));
1628 } else {
1629 for (size_t i=0; i<n; i++) {
1630 r_8 val = tab[i];
1631 bswap8(&val);
1632 PutRawBytes(&val, sizeof(r_8));
1633 }
1634 }
1635}
1636
1637void
1638PPFBinaryOutputStream::PutI1 (int_1 val)
1639{
1640 PutRawUByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_INTEGER);
1641 PutRawBytes(&val, sizeof(int_1));
1642}
1643
1644void
1645PPFBinaryOutputStream::PutI1s (int_1 const* tab, size_t n)
1646{
1647 PutArrayTag(1, n, PPS_DATATYPE_INTEGER);
1648 PutRawBytes(tab, n*sizeof(int_1));
1649}
1650
1651void
1652PPFBinaryOutputStream::PutU1 (uint_1 val)
1653{
1654 PutRawUByte(PPS_SIMPLE + datasize2tagsize(1) + PPS_DATATYPE_UNSIGNED);
1655 PutRawBytes(&val, sizeof(uint_1));
1656}
1657
1658void
1659PPFBinaryOutputStream::PutU1s (uint_1 const* tab, size_t n)
1660{
1661 PutArrayTag(1, n, PPS_DATATYPE_UNSIGNED);
1662 PutRawBytes(tab, n*sizeof(uint_1));
1663}
1664
1665void
1666PPFBinaryOutputStream::PutI2 (int_2 val)
1667{
1668 PutRawUByte(PPS_SIMPLE + datasize2tagsize(2) + PPS_DATATYPE_INTEGER);
1669
1670 if (bigEndian != IS_BIG_ENDIAN)
1671 bswap2(&val);
1672
1673 PutRawBytes(&val, sizeof(int_2));
1674}
1675
1676void
1677PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1678{
1679 PutArrayTag(2, n, PPS_DATATYPE_INTEGER);
1680
1681 if (bigEndian == IS_BIG_ENDIAN) {
1682 PutRawBytes(tab, n*sizeof(int_2));
1683 } else {
1684 for (size_t i=0; i<n; i++) {
1685 int_2 val = tab[i];
1686 bswap2(&val);
1687 PutRawBytes(&val, sizeof(int_2));
1688 }
1689 }
1690}
1691
1692void
1693PPFBinaryOutputStream::PutU2 (uint_2 val)
1694{
1695 PutRawUByte(PPS_SIMPLE + datasize2tagsize(2) + PPS_DATATYPE_UNSIGNED);
1696
1697 if (bigEndian != IS_BIG_ENDIAN)
1698 bswap2(&val);
1699
1700 PutRawBytes(&val, sizeof(uint_2));
1701}
1702
1703void
1704PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1705{
1706 PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED);
1707
1708 if (bigEndian == IS_BIG_ENDIAN) {
1709 PutRawBytes(tab, n*sizeof(uint_2));
1710 } else {
1711 for (size_t i=0; i<n; i++) {
1712 uint_2 val = tab[i];
1713 bswap2(&val);
1714 PutRawBytes(&val, sizeof(uint_2));
1715 }
1716 }
1717}
1718
1719void
1720PPFBinaryOutputStream::PutI4 (int_4 val)
1721{
1722 PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_INTEGER);
1723
1724 if (bigEndian != IS_BIG_ENDIAN)
1725 bswap4(&val);
1726
1727 PutRawBytes(&val, sizeof(int_4));
1728}
1729
1730void
1731PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1732{
1733 PutArrayTag(4, n, PPS_DATATYPE_INTEGER);
1734
1735 if (bigEndian == IS_BIG_ENDIAN) {
1736 PutRawBytes(tab, n*sizeof(int_4));
1737 } else {
1738 for (size_t i=0; i<n; i++) {
1739 int_4 val = tab[i];
1740 bswap4(&val);
1741 PutRawBytes(&val, sizeof(int_4));
1742 }
1743 }
1744}
1745
1746void
1747PPFBinaryOutputStream::PutU4 (uint_4 val)
1748{
1749 PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_UNSIGNED);
1750
1751 if (bigEndian != IS_BIG_ENDIAN)
1752 bswap4(&val);
1753
1754 PutRawBytes(&val, sizeof(uint_4));
1755}
1756
1757void
1758PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1759{
1760 PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED);
1761
1762 if (bigEndian == IS_BIG_ENDIAN) {
1763 PutRawBytes(tab, n*sizeof(uint_4));
1764 } else {
1765 for (size_t i=0; i<n; i++) {
1766 uint_4 val = tab[i];
1767 bswap4(&val);
1768 PutRawBytes(&val, sizeof(uint_4));
1769 }
1770 }
1771}
1772
1773void
1774PPFBinaryOutputStream::PutI8 (int_8 val)
1775{
1776 PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_INTEGER);
1777
1778 if (bigEndian != IS_BIG_ENDIAN)
1779 bswap8(&val);
1780
1781 PutRawBytes(&val, sizeof(int_8));
1782}
1783
1784void
1785PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1786{
1787 PutArrayTag(8, n, PPS_DATATYPE_INTEGER);
1788
1789 if (bigEndian == IS_BIG_ENDIAN) {
1790 PutRawBytes(tab, n*sizeof(int_8));
1791 } else {
1792 for (size_t i=0; i<n; i++) {
1793 int_8 val = tab[i];
1794 bswap8(&val);
1795 PutRawBytes(&val, sizeof(int_8));
1796 }
1797 }
1798}
1799
1800void
1801PPFBinaryOutputStream::PutU8 (uint_8 val)
1802{
1803 PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_UNSIGNED);
1804
1805 if (bigEndian != IS_BIG_ENDIAN)
1806 bswap8(&val);
1807
1808 PutRawBytes(&val, sizeof(uint_8));
1809}
1810
1811void
1812PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1813{
1814 PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED);
1815
1816 if (bigEndian == IS_BIG_ENDIAN) {
1817 PutRawBytes(tab, n*sizeof(uint_8));
1818 } else {
1819 for (size_t i=0; i<n; i++) {
1820 uint_8 val = tab[i];
1821 bswap8(&val);
1822 PutRawBytes(&val, sizeof(uint_8));
1823 }
1824 }
1825}
1826
1827void
1828PPFBinaryOutputStream::PutStr(string const& str)
1829{
1830 PutRawUByte(PPS_STRING);
1831 PutRawI4(str.length());
1832 PutRawBytes(str.c_str(), str.length());
1833}
1834
1835void
1836PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1837{
1838 string str = ptr;
1839 PutStr(str);
1840}
1841
1842
1843void
1844PPFBinaryOutputStream::PutZ4 (complex<r_4> val)
1845{
1846 PutRawUByte(PPS_SIMPLE + datasize2tagsize(4) + PPS_DATATYPE_COMPLEX);
1847 r_4 reim[2];
1848 reim[0] = val.real();
1849 reim[1] = val.imag();
1850 if (bigEndian != IS_BIG_ENDIAN) {
1851 bswap4(reim);
1852 bswap4(reim+1);
1853 }
1854 PutRawBytes(reim, 2*sizeof(r_4));
1855}
1856
1857void
1858PPFBinaryOutputStream::PutZ4s (complex<r_4> const* tab, size_t n)
1859{
1860 PutArrayTag(4, n, PPS_DATATYPE_COMPLEX);
1861
1862 if (bigEndian == IS_BIG_ENDIAN) {
1863 PutRawBytes(tab, n*2*sizeof(r_4));
1864 } else {
1865 for (size_t i=0; i<n; i++) {
1866 r_4 reim[2];
1867 reim[0] = tab[i].real();
1868 reim[1] = tab[i].imag();
1869 bswap4(reim);
1870 bswap4(reim+1);
1871 PutRawBytes(reim, 2*sizeof(r_4));
1872 }
1873 }
1874}
1875
1876void
1877PPFBinaryOutputStream::PutZ8 (complex<r_8> val)
1878{
1879 PutRawUByte(PPS_SIMPLE + datasize2tagsize(8) + PPS_DATATYPE_COMPLEX);
1880 r_8 reim[2];
1881 reim[0] = val.real();
1882 reim[1] = val.imag();
1883 if (bigEndian != IS_BIG_ENDIAN) {
1884 bswap8(reim);
1885 bswap8(reim+1);
1886 }
1887 PutRawBytes(reim, 2*sizeof(r_8));
1888}
1889
1890void
1891PPFBinaryOutputStream::PutZ8s (complex<r_8> const* tab, size_t n)
1892{
1893 PutArrayTag(8, n, PPS_DATATYPE_COMPLEX);
1894
1895 if (bigEndian == IS_BIG_ENDIAN) {
1896 PutRawBytes(tab, n*2*sizeof(r_8));
1897 } else {
1898 for (size_t i=0; i<n; i++) {
1899 r_8 reim[2];
1900 reim[0] = tab[i].real();
1901 reim[1] = tab[i].imag();
1902 bswap8(reim);
1903 bswap8(reim+1);
1904 PutRawBytes(reim, 2*sizeof(r_8));
1905 }
1906 }
1907}
1908
1909// Support pour les long double (128 bits floating point numbers)
1910#ifdef SO_LDBLE128
1911void
1912PPFBinaryOutputStream::PutR16 (r_16 val)
1913{
1914 PutRawUByte(PPS_SIMPLE + datasize2tagsize(16) + PPS_DATATYPE_FLOAT);
1915
1916 if (bigEndian != IS_BIG_ENDIAN)
1917 bswap16(&val);
1918
1919 PutRawBytes(&val, sizeof(r_16));
1920}
1921
1922void
1923PPFBinaryOutputStream::PutR16s (r_16 const* tab, size_t n)
1924{
1925 PutArrayTag(16, n, PPS_DATATYPE_FLOAT);
1926
1927 if (bigEndian == IS_BIG_ENDIAN) {
1928 PutRawBytes(tab, n*sizeof(r_16));
1929 } else {
1930 for (size_t i=0; i<n; i++) {
1931 r_16 val = tab[i];
1932 bswap16(&val);
1933 PutRawBytes(&val, sizeof(r_16));
1934 }
1935 }
1936}
1937
1938void
1939PPFBinaryOutputStream::PutZ16 (complex<r_16> val)
1940{
1941 PutRawUByte(PPS_SIMPLE + datasize2tagsize(16) + PPS_DATATYPE_COMPLEX);
1942 r_16 reim[2];
1943 reim[0] = val.real();
1944 reim[1] = val.imag();
1945 if (bigEndian != IS_BIG_ENDIAN) {
1946 bswap16(reim);
1947 bswap16(reim+1);
1948 }
1949 PutRawBytes(reim, 2*sizeof(r_16));
1950}
1951
1952void
1953PPFBinaryOutputStream::PutZ16s (complex<r_16> const* tab, size_t n)
1954{
1955 PutArrayTag(16, n, PPS_DATATYPE_COMPLEX);
1956
1957 if (bigEndian == IS_BIG_ENDIAN) {
1958 PutRawBytes(tab, n*2*sizeof(r_16));
1959 } else {
1960 for (size_t i=0; i<n; i++) {
1961 r_16 reim[2];
1962 reim[0] = tab[i].real();
1963 reim[1] = tab[i].imag();
1964 bswap16(reim);
1965 bswap16(reim+1);
1966 PutRawBytes(reim, 2*sizeof(r_16));
1967 }
1968 }
1969}
1970#endif
1971
1972void
1973PPFBinaryOutputStream::PutPosTagTable(int_8 const * ptab, size_t sz)
1974{
1975 PutRawUByte(PPS_POSTAG_TABLE);
1976 int_4 tsz = sz;
1977 PutRawI4(tsz);
1978 for(int kk=0; kk<tsz; kk++)
1979 PutRawI8(ptab[kk]);
1980 return;
1981}
1982
1983void
1984PPFBinaryOutputStream::PutPosTagTable(vector<int_8> const& ptab)
1985{
1986 PutRawUByte(PPS_POSTAG_TABLE);
1987 int_4 tsz = ptab.size();
1988 PutRawI4(tsz);
1989 for(int kk=0; kk<tsz; kk++)
1990 PutRawI8(ptab[kk]);
1991 return;
1992}
1993
Note: See TracBrowser for help on using the repository browser.