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

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

Corrections petites erreurs / warning, suite compilation sur SGI-CC - Reza 22 Dec 2003

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