-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.cpp
131 lines (109 loc) · 2.5 KB
/
csv.cpp
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include "csv.hpp"
#ifndef QUOTED_MULTILINE_SUPPORT
#define QUOTED_MULTILINE_SUPPORT 1
#endif
#ifndef QUOTED_DELIMITER_SUPPORT
#define QUOTED_DELIMITER_SUPPORT 1
#endif
#ifndef COMMENT_LINE_SUPPORT
#define COMMENT_LINE_SUPPORT 1
#endif
#ifndef BLANK_LINE_SUPPORT
#define BLANK_LINE_SUPPORT 1
#endif
CSV::Row CSV::parseQuotedLine(const std::string& line, char delimiter)
{
std::string copy = line;
std::size_t first = copy.find_first_of("\"");
std::size_t last = copy.find_last_of("\"");
copy.erase(last, 1);
copy.erase(first, 1);
return parseLine(copy, delimiter);
}
CSV::Row CSV::parseLine(const std::string& line, char delimiter)
{
Row row;
std::stringstream ss(line);
Cell cell;
while (std::getline(ss, cell, delimiter))
{
#if (QUOTED_DELIMITER_SUPPORT == 1)
Cell extra;
// If there is an odd number of '"' then element continues
while (std::count(cell.begin(), cell.end(), '"') % 2)
{
std::getline(ss, extra, delimiter);
cell.append(1, delimiter);
cell.append(extra);
}
#endif
row.push_back(cell);
}
return row;
}
CSV::Table CSV::parseFile(const std::string& filename, char delimiter)
{
Table table;
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Failed to open file: " << filename << std::endl;
return table;
}
std::string line;
while (std::getline(file, line))
{
#if (BLANK_LINE_SUPPORT == 1)
if (line.length() == 0)
{
continue;
}
#endif
#if (COMMENT_LINE_SUPPORT == 1)
if (line.at(0) == COMMENT_CHARACTER)
{
continue;
}
#endif
#if (QUOTED_MULTILINE_SUPPORT == 1)
std::string extra;
// If there is an odd number of '"' then line continues onto next line of the file
while (std::count(line.begin(), line.end(), '"') % 2)
{
std::getline(file, extra);
line.append("\n");
line.append(extra);
}
#endif
table.push_back(parseLine(line, delimiter));
}
file.close();
return table;
}
std::ostream& CSV::operator<<(std::ostream& os, Row row)
{
for(int i=0; i<row.size(); i++)
{
os << row.at(i);
if (i < row.size()-1)
{
os << ",";
}
}
os << std::endl;
return os;
}
std::ostream& CSV::operator<<(std::ostream& os, Table table)
{
for(int i=0; i<table.size(); i++)
{
os << table.at(i);
}
return os;
}