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

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

remis declaration strptime pour gcc 2.95 et ajout statistiques en fin de fichier / modifs Write/ReadNameTagTable - Reza 9 Dec 2003

File size: 41.8 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 int_8 i8;
370 uint_8 ui8;
371
372 if ((ppst2 == 0) && (ppst3 == 0) ) {
373 switch (ppst1) {
374 case PPS_STRING :
375 GetRawI4(i4);
376 datasz = 1;
377 asz = i4;
378 break;
379
380 case PPS_NULL :
381 case PPS_OBJECT :
382 case PPS_REFERENCE :
383 case PPS_NAMETAG_MARK :
384 case PPS_POSTAG_MARK :
385 case PPS_ENDOBJECT :
386 case PPS_POSTAG_TABLE :
387 case PPS_NAMETAG_TABLE :
388 case PPS_EOF :
389 datasz = 0;
390 asz = 0;
391 break;
392
393 default :
394 throw FileFormatExc("PPFBinaryInputStream::NextItemTag() - Unexpected tag value !");
395 break;
396 }
397 }
398 else {
399 int_4 dsize = ppst1;
400 int_4 dsizeskip = dsize;
401 if (ppst30 == PPS_DATATYPE_COMPLEX) {
402 dsize--;
403 dsizeskip = 2*dsize;
404 }
405 switch (ppst2) {
406 case PPS_SIMPLE :
407 datasz = dsize;
408 asz = 1;
409 break;
410
411 case PPS_SIMPLE_ARRAY4 :
412 GetRawI4(i4);
413 datasz = dsize;
414 asz = i4;
415 break;
416
417 case PPS_SIMPLE_ARRAY8 :
418 GetRawU8(ui8);
419 datasz = dsize;
420 asz = ui8;
421 break;
422 }
423 }
424
425 s->seekg(cpos,ios::beg);
426 return(ppstag);
427}
428
429void
430PPFBinaryInputStream::SkipItem(bool fgrdt, unsigned char itag)
431{
432 unsigned char ppstag=0;
433 unsigned char ppst1,ppst2,ppst3,ppst30;
434 uint_8 ui8;
435 int_4 i4;
436 int_8 i8;
437
438
439 if (fgrdt) GetRawUByte(ppstag);
440 else ppstag = itag;
441
442 ppst1 = ppstag&0x0f; // bits 0123
443 ppst2 = ppstag&0x30; // bits 45
444 ppst3 = ppstag&0xc0; // bits 67
445 ppst30 = ppstag&0xc1; // bits 0 67
446 if ((ppst2 == 0) && (ppst3 == 0) ) {
447 switch (ppst1) {
448 case PPS_NULL :
449 case PPS_NAMETAG_MARK :
450 break;
451
452 case PPS_STRING :
453 GetRawI4(i4);
454 s->seekg(i4,ios::cur);
455 break;
456
457 case PPS_OBJECT :
458 GetRawU8(ui8);
459 GetRawU8(ui8);
460 break;
461
462 case PPS_REFERENCE :
463 GetRawU8(ui8);
464 GetRawI8(i8);
465 break;
466
467 case PPS_POSTAG_MARK :
468 GetRawI8(i8);
469 break;
470
471 case PPS_ENDOBJECT :
472 GetRawU8(ui8);
473 break;
474
475 case PPS_POSTAG_TABLE :
476 GetRawI4(i4);
477 // for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);
478 s->seekg((int_8)i4*8,ios::cur);
479 break;
480
481 case PPS_NAMETAG_TABLE :
482 if (Version() < 3) {
483 GetRawI8(i8);
484 GetRawI4(i4);
485 s->seekg(i4,ios::cur);
486 }
487 else {
488 GetI8(i8); // nb pos tag
489 GetI8(i8); // nb d'objets
490 GetI8(i8); // nb objets toplevel
491 GetU8(ui8); // nb de nametag
492 for(int kt=0; kt<ui8; kt++) {
493 GetRawI4(i4);
494 s->seekg(i4,ios::cur);
495 }
496 GetI8(i8); // position debut NameTagTable
497 }
498 break;
499
500
501 case PPS_EOF :
502 if (Version() < 3) GetRawI8(i8);
503 break;
504
505 default :
506 cerr << "PPFBinaryInputStream::SkipItem() ERROR : Unexpected tag value "
507 << hex << ppstag << " At position" << s->tellg() << dec << endl;
508 throw FileFormatExc("PPFBinaryInputStream::SkipItem() - Unexpected tag value !");
509 }
510 }
511 else {
512 string dtype = "???? x";
513 int_4 dsize = ppst1;
514 int_8 dsizeskip = dsize;
515 if (ppst30 == PPS_DATATYPE_COMPLEX) {
516 dsize--;
517 dsizeskip = 2*dsize;
518 }
519
520 switch (ppst2) {
521 case PPS_SIMPLE :
522 s->seekg(dsizeskip, ios::cur);
523 break;
524
525 case PPS_SIMPLE_ARRAY4 :
526 GetRawI4(i4);
527 s->seekg(dsizeskip*(int_8)i4, ios::cur);
528 break;
529
530 case PPS_SIMPLE_ARRAY8 :
531 GetRawU8(ui8);
532 s->seekg(dsizeskip*ui8, ios::cur);
533 break;
534 }
535 }
536return;
537}
538
539//++
540// void PPFBinaryInputStream::GetByte(char& c)
541// void PPFBinaryInputStream::GetBytes(void* ptr, size_t bytes)
542// void PPFBinaryInputStream::GetR4 (r_4& result)
543// void PPFBinaryInputStream::GetR4s (r_4* tab, size_t n)
544// void PPFBinaryInputStream::GetR8 (r_8& result)
545// void PPFBinaryInputStream::GetR8s (r_8* tab, size_t n)
546// void PPFBinaryInputStream::GetI2 (int_2& result)
547// void PPFBinaryInputStream::GetI2s (int_2* tab, size_t n)
548// void PPFBinaryInputStream::GetU2 (uint_2& result)
549// void PPFBinaryInputStream::GetU2s (uint_2* tab, size_t n)
550// void PPFBinaryInputStream::GetI4 (int_4& result)
551// void PPFBinaryInputStream::GetI4s (int_4* tab, size_t n)
552// void PPFBinaryInputStream::GetU4 (uint_4& result)
553// void PPFBinaryInputStream::GetU4s (uint_4* tab, size_t n)
554// void PPFBinaryInputStream::GetI8 (int_8& result)
555// void PPFBinaryInputStream::GetI8s (int_8* tab, size_t n)
556// void PPFBinaryInputStream::GetU8 (uint_8& result)
557// void PPFBinaryInputStream::GetU8s (uint_8* tab, size_t n)
558// Lecture de données portables depuis le fichier PPersist. Pour chaque type
559// de données, on peut lire une valeur, ou un tableau de valeurs.
560// void PPFBinaryInputStream::GetLine(char* ptr, size_t len)
561// Lecture d'une ligne de texte depuis le fichier PPersist.
562//--
563
564
565void
566PPFBinaryInputStream::GetTypeTag(unsigned char& c)
567{
568 int_8 tpos;
569 c = PPS_NAMETAG_MARK;
570 while ( (c == PPS_NAMETAG_MARK) || (c == PPS_POSTAG_MARK) ) {
571 GetRawUByte(c);
572 if (c == PPS_POSTAG_MARK) GetRawI8(tpos);
573 }
574 // while (c == PPS_NAMETAG_MARK) { Il ne faut plus faire ca !
575 // objList.clear(); $CHECK$ Reza 03/2000
576 // GetRawByte(c);
577 // }
578}
579
580
581void
582PPFBinaryInputStream::GetRawByte(char& c)
583{
584 GetRawBytes(&c, 1);
585}
586
587void
588PPFBinaryInputStream::GetRawUByte(unsigned char& c)
589{
590 GetRawBytes(&c, 1);
591}
592
593void
594PPFBinaryInputStream::GetRawBytes(void* ptr, size_t bytes)
595{
596 s->read((char*)ptr, bytes);
597}
598
599void
600PPFBinaryInputStream::GetRawI2 (int_2& result)
601{
602 GetRawBytes(&result, sizeof(int_2));
603 if (bigEndian != IS_BIG_ENDIAN)
604 bswap2(&result);
605}
606
607void
608PPFBinaryInputStream::GetRawI4 (int_4& result)
609{
610 GetRawBytes(&result, sizeof(int_4));
611 if (bigEndian != IS_BIG_ENDIAN)
612 bswap4(&result);
613}
614
615void
616PPFBinaryInputStream::GetRawI8 (int_8& result)
617{
618 GetRawBytes(&result, sizeof(int_8));
619 if (bigEndian != IS_BIG_ENDIAN)
620 bswap8(&result);
621}
622
623void
624PPFBinaryInputStream::GetRawU8 (uint_8& result)
625{
626 GetRawBytes(&result, sizeof(uint_8));
627 if (bigEndian != IS_BIG_ENDIAN)
628 bswap8(&result);
629}
630
631void
632PPFBinaryInputStream::CheckTag(short datasz, short datatype)
633// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
634{
635 unsigned char ppstype;
636 GetTypeTag(ppstype);
637 if (ppstype != PPS_SIMPLE + datasz + datatype)
638 throw FileFormatExc("PPFBinaryInputStream::CheckTag bad type in ppersist file");
639}
640
641void
642PPFBinaryInputStream::CheckArrayTag(short datasz, size_t sz, short datatype)
643// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
644{
645 unsigned char ppstype;
646 GetTypeTag(ppstype);
647 size_t filesz;
648 if (sz <= 0x7fffffff) {
649 if (ppstype != PPS_SIMPLE_ARRAY4 + datasz + datatype)
650 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad type in ppersist file");
651 int_4 ff;
652 GetRawI4(ff); filesz=ff;
653 } else {
654 if (ppstype != PPS_SIMPLE_ARRAY8 + datasz + datatype)
655 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad type in ppersist file");
656 uint_8 ff;
657 GetRawU8(ff); filesz=ff;
658 }
659 if (filesz != sz)
660 throw FileFormatExc("PPFBinaryInputStream::CheckArrayTag bad array size in ppersist file");
661}
662
663void
664PPFBinaryInputStream::GetByte(char& c)
665{
666 CheckTag(1,PPS_DATATYPE_CHAR);
667 GetRawBytes(&c, 1);
668}
669
670
671void
672PPFBinaryInputStream::GetBytes(void* ptr, size_t bytes)
673{
674 CheckArrayTag(1, bytes, PPS_DATATYPE_CHAR);
675 GetRawBytes(ptr, bytes);
676}
677void
678PPFBinaryInputStream::GetR4 (r_4& result)
679{
680 CheckTag(4,PPS_DATATYPE_FLOAT);
681 GetRawBytes(&result, sizeof(r_4));
682 if (bigEndian != IS_BIG_ENDIAN)
683 bswap4(&result);
684}
685
686
687void
688PPFBinaryInputStream::GetR4s (r_4* tab, size_t n)
689{
690 CheckArrayTag(4,n,PPS_DATATYPE_FLOAT);
691 GetRawBytes(tab, n*sizeof(r_4));
692 if (bigEndian == IS_BIG_ENDIAN) return;
693
694 for (unsigned int i=0; i<n; i++)
695 bswap4(tab+i);
696
697 return;
698}
699
700void
701PPFBinaryInputStream::GetR8 (r_8& result)
702{
703 CheckTag(8,PPS_DATATYPE_FLOAT);
704 GetRawBytes(&result, sizeof(r_8));
705 if (bigEndian != IS_BIG_ENDIAN)
706 bswap8(&result);
707}
708
709void
710PPFBinaryInputStream::GetR8s (r_8* tab, size_t n)
711{
712 CheckArrayTag(8,n,PPS_DATATYPE_FLOAT);
713 GetRawBytes(tab, n*sizeof(r_8));
714 if (bigEndian == IS_BIG_ENDIAN) return;
715
716 for (unsigned int i=0; i<n; i++)
717 bswap8(tab+i);
718
719 return;
720}
721
722void
723PPFBinaryInputStream::GetI1 (int_1& result)
724{
725 CheckTag(1,PPS_DATATYPE_INTEGER);
726 GetRawBytes(&result, sizeof(int_1));
727}
728
729void
730PPFBinaryInputStream::GetI1s (int_1* tab, size_t n)
731{
732 CheckArrayTag(1,n,PPS_DATATYPE_INTEGER);
733 GetRawBytes(tab, n*sizeof(int_1));
734}
735
736void
737PPFBinaryInputStream::GetU1 (uint_1& result)
738{
739 CheckTag(1,PPS_DATATYPE_UNSIGNED);
740 GetRawBytes(&result, sizeof(uint_1));
741}
742
743void
744PPFBinaryInputStream::GetU1s (uint_1* tab, size_t n)
745{
746 CheckArrayTag(1,n,PPS_DATATYPE_UNSIGNED);
747 GetRawBytes(tab, n*sizeof(uint_1));
748}
749
750void
751PPFBinaryInputStream::GetI2 (int_2& result)
752{
753 CheckTag(2,PPS_DATATYPE_INTEGER);
754 GetRawBytes(&result, sizeof(int_2));
755 if (bigEndian != IS_BIG_ENDIAN)
756 bswap2(&result);
757}
758
759void
760PPFBinaryInputStream::GetI2s (int_2* tab, size_t n)
761{
762 CheckArrayTag(2,n,PPS_DATATYPE_INTEGER);
763 GetRawBytes(tab, n*sizeof(int_2));
764 if (bigEndian == IS_BIG_ENDIAN) return;
765
766 for (unsigned int i=0; i<n; i++)
767 bswap2(tab+i);
768
769 return;
770}
771
772void
773PPFBinaryInputStream::GetU2 (uint_2& result)
774{
775 CheckTag(2,PPS_DATATYPE_UNSIGNED);
776 GetRawBytes(&result, sizeof(uint_2));
777 if (bigEndian != IS_BIG_ENDIAN)
778 bswap2(&result);
779}
780
781void
782PPFBinaryInputStream::GetU2s (uint_2* tab, size_t n)
783{
784 CheckArrayTag(2,n,PPS_DATATYPE_UNSIGNED);
785 GetRawBytes(tab, n*sizeof(uint_2));
786 if (bigEndian == IS_BIG_ENDIAN) return;
787
788 for (unsigned int i=0; i<n; i++)
789 bswap2(tab+i);
790
791 return;
792}
793
794void
795PPFBinaryInputStream::GetI4 (int_4& result)
796{
797 CheckTag(4,PPS_DATATYPE_INTEGER);
798 GetRawBytes(&result, sizeof(int_4));
799 if (bigEndian != IS_BIG_ENDIAN)
800 bswap4(&result);
801}
802
803void
804PPFBinaryInputStream::GetI4s (int_4* tab, size_t n)
805{
806 CheckArrayTag(4,n,PPS_DATATYPE_INTEGER);
807 GetRawBytes(tab, n*sizeof(int_4));
808 if (bigEndian == IS_BIG_ENDIAN) return;
809
810 for (unsigned int i=0; i<n; i++)
811 bswap4(tab+i);
812
813 return;
814}
815
816void
817PPFBinaryInputStream::GetU4 (uint_4& result)
818{
819 CheckTag(4,PPS_DATATYPE_UNSIGNED);
820 GetRawBytes(&result, sizeof(uint_4));
821 if (bigEndian != IS_BIG_ENDIAN)
822 bswap4(&result);
823}
824
825void
826PPFBinaryInputStream::GetU4s (uint_4* tab, size_t n)
827{
828 CheckArrayTag(4,n,PPS_DATATYPE_UNSIGNED);
829 GetRawBytes(tab, n*sizeof(uint_4));
830 if (bigEndian == IS_BIG_ENDIAN) return;
831
832 for (unsigned int i=0; i<n; i++)
833 bswap4(tab+i);
834
835 return;
836}
837
838
839void
840PPFBinaryInputStream::GetI8 (int_8& result)
841{
842 CheckTag(8,PPS_DATATYPE_INTEGER);
843 GetRawBytes(&result, sizeof(int_8));
844 if (bigEndian != IS_BIG_ENDIAN)
845 bswap8(&result);
846}
847
848void
849PPFBinaryInputStream::GetI8s (int_8* tab, size_t n)
850{
851 CheckArrayTag(8,n,PPS_DATATYPE_INTEGER);
852 GetRawBytes(tab, n*sizeof(int_8));
853 if (bigEndian == IS_BIG_ENDIAN) return;
854
855 for (unsigned int i=0; i<n; i++)
856 bswap8(tab+i);
857
858 return;
859}
860
861void
862PPFBinaryInputStream::GetU8 (uint_8& result)
863{
864 CheckTag(8,PPS_DATATYPE_UNSIGNED);
865 GetRawBytes(&result, sizeof(uint_8));
866 if (bigEndian != IS_BIG_ENDIAN)
867 bswap8(&result);
868}
869
870void
871PPFBinaryInputStream::GetU8s (uint_8* tab, size_t n)
872{
873 CheckArrayTag(8,n,PPS_DATATYPE_UNSIGNED);
874 GetRawBytes(tab, n*sizeof(uint_8));
875 if (bigEndian == IS_BIG_ENDIAN) return;
876
877 for (unsigned int i=0; i<n; i++)
878 bswap8(tab+i);
879
880 return;
881}
882
883
884void
885PPFBinaryInputStream::GetLine(char* ptr, size_t len)
886{
887 string str;
888 GetStr(str);
889 strncpy(ptr, str.c_str(), len);
890 ptr[len] = '\0';
891}
892
893void
894PPFBinaryInputStream::GetStr(string& str)
895{
896 unsigned char ppstype;
897 GetTypeTag(ppstype);
898 if (ppstype != PPS_STRING)
899 throw FileFormatExc("PPFBinaryInputStream::GetStr bad type in ppersist file");
900 int_4 len;
901 GetRawI4(len);
902 char * buff = new char[len+1];
903 GetRawBytes(buff, len);
904 buff[len] = '\0';
905 str = buff;
906 delete[] buff;
907}
908
909void
910PPFBinaryInputStream::GetZ4 (complex<r_4>& result)
911{
912 CheckTag(4,PPS_DATATYPE_COMPLEX);
913 r_4 reim[2];
914 GetRawBytes(reim, 2*sizeof(r_4));
915 if (bigEndian != IS_BIG_ENDIAN) {
916 bswap4(reim);
917 bswap4(reim+1);
918 }
919 result = complex<r_4>(reim[0], reim[1]);
920}
921
922void
923PPFBinaryInputStream::GetZ4s (complex<r_4>* tab, size_t n)
924{
925 CheckArrayTag(4,n,PPS_DATATYPE_COMPLEX);
926 GetRawBytes(tab, n*2*sizeof(r_4));
927 if (bigEndian == IS_BIG_ENDIAN) return;
928
929 r_4 * p = (r_4 *)tab;
930 for (unsigned int i=0; i<n; i++) {
931 bswap4(p); p++;
932 bswap4(p); p++;
933 }
934 return;
935}
936
937void
938PPFBinaryInputStream::GetZ8 (complex<r_8>& result)
939{
940 CheckTag(8,PPS_DATATYPE_COMPLEX);
941 r_8 reim[2];
942 GetRawBytes(reim, 2*sizeof(r_8));
943 if (bigEndian != IS_BIG_ENDIAN) {
944 bswap8(reim);
945 bswap8(reim+1);
946 }
947 result = complex<r_8>(reim[0], reim[1]);
948}
949
950void
951PPFBinaryInputStream::GetZ8s (complex<r_8>* tab, size_t n)
952{
953 CheckArrayTag(8,n,PPS_DATATYPE_COMPLEX);
954 GetRawBytes(tab, n*2*sizeof(r_8));
955 if (bigEndian == IS_BIG_ENDIAN) return;
956
957 r_8 * p = (r_8 *)tab;
958 for (unsigned int i=0; i<n; i++) {
959 bswap8(p); p++;
960 bswap8(p); p++;
961 }
962 return;
963}
964
965
966void
967PPFBinaryInputStream::GetPosTagTable(int_8* ptab, size_t sz)
968{
969 unsigned char ppstype;
970 GetTypeTag(ppstype);
971 if (ppstype != PPS_POSTAG_TABLE)
972 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable bad type in ppersist stream");
973 int_4 tsz;
974 GetRawI4(tsz);
975 if (tsz != sz)
976 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable Size mismatch ");
977 for(int kk=0; kk<tsz; kk++)
978 GetRawI8(ptab[kk]);
979 return;
980}
981
982void
983PPFBinaryInputStream::GetPosTagTable(vector<int_8>& ptab)
984{
985 unsigned char ppstype;
986 GetTypeTag(ppstype);
987 if (ppstype != PPS_POSTAG_TABLE)
988 throw FileFormatExc("PPFBinaryInputStream::GetPosTagTable bad type in ppersist stream");
989 ptab.clear();
990 int_4 tsz;
991 GetRawI4(tsz);
992 int_8 tpos;
993 for(int kk=0; kk<tsz; kk++) {
994 GetRawI8(tpos);
995 ptab.push_back(tpos);
996 }
997 return;
998}
999
1000
1001void
1002PPFBinaryInputStream::AnalyseTags(int lev)
1003{
1004 unsigned char ppstag=0;
1005 unsigned char ppst1,ppst2,ppst3,ppst30;
1006 int_8 cpos,fsize;
1007 uint_8 ui8,cid,oid;
1008 int_4 i4;
1009 int_8 i8;
1010 char * buff;
1011 string str;
1012
1013 cout << "\n ---------------------------------------------------------- " << endl;
1014 cout << " PPFBinaryInputStream::AnalyseTags(Level= " << lev << ")" << endl;
1015
1016
1017 cpos = s->tellg();
1018 s->seekg(0,ios::end);
1019 fsize = s->tellg();
1020 s->seekg(cpos,ios::beg);
1021
1022 cout << " FileName= " << FileName() << endl;
1023 cout << " Version= " << Version() << " FileSize= " << fsize
1024 << " Creation Date= " << CreationDateStr() << endl;
1025 cout << " NbPosTag=" << NbPosTags() << " NbNameTag=" << tags.size()
1026 << " NbObjs=" << NbObjects() << " NbTopLevObjs=" << NbTopLevelObjects()
1027 << " NbRefs=" << NbReferences() << " MaxNest=" << MaxNestedObjsLevel()
1028 << endl << endl;
1029
1030 uint_8 totntags = 0;
1031 bool eofok = false;
1032
1033 while ( (ppstag != PPS_EOF) && (cpos < fsize) ) {
1034 cpos = s->tellg();
1035 GetRawUByte(ppstag);
1036 totntags++;
1037
1038 ppst1 = ppstag&0x0f; // bits 0123
1039 ppst2 = ppstag&0x30; // bits 45
1040 ppst3 = ppstag&0xc0; // bits 67
1041 ppst30 = ppstag&0xc1; // bits 0 67
1042 if ((ppst2 == 0) && (ppst3 == 0) ) {
1043 switch (ppst1) {
1044
1045 case PPS_NULL :
1046 if (lev > 1) cout << "<PPS_NULL> tag at position " << hex << cpos << dec << endl;
1047 break;
1048
1049 case PPS_STRING :
1050 GetRawI4(i4);
1051 if (lev > 1) cout << "<PPS_STRING> tag at position " << hex << cpos << dec
1052 << " Length=" << i4 << endl;
1053 s->seekg(i4,ios::cur);
1054 break;
1055
1056 case PPS_OBJECT :
1057 GetRawU8(cid);
1058 GetRawU8(oid);
1059 cout << "<PPS_OBJECT> tag at position " << hex << cpos << " ClassId= " << cid
1060 << " ObjectId= " << oid << dec << endl;
1061 break;
1062
1063 case PPS_REFERENCE :
1064 GetRawU8(oid);
1065 GetRawI8(i8);
1066 cout << "<PPS_REFERENCE> tag at position " << hex << cpos << " ObjectId= "
1067 << oid << " OrigPos=" << i8 << dec << endl;
1068 break;
1069
1070 case PPS_NAMETAG_MARK :
1071 cout << "<PPS_NAMETAG_MARK> tag at position " << hex << cpos << dec << endl;
1072 break;
1073
1074 case PPS_POSTAG_MARK :
1075 GetRawI8(i8);
1076 cout << "<PPS_POSTAG_MARK> tag at position " << hex << cpos
1077 << " TPos=" << i8 << dec << endl;
1078 break;
1079
1080 case PPS_ENDOBJECT :
1081 GetRawU8(oid);
1082 cout << "<PPS_ENDOBJECT> tag at position " << hex << cpos << " ObjectId= "
1083 << oid << dec << endl;
1084 break;
1085
1086 case PPS_POSTAG_TABLE :
1087 GetRawI4(i4);
1088 for(int kkt=0; kkt<i4; kkt++) GetRawI8(i8);
1089 cout << "<PPS_POSTAG_TABLE> tag at position " << hex << cpos << dec << endl;
1090 break;
1091
1092 case PPS_NAMETAG_TABLE :
1093 if (Version() < 3) {
1094 GetRawI8(i8);
1095 GetRawI4(i4);
1096 buff = new char[i4+1];
1097 GetRawBytes(buff, i4);
1098 buff[i4] = '\0'; str = buff;
1099 delete[] buff;
1100 cout << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec
1101 << " Name= " << str << endl;
1102 }
1103 else {
1104 cout << "<PPS_NAMETAG_TABLE> tag at position " << hex << cpos << dec << endl;
1105 int_8 stats[8];
1106 GetI8s(stats,8);
1107 GetRawU8(ui8); // nb de nametag
1108 for(int kt=0; kt<ui8; kt++) {
1109 string tname;
1110 GetRawI8(i8);
1111 GetStr(tname);
1112 if (lev > 0)
1113 cout << "<PPS_NAMETAG_ENTRY> NameTag=" << tname
1114 << " NameTagMark Position=" << hex << i8 << dec << endl;
1115 }
1116 GetRawI8(i8); // position debut NameTagTable
1117 }
1118 break;
1119
1120 case PPS_EOF :
1121 if (Version() < 3) GetRawI8(i8);
1122 cout << "<PPS_EOF> tag at position " << hex << cpos
1123 << " TagPos=" << i8 << dec << endl;
1124 eofok = true;
1125 break;
1126
1127 default :
1128 cerr << " ERROR : Unexpected tag value " << hex << ppstag
1129 << " At position" << cpos << dec << endl;
1130 throw FileFormatExc("PPFBinaryInputStream::AnalyseTags() - Unexpected tag value !");
1131 }
1132 }
1133 else {
1134 string dtype = "???? x";
1135 if (ppst30 == PPS_DATATYPE_COMPLEX) dtype = "COMPLEX x";
1136 else if (ppst3 == PPS_DATATYPE_CHAR) dtype = "CHAR x";
1137 else if (ppst3 == PPS_DATATYPE_FLOAT) dtype = "FLOAT x";
1138 else if (ppst3 == PPS_DATATYPE_INTEGER) dtype = "INTEGER x";
1139 else if (ppst3 == PPS_DATATYPE_UNSIGNED) dtype = "UNSIGNED x";
1140 int_4 dsize = ppst1;
1141 int_8 dsizeskip = dsize;
1142 if (ppst30 == PPS_DATATYPE_COMPLEX) {
1143 dsize--;
1144 dsizeskip = 2*dsize;
1145 }
1146 char sb[16];
1147 sprintf(sb, "%d", dsize);
1148 dtype += sb;
1149
1150 switch (ppst2) {
1151
1152 case PPS_SIMPLE :
1153 if (lev > 2) cout << "<PPS_SIMPLE> tag at position " << hex << cpos << dec
1154 << " DataType=" << dtype << endl;
1155 s->seekg(dsizeskip, ios::cur);
1156 break;
1157
1158 case PPS_SIMPLE_ARRAY4 :
1159 GetRawI4(i4);
1160 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY4> tag at position " << hex << cpos << dec
1161 << " DataType=" << dtype << " NElts= " << i4 << endl;
1162 s->seekg(dsizeskip*(int_8)i4, ios::cur);
1163 break;
1164
1165 case PPS_SIMPLE_ARRAY8 :
1166 GetRawU8(ui8);
1167 if (lev > 0) cout << "<PPS_SIMPLE_ARRAY8> tag at position " << hex << cpos << dec
1168 << " DataType=" << dtype << " NElts= " << ui8 << endl;
1169 s->seekg(dsizeskip*ui8, ios::cur);
1170 break;
1171 }
1172 }
1173 }
1174 if (!eofok)
1175 throw FileFormatExc("PPFBinaryInputStream::AnalyseTags() - Not found <PPS_EOF> tag ");
1176
1177 cout << " PPFBinaryInputStream::AnalyseTags() - End - Total Number of Tags= " << totntags << endl;
1178 cout << " ---------------------------------------------------------- \n" << endl;
1179 return;
1180}
1181
1182
1183//-------------------------------------------------------------------------
1184//------------------- Classe PPFBinaryOutputStream -----------------------
1185//-------------------------------------------------------------------------
1186
1187
1188//++
1189// Class POutPersist
1190// Lib Outils++
1191// include ppersist.h
1192//
1193// Fichier d'objets persistants, en écriture.
1194//--
1195
1196
1197//++
1198// POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE)
1199//
1200// Crée un nouveau fichier ppersist. Par défaut, il est petit=boutien
1201// sur machines petit-boutiennes, et gros-boutien sur machines
1202// gros-boutiennes. On peut explicitement spécifier PPersist::PPS_LITTLE_ENDIAN
1203// ou PPersist::PPS_BIG_ENDIAN.
1204//--
1205
1206PPFBinaryOutputStream::PPFBinaryOutputStream(RawInOutStream* os, bool ad, int endianness)
1207{
1208 s = os;
1209 _ads = ad;
1210 Init(endianness);
1211}
1212
1213PPFBinaryOutputStream::PPFBinaryOutputStream(string const& flnm, int endianness)
1214{
1215 // Output stream creation
1216 s = new RawOutFileStream(flnm.c_str());
1217 _ads = true;
1218 Init(endianness);
1219}
1220
1221PPFBinaryOutputStream::~PPFBinaryOutputStream()
1222{
1223 WriteNameTagTable();
1224 if (_ads && (s != NULL)) delete s; // Close the stream
1225}
1226
1227void
1228PPFBinaryOutputStream::Init(int endianness)
1229{
1230 if (endianness == -1)
1231 bigEndian = IS_BIG_ENDIAN;
1232 else
1233 bigEndian = endianness;
1234
1235 version = 3;
1236 // Header
1237 PutRawBytes("SOS-SOPHYA-PPersistFile V3 ",32);
1238 PutRawBytes(bigEndian
1239 ? "BIG-ENDIAN "
1240 : "LITTLE-ENDIAN ",32);
1241
1242// ---- GMT creation date of the file
1243 time_t tm = time(NULL);
1244 creationdate = tm;
1245 char datestring[33];
1246 int l=strftime(datestring,32,"%d/%m/%Y %H:%M:%S GMT",gmtime(&tm));
1247 for(int i=l; i<32; i++) datestring[i] = ' ';
1248 datestring[32] = '\0';
1249 PutRawBytes(datestring, 32);
1250}
1251
1252void
1253PPFBinaryOutputStream::WriteNameTagTable()
1254{
1255 int_8 tagPos;
1256 tagPos = s->tellp();
1257 PutRawUByte(PPS_NAMETAG_TABLE); // NameTagTable tag
1258 // Ecriture nb de PosTag et nb d'objets dans le flot
1259 int_8 stats[8] = {0,0,0,0,0,0,0,0};
1260 stats[0] = _nbpostag;
1261 stats[1] = _nbobjs;
1262 stats[2] = _nbtlobjs;
1263 stats[3] = _nbrefs;
1264 stats[4] = _maxnestlevel;
1265 PutI8s(stats, 8);
1266 // Ecriture nb de tag et les tags
1267 PutRawU8((uint_8)tags.size()); // Number of tags
1268 if (tags.size() > 0) {
1269 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1270 int_8 pos = (*i).second;
1271 PutRawI8(pos);
1272 PutStr((*i).first);
1273 }
1274 }
1275 PutRawI8(tagPos);
1276 PutRawUByte(PPS_EOF);
1277 return;
1278}
1279
1280void
1281PPFBinaryOutputStream::WriteNameTagTableV2()
1282{
1283 if (tags.size() == 0) {
1284 PutRawUByte(PPS_EOF);
1285 PutRawI8(-1);
1286 } else {
1287 int_8 tagPos;
1288 tagPos = s->tellp();
1289 for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
1290 string name = (*i).first;
1291 int_8 pos = (*i).second;
1292 PutRawUByte(PPS_NAMETAG_TABLE); // This is a tag
1293 PutRawI8(pos); // position of previous tag
1294 PutRawI4(name.length()); // length of the name
1295 PutRawBytes(name.c_str(), name.length()); // name, without final "0".
1296 }
1297 PutRawUByte(PPS_EOF);
1298 PutRawI8(tagPos);
1299 }
1300
1301}
1302
1303
1304int_8
1305PPFBinaryOutputStream::WritePositionTag()
1306{
1307 int_8 tagpos;
1308 tagpos = s->tellp();
1309 PutRawByte(PPS_POSTAG_MARK);
1310 PutRawI8(tagpos);
1311 _nbpostag++; // Compteur de nombre de tags
1312 return(tagpos);
1313}
1314
1315void
1316PPFBinaryOutputStream::WriteNameTag(string const& name)
1317{
1318 // if (name.length() > MAXTAGLEN_V2)
1319 // throw ParmError("PPFBinaryOutputStream::WriteNameTag tag name too long");
1320
1321 if (tags.find(name) != tags.end())
1322 throw DuplicateIdExc("PPFBinaryOutputStream::WriteNameTag duplicate tag name");
1323
1324 // Get current file position
1325 int_8 tagPos;
1326 tagPos = s->tellp();
1327
1328 tags[name] = tagPos;
1329 PutRawUByte(PPS_NAMETAG_MARK); // This is a tag
1330 // objList.clear(); // $CHECK$ EA 171199 - Ne pas faire ? Reza 03/2000
1331}
1332
1333//++
1334// void PPFBinaryOutputStream::PutByte(char& c)
1335// void PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1336// void PPFBinaryOutputStream::PutR4 (r_4 result)
1337// void PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1338// void PPFBinaryOutputStream::PutR8 (r_8 result)
1339// void PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1340// void PPFBinaryOutputStream::PutI2 (int_2 result)
1341// void PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1342// void PPFBinaryOutputStream::PutU2 (uint_2 result)
1343// void PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1344// void PPFBinaryOutputStream::PutI4 (int_4 result)
1345// void PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1346// void PPFBinaryOutputStream::PutU4 (uint_4 result)
1347// void PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1348// void PPFBinaryOutputStream::PutI8 (int_8 result)
1349// void PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1350// void PPFBinaryOutputStream::PutU8 (uint_8 result)
1351// void PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1352// void PPFBinaryOutputStream::PutStr (string const&)
1353// Ecriture de données portables.. Pour chaque type
1354// de données, on peut écrire une valeur, ou un tableau de valeurs.
1355// void PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1356// Ecriture d'une ligne de texte dans le fichier PPersist.
1357//--
1358
1359
1360
1361
1362void
1363PPFBinaryOutputStream::PutRawBytes(void const* ptr, size_t bytes)
1364{
1365 s->write((char const*)ptr, bytes);
1366}
1367
1368void
1369PPFBinaryOutputStream::PutRawByte(char c)
1370{
1371 PutRawBytes(&c, 1);
1372}
1373
1374void
1375PPFBinaryOutputStream::PutRawUByte(unsigned char c)
1376{
1377 PutRawBytes(&c, 1);
1378}
1379
1380void
1381PPFBinaryOutputStream::PutRawI2 (int_2 val)
1382{
1383 if (bigEndian != IS_BIG_ENDIAN)
1384 bswap2(&val);
1385
1386 PutRawBytes(&val, sizeof(int_2));
1387}
1388
1389void
1390PPFBinaryOutputStream::PutRawI4 (int_4 val)
1391{
1392 if (bigEndian != IS_BIG_ENDIAN)
1393 bswap4(&val);
1394
1395 PutRawBytes(&val, sizeof(int_4));
1396}
1397
1398void
1399PPFBinaryOutputStream::PutRawI8 (int_8 val)
1400{
1401 if (bigEndian != IS_BIG_ENDIAN)
1402 bswap8(&val);
1403
1404 PutRawBytes(&val, sizeof(int_8));
1405}
1406
1407void
1408PPFBinaryOutputStream::PutRawU8 (uint_8 val)
1409{
1410 if (bigEndian != IS_BIG_ENDIAN)
1411 bswap8(&val);
1412
1413 PutRawBytes(&val, sizeof(uint_8));
1414}
1415
1416void
1417PPFBinaryOutputStream::PutArrayTag(short datasz, size_t sz, short datatype)
1418// datatype = PPS_DATATYPE_CHAR or PPS_DATATYPE_FLOAT or PPS_DATATYPE_INTEGER or PPS_DATATYPE_UNSIGNED
1419{
1420 if (sz <= 0x7fffffff) {
1421 PutRawUByte(PPS_SIMPLE_ARRAY4 + datasz + datatype);
1422 PutRawI4(sz);
1423 } else {
1424 PutRawUByte(PPS_SIMPLE_ARRAY8 + datasz + datatype);
1425 PutRawU8(sz);
1426 }
1427}
1428
1429void
1430PPFBinaryOutputStream::PutByte(char c)
1431{
1432 PutRawByte(PPS_SIMPLE + 1 + PPS_DATATYPE_CHAR);
1433 PutRawBytes(&c, 1);
1434}
1435
1436
1437
1438void
1439PPFBinaryOutputStream::PutBytes(void const* ptr, size_t bytes)
1440{
1441 PutArrayTag(1, bytes, PPS_DATATYPE_CHAR);
1442 PutRawBytes(ptr, bytes);
1443}
1444
1445void
1446PPFBinaryOutputStream::PutR4 (r_4 val)
1447{
1448 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_FLOAT);
1449
1450 if (bigEndian != IS_BIG_ENDIAN)
1451 bswap4(&val);
1452
1453 PutRawBytes(&val, sizeof(r_4));
1454}
1455
1456void
1457PPFBinaryOutputStream::PutR4s (r_4 const* tab, size_t n)
1458{
1459 PutArrayTag(4, n, PPS_DATATYPE_FLOAT);
1460
1461 if (bigEndian == IS_BIG_ENDIAN) {
1462 PutRawBytes(tab, n*sizeof(r_4));
1463 } else {
1464 for (unsigned int i=0; i<n; i++) {
1465 r_4 val = tab[i];
1466 bswap4(&val);
1467 PutRawBytes(&val, sizeof(r_4));
1468 }
1469 }
1470}
1471
1472void
1473PPFBinaryOutputStream::PutR8 (r_8 val)
1474{
1475 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_FLOAT);
1476
1477 if (bigEndian != IS_BIG_ENDIAN)
1478 bswap8(&val);
1479
1480 PutRawBytes(&val, sizeof(r_8));
1481}
1482
1483void
1484PPFBinaryOutputStream::PutR8s (r_8 const* tab, size_t n)
1485{
1486 PutArrayTag(8, n, PPS_DATATYPE_FLOAT);
1487
1488 if (bigEndian == IS_BIG_ENDIAN) {
1489 PutRawBytes(tab, n*sizeof(r_8));
1490 } else {
1491 for (unsigned int i=0; i<n; i++) {
1492 r_8 val = tab[i];
1493 bswap8(&val);
1494 PutRawBytes(&val, sizeof(r_8));
1495 }
1496 }
1497}
1498
1499void
1500PPFBinaryOutputStream::PutI1 (int_1 val)
1501{
1502 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_INTEGER);
1503 PutRawBytes(&val, sizeof(int_1));
1504}
1505
1506void
1507PPFBinaryOutputStream::PutI1s (int_1 const* tab, size_t n)
1508{
1509 PutArrayTag(1, n, PPS_DATATYPE_INTEGER);
1510 PutRawBytes(tab, n*sizeof(int_1));
1511}
1512
1513void
1514PPFBinaryOutputStream::PutU1 (uint_1 val)
1515{
1516 PutRawUByte(PPS_SIMPLE + 1 + PPS_DATATYPE_UNSIGNED);
1517 PutRawBytes(&val, sizeof(uint_1));
1518}
1519
1520void
1521PPFBinaryOutputStream::PutU1s (uint_1 const* tab, size_t n)
1522{
1523 PutArrayTag(1, n, PPS_DATATYPE_UNSIGNED);
1524 PutRawBytes(tab, n*sizeof(uint_1));
1525}
1526
1527void
1528PPFBinaryOutputStream::PutI2 (int_2 val)
1529{
1530 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_INTEGER);
1531
1532 if (bigEndian != IS_BIG_ENDIAN)
1533 bswap2(&val);
1534
1535 PutRawBytes(&val, sizeof(int_2));
1536}
1537
1538void
1539PPFBinaryOutputStream::PutI2s (int_2 const* tab, size_t n)
1540{
1541 PutArrayTag(2, n, PPS_DATATYPE_INTEGER);
1542
1543 if (bigEndian == IS_BIG_ENDIAN) {
1544 PutRawBytes(tab, n*sizeof(int_2));
1545 } else {
1546 for (unsigned int i=0; i<n; i++) {
1547 int_2 val = tab[i];
1548 bswap2(&val);
1549 PutRawBytes(&val, sizeof(int_2));
1550 }
1551 }
1552}
1553
1554void
1555PPFBinaryOutputStream::PutU2 (uint_2 val)
1556{
1557 PutRawUByte(PPS_SIMPLE + 2 + PPS_DATATYPE_UNSIGNED);
1558
1559 if (bigEndian != IS_BIG_ENDIAN)
1560 bswap2(&val);
1561
1562 PutRawBytes(&val, sizeof(uint_2));
1563}
1564
1565void
1566PPFBinaryOutputStream::PutU2s (uint_2 const* tab, size_t n)
1567{
1568 PutArrayTag(2, n, PPS_DATATYPE_UNSIGNED);
1569
1570 if (bigEndian == IS_BIG_ENDIAN) {
1571 PutRawBytes(tab, n*sizeof(uint_2));
1572 } else {
1573 for (unsigned int i=0; i<n; i++) {
1574 uint_2 val = tab[i];
1575 bswap2(&val);
1576 PutRawBytes(&val, sizeof(uint_2));
1577 }
1578 }
1579}
1580
1581void
1582PPFBinaryOutputStream::PutI4 (int_4 val)
1583{
1584 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_INTEGER);
1585
1586 if (bigEndian != IS_BIG_ENDIAN)
1587 bswap4(&val);
1588
1589 PutRawBytes(&val, sizeof(int_4));
1590}
1591
1592void
1593PPFBinaryOutputStream::PutI4s (int_4 const* tab, size_t n)
1594{
1595 PutArrayTag(4, n, PPS_DATATYPE_INTEGER);
1596
1597 if (bigEndian == IS_BIG_ENDIAN) {
1598 PutRawBytes(tab, n*sizeof(int_4));
1599 } else {
1600 for (unsigned int i=0; i<n; i++) {
1601 int_4 val = tab[i];
1602 bswap4(&val);
1603 PutRawBytes(&val, sizeof(int_4));
1604 }
1605 }
1606}
1607
1608void
1609PPFBinaryOutputStream::PutU4 (uint_4 val)
1610{
1611 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_UNSIGNED);
1612
1613 if (bigEndian != IS_BIG_ENDIAN)
1614 bswap4(&val);
1615
1616 PutRawBytes(&val, sizeof(uint_4));
1617}
1618
1619void
1620PPFBinaryOutputStream::PutU4s (uint_4 const* tab, size_t n)
1621{
1622 PutArrayTag(4, n, PPS_DATATYPE_UNSIGNED);
1623
1624 if (bigEndian == IS_BIG_ENDIAN) {
1625 PutRawBytes(tab, n*sizeof(uint_4));
1626 } else {
1627 for (unsigned int i=0; i<n; i++) {
1628 uint_4 val = tab[i];
1629 bswap4(&val);
1630 PutRawBytes(&val, sizeof(uint_4));
1631 }
1632 }
1633}
1634
1635void
1636PPFBinaryOutputStream::PutI8 (int_8 val)
1637{
1638 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_INTEGER);
1639
1640 if (bigEndian != IS_BIG_ENDIAN)
1641 bswap8(&val);
1642
1643 PutRawBytes(&val, sizeof(int_8));
1644}
1645
1646void
1647PPFBinaryOutputStream::PutI8s (int_8 const* tab, size_t n)
1648{
1649 PutArrayTag(8, n, PPS_DATATYPE_INTEGER);
1650
1651 if (bigEndian == IS_BIG_ENDIAN) {
1652 PutRawBytes(tab, n*sizeof(int_8));
1653 } else {
1654 for (unsigned int i=0; i<n; i++) {
1655 int_8 val = tab[i];
1656 bswap8(&val);
1657 PutRawBytes(&val, sizeof(int_8));
1658 }
1659 }
1660}
1661
1662void
1663PPFBinaryOutputStream::PutU8 (uint_8 val)
1664{
1665 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_UNSIGNED);
1666
1667 if (bigEndian != IS_BIG_ENDIAN)
1668 bswap8(&val);
1669
1670 PutRawBytes(&val, sizeof(uint_8));
1671}
1672
1673void
1674PPFBinaryOutputStream::PutU8s (uint_8 const* tab, size_t n)
1675{
1676 PutArrayTag(8, n, PPS_DATATYPE_UNSIGNED);
1677
1678 if (bigEndian == IS_BIG_ENDIAN) {
1679 PutRawBytes(tab, n*sizeof(uint_8));
1680 } else {
1681 for (unsigned int i=0; i<n; i++) {
1682 uint_8 val = tab[i];
1683 bswap8(&val);
1684 PutRawBytes(&val, sizeof(uint_8));
1685 }
1686 }
1687}
1688
1689void
1690PPFBinaryOutputStream::PutStr(string const& str)
1691{
1692 PutRawUByte(PPS_STRING);
1693 PutRawI4(str.length());
1694 PutRawBytes(str.c_str(), str.length());
1695}
1696
1697void
1698PPFBinaryOutputStream::PutLine(char const* ptr, size_t len)
1699{
1700 string str = ptr;
1701 PutStr(str);
1702}
1703
1704
1705void
1706PPFBinaryOutputStream::PutZ4 (complex<r_4> val)
1707{
1708 PutRawUByte(PPS_SIMPLE + 4 + PPS_DATATYPE_COMPLEX);
1709 r_4 reim[2];
1710 reim[0] = val.real();
1711 reim[1] = val.imag();
1712 if (bigEndian != IS_BIG_ENDIAN) {
1713 bswap4(reim);
1714 bswap4(reim+1);
1715 }
1716 PutRawBytes(reim, 2*sizeof(r_4));
1717}
1718
1719void
1720PPFBinaryOutputStream::PutZ4s (complex<r_4> const* tab, size_t n)
1721{
1722 PutArrayTag(4, n, PPS_DATATYPE_COMPLEX);
1723
1724 if (bigEndian == IS_BIG_ENDIAN) {
1725 PutRawBytes(tab, n*2*sizeof(r_4));
1726 } else {
1727 for (unsigned int i=0; i<n; i++) {
1728 r_4 reim[2];
1729 reim[0] = tab[i].real();
1730 reim[1] = tab[i].imag();
1731 bswap4(reim);
1732 bswap4(reim+1);
1733 PutRawBytes(reim, 2*sizeof(r_4));
1734 }
1735 }
1736}
1737
1738void
1739PPFBinaryOutputStream::PutZ8 (complex<r_8> val)
1740{
1741 PutRawUByte(PPS_SIMPLE + 8 + PPS_DATATYPE_COMPLEX);
1742 r_8 reim[2];
1743 reim[0] = val.real();
1744 reim[1] = val.imag();
1745 if (bigEndian != IS_BIG_ENDIAN) {
1746 bswap8(reim);
1747 bswap8(reim+1);
1748 }
1749 PutRawBytes(reim, 2*sizeof(r_8));
1750}
1751
1752void
1753PPFBinaryOutputStream::PutZ8s (complex<r_8> const* tab, size_t n)
1754{
1755 PutArrayTag(8, n, PPS_DATATYPE_COMPLEX);
1756
1757 if (bigEndian == IS_BIG_ENDIAN) {
1758 PutRawBytes(tab, n*2*sizeof(r_8));
1759 } else {
1760 for (unsigned int i=0; i<n; i++) {
1761 r_8 reim[2];
1762 reim[0] = tab[i].real();
1763 reim[1] = tab[i].imag();
1764 bswap8(reim);
1765 bswap8(reim+1);
1766 PutRawBytes(reim, 2*sizeof(r_8));
1767 }
1768 }
1769}
1770
1771void
1772PPFBinaryOutputStream::PutPosTagTable(int_8 const * ptab, size_t sz)
1773{
1774 PutRawUByte(PPS_POSTAG_TABLE);
1775 int_4 tsz = sz;
1776 PutRawI4(tsz);
1777 for(int kk=0; kk<tsz; kk++)
1778 PutRawI8(ptab[kk]);
1779 return;
1780}
1781
1782void
1783PPFBinaryOutputStream::PutPosTagTable(vector<int_8> const& ptab)
1784{
1785 PutRawUByte(PPS_POSTAG_TABLE);
1786 int_4 tsz = ptab.size();
1787 PutRawI4(tsz);
1788 for(int kk=0; kk<tsz; kk++)
1789 PutRawI8(ptab[kk]);
1790 return;
1791}
1792
Note: See TracBrowser for help on using the repository browser.