libodsstream
Library for mass spectrometry
Loading...
Searching...
No Matches
tsvdirectorywriter.cpp
Go to the documentation of this file.
1/*
2 libodsstream is a library to read and write ODS documents as streams
3 Copyright (C) 2013 Olivier Langella <Olivier.Langella@moulon.inra.fr>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include <QFileInfo>
21#include <QDebug>
22#include "tsvdirectorywriter.h"
23#include "odsexception.h"
24
25
29
31 : _directory(directory)
32{
33 if(_directory.exists())
34 {
35 }
36 else
37 {
38 if(!_directory.mkdir(_directory.absolutePath()))
39 {
40 throw OdsException(QString("unable to create output directory %1")
41 .arg(_directory.absolutePath()));
42 }
43 }
44}
45
50
51void
53{
54 qDebug();
55 m_tsvSeparatorEnum = separator;
56 switch(separator)
57 {
59 m_separator = "\t";
60 break;
62 m_separator = ",";
63 break;
65 m_separator = ";";
66 break;
67 }
68 qDebug();
69}
70
71void
73{
74 if(mpa_otxtstream != nullptr)
75 {
77 mpa_otxtstream->flush();
78 //_p_otxtstream->close();
79 delete(mpa_otxtstream);
80 mpa_otxtstream = nullptr;
81 }
82 if(mpa_ofile != nullptr)
83 {
84 mpa_ofile->flush();
85 mpa_ofile->close();
86 delete(mpa_ofile);
87 mpa_ofile = nullptr;
88 }
89}
90
91void
92TsvDirectoryWriter::writeSheet(const QString &sheetName)
93{
94 close();
95 // qDebug() << " TsvDirectoryWriter::writeSheet " <<
96 // _directory.absolutePath();
97 mpa_ofile = new QFile(QString(_directory.absolutePath())
98 .append("/")
99 .append(sheetName)
100 .append(this->_file_extension));
101 // qDebug() << " TsvDirectoryWriter::writeSheet " <<
102 // QFileInfo(*_p_ofile).absoluteFilePath();
103 if(mpa_ofile->open(QIODevice::WriteOnly))
104 {
105 mpa_otxtstream = new QTextStream(mpa_ofile);
106 }
107 else
108 {
109 throw OdsException(QString("unable to write into file %1")
110 .arg(QFileInfo(*mpa_ofile).absoluteFilePath()));
111 }
113}
114
115void
122
123
124void
126{
127 if(mpa_otxtstream == nullptr)
128 {
129 writeSheet("default");
130 }
131}
132
133void
135{
136 ensureSheet();
138 {
139 }
140 else
141 {
143 }
144 _startingSheet = false;
145 _tableRowStart = true;
146
147 if(m_flushLines)
148 mpa_otxtstream->flush();
149}
150void
152{
153 writeCell(QString(cstr));
154}
155void
157{
158 ensureSheet();
159 if(!_tableRowStart)
160 {
162 }
163 _tableRowStart = false;
164
166 {
167 *mpa_otxtstream << QString("\"%1\"").arg(
168 QString(text).replace("\"", "\"\""));
169 }
170 else
171 {
172 if(text.contains(_end_of_line) || text.contains(m_separator) ||
173 text.contains("\""))
174 {
175 *mpa_otxtstream << QString("\"%1\"").arg(
176 QString(text).replace("\"", "\"\""));
177 }
178 else
179 {
180 *mpa_otxtstream << text;
181 }
182 }
183
184 _startingSheet = false;
185}
186
187void
189{
190 ensureSheet();
191 if(!_tableRowStart)
192 {
194 }
195 _tableRowStart = false;
196 *mpa_otxtstream << text;
197 _startingSheet = false;
198}
199void
201{
202 ensureSheet();
203 if(!_tableRowStart)
204 {
206 }
207 _tableRowStart = false;
208 _startingSheet = false;
209}
210void
212{
213 writeRawCell(QString::number(num, 'g', 10));
214}
215void
217{
218 writeRawCell(QString::number(num, 'g', 10));
219}
220void
222{
223 writeRawCell(QString::number(num, 'g', numFloatPrecision));
224}
225void
227{
228 writeRawCell(QString::number(num, 'g', numFloatPrecision));
229}
230
231void
233{
234 writeCell(value);
235}
236void
238{
239 if(isOk)
240 {
241 writeRawCell(QString("T"));
242 }
243 else
244 {
245 writeRawCell(QString("F"));
246 }
247}
248void
250{
251 writeRawCell(date.toString("yyyy-MM-dd"));
252}
253void
254TsvDirectoryWriter::writeCell(const QDateTime &date)
255{
256 writeRawCell(date.toString("yyyy-MM-ddThh:mm:ss"));
257}
258void
259TsvDirectoryWriter::writeCell([[maybe_unused]] const QUrl &url,
260 const QString &text)
261{
262 writeCell(text);
263}
264
265
266bool
268{
269 m_quoteStrings = quote_strings;
270 return m_quoteStrings;
271}
272
273bool
278
279bool
281{
282 return m_flushLines;
283}
284
285bool
287{
288 m_flushLines = flushOk;
289 return m_flushLines;
290}
291
void writeLine() override
open a new line
virtual void writeSheet(const QString &sheetName) override
open a new sheet
bool setFlushLines(bool flushOk)
enable a physical flush on device at each new line enables this if you want to ensure that each line ...
TsvSeparator getSeparator() const
get the separator used between values (cells)
void writeEmptyCell() override
write an empty cell
QTextStream * mpa_otxtstream
bool isQuoteStrings() const
tells if the quote string flag is enabled
virtual void close() override
bool setQuoteStrings(bool quote_strings)
set a flag to quote strings
void writeCellPercentage(double value) override
write a double as a percentage
void writeRawCell(const QString &text)
void setSeparator(TsvSeparator separator)
sets the separator to use between values (cells)
unsigned int numFloatPrecision
void writeCell(const char *) override
write a text cell
bool isFlushLines() const
tells if the flush lines flag is enabled
TsvSeparator m_tsvSeparatorEnum
TsvSeparator
Definition config.h:11