-
Notifications
You must be signed in to change notification settings - Fork 4
/
MatfileReader.hpp
368 lines (340 loc) · 10.5 KB
/
MatfileReader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef _MATFILEREADER_H_
#define _MATFILEREADER_H_
#include <cstdint>
#include <iosfwd>
#include <fstream>
#include <vector>
#include <cstring>
enum EDataType {
miINT8 = 1,
miUINT8 = 2,
miINT16 = 3,
miUINT16 = 4,
miINT32 = 5,
miUINT32 = 6,
miSINGLE = 7,
miDOUBLE = 9,
miINT64 = 12,
miUINT64 = 13,
miMATRIX = 14,
miCOMPRESSED = 15,
miUTF8 = 16,
miUTF16 = 17,
miUTF32 = 18,
miINVALID = 0,
};
enum EMatrixClass {
mxCELL_CLASS = 1,
mxSTRUCT_CLASS = 2,
mxOBJECT_CLASS = 3,
mxCHAR_CLASS = 4,
mxSPARSE_CLASS = 5,
mxDOUBLE_CLASS = 6,
mxSINGLE_CLASS = 7,
mxINT8_CLASS = 8,
mxUINT8_CLASS = 9,
mxINT16_CLASS = 10,
mxUINT16_CLASS = 11,
mxINT32_CLASS = 12,
mxUINT32_CLASS = 13,
mxINT64_CLASS = 14,
mxUINT64_CLASS = 15,
mxINVALID = 0,
};
std::pair<EDataType, EMatrixClass> classify(char* data);
class DataElement;
DataElement* parse(char* data, bool endianSwap = false);
class DataElement
{
public:
DataElement(bool endianSwap = false) :
_dataType(miINVALID), _numberOfBytes(0), _isSmallDataElement(false), _endianSwap(endianSwap) {}
virtual ~DataElement() {}
public:
EDataType dataType() { return _dataType; }
uint32_t numberOfBytes() { return _numberOfBytes; }
bool isSmallDataElement() {return _isSmallDataElement; }
bool endianSwap() { return _endianSwap; }
virtual uint32_t totalBytes() { return _isSmallDataElement? 8 : _numberOfBytes + padding() + 8; }
protected:
virtual uint32_t padding() { return _numberOfBytes % 8? 8 - _numberOfBytes % 8 : 0; }
virtual void parseTag(char* tag);
protected:
EDataType _dataType;
uint32_t _numberOfBytes;
bool _isSmallDataElement;
bool _endianSwap;
};
template <typename T>
class FlatDataElement : public DataElement
{
public:
FlatDataElement(char* data, bool endianSwap = false);
virtual ~FlatDataElement() {}
public:
std::vector<T>& data() { return _data; }
protected:
std::vector<T> _data;
};
class ArrayFlags: public FlatDataElement<uint32_t>
{
public:
ArrayFlags(char* data, bool endianSwap = false) : FlatDataElement<uint32_t>(data, endianSwap) {}
virtual ~ArrayFlags() {}
public:
// NOTE: this differs from that described by offcial document
bool complex() { return _data[0] & 0x001000; }
bool global() { return _data[0] & 0x002000; }
bool logical() { return _data[0] & 0x004000; }
EMatrixClass klass() { return static_cast<EMatrixClass>(_data[0] & 0xFF); }
uint32_t nzmax() { return _data[1]; }
};
class DimensionsArray: public FlatDataElement<int32_t>
{
public:
DimensionsArray(char* data, bool endianSwap = false) : FlatDataElement<int32_t>(data, endianSwap) {}
virtual ~DimensionsArray() {}
public:
int32_t dimensionNumber() { return _data.size(); }
std::vector<int32_t>& dimensions() { return _data; }
};
class Name: public FlatDataElement<int8_t>
{
public:
Name(char* data, bool endianSwap = false) : FlatDataElement<int8_t>(data, endianSwap), _name() {
_name.assign(_data.begin(), _data.end());
}
virtual ~Name() {}
public:
std::string name() { return _name; }
protected:
std::string _name;
};
template <typename T>
class RealImag: public FlatDataElement<T>
{
public:
RealImag(char* data, bool endianSwap = false) : FlatDataElement<T>(data, endianSwap) {}
virtual ~RealImag() {}
public:
uint32_t size() { return this->_data.size(); }
};
class SparseIndex: public FlatDataElement<int32_t>
{
public:
SparseIndex(char* data, bool endianSwap = false) : FlatDataElement<int32_t>(data, endianSwap) {}
virtual ~SparseIndex() {}
public:
std::vector<int32_t>& indices() { return _data; }
};
class FieldNameLength: public FlatDataElement<int32_t>
{
public:
FieldNameLength(char* data, bool endianSwap = false) : FlatDataElement<int32_t>(data, endianSwap) {}
virtual ~FieldNameLength() {}
public:
int32_t fieldNameLength() { return _data[0]; }
};
class FieldNames: public FlatDataElement<int8_t>
{
public:
FieldNames(char* data, bool endianSwap = false, int32_t fieldNameLength = 32);
virtual ~FieldNames() {}
public:
std::vector<std::string> fieldNames() { return _fieldNames; }
int32_t fieldNameLength() { return _fieldNameLength; }
protected:
std::vector<std::string> _fieldNames;
int32_t _fieldNameLength;
};
class CompressedDataElement: public DataElement
{
public:
CompressedDataElement(char* data, bool endianSwap = false);
virtual ~CompressedDataElement() { if (_decompressedData) delete[] _decompressedData; }
public:
char* decompressedData() { return _decompressedData; }
uint32_t decompressedSize() { return _decompressedSize; }
DataElement* reparse();
protected:
char* _decompressedData;
uint32_t _decompressedSize;
};
// FIXME: UNICODE datatype unimplemented
// unimplemented UNICODE support, but works with ASCII strings
template <EDataType UE> // UE can be miUTF8, miUTF16, miUTF32
class UnicodeDataElement: public FlatDataElement<unsigned char>
{
public:
UnicodeDataElement(char* data, bool endianSwap) : FlatDataElement<unsigned char>(data, endianSwap) {}
virtual ~UnicodeDataElement() {}
public:
std::string str() { return std::string(_data.begin(), _data.end()); }
};
class MatrixDataElement: public DataElement
{
public:
MatrixDataElement(bool endianSwap = false) :
DataElement(endianSwap), _flags(NULL), _dimensionsArray(NULL), _name(NULL) {}
virtual ~MatrixDataElement() {
if (_flags) delete _flags;
if (_dimensionsArray) delete _dimensionsArray;
if (_name) delete _name;
}
public:
ArrayFlags* arrayFlags() { return _flags;}
DimensionsArray* dimensionsArray() { return _dimensionsArray; }
Name* name() { return _name; }
protected:
void parseFlags(char* flags) {
_flags = new ArrayFlags(flags, _endianSwap);
};
void parseDimensionsArray(char* dimensionsArray) {
_dimensionsArray = new DimensionsArray(dimensionsArray, _endianSwap);
}
void parseArrayName(char* arrayName) {
_name = new Name(arrayName, _endianSwap);
}
protected:
ArrayFlags* _flags;
DimensionsArray* _dimensionsArray;
Name* _name;
};
template <typename T>
class NumericArray: public MatrixDataElement
{
public:
NumericArray(char* data, bool endianSwap = false);
virtual ~NumericArray() {
if (_real) delete _real;
if (_imag) delete _imag;
}
public:
DataElement* real() { return _real; }
DataElement* imag() { return _imag; }
protected:
void parseReal(char* real) { _real = parse(real, _endianSwap); }
void parseImag(char* imag) { _imag = parse(imag, _endianSwap); }
protected:
// The real/imag subelement could be RealImag<U> in which U is not T
// due to automatic compression of numeric data
DataElement* _real;
DataElement* _imag;
};
template <typename T>
class SparseArray: public MatrixDataElement
{
public:
SparseArray(char* data, bool endianSwap = false);
virtual ~SparseArray() {
if (_rows) delete _rows;
if (_cols) delete _cols;
if (_real) delete _real;
if (_imag) delete _imag;
}
public:
SparseIndex* rows() { return _rows; }
SparseIndex* cols() { return _cols; }
DataElement* real() { return _real; }
DataElement* imag() { return _imag; }
protected:
void parseRows(char* rows) { _rows = new SparseIndex(rows, _endianSwap); }
void parseCols(char* cols) { _cols = new SparseIndex(cols, _endianSwap); }
void parseReal(char* real) { _real = parse(real, _endianSwap); }
void parseImag(char* imag) { _imag = parse(imag, _endianSwap); }
protected:
SparseIndex* _rows;
SparseIndex* _cols;
// The real/imag subelement could be RealImag<U> in which U is not T
// due to automatic compression of numeric data
DataElement* _real;
DataElement* _imag;
};
class Cell: public MatrixDataElement
{
public:
Cell(char* data, bool endianSwap = false);
virtual ~Cell() {
for (int i = 0; i < _cells.size(); ++i)
delete _cells[i];
}
public:
vector<MatrixDataElement*>& cells() { return _cells; }
protected:
vector<MatrixDataElement*> _cells;
};
class Struct: public MatrixDataElement
{
public:
Struct(bool endianSwap = false) :
MatrixDataElement(endianSwap), _fieldNameLength(NULL), _fieldNames(NULL), _fields() {}
Struct(char* data, bool endianSwap = false);
virtual ~Struct() {
if (_fieldNameLength) delete _fieldNameLength;
if (_fieldNames) delete _fieldNames;
for (int i = 0; i < _fields.size(); ++i)
delete _fields[i];
}
public:
FieldNameLength* fieldNameLength() { return _fieldNameLength; }
FieldNames* fieldNames() { return _fieldNames; }
vector<MatrixDataElement*> fields() { return _fields; }
protected:
void parseFieldNameLength(char* fieldNameLength) {
_fieldNameLength = new FieldNameLength(fieldNameLength, _endianSwap);
}
void parseFieldNames(char* fieldNames) {
_fieldNames = new FieldNames(fieldNames, _endianSwap, _fieldNameLength->fieldNameLength());
}
protected:
FieldNameLength* _fieldNameLength;
FieldNames* _fieldNames;
vector<MatrixDataElement*> _fields;
};
class Object: public Struct
{
public:
Object(char* data, bool endianSwap);
virtual ~Object() {
if (_className)
delete _className;
}
public:
Name* className() { return _className; }
protected:
void parseClassName(char* className) { _className = new Name(className, _endianSwap); }
protected:
Name* _className;
};
class MatfileReader
{
public:
MatfileReader(std::string matfile);
virtual ~MatfileReader();
public:
std::string descriptiveText() { return _descriptiveText; }
uint64_t subsysDataOffset() { return _subsysDataOffset; }
uint16_t version() { return _version; }
std::string endianIndicator() { return _endianIndicator; }
int numberDataElements() { return _dataElements.size(); }
std::vector<DataElement*> dataElements() { return _dataElements; }
public:
void gotoHeader() { _inputStream.seekg(0, ios_base::beg); }
void gotoData() { _inputStream.seekg(128, ios_base::beg); }
void parseHeader();
void parseDataElement();
void parseAllDataElements();
bool eof() { return _inputStream.eof(); }
private:
std::string _matfile;
std::fstream _inputStream;
char _header[128];
std::string _descriptiveText;
uint64_t _subsysDataOffset;
uint16_t _version;
std::string _endianIndicator;
bool _endianSwap;
// infomation of current data element
std::vector<DataElement*> _dataElements;
};
#endif /* _MATFILEREADER_H_ */