source: Sophya/trunk/SophyaLib/BaseTools/ppfbinstream.h@ 2475

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

Separation ppersist.h .cc en deux : ajout de ppfbinstream.h .cc - Reza 4 Dec 2003

File size: 9.1 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2
3#ifndef PPFBINSTREAM_H_SEEN
4#define PPFBINSTREAM_H_SEEN
5
6// PPF (Portable Persistence Format) Input/Output streams
7//
8// E. Aubourg CEA DAPNIA/SPP 1999
9// R. Ansari LAL IN2P3/CNRS 2000-2003
10
11
12#include "machdefs.h"
13#include "rawstream.h"
14
15#include <time.h>
16
17#include <complex>
18#include <string>
19#include <map>
20#include <vector>
21
22
23
24namespace SOPHYA {
25
26class PPFBinaryIOStream {
27public:
28
29 enum PPFByteOrdering {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1};
30
31 // Value of item identification tags in PPF binary streams
32 enum PPFItemTag {
33 PPS_NULL = 0, // this is a null object
34 PPS_STRING = 1, // string, length (4b) + data
35 PPS_OBJECT = 2, // classId, data...
36 PPS_REFERENCE = 3, // objectId
37 PPS_NAMETAG_TABLE = 4, // Name tag table (Written at the end of file/stream)
38 PPS_EOF = 5, // Just before tag infomation, offset to PPS_TAG
39 PPS_ENDOBJECT = 6, // marks the end of a given object information
40 PPS_NAMETAG_MARK = 7, // To have a name tag, position marker in a file
41 PPS_POSTAG_MARK = 8, // Position tag mark + 8 bytes (=stream position)
42 PPS_POSTAG_TABLE = 9, // Position tag table + 8 bytes (=stream position)
43 PPS_SIMPLE = 16, // 16 + number of bytes, up to 8 bytes
44 PPS_SIMPLE_ARRAY4 = 32, // 32 + number of bytes, up to 8 bytes, then 4 bytes of length
45 PPS_SIMPLE_ARRAY8 = 48 // 48 + number of bytes, up to 8 bytes, then 8 bytes of length
46 };
47
48 // The following values are used with PPS_SIMPLE and PPS_SIMPLE_ARRAY (Using OR)
49 enum PPFItemTagDataType {
50 PPS_DATATYPE_CHAR = 0, // 0 : DataType=character
51 PPS_DATATYPE_FLOAT = 64, // 64 : DataType=float
52 PPS_DATATYPE_COMPLEX = 65, // 65 : DataType=complex
53 PPS_DATATYPE_INTEGER = 128, // 128 :DataType=integer
54 PPS_DATATYPE_UNSIGNED = 192 // 192 :DataType=Unsigned integer
55 };
56
57 PPFBinaryIOStream();
58 virtual ~PPFBinaryIOStream();
59 int Version() {return version;} // PIn/OutPersist version number
60 string FileName() { return filename; } // Retourne le nom de fichier
61
62protected:
63
64 map<string, int_8> tags;
65 string filename;
66 int version; // PPersist(In/Out) version
67};
68
69
70//! Input PPF (Portable Persist Format) streams
71class PPFBinaryInputStream : public PPFBinaryIOStream {
72public:
73 PPFBinaryInputStream();
74 PPFBinaryInputStream(string const& flnm, bool scan=true);
75 virtual ~PPFBinaryInputStream();
76
77 // Gestion des tags
78 bool GotoPositionTag(int_8 pos);
79 bool GotoNameTag(string const& name);
80 int NbNameTags();
81 bool GotoNameTagNum(int itag); // 0..NbTags-1
82 string GetTagName(int itag); // 0..NbTags-1
83 vector<string> const & GetNameTags();
84
85 // Saut jusqu'au prochain objet
86 bool SkipToNextObject();
87 // Saut d'un item de base (tag+donnees correspondantes)
88 bool SkipItem();
89
90 // Lecture donnees de base et tableaux de donnees de base
91 // Basic data type (and corresponding arrays) reading
92 void GetByte (char& c);
93 void GetBytes(void* ptr, size_t bytes);
94 void GetR4 (r_4&);
95 void GetR4s (r_4*, size_t);
96 void GetR8 (r_8&);
97 void GetR8s (r_8*, size_t);
98 void GetI1 (int_1&);
99 void GetI1s (int_1*, size_t);
100 void GetU1 (uint_1&);
101 void GetU1s (uint_1*, size_t);
102 void GetI2 (int_2&);
103 void GetI2s (int_2*, size_t);
104 void GetU2 (uint_2&);
105 void GetU2s (uint_2*, size_t);
106 void GetI4 (int_4&);
107 void GetI4s (int_4*, size_t);
108 void GetU4 (uint_4&);
109 void GetU4s (uint_4*, size_t);
110 void GetI8 (int_8&);
111 void GetI8s (int_8*, size_t);
112 void GetU8 (uint_8&);
113 void GetU8s (uint_8*, size_t);
114 void GetLine (char* ptr, size_t len);
115 void GetStr (string&);
116 void GetZ4 (complex<r_4> &);
117 void GetZ4s (complex<r_4> *, size_t);
118 void GetZ8 (complex<r_8> &);
119 void GetZ8s (complex<r_8> *, size_t);
120
121 inline void Get(char& c) {GetByte(c);}
122 inline void Get(r_4& x) {GetR4(x);}
123 inline void Get(r_8& x) {GetR8(x);}
124 inline void Get(uint_1& x) {GetU1(x);}
125 inline void Get(int_1& x) {GetI1(x);}
126 inline void Get(uint_2& x) {GetU2(x);}
127 inline void Get(int_2& x) {GetI2(x);}
128 inline void Get(uint_4& x) {GetU4(x);}
129 inline void Get(int_4& x) {GetI4(x);}
130 inline void Get(uint_8& x) {GetU8(x);}
131 inline void Get(int_8& x) {GetI8(x);}
132 inline void Get(complex<r_4> & x) {GetZ4(x);}
133 inline void Get(complex<r_8> & x) {GetZ8(x);}
134
135 inline void Get(r_4* x, size_t n) {GetR4s(x,n);}
136 inline void Get(r_8* x, size_t n) {GetR8s(x,n);}
137 inline void Get(uint_1* x, size_t n) {GetU1s(x,n);}
138 inline void Get(int_1* x, size_t n) {GetI1s(x,n);}
139 inline void Get(uint_2* x, size_t n) {GetU2s(x,n);}
140 inline void Get(int_2* x, size_t n) {GetI2s(x,n);}
141 inline void Get(uint_4* x, size_t n) {GetU4s(x,n);}
142 inline void Get(int_4* x, size_t n) {GetI4s(x,n);}
143 inline void Get(uint_8* x, size_t n) {GetU8s(x,n);}
144 inline void Get(int_8* x, size_t n) {GetI8s(x,n);}
145 inline void Get(string& x) {GetStr(x);}
146 inline void Get(complex<r_4> * x, size_t n) { GetZ4s(x, n); }
147 inline void Get(complex<r_8> * x, size_t n) { GetZ8s(x, n); }
148
149 // Reading a list (table) of position tags
150 void GetPosTagTable(int_8*, size_t);
151 void GetPosTagTable(vector<int_8>&);
152
153 time_t CreationDate() { return creationdate; }
154 string CreationDateStr();
155
156 void AnalyseTags(int lev=0); // List (all or some) tags ...
157
158protected:
159 void CheckTag (short datasz, short datatype);
160 void CheckArrayTag(short datasz, size_t sz, short datatype);
161 void GetTypeTag (unsigned char& c);
162 void GetRawByte (char& c);
163 void GetRawUByte (unsigned char& c);
164 void GetRawBytes(void* ptr, size_t bytes);
165 void GetRawI2 (int_2&);
166 void GetRawI4 (int_4&);
167 void GetRawI8 (int_8&);
168 void GetRawU8 (uint_8&);
169
170 void Scan();
171
172 RawInOutStream* s;
173
174 bool bigEndian;
175 time_t creationdate;
176 // Si on a fait une lecture non sequentielle -> seqread = false
177 bool seqread;
178};
179
180//! Output stream for PPersit objects.
181class PPFBinaryOutputStream : public PPFBinaryIOStream {
182public:
183 PPFBinaryOutputStream();
184 PPFBinaryOutputStream(string const& flnm, int endianness = PPS_NATIVE);
185 virtual ~PPFBinaryOutputStream();
186
187 // Ecriture de tags
188 int_8 WritePositionTag();
189 void WriteNameTag(string const& name);
190 inline void WriteTag(string const& name) { WriteNameTag(name); }
191
192 void PutByte (char c);
193 void PutBytes(void const* ptr, size_t bytes);
194 void PutR4 (r_4);
195 void PutR4s (r_4 const*, size_t);
196 void PutR8 (r_8);
197 void PutR8s (r_8 const*, size_t);
198 void PutI1 (int_1);
199 void PutI1s (int_1 const*, size_t);
200 void PutU1 (uint_1);
201 void PutU1s (uint_1 const*, size_t);
202 void PutI2 (int_2);
203 void PutI2s (int_2 const*, size_t);
204 void PutU2 (uint_2);
205 void PutU2s (uint_2 const*, size_t);
206 void PutI4 (int_4);
207 void PutI4s (int_4 const*, size_t);
208 void PutU4 (uint_4);
209 void PutU4s (uint_4 const*, size_t);
210 void PutI8 (int_8);
211 void PutI8s (int_8 const*, size_t);
212 void PutU8 (uint_8);
213 void PutU8s (uint_8 const*, size_t);
214 void PutLine (char const* ptr, size_t len=0); // deprecated ?
215 void PutStr (string const&);
216 void PutZ4 (complex<r_4>);
217 void PutZ4s (complex<r_4> const*, size_t);
218 void PutZ8 (complex<r_8>);
219 void PutZ8s (complex<r_8> const*, size_t);
220
221
222 void Put(char c) {PutByte(c);}
223 void Put(r_4 x) {PutR4(x);}
224 void Put(r_8 x) {PutR8(x);}
225 void Put(complex<r_4> x) {PutZ4(x);}
226 void Put(complex<r_8> x) {PutZ8(x);}
227 void Put(uint_1 x) {PutU1(x);}
228 void Put(int_1 x) {PutI1(x);}
229 void Put(uint_2 x) {PutU2(x);}
230 void Put(int_2 x) {PutI2(x);}
231 void Put(uint_4 x) {PutU4(x);}
232 void Put(int_4 x) {PutI4(x);}
233 void Put(uint_8 x) {PutU8(x);}
234 void Put(int_8 x) {PutI8(x);}
235 void Put(r_4 const* x, size_t n) {PutR4s(x,n);}
236 void Put(r_8 const* x, size_t n) {PutR8s(x,n);}
237 void Put(complex<r_4> const* x, size_t n) {PutZ4s(x,n);}
238 void Put(complex<r_8> const* x, size_t n) {PutZ8s(x,n);}
239 void Put(uint_1 const* x, size_t n) {PutU1s(x,n);}
240 void Put(int_1 const* x, size_t n) {PutI1s(x,n);}
241 void Put(uint_2 const* x, size_t n) {PutU2s(x,n);}
242 void Put(int_2 const* x, size_t n) {PutI2s(x,n);}
243 void Put(uint_4 const* x, size_t n) {PutU4s(x,n);}
244 void Put(int_4 const* x, size_t n) {PutI4s(x,n);}
245 void Put(uint_8 const* x, size_t n) {PutU8s(x,n);}
246 void Put(int_8 const* x, size_t n) {PutI8s(x,n);}
247 void Put(string const& s) {PutStr(s);}
248
249 // Writing a list of position tag table
250 void PutPosTagTable(int_8 const *, size_t);
251 void PutPosTagTable(vector<int_8> const&);
252
253
254protected:
255 RawInOutStream* s;
256 bool bigEndian;
257
258 void PutArrayTag(short datasz, size_t sz, short datatype);
259 void PutRawByte (char);
260 void PutRawUByte (unsigned char);
261 void PutRawI2 (int_2);
262 void PutRawI4 (int_4);
263 void PutRawI8 (int_8);
264 void PutRawU8 (uint_8);
265 void PutRawBytes(void const* ptr, size_t bytes);
266
267};
268
269
270
271} // namespace
272
273#endif
Note: See TracBrowser for help on using the repository browser.