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

Last change on this file since 2481 was 2481, checked in by ansari, 22 years ago

Corrections bugs ds PPFStream::Write/ReadPositionTag() - Reza 9 Dec 2003

File size: 41.4 KB
RevLine 
[2475]1#include "machdefs.h"
2#include <stdio.h>
3#include <sys/types.h>
4#include <time.h>
5#include "ppfbinstream.h"
6#include "pexceptions.h"
7#include <iostream>
8
9
10/*
11// strptime n'est pas defini sous Linux - Reza Mars 2000
12#if defined(OS_LINUX) || defined(OS_MACOSX)
13extern "C" {
14char *strptime(const char *buf, const char *format, struct tm *tm);
15}
16#endif
17*/
18
19#define MAXTAGLEN_V2 255
20
21
22static inline void bswap8(void* p)
23{
24 uint_8 tmp = *(uint_8*)p;
25 *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |
26 ((tmp >> (5*8)) & 0x0000FF00) |
27 ((tmp >> (3*8)) & 0x00FF0000) |
28 ((tmp >> (1*8)) & 0xFF000000) |
29 ((tmp & 0xFF000000) << (1*8)) |
30 ((tmp & 0x00FF0000) << (3*8)) |
31 ((tmp & 0x0000FF00) << (5*8)) |
32 ((tmp & 0x000000FF) << (7*8));
33}
34
35static inline void bswap4(void* p)
36{
37 uint_4 tmp = *(uint_4*)p;
38 *(uint_4*)p = ((tmp >> 24) & 0x000000FF) |
39 ((tmp >> 8) & 0x0000FF00) |
40 ((tmp & 0x0000FF00) << 8) |
41 ((tmp & 0x000000FF) << 24);
42}
43
44static inline void bswap2(void* p)
45{
46 uint_2 tmp = *(uint_2*)p;
47 *(uint_2*)p = ((tmp >> 8) & 0x00FF) |
48 ((tmp & 0x00FF) << 8);
49}
50
[2477]51//-------------------------------------------------------------------------
52//---------------------- Classe PPFBinaryIOStream ------------------------
53//-------------------------------------------------------------------------
[2475]54
[2477]55
56PPFBinaryIOStream::PPFBinaryIOStream()
57{
58 version = 0; // PPersist(In/Out) version
59 // creationdate = time(NULL); // Date de creation du fichier
60 _nbpostag = 0; // Nb de tag de positionnement
61 _nbobjs = 0; // Nb total d'objets
62 _nbtlobjs = 0; // Nb d'objets de niveau 1
63}
64
65PPFBinaryIOStream::~PPFBinaryIOStream()
66{
67}
68
69string
70PPFBinaryIOStream::CreationDateStr()
71{
72 time_t cdt = CreationDate();
73 string cdate = ctime(&cdt);
74 return(cdate);
75}
76
77string
78PPFBinaryIOStream::InfoString()
79{
80 string rs;
81 char buff[256];
82 sprintf(buff,"PPFStream Version= %d CreationDate= ", Version());
83 rs += buff;
84 rs += CreationDateStr();
85 sprintf(buff,"\n NbObjs= %ld NbTopLevObjs= %ld ", (long)NbObjects(),
86 (long)NbTopLevelObjects());
87 rs += buff;
88 sprintf(buff,"\n NbPosTag= %ld NbNameTag= %ld ", (long)NbPosTags(),
89 (long)NbNameTags());
90 rs += buff;
91 return rs;
92}
93
94
95string
96PPFBinaryIOStream::GetTagName(int itag)
97{
98 if (itag<0 || itag >= (int)tags.size()) return "";
99 map<string, int_8>::iterator i = tags.begin();
100 for (int j=0; j<itag; j++) i++;
101 return((*i).first);
102}
103
104
105static vector<string> * ret_tag_names = NULL;
106vector<string> const &
107PPFBinaryIOStream::GetNameTags()
108{
109if (ret_tag_names) delete ret_tag_names;
110ret_tag_names = new vector<string> ;
111map<string, int_8>::iterator i;
112for(i=tags.begin(); i!=tags.end(); i++) ret_tag_names->push_back((*i).first);
113return(*ret_tag_names);
114}
115
116//-------------------------------------------------------------------------
117//-------------------- Classe PPFBinaryInputStream -----------------------
118//-------------------------------------------------------------------------
119
[2476]120PPFBinaryInputStream::PPFBinaryInputStream(RawInOutStream * is, bool ad, bool scan)
[2475]121{
[2476]122 s = is;
123 _ads = ad;
124 Init(scan);
[2475]125}
126
127//++
128PPFBinaryInputStream::PPFBinaryInputStream(string const& flnm, bool scan)
129//
130// Constructeur. Ouvre le fichier.
131//--
132{
133 s = new RawInFileStream(flnm.c_str());
[2476]134 _ads = true;
135 Init(scan);
136}
[2475]137
[2476]138PPFBinaryInputStream::~PPFBinaryInputStream()
139{
140 if (_ads && (s!=NULL) ) delete s;
141}
142
143void
144PPFBinaryInputStream::Init(bool scan)
145{
[2475]146 // Read and check header
147
148 char rbuf[36];
149 GetRawBytes(rbuf, 32);
150 if (strncmp(rbuf,"SOS-SOPHYA-PPersistFile", 23) != 0) {
151 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream bad header");
152 }
153 rbuf[32] = '\0';
154 version = atoi(rbuf+25);
155 if (version < 2) {
[2476]156 cerr << "PPFBinaryInputStream::PPFBinaryInputStream(" << FileName() << ") Version(=" << version
[2475]157 << ") < 2 not supported !" << endl;
158 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream() - Unsupported (Old) Version");
159 }
160 // read endianness
161 GetRawBytes(rbuf, 32);
162 if (strncmp(rbuf,"BIG-ENDIAN",10) == 0)
163 bigEndian = true;
164 else if (strncmp(rbuf,"LITTLE-ENDIAN",13) == 0)
165 bigEndian = false;
166 else {
167 throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream bad header - endianness");
168 }
169
170 // read creation date
171 GetRawBytes(rbuf, 32);
172 rbuf[32] = '\0';
173 struct tm tm;
174 /* #if !(defined(__MWERKS__) || defined(OS_MACOSX)) RZ-DEL */
175 strptime(rbuf,"%d/%m/%Y %H:%M:%S GMT",&tm);
176 /* #else
177 sscanf(rbuf,"%2d/%2d/%4d %2d:%2d:%2d GMT",&tm.tm_mday,&tm.tm_mon,&tm.tm_year,
178 &tm.tm_hour,&tm.tm_min,&tm.tm_sec);
179
180 tm.tm_mon --;
181 tm.tm_year -= 1900;
182 #endif RZ-DEL */
183
184 creationdate = mktime(&tm);
185 seqread = true; // To flag non sequential reads
[2476]186 if (scan && s->isSeekable()) {
187 if (Version() >= 3) ReadNameTagTable();
188 else ReadNameTagTableV2();
189 }
[2475]190}
191
192
193void
[2476]194PPFBinaryInputStream::ReadNameTagTable()
[2475]195{
[2476]196 unsigned char ppstype;
197 int_8 debut;
198 debut = s->tellg();
199
200 s->seekg(-(sizeof(int_8)+1), ios::end);
[2477]201 // Lecture position NameTagTable et tag EOF
202 int_8 pos;
203 GetRawI8(pos);
[2481]204 GetRawUByte(ppstype);
[2476]205 if (ppstype != PPS_EOF)
206 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable() Corrupted file, no EOF tag at end of file");
207
208 // On se positionne au debut du NameTagTable
209 s->seekg(pos);
[2481]210 GetRawUByte(ppstype);
[2476]211 if (ppstype != PPS_NAMETAG_TABLE)
212 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable() Corrupted file PPS_NAMETAG_TABLE not found");
[2477]213 // Lecture nb de PosTag et nb d'objets dans le flot
214 GetI8(_nbpostag);
215 GetI8(_nbobjs);
216 GetI8(_nbtlobjs);
[2476]217 uint_8 ttsz,it;
[2477]218 // Lecture nombre de NameTag
[2476]219 GetU8(ttsz);
[2477]220 if (ttsz > 0) {
221 for(it=0; it<ttsz; it++) {
222 int_8 tpos;
223 string tname;
224 GetI8(tpos);
225 GetStr(tname);
226 tags[tname] = tpos;
227 }
[2476]228 }
229 // On revient au debut du float, juste apres l'entete
230 s->seekg(debut);
231}
232
233void
234PPFBinaryInputStream::ReadNameTagTableV2()
235{
[2475]236 // On cherche la liste des tags, a la fin du fichier
237
238 unsigned char ppstype;
239 int_8 debut;
240 debut = s->tellg();
241
242 s->seekg(-(sizeof(int_8)+1), ios::end);
243
[2481]244 GetRawUByte(ppstype);
[2475]245 if (ppstype != PPS_EOF)
[2476]246 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, no eof entry at end of file");
[2475]247
248 int_8 pos;
249 GetRawI8(pos);
250 if (pos < 0) { // no tags
251 s->seekg(debut);
252 return;
253 }
254
255 char buffer[MAXTAGLEN_V2+1];
256 s->seekg(pos);
257 while (true) {
[2481]258 GetRawUByte(ppstype);
[2475]259 if (ppstype == PPS_EOF) break;
260
261 if (ppstype != PPS_NAMETAG_TABLE)
[2476]262 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, bad tag entry");
[2475]263
264 GetRawI8(pos);
265 int_4 len;
266 GetRawI4(len);
267 if (len > MAXTAGLEN_V2)
[2476]268 throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, tag name too long");
[2475]269 GetRawBytes(buffer, len);
270 buffer[len] = '\0';
271
272 tags[buffer] = pos;
273 }
274 s->seekg(debut);
275}
276
277
278bool
279PPFBinaryInputStream::GotoPositionTag(int_8 pos)
280{
281 s->seekg(pos);
[2481]282 unsigned char ppstag;
283 GetRawUByte(ppstag);
284 if (ppstag != PPS_POSTAG_MARK)
285 throw FileFormatExc("PPFBinaryInputStream::GotoPositionTag() - PPS_POSTAG_MARK not found!");
[2475]286 int_8 tpos;
287 GetRawI8(tpos);
288 if (tpos != pos)
289 throw FileFormatExc("PPFBinaryInputStream::GotoPositionTag() - Wrong tag position!");
290 return true;
291}
292
293bool
294PPFBinaryInputStream::GotoNameTag(string const& name)
295{
296 map<string, int_8>::iterator i = tags.find(name);
297 if (i == tags.end())
298 return false;
299 // throw NotFoundExc("PPFBinaryInputStream::GotoNameTag tag not found");
300 s->seekg((*i).second);
301 seqread = false;
302 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
303 return(true);
304}
305
306
307bool
308PPFBinaryInputStream::GotoNameTagNum(int itag)
309{
310 if (itag<0 || itag >= (int)tags.size()) return false;
311 map<string, int_8>::iterator i = tags.begin();
312 for (int j=0; j<itag; j++) i++;
313 s->seekg((*i).second);
314 seqread = false;
315 // objList.clear(); $CHECK$ EA 171199 Ne pas faire ? Reza 03/2000 ?
316 return(true);
317}
318
319
320bool
321PPFBinaryInputStream::SkipToNextObject()
322{
[2477]323 if (! s->isSeekable()) return false;
324 unsigned char ppstag=0;
325 // int kss;
326 while ((ppstag != PPS_OBJECT) && (ppstag != PPS_REFERENCE) && (ppstag != PPS_EOF)) {
327 // kss++;
328 GetRawUByte(ppstag);
329 // cout << " DBG--SkipToNextObject(" << kss << ")" << (int)ppstag << ","
330 // << (int)PPS_OBJECT << " fpos=" << s->tellg() << endl;
331 if ((ppstag != PPS_OBJECT) && (ppstag != PPS_REFERENCE) && (ppstag != PPS_EOF))
332 SkipItem(false, ppstag);
333 }
334 s->seekg(-1, ios::cur);
335 if (ppstag == PPS_OBJECT) return true;
336 else return false;
337
[2475]338}
339
340bool
[2476]341PPFBinaryInputStream::SkipNextItem()
[2475]342{
[2477]343 if (! s->isSeekable()) return false;
344 SkipItem();
[2475]345 return true;
346}
347
[2476]348char
[2477]349PPFBinaryInputStream::NextItemTag(short datasz, size_t asz)
[2476]350{
[2477]351 int_8 cpos;
352 cpos = s->tellg();
353
354 unsigned char ppstag=0;
355 unsigned char ppst1,ppst2,ppst3,ppst30;
356 GetRawUByte(ppstag);
357 ppst1 = ppstag&0x0f; // bits 0123
358 ppst2 = ppstag&0x30; // bits 45
359 ppst3 = ppstag&0xc0; // bits 67
360 ppst30 = ppstag&0xc1; // bits 0 67
361
[2476]362 datasz = 0;
[2477]363 asz = 0;
364
365 int_4 i4;
366 int_8 i8;
367 uint_8 ui8;
368
369 if ((ppst2 == 0) && (ppst3 == 0) ) {
370 switch (ppst1) {
371 case PPS_STRING :
372 GetRawI4(i4);
373 datasz = 1;
374 asz = i4;
375 break;
376
377 case PPS_NULL :
378 case PPS_OBJECT :
379 case PPS_REFERENCE :
380 case PPS_NAMETAG_MARK :
381 case PPS_POSTAG_MARK :
382 case PPS_ENDOBJECT :
383 case PPS_POSTAG_TABLE :
384 case PPS_NAMETAG_TABLE :
385 case PPS_EOF :
386 datasz = 0;
387 asz = 0;
388 break;
389
390 default :
391 throw FileFormatExc("PPFBinaryInputStream::NextItemTag() - Unexpected tag value !");
392 break;
393 }
394 }
395 else {
396 int_4 dsize = ppst1;
397 int_4 dsizeskip = dsize;
398 if (ppst30 == PPS_DATATYPE_COMPLEX) {
399 dsize--;
400 dsizeskip = 2*dsize;
401 }
402 switch (ppst2) {
403 case PPS_SIMPLE :
404 datasz = dsize;
405 asz = 1;
406 break;
407
408 case PPS_SIMPLE_ARRAY4 :
409 GetRawI4(i4);
410 datasz = dsize;
411 asz = i4;
412 break;
413
414 case PPS_SIMPLE_ARRAY8 :
415 GetRawU8(ui8);
416 datasz = dsize;
[2481]417 asz = ui8;
[2477]418 break;
419 }
420 }
421
422 s->seekg(cpos,ios::beg);
423 return(ppstag);
[2476]424}
425
426void
[2477]427PPFBinaryInputStream::SkipItem(bool fgrdt, unsigned char itag)
[2476]428{
[2477]429 unsigned char ppstag=0;
430 unsigned char ppst1,ppst2,ppst3,ppst30;
431 uint_8 ui8;
432 int_4 i4;
433 int_8 i8;
434
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()
1024 << endl << endl;
1025
[2475]1026 uint_8 totntags = 0;
1027 bool eofok = false;
1028
1029 while ( (ppstag != PPS_EOF) && (cpos < fsize) ) {
1030 cpos = s->tellg();
1031 GetRawUByte(ppstag);
1032 totntags++;
1033
1034 ppst1 = ppstag&0x0f; // bits 0123
1035 ppst2 = ppstag&0x30; // bits 45
1036 ppst3 = ppstag&0xc0; // bits 67
1037 ppst30 = ppstag&0xc1; // bits 0 67
1038 if ((ppst2 == 0) && (ppst3 == 0) ) {
1039 switch (ppst1) {
1040
1041 case PPS_NULL :
1042 if (lev > 1) cout << "<PPS_NULL> tag at position " << hex << cpos << dec << endl;
1043 break;
1044
1045 case PPS_STRING :
1046 GetRawI4(i4);
1047 if (lev > 1) cout << "<PPS_STRING> tag at position " << hex << cpos << dec
1048 << " Length=" << i4 << endl;
1049 s->seekg(i4,ios::cur);
1050 break;
1051
1052 case PPS_OBJECT :
1053 GetRawU8(cid);
1054 GetRawU8(oid);
1055 cout << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid
1056 << " ObjectId= " << oid << dec << endl;
1057 break;
1058
1059 case PPS_REFERENCE :
1060 GetRawU8(oid);
1061 GetRawI8(i8);
1062 cout << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= "
1063 << oid << " OrigPos=" << i8 << dec << endl;
1064 break;
1065
1066 case PPS_NAMETAG_MARK :
1067 cout << "<PPS_NAMETAG_MARK> tag at position " << hex << cpos << dec << endl;
1068 break;
1069
1070 case PPS_POSTAG_MARK :
1071 GetRawI8(i8);
1072 cout << "<PPS_POSTAG_MARK> tag at position " << hex << cpos
1073 << " TPos=" << i8 << dec << endl;
1074 break;
1075
1076 case PPS_ENDOBJECT :
1077 GetRawU8(oid);
1078 cout << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= "
1079 << oid << dec << endl;
1080 break;
1081
1082 case PPS_POSTAG_TABLE :
1083 GetRawI4(i4);
1084 for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);
1085 cout << "<PPS_POSTAG_TABLE> tag at position " << hex << cpos << dec << endl;
1086 break;
1087
1088 case PPS_NAMETAG_TABLE :
[2477]1089 if (Version() < 3) {
1090 GetRawI8(i8);
1091 GetRawI4(i4);
1092 buff = new char[i4+1];
1093 GetRawBytes(buff, i4);
1094 buff[i4] = '\0'; str = buff;
1095 delete[] buff;
1096 cout << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec
1097 << " Name= " << str << endl;
1098 }
1099 else {
1100 cout << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec << endl;
1101 GetI8(i8); // nb pos tag
1102 GetI8(i8); // nb d'objets
1103 GetI8(i8); // nb objets toplevel
1104 GetU8(ui8); // nb de nametag
1105 for(int kt=0; kt<ui8; kt++) {
1106 string tname;
1107 GetI8(i8);
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
1256 PutI8(_nbpostag);
1257 PutI8(_nbobjs);
1258 PutI8(_nbtlobjs);
1259 // Ecriture nb de tag et les tags
1260 PutU8((uint_8)tags.size()); // Number of tags
1261 if (tags.size() > 0) {
[2476]1262 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1263 int_8 pos = (*i).second;
1264 PutI8(pos);
1265 PutStr((*i).first);
1266 }
1267 }
[2477]1268 PutRawI8(tagPos);
1269 PutRawUByte(PPS_EOF);
1270 return;
[2476]1271}
1272
1273void
1274PPFBinaryOutputStream::WriteNameTagTableV2()
[2475]1275{
1276 if (tags.size() == 0) {
1277 PutRawUByte(PPS_EOF);
1278 PutRawI8(-1);
1279 } else {
1280 int_8 tagPos;
1281 tagPos = s->tellp();
1282 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1283 string name = (*i).first;
1284 int_8 pos = (*i).second;
1285 PutRawUByte(PPS_NAMETAG_TABLE); // This is a tag
1286 PutRawI8(pos); // position of previous tag
1287 PutRawI4(name.length()); // length of the name
1288 PutRawBytes(name.c_str(), name.length()); // name, without final "0".
1289 }
1290 PutRawUByte(PPS_EOF);
1291 PutRawI8(tagPos);
1292 }
1293
1294}
1295
[2476]1296
[2475]1297int_8
1298PPFBinaryOutputStream::WritePositionTag()
1299{
1300 int_8 tagpos;
1301 tagpos = s->tellp();
1302 PutRawByte(PPS_POSTAG_MARK);
[2481]1303 PutRawI8(tagpos);
[2477]1304 _nbpostag++; // Compteur de nombre de tags
[2475]1305 return(tagpos);
1306}
1307
1308void
1309PPFBinaryOutputStream::WriteNameTag(string const& name)
1310{
1311 // if (name.length() > MAXTAGLEN_V2)
1312 // throw ParmError("PPFBinaryOutputStream::WriteNameTag tag name too long");
1313
1314 if (tags.find(name) != tags.end())
1315 throw DuplicateIdExc("PPFBinaryOutputStream::WriteNameTag duplicate tag name");
1316
1317 // Get current file position
1318 int_8 tagPos;
1319 tagPos = s->tellp();
1320
1321 tags[name] = tagPos;
1322 PutRawUByte(PPS_NAMETAG_MARK); // This is a tag
1323 // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000
1324}
1325
1326//++
1327// void PPFBinaryOutputStream::PutByte(char& c)
1328// void PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1329// void PPFBinaryOutputStream::PutR4 (r_4 result)
1330// void PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1331// void PPFBinaryOutputStream::PutR8 (r_8 result)
1332// void PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1333// void PPFBinaryOutputStream::PutI2 (int_2 result)
1334// void PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1335// void PPFBinaryOutputStream::PutU2 (uint_2 result)
1336// void PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1337// void PPFBinaryOutputStream::PutI4 (int_4 result)
1338// void PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1339// void PPFBinaryOutputStream::PutU4 (uint_4 result)
1340// void PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1341// void PPFBinaryOutputStream::PutI8 (int_8 result)
1342// void PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1343// void PPFBinaryOutputStream::PutU8 (uint_8 result)
1344// void PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1345// void PPFBinaryOutputStream::PutStr (string const&)
1346// Ecriture de données portables.. Pour chaque type
1347// de données, on peut écrire une valeur, ou un tableau de valeurs.
1348// void PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1349// Ecriture d'une ligne de texte dans le fichier PPersist.
1350//--
1351
1352
1353
1354
1355void
1356PPFBinaryOutputStream::PutRawBytes(void const* ptr, size_t bytes)
1357{
1358 s->write((char const*)ptr, bytes);
1359}
1360
1361void
1362PPFBinaryOutputStream::PutRawByte(char c)
1363{
1364 PutRawBytes(&c, 1);
1365}
1366
1367void
1368PPFBinaryOutputStream::PutRawUByte(unsigned char c)
1369{
1370 PutRawBytes(&c, 1);
1371}
1372
1373void
1374PPFBinaryOutputStream::PutRawI2 (int_2 val)
1375{
1376 if (bigEndian != IS_BIG_ENDIAN)
1377 bswap2(&val);
1378
1379 PutRawBytes(&val, sizeof(int_2));
1380}
1381
1382void
1383PPFBinaryOutputStream::PutRawI4 (int_4 val)
1384{
1385 if (bigEndian != IS_BIG_ENDIAN)
1386 bswap4(&val);
1387
1388 PutRawBytes(&val, sizeof(int_4));
1389}
1390
1391void
1392PPFBinaryOutputStream::PutRawI8 (int_8 val)
1393{
1394 if (bigEndian != IS_BIG_ENDIAN)
1395 bswap8(&val);
1396
1397 PutRawBytes(&val, sizeof(int_8));
1398}
1399
1400void
1401PPFBinaryOutputStream::PutRawU8 (uint_8 val)
1402{
1403 if (bigEndian != IS_BIG_ENDIAN)
1404 bswap8(&val);
1405
1406 PutRawBytes(&val, sizeof(uint_8));
1407}
1408
1409void
1410PPFBinaryOutputStream::PutArrayTag(short datasz, size_t sz, short datatype)
1411// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
1412{
1413 if (sz <= 0x7fffffff) {
1414 PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype);
1415 PutRawI4(sz);
1416 } else {
1417 PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype);
1418 PutRawU8(sz);
1419 }
1420}
1421
1422void
1423PPFBinaryOutputStream::PutByte(char c)
1424{
1425 PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR);
1426 PutRawBytes(&c, 1);
1427}
1428
1429
1430
1431void
1432PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1433{
1434 PutArrayTag(1, bytes, PPS_DATATYPE_CHAR);
1435 PutRawBytes(ptr, bytes);
1436}
1437
1438void
1439PPFBinaryOutputStream::PutR4 (r_4 val)
1440{
1441 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT);
1442
1443 if (bigEndian != IS_BIG_ENDIAN)
1444 bswap4(&val);
1445
1446 PutRawBytes(&val, sizeof(r_4));
1447}
1448
1449void
1450PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1451{
1452 PutArrayTag(4, n, PPS_DATATYPE_FLOAT);
1453
1454 if (bigEndian == IS_BIG_ENDIAN) {
1455 PutRawBytes(tab, n*sizeof(r_4));
1456 } else {
1457 for (unsigned int i=0; i<n; i++) {
1458 r_4 val = tab[i];
1459 bswap4(&val);
1460 PutRawBytes(&val, sizeof(r_4));
1461 }
1462 }
1463}
1464
1465void
1466PPFBinaryOutputStream::PutR8 (r_8 val)
1467{
1468 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT);
1469
1470 if (bigEndian != IS_BIG_ENDIAN)
1471 bswap8(&val);
1472
1473 PutRawBytes(&val, sizeof(r_8));
1474}
1475
1476void
1477PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1478{
1479 PutArrayTag(8, n, PPS_DATATYPE_FLOAT);
1480
1481 if (bigEndian == IS_BIG_ENDIAN) {
1482 PutRawBytes(tab, n*sizeof(r_8));
1483 } else {
1484 for (unsigned int i=0; i<n; i++) {
1485 r_8 val = tab[i];
1486 bswap8(&val);
1487 PutRawBytes(&val, sizeof(r_8));
1488 }
1489 }
1490}
1491
1492void
1493PPFBinaryOutputStream::PutI1 (int_1 val)
1494{
1495 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_INTEGER);
1496 PutRawBytes(&val, sizeof(int_1));
1497}
1498
1499void
1500PPFBinaryOutputStream::PutI1s (int_1 const* tab, size_t n)
1501{
1502 PutArrayTag(1, n, PPS_DATATYPE_INTEGER);
1503 PutRawBytes(tab, n*sizeof(int_1));
1504}
1505
1506void
1507PPFBinaryOutputStream::PutU1 (uint_1 val)
1508{
1509 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_UNSIGNED);
1510 PutRawBytes(&val, sizeof(uint_1));
1511}
1512
1513void
1514PPFBinaryOutputStream::PutU1s (uint_1 const* tab, size_t n)
1515{
1516 PutArrayTag(1, n, PPS_DATATYPE_UNSIGNED);
1517 PutRawBytes(tab, n*sizeof(uint_1));
1518}
1519
1520void
1521PPFBinaryOutputStream::PutI2 (int_2 val)
1522{
1523 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER);
1524
1525 if (bigEndian != IS_BIG_ENDIAN)
1526 bswap2(&val);
1527
1528 PutRawBytes(&val, sizeof(int_2));
1529}
1530
1531void
1532PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1533{
1534 PutArrayTag(2, n, PPS_DATATYPE_INTEGER);
1535
1536 if (bigEndian == IS_BIG_ENDIAN) {
1537 PutRawBytes(tab, n*sizeof(int_2));
1538 } else {
1539 for (unsigned int i=0; i<n; i++) {
1540 int_2 val = tab[i];
1541 bswap2(&val);
1542 PutRawBytes(&val, sizeof(int_2));
1543 }
1544 }
1545}
1546
1547void
1548PPFBinaryOutputStream::PutU2 (uint_2 val)
1549{
1550 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED);
1551
1552 if (bigEndian != IS_BIG_ENDIAN)
1553 bswap2(&val);
1554
1555 PutRawBytes(&val, sizeof(uint_2));
1556}
1557
1558void
1559PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1560{
1561 PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED);
1562
1563 if (bigEndian == IS_BIG_ENDIAN) {
1564 PutRawBytes(tab, n*sizeof(uint_2));
1565 } else {
1566 for (unsigned int i=0; i<n; i++) {
1567 uint_2 val = tab[i];
1568 bswap2(&val);
1569 PutRawBytes(&val, sizeof(uint_2));
1570 }
1571 }
1572}
1573
1574void
1575PPFBinaryOutputStream::PutI4 (int_4 val)
1576{
1577 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER);
1578
1579 if (bigEndian != IS_BIG_ENDIAN)
1580 bswap4(&val);
1581
1582 PutRawBytes(&val, sizeof(int_4));
1583}
1584
1585void
1586PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1587{
1588 PutArrayTag(4, n, PPS_DATATYPE_INTEGER);
1589
1590 if (bigEndian == IS_BIG_ENDIAN) {
1591 PutRawBytes(tab, n*sizeof(int_4));
1592 } else {
1593 for (unsigned int i=0; i<n; i++) {
1594 int_4 val = tab[i];
1595 bswap4(&val);
1596 PutRawBytes(&val, sizeof(int_4));
1597 }
1598 }
1599}
1600
1601void
1602PPFBinaryOutputStream::PutU4 (uint_4 val)
1603{
1604 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED);
1605
1606 if (bigEndian != IS_BIG_ENDIAN)
1607 bswap4(&val);
1608
1609 PutRawBytes(&val, sizeof(uint_4));
1610}
1611
1612void
1613PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1614{
1615 PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED);
1616
1617 if (bigEndian == IS_BIG_ENDIAN) {
1618 PutRawBytes(tab, n*sizeof(uint_4));
1619 } else {
1620 for (unsigned int i=0; i<n; i++) {
1621 uint_4 val = tab[i];
1622 bswap4(&val);
1623 PutRawBytes(&val, sizeof(uint_4));
1624 }
1625 }
1626}
1627
1628void
1629PPFBinaryOutputStream::PutI8 (int_8 val)
1630{
1631 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER);
1632
1633 if (bigEndian != IS_BIG_ENDIAN)
1634 bswap8(&val);
1635
1636 PutRawBytes(&val, sizeof(int_8));
1637}
1638
1639void
1640PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1641{
1642 PutArrayTag(8, n, PPS_DATATYPE_INTEGER);
1643
1644 if (bigEndian == IS_BIG_ENDIAN) {
1645 PutRawBytes(tab, n*sizeof(int_8));
1646 } else {
1647 for (unsigned int i=0; i<n; i++) {
1648 int_8 val = tab[i];
1649 bswap8(&val);
1650 PutRawBytes(&val, sizeof(int_8));
1651 }
1652 }
1653}
1654
1655void
1656PPFBinaryOutputStream::PutU8 (uint_8 val)
1657{
1658 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED);
1659
1660 if (bigEndian != IS_BIG_ENDIAN)
1661 bswap8(&val);
1662
1663 PutRawBytes(&val, sizeof(uint_8));
1664}
1665
1666void
1667PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1668{
1669 PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED);
1670
1671 if (bigEndian == IS_BIG_ENDIAN) {
1672 PutRawBytes(tab, n*sizeof(uint_8));
1673 } else {
1674 for (unsigned int i=0; i<n; i++) {
1675 uint_8 val = tab[i];
1676 bswap8(&val);
1677 PutRawBytes(&val, sizeof(uint_8));
1678 }
1679 }
1680}
1681
1682void
1683PPFBinaryOutputStream::PutStr(string const& str)
1684{
1685 PutRawUByte(PPS_STRING);
1686 PutRawI4(str.length());
1687 PutRawBytes(str.c_str(), str.length());
1688}
1689
1690void
1691PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1692{
1693 string str = ptr;
1694 PutStr(str);
1695}
1696
1697
1698void
1699PPFBinaryOutputStream::PutZ4 (complex<r_4> val)
1700{
1701 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_COMPLEX);
1702 r_4 reim[2];
1703 reim[0] = val.real();
1704 reim[1] = val.imag();
1705 if (bigEndian != IS_BIG_ENDIAN) {
1706 bswap4(reim);
1707 bswap4(reim+1);
1708 }
1709 PutRawBytes(reim, 2*sizeof(r_4));
1710}
1711
1712void
1713PPFBinaryOutputStream::PutZ4s (complex<r_4> const* tab, size_t n)
1714{
1715 PutArrayTag(4, n, PPS_DATATYPE_COMPLEX);
1716
1717 if (bigEndian == IS_BIG_ENDIAN) {
1718 PutRawBytes(tab, n*2*sizeof(r_4));
1719 } else {
1720 for (unsigned int i=0; i<n; i++) {
1721 r_4 reim[2];
1722 reim[0] = tab[i].real();
1723 reim[1] = tab[i].imag();
1724 bswap4(reim);
1725 bswap4(reim+1);
1726 PutRawBytes(reim, 2*sizeof(r_4));
1727 }
1728 }
1729}
1730
1731void
1732PPFBinaryOutputStream::PutZ8 (complex<r_8> val)
1733{
1734 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_COMPLEX);
1735 r_8 reim[2];
1736 reim[0] = val.real();
1737 reim[1] = val.imag();
1738 if (bigEndian != IS_BIG_ENDIAN) {
1739 bswap8(reim);
1740 bswap8(reim+1);
1741 }
1742 PutRawBytes(reim, 2*sizeof(r_8));
1743}
1744
1745void
1746PPFBinaryOutputStream::PutZ8s (complex<r_8> const* tab, size_t n)
1747{
1748 PutArrayTag(8, n, PPS_DATATYPE_COMPLEX);
1749
1750 if (bigEndian == IS_BIG_ENDIAN) {
1751 PutRawBytes(tab, n*2*sizeof(r_8));
1752 } else {
1753 for (unsigned int i=0; i<n; i++) {
1754 r_8 reim[2];
1755 reim[0] = tab[i].real();
1756 reim[1] = tab[i].imag();
1757 bswap8(reim);
1758 bswap8(reim+1);
1759 PutRawBytes(reim, 2*sizeof(r_8));
1760 }
1761 }
1762}
1763
1764void
1765PPFBinaryOutputStream::PutPosTagTable(int_8 const * ptab, size_t sz)
1766{
1767 PutRawUByte(PPS_POSTAG_TABLE);
1768 int_4 tsz = sz;
1769 PutRawI4(tsz);
1770 for(int kk=0; kk<tsz; kk++)
1771 PutRawI8(ptab[kk]);
1772 return;
1773}
1774
1775void
1776PPFBinaryOutputStream::PutPosTagTable(vector<int_8> const& ptab)
1777{
1778 PutRawUByte(PPS_POSTAG_TABLE);
1779 int_4 tsz = ptab.size();
1780 PutRawI4(tsz);
1781 for(int kk=0; kk<tsz; kk++)
1782 PutRawI8(ptab[kk]);
1783 return;
1784}
1785
Note: See TracBrowser for help on using the repository browser.