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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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