v2.0.0
Loading...
Searching...
No Matches
bids_tsv.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "bids_tsv.h"
40
41//=============================================================================================================
42// QT INCLUDES
43//=============================================================================================================
44
45#include <QFile>
46#include <QTextStream>
47#include <QDebug>
48
49//=============================================================================================================
50// USED NAMESPACES
51//=============================================================================================================
52
53using namespace BIDSLIB;
54
55//=============================================================================================================
56// STATIC HELPERS
57//=============================================================================================================
58
59static const QString NA = QStringLiteral("n/a");
60
61static QString valOrNA(const QString& s)
62{
63 return s.isEmpty() ? NA : s;
64}
65
66//=============================================================================================================
67// DEFINE MEMBER METHODS
68//=============================================================================================================
69
73
74//=============================================================================================================
75
79
80//=============================================================================================================
81
82QList<BidsTsvRow> BidsTsv::readTsv(const QString& sFilePath,
83 QStringList& headers)
84{
85 QList<BidsTsvRow> rows;
86 headers.clear();
87
88 QFile file(sFilePath);
89 if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
90 qWarning() << "[BidsTsv::readTsv] Cannot open" << sFilePath;
91 return rows;
92 }
93
94 QTextStream in(&file);
95 bool firstLine = true;
96
97 while(!in.atEnd()) {
98 QString line = in.readLine().trimmed();
99 if(line.isEmpty())
100 continue;
101
102 QStringList fields = line.split(QLatin1Char('\t'));
103
104 if(firstLine) {
105 headers = fields;
106 firstLine = false;
107 continue;
108 }
109
110 BidsTsvRow row;
111 for(int i = 0; i < headers.size() && i < fields.size(); ++i) {
112 row[headers[i]] = fields[i];
113 }
114 rows.append(row);
115 }
116
117 file.close();
118 return rows;
119}
120
121//=============================================================================================================
122
123bool BidsTsv::writeTsv(const QString& sFilePath,
124 const QStringList& headers,
125 const QList<BidsTsvRow>& rows)
126{
127 QFile file(sFilePath);
128 if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
129 qWarning() << "[BidsTsv::writeTsv] Cannot open" << sFilePath << "for writing";
130 return false;
131 }
132
133 QTextStream out(&file);
134
135 // Header row
136 out << headers.join(QLatin1Char('\t')) << QLatin1Char('\n');
137
138 // Data rows
139 for(const auto& row : rows) {
140 QStringList fields;
141 fields.reserve(headers.size());
142 for(const auto& header : headers) {
143 fields << valOrNA(row.value(header));
144 }
145 out << fields.join(QLatin1Char('\t')) << QLatin1Char('\n');
146 }
147
148 file.close();
149 return true;
150}
BidsTsv class declaration — generic BIDS TSV file reading and writing.
BIDS dataset reading, writing, path construction, and sidecar metadata handling for iEEG/EEG/MEG.
QMap< QString, QString > BidsTsvRow
Definition bids_tsv.h:65
static QList< BidsTsvRow > readTsv(const QString &sFilePath, QStringList &headers)
Definition bids_tsv.cpp:82
static bool writeTsv(const QString &sFilePath, const QStringList &headers, const QList< BidsTsvRow > &rows)
Definition bids_tsv.cpp:123