-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsvfile.cpp
More file actions
132 lines (120 loc) · 4.96 KB
/
csvfile.cpp
File metadata and controls
132 lines (120 loc) · 4.96 KB
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
/*
This file is part of WebTooth-Extractor and for personal use the
GPL3 license applies. For any kind of commercial usage, you have to
acquire a commercial license before use. The copyright of the WebTooth
application and source code remains with Patrick Scheller and any
contributor to the project agrees with his commits to renounce to the
copyright of his parts completely.
WebTooth-Extractor 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 3 of the License.
WebTooth-Extractor 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 WebTooth-Extractor. If not, see http://www.gnu.org/licenses/ .
*/
#include <QFile>
#include <QTextStream>
#include <QMap>
#include <QDebug>
#include "csvfile.h"
CSVFile::CSVFile(QObject *parent) : QObject(parent)
{
}
CSVFile::~CSVFile()
{
}
bool CSVFile::WriteResultCSV(const QMap<QString, QString> &tbl_result, const QString& csvfilepath, const QString& csvSeparator)
{
bool bRes = false;
QFile csvFile(csvfilepath);
if (csvFile.exists())
{
qDebug() << "Error: File" << csvfilepath << "does already exist!";
}
//else
{
//qDebug() << "File" << prjfilepath << "does not yet exists";
if (!csvFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Couldn't open the CSV file.";
}
else
{
if (!tbl_result.size() == 0)
{
QTextStream outstream(&csvFile);
// Write header
outstream << "Data Field" << csvSeparator << "Value" << endline;
// Write all the content
QMap<QString, QString>::const_iterator it = tbl_result.constBegin();
QString tmpKey = "";
QString tmpValue = "";
bool isSpecialCase = false;
while (it != tbl_result.constEnd())
{
tmpKey = it.key();
tmpValue = it.value();
// KEY: Check if we need to enclose the text of each field with extra "
if (tmpKey.contains("\"")) // " in the field?
{
isSpecialCase = true;
// Search for all occurrences of "
for (int i=0; i < tmpKey.size(); i++)
{
if (tmpKey.indexOf('"', i) > -1) // -1 = not found
{
i = tmpKey.indexOf('"', i);
tmpKey.insert(i+1, '"');
}
}
}
if (tmpKey.contains(",") || tmpKey.contains(endline)) // Comma or CRLF in the field?
{
isSpecialCase = true;
tmpKey = "\"" + tmpKey + "\"";
}
// VALUE: Check if we need to enclose the text of each field with extra "
if (tmpValue.contains("\"")) // " in the field?
{
isSpecialCase = true;
// Search for all occurrences of "
for (int i=0; i < tmpValue.size(); i++)
{
if (tmpValue.indexOf('"', i) > -1) // -1 = not found
{
i = tmpValue.indexOf('"', i);
tmpValue.insert(i+1, '"');
}
}
}
if (tmpValue.contains(",") || tmpValue.contains(endline)) // Comma or CRLF in the field?
{
isSpecialCase = true;
tmpValue = "\"" + tmpValue + "\"";
}
// Special cases?
if (isSpecialCase)
{
outstream << tmpKey << csvSeparator << tmpValue << endline;
}
else
{
outstream << it.key() << csvSeparator << it.value() << endline;
}
tmpKey = "";
tmpValue = "";
isSpecialCase = false;
it++;
}
outstream << "Data from the WebTooth Extractor (c) by Patrick Scheller" << csvSeparator << "2015" << endline;
outstream.flush(); // Write the data to the CSV file
bRes = true;
}
csvFile.close();
}
}
return (bRes);
}