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
|
/*
* Copyright (c) 2009 Samit Basu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __MatIO_hpp__
#define __MatIO_hpp__
#include "Array.hpp"
#include <string>
#include <zlib.h>
#include <QFile>
enum MatTypes {
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
};
// Sparse yet to do..
class MatIO {
public:
enum openMode {
readMode = 0,
writeMode
};
enum mxArrayTypes {
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,
mxFUNCTION_CLASS = 16
};
private:
QFile *m_fp;
QString m_filename;
bool m_endianSwap;
openMode m_mode;
QString m_txt;
bool m_compressed_data;
uint8* m_compression_buffer;
z_streamp zstream;
uint32 m_writecount;
bool m_phantomWriteMode;
private:
// Reads various types of arrays
Array getSparseArray(NTuple dm, bool complexFlag);
Array getNumericArray(mxArrayTypes arrayType, NTuple dm, bool complexFlag);
Array getClassArray(NTuple dm);
Array getStructArray(NTuple dm);
Array getCellArray(NTuple dm);
Array getDataElement();
// Writes various types of arrays
void putSparseArray(const Array &x);
void putNumericArray(const Array &x);
void putClassArray(const Array &x);
void putStructArray(const Array &x);
void putCellArray(const Array &x);
void putDataElement(const Array &x);
void putArraySpecific(const Array &x, Array aFlags, QString name, mxArrayTypes arrayType);
// Align us to the next 64 bit boundary.
void Align64Bit();
// Elementary read/write operations
uint16 getUint16();
uint32 getUint32();
void putUint16(uint16 x);
void putUint32(uint32 x);
// Methods that control the decompression engine
void InitializeDecompressor(uint32 bcount);
void ReadCompressedBytes(void *dest, uint32 toread);
void CloseDecompressor();
// Methods that control the compression engine
void InitializeCompressor();
void WriteCompressedBytes(const void *dest, uint32 towrite);
void CloseCompressor();
// Read data directly from the file
void ReadFileBytes(void *dest, uint32 toread);
// Read data (selects decompression if necessary)
void ReadData(void *dest, uint32 toread);
// Write data directly to the file
void WriteFileBytes(const void *dest, uint32 towrite);
// Write data (selects compression if necessary)
void WriteData(const void *dest, uint32 towrite);
public:
// Constructor pairs
MatIO(QString filename, MatIO::openMode mode);
~MatIO();
// Get/Put for arrays
Array getArray(bool &atEof, QString &name, bool &match, bool &isGlobal);
void putArray(const Array &x, QString name = QString(), bool isGlobal = false);
void putArrayCompressed(const Array &x, QString name);
// Header routines
QString getHeader();
void putHeader(QString header);
};
ArrayVector MatLoadFunction(int nargout, QString filename, StringVector varnames,
bool regexp, Interpreter *eval);
ArrayVector MatSaveFunction(QString filename, StringVector names, Interpreter *eval);
#endif
|