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

Last change on this file since 2774 was 2664, checked in by ansari, 20 years ago

Correction indentation impression ds AnalyseTag (ppfbinstream.cc) - Reza 7 Avril 2005

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