-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatFile.cpp
201 lines (154 loc) · 5.94 KB
/
MatFile.cpp
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
#include "MatFile.h"
MATFile::MATFile(const std::string& _filename, const std::string& _mode) {
filename = _filename;
mode = 0;
if(_mode == "r") {
mode = READABLE;
} else if(_mode == "u") {
mode = READABLE | WRITABLE;
} else if (_mode == "w") {
mode = WRITABLE;
} else if (_mode == "w4") {
mode = WRITABLE | V4_OR_OLDER;
} else if (_mode == "wL") {
mode = WRITABLE | LOCAL_VARCHAR;
} else if (_mode == "wz") {
mode = WRITABLE | COMPRESSED;
} else if (_mode == "w7.3") {
mode = WRITABLE | BIGVARS;
} else {
throw(std::runtime_error("Invalid I/O mode " + _mode));
}
mfp = MW::matOpen(filename.c_str(), _mode.c_str());
if(!mfp) {
throw(std::runtime_error("Unable to open file " + filename + " (mode: " + _mode + ")"));
}
}
MATFile::~MATFile() {
if(mode) {
MW::matClose(mfp);
}
}
void MATFile::close() {
mode = 0; // prevent reading AND writing!
MW::matClose(mfp);
}
void MATFile::rmVar(const std::string& varname) {
if(!(isReadable() && isWritable()))
throw(std::runtime_error("File " + filename + " must be readable and writable to remove variables (open with mode \"u\"."));
MW::matDeleteVariable(mfp, varname.c_str());
}
FILE* MATFile::getFP() {
/* Returns the underlying file pointer. Be careful, because the file is
presumably closed when this object is destroyed. */
return MW::matGetFp(mfp);
}
std::vector<std::string> MATFile::getDir() {
int n;
char** names = MW::matGetDir(mfp, &n);
std::vector<std::string> v;
v.reserve(n);
for(int i=0; i<n; i++) {
v.push_back(names[i]);
}
MW::mxFree(names);
return v;
}
MW::mxArray* MATFile::rawGetVar(const std::string& varname) {
/* Returns a pointer to the mxArray object associated with varname, or
returns a null pointer if the variable does not exist in this file.
*YOU* are responsible for deleting this later, using mxDestroyArray().
See the Mathworks docs (matGetVariable) for additional details */
if(!isReadable())
throw(std::runtime_error("File " + filename + " is was not opened for reading (or has been closed)"));
MW::mxArray* ptr = MW::matGetVariable(mfp, varname.c_str());
return ptr;
}
MW::mxArray* MATFile::rawGetInfo(const std::string& varname) {
/* Returns a pointer to the mxArray object's header associated with varname, or
returns a null pointer if the variable does not exist in this file.
*YOU* are responsible for deleting this later, using mxDestroyArray().
See the Mathworks docs (matGetVariableInfo) for details */
if(!isReadable())
throw(std::runtime_error("File " + filename + " is was not opened for reading (or has been closed)"));
MW::mxArray* ptr = MW::matGetVariableInfo(mfp, varname.c_str());
return ptr;
}
template <>
void MATFile::putScalar<>(const std::string& varname, MW::mxArray* value, bool asGlobal) {
if(!isWritable())
throw(std::runtime_error("File " + filename + " is was not opened for writing (or has been closed)"));
int status;
if(asGlobal) {
status = MW::matPutVariableAsGlobal(mfp, varname.c_str(), value);
} else {
status = MW::matPutVariable(mfp, varname.c_str(), value);
}
if(status) {
throw(std::runtime_error("Unable to write variable " + varname + " to file " + filename + "."));
}
}
template <>
void MATFile::putScalar<const std::string>(const std::string& varname, const std::string value, bool asGlobal) {
MW::mxArray* newval = MW::mxCreateString(value.c_str());
check_put_and_dealloc(varname, newval, asGlobal);
}
template <>
void MATFile::putScalar<const std::string&>(const std::string& varname, const std::string& value, bool asGlobal) {
MW::mxArray* newval = MW::mxCreateString(value.c_str());
check_put_and_dealloc(varname, newval, asGlobal);
}
template <>
void MATFile::putScalar<const char*>(const std::string& varname, const char* value, bool asGlobal) {
MW::mxArray* newval = MW::mxCreateString(value);
check_put_and_dealloc(varname, newval, asGlobal);
}
template <>
void MATFile::putScalar<bool>(const std::string& varname, const bool value, bool asGlobal) {
MW::mxArray* newval = MW::mxCreateLogicalScalar(value);
check_put_and_dealloc(varname, newval, asGlobal);
}
template <>
void MATFile::putScalar<double>(const std::string& varname, const double value, bool asGlobal) {
MW::mxArray* newval = MW::mxCreateDoubleScalar(value);
check_put_and_dealloc(varname, newval, asGlobal);
}
template <>
void MATFile::putArray<bool>(const std::string& varname, const bool* values,
MW::mwSize ndims, const MW::mwSize* dims,
bool asGlobal) {
MW::mwSize n = 1;
for(MW::mwSize i=0; i < ndims; i++) {
n*= dims[i];
}
MW::mxArray* newval = MW::mxCreateLogicalArray(ndims, dims);
if(newval) {
bool* dst = static_cast<bool*>(MW::mxGetData(newval));
for(MW::mwSize i=0; i < n; i++)
dst[i] = values[i];
}
check_put_and_dealloc(varname, newval, asGlobal);
return;
}
void MATFile::check_put_and_dealloc(const std::string& varname, MW::mxArray* newval, bool asGlobal) {
/* This is a silly little wrapper around putScalar<MW::mxArray*> that
- checks whether the mxArray is NULL (and throws if it is),
- attempts to call putScalar<MW::mxArray*>, and
- cleans up appropriately.
Use this inside functions that create some sort of mxArray themselves. Use
putScalar<MW::mxArray*> for user-provided mxArray*, since cleaning those up is the user's
responsibility.
*/
if(!newval) {
std::runtime_error("Out of memory! (Failed to allocate an mxArray for new data)");
}
try {
putScalar<MW::mxArray*>(varname, newval, asGlobal);
} catch (...) {
MW::mxDestroyArray(newval);
newval = 0;
throw;
}
MW::mxDestroyArray(newval);
newval = 0;
}