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

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

Debug/correction des ppersist suite a la separation des PInOutPersist en PPFInOutStream - Reza 5 Dec 2003

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