[2475] | 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 |
|
---|
| 24 | namespace SOPHYA {
|
---|
| 25 |
|
---|
| 26 | class PPFBinaryIOStream {
|
---|
| 27 | public:
|
---|
| 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 |
|
---|
[2476] | 57 | PPFBinaryIOStream() { }
|
---|
| 58 | virtual ~PPFBinaryIOStream() { }
|
---|
[2475] | 59 | int Version() {return version;} // PIn/OutPersist version number
|
---|
| 60 |
|
---|
| 61 | protected:
|
---|
| 62 |
|
---|
| 63 | map<string, int_8> tags;
|
---|
| 64 | int version; // PPersist(In/Out) version
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | //! Input PPF (Portable Persist Format) streams
|
---|
| 69 | class PPFBinaryInputStream : public PPFBinaryIOStream {
|
---|
| 70 | public:
|
---|
[2476] | 71 | PPFBinaryInputStream(RawInOutStream * is, bool ad, bool scan=false);
|
---|
[2475] | 72 | PPFBinaryInputStream(string const& flnm, bool scan=true);
|
---|
| 73 | virtual ~PPFBinaryInputStream();
|
---|
[2476] | 74 |
|
---|
| 75 | inline string FileName() { return s->getFileName(); } // Retourne le nom de fichier
|
---|
[2475] | 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();
|
---|
[2476] | 87 | // Saut d'un item de base (tag+donnees correspondantes), le suivant ds le flot
|
---|
| 88 | bool SkipNextItem();
|
---|
| 89 | // Lecture du tag de type next item + infos correspondantes
|
---|
| 90 | // Le stream est re-positionne avant le tag
|
---|
| 91 | char NextItemTag(short datasz, size_t sz);
|
---|
[2475] | 92 |
|
---|
| 93 | // Lecture donnees de base et tableaux de donnees de base
|
---|
| 94 | // Basic data type (and corresponding arrays) reading
|
---|
| 95 | void GetByte (char& c);
|
---|
| 96 | void GetBytes(void* ptr, size_t bytes);
|
---|
| 97 | void GetR4 (r_4&);
|
---|
| 98 | void GetR4s (r_4*, size_t);
|
---|
| 99 | void GetR8 (r_8&);
|
---|
| 100 | void GetR8s (r_8*, size_t);
|
---|
| 101 | void GetI1 (int_1&);
|
---|
| 102 | void GetI1s (int_1*, size_t);
|
---|
| 103 | void GetU1 (uint_1&);
|
---|
| 104 | void GetU1s (uint_1*, size_t);
|
---|
| 105 | void GetI2 (int_2&);
|
---|
| 106 | void GetI2s (int_2*, size_t);
|
---|
| 107 | void GetU2 (uint_2&);
|
---|
| 108 | void GetU2s (uint_2*, size_t);
|
---|
| 109 | void GetI4 (int_4&);
|
---|
| 110 | void GetI4s (int_4*, size_t);
|
---|
| 111 | void GetU4 (uint_4&);
|
---|
| 112 | void GetU4s (uint_4*, size_t);
|
---|
| 113 | void GetI8 (int_8&);
|
---|
| 114 | void GetI8s (int_8*, size_t);
|
---|
| 115 | void GetU8 (uint_8&);
|
---|
| 116 | void GetU8s (uint_8*, size_t);
|
---|
| 117 | void GetLine (char* ptr, size_t len);
|
---|
| 118 | void GetStr (string&);
|
---|
| 119 | void GetZ4 (complex<r_4> &);
|
---|
| 120 | void GetZ4s (complex<r_4> *, size_t);
|
---|
| 121 | void GetZ8 (complex<r_8> &);
|
---|
| 122 | void GetZ8s (complex<r_8> *, size_t);
|
---|
| 123 |
|
---|
| 124 | inline void Get(char& c) {GetByte(c);}
|
---|
| 125 | inline void Get(r_4& x) {GetR4(x);}
|
---|
| 126 | inline void Get(r_8& x) {GetR8(x);}
|
---|
| 127 | inline void Get(uint_1& x) {GetU1(x);}
|
---|
| 128 | inline void Get(int_1& x) {GetI1(x);}
|
---|
| 129 | inline void Get(uint_2& x) {GetU2(x);}
|
---|
| 130 | inline void Get(int_2& x) {GetI2(x);}
|
---|
| 131 | inline void Get(uint_4& x) {GetU4(x);}
|
---|
| 132 | inline void Get(int_4& x) {GetI4(x);}
|
---|
| 133 | inline void Get(uint_8& x) {GetU8(x);}
|
---|
| 134 | inline void Get(int_8& x) {GetI8(x);}
|
---|
| 135 | inline void Get(complex<r_4> & x) {GetZ4(x);}
|
---|
| 136 | inline void Get(complex<r_8> & x) {GetZ8(x);}
|
---|
| 137 |
|
---|
| 138 | inline void Get(r_4* x, size_t n) {GetR4s(x,n);}
|
---|
| 139 | inline void Get(r_8* x, size_t n) {GetR8s(x,n);}
|
---|
| 140 | inline void Get(uint_1* x, size_t n) {GetU1s(x,n);}
|
---|
| 141 | inline void Get(int_1* x, size_t n) {GetI1s(x,n);}
|
---|
| 142 | inline void Get(uint_2* x, size_t n) {GetU2s(x,n);}
|
---|
| 143 | inline void Get(int_2* x, size_t n) {GetI2s(x,n);}
|
---|
| 144 | inline void Get(uint_4* x, size_t n) {GetU4s(x,n);}
|
---|
| 145 | inline void Get(int_4* x, size_t n) {GetI4s(x,n);}
|
---|
| 146 | inline void Get(uint_8* x, size_t n) {GetU8s(x,n);}
|
---|
| 147 | inline void Get(int_8* x, size_t n) {GetI8s(x,n);}
|
---|
| 148 | inline void Get(string& x) {GetStr(x);}
|
---|
| 149 | inline void Get(complex<r_4> * x, size_t n) { GetZ4s(x, n); }
|
---|
| 150 | inline void Get(complex<r_8> * x, size_t n) { GetZ8s(x, n); }
|
---|
| 151 |
|
---|
| 152 | // Reading a list (table) of position tags
|
---|
| 153 | void GetPosTagTable(int_8*, size_t);
|
---|
| 154 | void GetPosTagTable(vector<int_8>&);
|
---|
| 155 |
|
---|
| 156 | time_t CreationDate() { return creationdate; }
|
---|
| 157 | string CreationDateStr();
|
---|
| 158 |
|
---|
| 159 | void AnalyseTags(int lev=0); // List (all or some) tags ...
|
---|
| 160 |
|
---|
| 161 | protected:
|
---|
[2476] | 162 | void Init(bool scan);
|
---|
| 163 | void ReadNameTagTableV2();
|
---|
| 164 | void ReadNameTagTable();
|
---|
| 165 |
|
---|
| 166 | void SkipItem(bool fgrdi, char itag);
|
---|
| 167 |
|
---|
[2475] | 168 | void CheckTag (short datasz, short datatype);
|
---|
| 169 | void CheckArrayTag(short datasz, size_t sz, short datatype);
|
---|
| 170 | void GetTypeTag (unsigned char& c);
|
---|
| 171 | void GetRawByte (char& c);
|
---|
| 172 | void GetRawUByte (unsigned char& c);
|
---|
| 173 | void GetRawBytes(void* ptr, size_t bytes);
|
---|
| 174 | void GetRawI2 (int_2&);
|
---|
| 175 | void GetRawI4 (int_4&);
|
---|
| 176 | void GetRawI8 (int_8&);
|
---|
| 177 | void GetRawU8 (uint_8&);
|
---|
| 178 |
|
---|
| 179 | RawInOutStream* s;
|
---|
[2476] | 180 | bool _ads; // delete/close the stream at the end
|
---|
[2475] | 181 |
|
---|
| 182 | bool bigEndian;
|
---|
| 183 | time_t creationdate;
|
---|
| 184 | // Si on a fait une lecture non sequentielle -> seqread = false
|
---|
| 185 | bool seqread;
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | //! Output stream for PPersit objects.
|
---|
| 189 | class PPFBinaryOutputStream : public PPFBinaryIOStream {
|
---|
| 190 | public:
|
---|
[2476] | 191 | PPFBinaryOutputStream(RawInOutStream* os, bool ad, int endianness = PPS_NATIVE);
|
---|
[2475] | 192 | PPFBinaryOutputStream(string const& flnm, int endianness = PPS_NATIVE);
|
---|
| 193 | virtual ~PPFBinaryOutputStream();
|
---|
[2476] | 194 |
|
---|
| 195 | inline string FileName() { return s->getFileName(); } // Retourne le nom de fichier
|
---|
[2475] | 196 |
|
---|
| 197 | // Ecriture de tags
|
---|
| 198 | int_8 WritePositionTag();
|
---|
| 199 | void WriteNameTag(string const& name);
|
---|
| 200 | inline void WriteTag(string const& name) { WriteNameTag(name); }
|
---|
| 201 |
|
---|
| 202 | void PutByte (char c);
|
---|
| 203 | void PutBytes(void const* ptr, size_t bytes);
|
---|
| 204 | void PutR4 (r_4);
|
---|
| 205 | void PutR4s (r_4 const*, size_t);
|
---|
| 206 | void PutR8 (r_8);
|
---|
| 207 | void PutR8s (r_8 const*, size_t);
|
---|
| 208 | void PutI1 (int_1);
|
---|
| 209 | void PutI1s (int_1 const*, size_t);
|
---|
| 210 | void PutU1 (uint_1);
|
---|
| 211 | void PutU1s (uint_1 const*, size_t);
|
---|
| 212 | void PutI2 (int_2);
|
---|
| 213 | void PutI2s (int_2 const*, size_t);
|
---|
| 214 | void PutU2 (uint_2);
|
---|
| 215 | void PutU2s (uint_2 const*, size_t);
|
---|
| 216 | void PutI4 (int_4);
|
---|
| 217 | void PutI4s (int_4 const*, size_t);
|
---|
| 218 | void PutU4 (uint_4);
|
---|
| 219 | void PutU4s (uint_4 const*, size_t);
|
---|
| 220 | void PutI8 (int_8);
|
---|
| 221 | void PutI8s (int_8 const*, size_t);
|
---|
| 222 | void PutU8 (uint_8);
|
---|
| 223 | void PutU8s (uint_8 const*, size_t);
|
---|
| 224 | void PutLine (char const* ptr, size_t len=0); // deprecated ?
|
---|
| 225 | void PutStr (string const&);
|
---|
| 226 | void PutZ4 (complex<r_4>);
|
---|
| 227 | void PutZ4s (complex<r_4> const*, size_t);
|
---|
| 228 | void PutZ8 (complex<r_8>);
|
---|
| 229 | void PutZ8s (complex<r_8> const*, size_t);
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | void Put(char c) {PutByte(c);}
|
---|
| 233 | void Put(r_4 x) {PutR4(x);}
|
---|
| 234 | void Put(r_8 x) {PutR8(x);}
|
---|
| 235 | void Put(complex<r_4> x) {PutZ4(x);}
|
---|
| 236 | void Put(complex<r_8> x) {PutZ8(x);}
|
---|
| 237 | void Put(uint_1 x) {PutU1(x);}
|
---|
| 238 | void Put(int_1 x) {PutI1(x);}
|
---|
| 239 | void Put(uint_2 x) {PutU2(x);}
|
---|
| 240 | void Put(int_2 x) {PutI2(x);}
|
---|
| 241 | void Put(uint_4 x) {PutU4(x);}
|
---|
| 242 | void Put(int_4 x) {PutI4(x);}
|
---|
| 243 | void Put(uint_8 x) {PutU8(x);}
|
---|
| 244 | void Put(int_8 x) {PutI8(x);}
|
---|
| 245 | void Put(r_4 const* x, size_t n) {PutR4s(x,n);}
|
---|
| 246 | void Put(r_8 const* x, size_t n) {PutR8s(x,n);}
|
---|
| 247 | void Put(complex<r_4> const* x, size_t n) {PutZ4s(x,n);}
|
---|
| 248 | void Put(complex<r_8> const* x, size_t n) {PutZ8s(x,n);}
|
---|
| 249 | void Put(uint_1 const* x, size_t n) {PutU1s(x,n);}
|
---|
| 250 | void Put(int_1 const* x, size_t n) {PutI1s(x,n);}
|
---|
| 251 | void Put(uint_2 const* x, size_t n) {PutU2s(x,n);}
|
---|
| 252 | void Put(int_2 const* x, size_t n) {PutI2s(x,n);}
|
---|
| 253 | void Put(uint_4 const* x, size_t n) {PutU4s(x,n);}
|
---|
| 254 | void Put(int_4 const* x, size_t n) {PutI4s(x,n);}
|
---|
| 255 | void Put(uint_8 const* x, size_t n) {PutU8s(x,n);}
|
---|
| 256 | void Put(int_8 const* x, size_t n) {PutI8s(x,n);}
|
---|
| 257 | void Put(string const& s) {PutStr(s);}
|
---|
| 258 |
|
---|
| 259 | // Writing a list of position tag table
|
---|
| 260 | void PutPosTagTable(int_8 const *, size_t);
|
---|
| 261 | void PutPosTagTable(vector<int_8> const&);
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | protected:
|
---|
[2476] | 265 | void Init(int endianness);
|
---|
| 266 | void WriteNameTagTable();
|
---|
| 267 | void WriteNameTagTableV2();
|
---|
| 268 |
|
---|
[2475] | 269 | void PutArrayTag(short datasz, size_t sz, short datatype);
|
---|
| 270 | void PutRawByte (char);
|
---|
| 271 | void PutRawUByte (unsigned char);
|
---|
| 272 | void PutRawI2 (int_2);
|
---|
| 273 | void PutRawI4 (int_4);
|
---|
| 274 | void PutRawI8 (int_8);
|
---|
| 275 | void PutRawU8 (uint_8);
|
---|
| 276 | void PutRawBytes(void const* ptr, size_t bytes);
|
---|
[2476] | 277 |
|
---|
| 278 | // Attributs, variables
|
---|
| 279 | RawInOutStream* s;
|
---|
| 280 | bool _ads; // delete/close the stream at the end
|
---|
| 281 |
|
---|
| 282 | bool bigEndian;
|
---|
| 283 |
|
---|
[2475] | 284 | };
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | } // namespace
|
---|
| 289 |
|
---|
| 290 | #endif
|
---|