-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.cpp
More file actions
108 lines (92 loc) · 2.75 KB
/
Copy pathcsv.cpp
File metadata and controls
108 lines (92 loc) · 2.75 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
#include "csv.h"
CSVReader::CSVReader(const std::string filename) : fin(filename) {
while(fin.peek() && !fin.eof()){
std::vector<std::string> temp(getParsedLine());
if(temp.size())
table.push_back(temp);
}
}
std::vector<std::string> CSVReader::getParsedLine() {
std::string plainLine;
getline(fin >> std::ws, plainLine);
std::istringstream lineStream(plainLine);
std::vector<std::string> ret;
while(!lineStream.eof()) {
std::string cell;
if(lineStream.peek() == '"') {
char trash;
std::string alsoTrash;
getline(lineStream >> trash, cell, '"');
getline(lineStream, alsoTrash, ',');
}
else {
getline(lineStream >> std::ws, cell, ',');
}
if(find(cell.begin(), cell.end(), '\n') != cell.end())
std::cout << "there is a '\n' \n";
ret.push_back(cell);
}
return ret;
}
std::vector<std::string> CSVReader::getRow(int rowNum) {
return table.at(rowNum);
}
std::vector<std::string> CSVReader::getCol(int colNum) {
std::vector<std::string> ret;
for(auto it : table)
ret.push_back(it.at(colNum));
return ret;
}
std::string CSVReader::getCell(int rowNum, int colNum) {
return table.at(rowNum).at(colNum);
}
int CSVReader::getRowNum() {
return table.size();
}
int CSVReader::getColNum(int rowNum) {
return table.at(rowNum).size();
}
CSVWriter::CSVWriter(const std::string& filename) : fout(filename) {}
void CSVWriter::pushRow(const std::vector<std::string>& row) {
table.push_back(std::vector<std::string>());
std::vector<std::string>& thisRow = table.at(table.size() - 1);
for(auto& content : row)
thisRow.push_back(content);
}
void CSVWriter::setRow(const int rowNum, const std::vector<std::string>& row) {
table.at(rowNum).clear();
for(auto& it : row)
table.at(rowNum).push_back(it);
}
void CSVWriter::setCol(const int colNum, const std::vector<std::string>& col) {
if(col.size() != table.size())
throw std::invalid_argument("CSVWriter: argument size is not equal to CSVWriter::table.");
for(int i = 0; i < table.size(); i++) {
table.at(i).at(colNum) = col.at(colNum);
}
}
void CSVWriter::pushCell(const int rowNum, std::string content) {
table.at(rowNum).push_back(content);
}
void CSVWriter::setCell(const int rowNum, const int colNum, const std::string content) {
table.at(rowNum).at(colNum) = content;
}
void CSVWriter::writeFile() {
std::string line;
for(auto& row : table) {
line = produceLine(row);
fout << line << std::endl;
}
}
std::string CSVWriter::produceLine(const std::vector<std::string>& row) {
std::string rawLine = "";
for(auto& it : row) {
if(!rawLine.empty())
rawLine += ",";
if(it.find(',') != std::string::npos)
rawLine = "\"" + it + "\"";
else
rawLine += it;
}
return rawLine;
}