-
Notifications
You must be signed in to change notification settings - Fork 24
/
CSVWriter.h
167 lines (145 loc) · 5.03 KB
/
CSVWriter.h
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
#ifndef CSVWRITER_H
#define CSVWRITER_H
#include <fstream>
#include <iostream>
#include <sstream>
#include <typeinfo>
class CSVWriter
{
public:
CSVWriter(){
this->firstRow = true;
this->seperator = ";";
this->columnNum = -1;
this->valueCount = 0;
}
CSVWriter(int numberOfColums){
this->firstRow = true;
this->seperator = ";";
this->columnNum = numberOfColums;
this->valueCount = 0;
}
CSVWriter(const std::string& seperator){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = -1;
this->valueCount = 0;
}
CSVWriter(const std::string& seperator, int numberOfColums){
this->firstRow = true;
this->seperator = seperator;
this->columnNum = numberOfColums;
this->valueCount = 0;
}
CSVWriter& add(const char *str){
return this->add(std::string(str));
}
CSVWriter& add(char *str){
return this->add(std::string(str));
}
CSVWriter& add(std::string str){
//if " character was found, escape it
size_t position = str.find("\"",0);
bool foundQuotationMarks = position != std::string::npos;
while(position != std::string::npos){
str.insert(position,"\"");
position = str.find("\"",position + 2);
}
if(foundQuotationMarks){
str = "\"" + str + "\"";
}else if(str.find(this->seperator) != std::string::npos){
//if seperator was found and string was not escapted before, surround string with "
str = "\"" + str + "\"";
}
return this->add<std::string>(str);
}
template<typename T>
CSVWriter& add(T str){
if(this->columnNum > -1){
//if autoNewRow is enabled, check if we need a line break
if(this->valueCount == this->columnNum ){
this->newRow();
}
}
if(valueCount > 0)
this->ss << this->seperator;
this->ss << str;
this->valueCount++;
return *this;
}
template<typename T>
CSVWriter& operator<<(const T& t){
return this->add(t);
}
void operator+=(CSVWriter &csv){
this->ss << std::endl << csv;
}
std::string toString(){
return ss.str();
}
friend std::ostream& operator<<(std::ostream& os, CSVWriter & csv){
return os << csv.toString();
}
CSVWriter& newRow(){
if(!this->firstRow || this->columnNum > -1){
ss << std::endl;
}else{
//if the row is the first row, do not insert a new line
this->firstRow = false;
}
valueCount = 0;
return *this;
}
bool writeToFile(const std::string& filename){
return writeToFile(filename,false);
}
bool writeToFile(const std::string& filename, bool append){
std::ofstream file;
bool appendNewLine = false;
if (append) {
//check if last char of the file is newline
std::ifstream fin;
fin.open(filename);
if (fin.is_open()) {
fin.seekg(-1, std::ios_base::end); //go to end of file
int lastChar = fin.peek();
if (lastChar != -1 && lastChar != '\n') //if file is not empry and last char is not new line char
appendNewLine = true;
}
file.open(filename.c_str(), std::ios::out | std::ios::app);
}
else {
file.open(filename.c_str(), std::ios::out | std::ios::trunc);
}
if(!file.is_open())
return false;
if(append && appendNewLine)
file << std::endl;
file << this->toString();
file.close();
return file.good();
}
void enableAutoNewRow(int numberOfColumns){
this->columnNum = numberOfColumns;
}
void disableAutoNewRow(){
this->columnNum = -1;
}
//you can use this reset method in destructor if you gonna use it in heap mem.
void resetContent(){
const static std::stringstream initial;
ss.str(std::string());
ss.clear();
ss.copyfmt(initial);
}
~CSVWriter(){
resetContent();
}
protected:
std::string seperator;
int columnNum;
int valueCount;
bool firstRow;
std::stringstream ss;
};
#endif // CSVWRITER_H