MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
ioutils.cpp
Go to the documentation of this file.
1//=============================================================================================================
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "ioutils.h"
42#include <algorithm>
43#include <regex>
44
45//=============================================================================================================
46// QT INCLUDES
47//=============================================================================================================
48
49#include <QDataStream>
50#include <QRegularExpression>
51
52//=============================================================================================================
53// EIGEN INCLUDES
54//=============================================================================================================
55
56#include <Eigen/Core>
57
58//=============================================================================================================
59// USED NAMESPACES
60//=============================================================================================================
61
62using namespace Eigen;
63using namespace UTILSLIB;
64
65//=============================================================================================================
66// DEFINE MEMBER METHODS
67//=============================================================================================================
68
69qint32 IOUtils::fread3(QDataStream &p_qStream)
70{
71 char* bytes = new char[3];
72 p_qStream.readRawData(bytes, 3);
73 qint32 int3 = (((unsigned char) bytes[0]) << 16) + (((unsigned char) bytes[1]) << 8) + ((unsigned char) bytes[2]);
74 delete[] bytes;
75 return int3;
76}
77
78//=============================================================================================================
79
80qint32 IOUtils::fread3(std::iostream& stream)
81{
82 char* bytes = new char[3];
83 stream.read(bytes, 3);
84 qint32 int3 = (((unsigned char) bytes[0]) << 16) + (((unsigned char) bytes[1]) << 8) + ((unsigned char) bytes[2]);
85 delete[] bytes;
86 return int3;
87}
88
89//=============================================================================================================
90
91VectorXi IOUtils::fread3_many(QDataStream &p_qStream, qint32 count)
92{
93 VectorXi res(count);
94
95 for(qint32 i = 0; i < count; ++i)
96 res[i] = IOUtils::fread3(p_qStream);
97
98 return res;
99}
100
101//=============================================================================================================
102
103VectorXi IOUtils::fread3_many(std::iostream& stream, qint32 count)
104{
105 VectorXi res(count);
106
107 for(qint32 i = 0; i < count; ++i)
108 res[i] = IOUtils::fread3(stream);
109
110 return res;
111}
112
113//=============================================================================================================
114//fiff_combat
115qint16 IOUtils::swap_short(qint16 source)
116{
117 unsigned char *csource = (unsigned char *)(&source);
118 qint16 result;
119 unsigned char *cresult = (unsigned char *)(&result);
120
121 cresult[0] = csource[1];
122 cresult[1] = csource[0];
123 return (result);
124}
125
126//=============================================================================================================
127
128qint32 IOUtils::swap_int(qint32 source)
129{
130 unsigned char *csource = (unsigned char *)(&source);
131 qint32 result;
132 unsigned char *cresult = (unsigned char *)(&result);
133
134 cresult[0] = csource[3];
135 cresult[1] = csource[2];
136 cresult[2] = csource[1];
137 cresult[3] = csource[0];
138 return (result);
139}
140
141//=============================================================================================================
142
143void IOUtils::swap_intp(qint32 *source)
144
145{
146 unsigned char *csource = (unsigned char *)(source);
147
148 unsigned char c;
149
150 c = csource[3];
151 csource[3] = csource[0];
152 csource[0] = c;
153 c = csource[2];
154 csource[2] = csource[1];
155 csource[1] = c;
156
157 return;
158}
159
160//=============================================================================================================
161
162qint64 IOUtils::swap_long(qint64 source)
163{
164 unsigned char *csource = (unsigned char *)(&source);
165 qint64 result;
166 unsigned char *cresult = (unsigned char *)(&result);
167
168 cresult[0] = csource[7];
169 cresult[1] = csource[6];
170 cresult[2] = csource[5];
171 cresult[3] = csource[4];
172 cresult[4] = csource[3];
173 cresult[5] = csource[2];
174 cresult[6] = csource[1];
175 cresult[7] = csource[0];
176 return (result);
177}
178
179//=============================================================================================================
180
181void IOUtils::swap_longp(qint64 *source)
182{
183 unsigned char *csource = (unsigned char *)(source);
184 unsigned char c;
185
186 c = csource[0];
187 csource[0] = csource[7];
188 csource[7] = c;
189
190 c = csource[1];
191 csource[1] = csource[6];
192 csource[6] = c;
193
194 c = csource[2];
195 csource[2] = csource[5];
196 csource[5] = c;
197
198 c = csource[3];
199 csource[3] = csource[4];
200 csource[4] = c;
201
202 return;
203}
204
205//=============================================================================================================
206
207float IOUtils::swap_float(float source)
208{
209 unsigned char *csource = (unsigned char *)(&source);
210 float result;
211 unsigned char *cresult = (unsigned char *)(&result);
212
213 cresult[0] = csource[3];
214 cresult[1] = csource[2];
215 cresult[2] = csource[1];
216 cresult[3] = csource[0];
217 return (result);
218}
219
220//=============================================================================================================
221
222void IOUtils::swap_floatp(float *source)
223
224{
225 unsigned char *csource = (unsigned char *)(source);
226 unsigned char c;
227
228 c = csource[3];
229 csource[3] = csource[0];
230 csource[0] = c;
231 c = csource[2];
232 csource[2] = csource[1];
233 csource[1] = c;
234
235 return;
236}
237
238//=============================================================================================================
239
240void IOUtils::swap_doublep(double *source)
241
242{
243 unsigned char *csource = (unsigned char *)(source);
244 unsigned char c;
245
246 c = csource[7];
247 csource[7] = csource[0];
248 csource[0] = c;
249
250 c = csource[6];
251 csource[6] = csource[1];
252 csource[1] = c;
253
254 c = csource[5];
255 csource[5] = csource[2];
256 csource[2] = c;
257
258 c = csource[4];
259 csource[4] = csource[3];
260 csource[3] = c;
261
262 return;
263}
264
265//=============================================================================================================
266
267QStringList IOUtils::get_new_chnames_conventions(const QStringList& chNames)
268{
269 QStringList result;
270 QString replaceString;
271
272 for(int i = 0; i < chNames.size(); ++i) {
273 replaceString = chNames.at(i);
274 replaceString.replace(" ","");
275 result.append(replaceString);
276 }
277
278 return result;
279}
280
281//=============================================================================================================
282
283std::vector<std::string> IOUtils::get_new_chnames_conventions(const std::vector<std::string>& chNames)
284{
285 std::vector<std::string> result;
286
287 for(auto channelName : chNames){
288 std::remove(channelName.begin(), channelName.end(), ' ');
289 result.push_back(std::move(channelName));
290 }
291
292 return result;
293}
294
295//=============================================================================================================
296
297QStringList IOUtils::get_old_chnames_conventions(const QStringList& chNames)
298{
299 QStringList result, xList;
300 QString replaceString;
301 QRegularExpression xRegExp;
302
303 for(int i = 0; i < chNames.size(); ++i) {
304 xRegExp = QRegularExpression("[0-9]{1,100}");
305 QRegularExpressionMatch match = xRegExp.match(chNames.at(i));
306 xList = match.capturedTexts();
307
308 for(int k = 0; k < xList.size(); ++k) {
309 replaceString = chNames.at(i);
310 replaceString.replace(xList.at(k),QString("%1%2").arg(" ").arg(xList.at(k)));
311 result.append(replaceString);
312 }
313 }
314
315 return result;
316}
317
318//=============================================================================================================
319
320std::vector<std::string> IOUtils::get_old_chnames_conventions(const std::vector<std::string>& chNames)
321{
322 std::vector<std::string> result;
323
324 for(auto channelName : chNames){
325 std::regex_replace(channelName, std::regex("[0-9]{1,100}"), " $&");
326 result.push_back(std::move(channelName));
327 }
328
329 return result;
330}
331
332//=============================================================================================================
333
334bool IOUtils::check_matching_chnames_conventions(const QStringList& chNamesA, const QStringList& chNamesB, bool bCheckForNewNamingConvention)
335{
336 bool bMatching = false;
337
338 if(chNamesA.isEmpty()) {
339 qWarning("Warning in IOUtils::check_matching_chnames_conventions - chNamesA list is empty. Nothing to compare");
340 }
341
342 if(chNamesB.isEmpty()) {
343 qWarning("Warning in IOUtils::check_matching_chnames_conventions - chNamesB list is empty. Nothing to compare");
344 }
345
346 QString replaceStringOldConv, replaceStringNewConv;
347
348 for(int i = 0; i < chNamesA.size(); ++i) {
349 if(chNamesB.contains(chNamesA.at(i))) {
350 bMatching = true;
351 } else if(bCheckForNewNamingConvention) {
352 //Create new convention
353 replaceStringNewConv = chNamesA.at(i);
354 replaceStringNewConv.replace(" ","");
355
356 if(chNamesB.contains(replaceStringNewConv)) {
357 bMatching = true;
358 } else {
359 //Create old convention
360 QRegularExpression xRegExp("[0-9]{1,100}");
361 QRegularExpressionMatch match = xRegExp.match(chNamesA.at(i));
362 QStringList xList = match.capturedTexts();
363
364 for(int k = 0; k < xList.size(); ++k) {
365 replaceStringOldConv = chNamesA.at(i);
366 replaceStringOldConv.replace(xList.at(k),QString("%1%2").arg(" ").arg(xList.at(k)));
367
368 if(chNamesB.contains(replaceStringNewConv) || chNamesB.contains(replaceStringOldConv) ) {
369 bMatching = true;
370 } else {
371 bMatching = false;
372 }
373 }
374 }
375 }
376 }
377
378 return bMatching;
379}
380
381//=============================================================================================================
382
383bool IOUtils::check_matching_chnames_conventions(const std::vector<std::string>& chNamesA, const std::vector<std::string>& chNamesB, bool bCheckForNewNamingConvention)
384{
385 if(chNamesA.empty()){
386 qWarning("Warning in IOUtils::check_matching_chnames_conventions - chNamesA list is empty. Nothing to compare");
387 }
388 if(chNamesB.empty()){
389 qWarning("Warning in IOUtils::check_matching_chnames_conventions - chNamesB list is empty. Nothing to compare");
390 }
391
392 bool bMatching = false;
393
394 for(size_t i = 0 ; i < chNamesA.size(); ++i){
395 if (std::find(chNamesB.begin(), chNamesB.end(), chNamesA.at(i)) != chNamesB.end()){
396 bMatching = true;
397 } else if(bCheckForNewNamingConvention){
398 std::string replaceStringNewConv{chNamesA.at(i)};
399 std::remove(replaceStringNewConv.begin(), replaceStringNewConv.end(), ' ');
400
401 if(std::find(chNamesB.begin(), chNamesB.end(), replaceStringNewConv) != chNamesB.end()){
402 bMatching = true;
403 } else {
404 std::string replaceStringOldConv{chNamesA.at(i)};
405 std::regex_replace(replaceStringOldConv, std::regex("[0-9]{1,100}"), " $&");
406 if(std::find(chNamesB.begin(), chNamesB.end(), replaceStringNewConv) != chNamesB.end() || std::find(chNamesB.begin(), chNamesB.end(), replaceStringOldConv) != chNamesB.end()){
407 bMatching = true;
408 } else {
409 bMatching = false;
410 }
411 }
412 }
413 }
414 return bMatching;
415}
int k
Definition fiff_tag.cpp:324
IOUtils class declaration.
static void swap_doublep(double *source)
Definition ioutils.cpp:240
static qint32 fread3(QDataStream &p_qStream)
Definition ioutils.cpp:69
static QStringList get_old_chnames_conventions(const QStringList &chNames)
Definition ioutils.cpp:297
static qint32 swap_int(qint32 source)
Definition ioutils.cpp:128
static void swap_floatp(float *source)
Definition ioutils.cpp:222
static QStringList get_new_chnames_conventions(const QStringList &chNames)
Definition ioutils.cpp:267
static void swap_longp(qint64 *source)
Definition ioutils.cpp:181
static bool check_matching_chnames_conventions(const QStringList &chNamesA, const QStringList &chNamesB, bool bCheckForNewNamingConvention=false)
Definition ioutils.cpp:334
static void swap_intp(qint32 *source)
Definition ioutils.cpp:143
static qint64 swap_long(qint64 source)
Definition ioutils.cpp:162
static float swap_float(float source)
Definition ioutils.cpp:207
static Eigen::VectorXi fread3_many(QDataStream &p_qStream, qint32 count)
Definition ioutils.cpp:91
static qint16 swap_short(qint16 source)
Definition ioutils.cpp:115