|
3 | 3 | #include <iomanip>
|
4 | 4 | #include "CSVparser.hpp"
|
5 | 5 |
|
6 |
| -CSVparser::CSVparser(const std::string &file, char sep) |
7 |
| - : _sep(sep) |
8 |
| -{ |
9 |
| - std::ifstream ifile(file); |
10 |
| - std::string line; |
11 |
| - |
12 |
| - if (ifile.is_open()) |
| 6 | +namespace csv { |
| 7 | + |
| 8 | + Parser::Parser(const std::string &file, char sep) |
| 9 | + : _file(file), _sep(sep) |
| 10 | + { |
| 11 | + std::ifstream ifile(file.c_str()); |
| 12 | + std::string line; |
| 13 | + |
| 14 | + if (ifile.is_open()) |
| 15 | + { |
| 16 | + while (ifile.good()) |
| 17 | + { |
| 18 | + getline(ifile, line); |
| 19 | + if (line != "") |
| 20 | + _originalFile.push_back(line); |
| 21 | + } |
| 22 | + ifile.close(); |
| 23 | + |
| 24 | + if (_originalFile.size() == 0) |
| 25 | + throw Error(std::string("No Data in ").append(file)); |
| 26 | + |
| 27 | + parseHeader(); |
| 28 | + parseContent(); |
| 29 | + } |
| 30 | + else |
| 31 | + throw Error(std::string("Failed to open ").append(file)); |
| 32 | + } |
| 33 | + |
| 34 | + Parser::~Parser(void) |
| 35 | + { |
| 36 | + std::vector<Row *>::iterator it; |
| 37 | + |
| 38 | + for (it = _content.begin(); it != _content.end(); it++) |
| 39 | + delete *it; |
| 40 | + } |
| 41 | + |
| 42 | + void Parser::parseHeader(void) |
| 43 | + { |
| 44 | + std::stringstream ss(_originalFile[0]); |
| 45 | + std::string item; |
| 46 | + |
| 47 | + while (std::getline(ss, item, _sep)) |
| 48 | + _header.push_back(item); |
| 49 | + } |
| 50 | + |
| 51 | + void Parser::parseContent(void) |
| 52 | + { |
| 53 | + std::vector<std::string>::iterator it; |
| 54 | + |
| 55 | + it = _originalFile.begin(); |
| 56 | + it++; // skip header |
| 57 | + |
| 58 | + for (; it != _originalFile.end(); it++) |
| 59 | + { |
| 60 | + bool quoted = false; |
| 61 | + int tokenStart = 0; |
| 62 | + int i = 0; |
| 63 | + |
| 64 | + Row *row = new Row(_header); |
| 65 | + |
| 66 | + for (; i != it->length(); i++) |
| 67 | + { |
| 68 | + if (it->at(i) == '"') |
| 69 | + quoted = ((quoted) ? (false) : (true)); |
| 70 | + else if (it->at(i) == ',' && !quoted) |
| 71 | + { |
| 72 | + row->push(it->substr(tokenStart, i - tokenStart)); |
| 73 | + tokenStart = i + 1; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + //end |
| 78 | + row->push(it->substr(tokenStart, it->length() - tokenStart)); |
| 79 | + |
| 80 | + // if value(s) missing |
| 81 | + if (row->size() != _header.size()) |
| 82 | + throw Error("corrupted data !"); |
| 83 | + _content.push_back(row); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Row &Parser::getRow(unsigned int rowPosition) const |
| 88 | + { |
| 89 | + if (rowPosition >= 0 && rowPosition < _content.size()) |
| 90 | + return *(_content[rowPosition]); |
| 91 | + throw Error("can't return this row (doesn't exist)"); |
| 92 | + } |
| 93 | + |
| 94 | + Row &Parser::operator[](unsigned int rowPosition) const |
| 95 | + { |
| 96 | + return Parser::getRow(rowPosition); |
| 97 | + } |
| 98 | + |
| 99 | + unsigned int Parser::rowCount(void) const |
| 100 | + { |
| 101 | + return _content.size(); |
| 102 | + } |
| 103 | + |
| 104 | + unsigned int Parser::columnCount(void) const |
| 105 | + { |
| 106 | + return _header.size(); |
| 107 | + } |
| 108 | + |
| 109 | + std::vector<std::string> Parser::getHeader(void) const |
| 110 | + { |
| 111 | + return _header; |
| 112 | + } |
| 113 | + |
| 114 | + const std::string Parser::getHeaderElement(unsigned int pos) const |
| 115 | + { |
| 116 | + if (pos >= _header.size()) |
| 117 | + throw Error("can't return this header (doesn't exist)"); |
| 118 | + return _header[pos]; |
| 119 | + } |
| 120 | + |
| 121 | + bool Parser::deleteRow(unsigned int pos) |
| 122 | + { |
| 123 | + if (pos >= 0 && pos < _content.size()) |
13 | 124 | {
|
14 |
| - while (ifile.good()) |
15 |
| - { |
16 |
| - getline(ifile, line); |
17 |
| - if (line != "") |
18 |
| - _originalFile.push_back(line); |
19 |
| - } |
20 |
| - ifile.close(); |
21 |
| - parseHeader(); |
22 |
| - parseContent(); |
| 125 | + delete *(_content.begin() + pos); |
| 126 | + _content.erase(_content.begin() + pos); |
| 127 | + return true; |
23 | 128 | }
|
24 |
| - else |
25 |
| - throw CSVError(std::string("Failed to open ").append(file)); |
26 |
| -} |
27 |
| - |
28 |
| -CSVparser::~CSVparser(void) |
29 |
| -{ |
30 |
| - std::vector<CSVrow *>::iterator it; |
| 129 | + return false; |
| 130 | + } |
31 | 131 |
|
32 |
| - for (it = _content.begin(); it != _content.end(); it++) |
33 |
| - delete *it; |
34 |
| -} |
| 132 | + bool Parser::addRow(unsigned int pos, const std::vector<std::string> &r) |
| 133 | + { |
| 134 | + Row *row = new Row(_header); |
35 | 135 |
|
36 |
| -void CSVparser::parseHeader(void) |
37 |
| -{ |
38 |
| - std::stringstream ss(_originalFile[0]); |
39 |
| - std::string item; |
| 136 | + for (auto it = r.begin(); it != r.end(); it++) |
| 137 | + row->push(*it); |
| 138 | + |
| 139 | + if (pos >= 0 && pos <= _content.size()) |
| 140 | + { |
| 141 | + _content.insert(_content.begin() + pos, row); |
| 142 | + return true; |
| 143 | + } |
| 144 | + return false; |
| 145 | + } |
40 | 146 |
|
41 |
| - while (std::getline(ss, item, _sep)) |
42 |
| - _header.push_back(item); |
43 |
| -} |
| 147 | + void Parser::sync(void) const |
| 148 | + { |
| 149 | + std::ofstream f; |
| 150 | + f.open(_file, std::ios::out | std::ios::trunc); |
44 | 151 |
|
45 |
| -void CSVparser::parseContent(void) |
46 |
| -{ |
47 |
| - std::vector<const std::string>::iterator it; |
| 152 | + // header |
| 153 | + int i = 0; |
| 154 | + for (auto it = _header.begin(); it != _header.end(); it++) |
| 155 | + { |
| 156 | + f << *it; |
| 157 | + if (i < _header.size() - 1) |
| 158 | + f << ","; |
| 159 | + else |
| 160 | + f << std::endl; |
| 161 | + i++; |
| 162 | + } |
48 | 163 |
|
49 |
| - it = _originalFile.begin(); |
50 |
| - it++; // skip header |
51 |
| - |
52 |
| - for (; it != _originalFile.end(); it++) |
53 |
| - { |
54 |
| - bool quoted = false; |
55 |
| - int tokenStart = 0; |
56 |
| - int i = 0; |
57 |
| - |
58 |
| - CSVrow *row = new CSVrow(_header); |
59 |
| - |
60 |
| - for (; i != it->length(); i++) |
61 |
| - { |
62 |
| - if (it->at(i) == '"') |
63 |
| - quoted = ((quoted) ? (false) : (true)); |
64 |
| - else if (it->at(i) == ',' && !quoted) |
65 |
| - { |
66 |
| - row->push(it->substr(tokenStart, i - tokenStart)); |
67 |
| - tokenStart = i + 1; |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - //end |
72 |
| - row->push(it->substr(tokenStart, it->length() - tokenStart)); |
73 |
| - _content.push_back(row); |
74 |
| - } |
75 |
| -} |
76 |
| - |
77 |
| -CSVrow &CSVparser::getRow(unsigned int rowPosition) const |
78 |
| -{ |
79 |
| - if (rowPosition >= 0 && rowPosition < _content.size()) |
80 |
| - return *(_content[rowPosition]); |
81 |
| - throw CSVError("can't return this row (doesn't exist)"); |
82 |
| -} |
83 |
| - |
84 |
| -CSVrow &CSVparser::operator[](unsigned int rowPosition) const |
85 |
| -{ |
86 |
| - return CSVparser::getRow(rowPosition); |
87 |
| -} |
88 |
| - |
89 |
| -unsigned int CSVparser::rowCount(void) const |
90 |
| -{ |
91 |
| - return _content.size(); |
92 |
| -} |
93 |
| - |
94 |
| -unsigned int CSVparser::columnCount(void) const |
95 |
| -{ |
96 |
| - return _header.size(); |
97 |
| -} |
98 |
| - |
99 |
| -std::vector<const std::string> CSVparser::getHeader(void) const |
100 |
| -{ |
101 |
| - return _header; |
102 |
| -} |
103 |
| - |
104 |
| -const std::string CSVparser::getHeader(unsigned int pos) const |
105 |
| -{ |
106 |
| - if (pos >= _header.size()) |
107 |
| - throw CSVError("can't return this header (doesn't exist)"); |
108 |
| - return _header[pos]; |
109 |
| -} |
110 |
| - |
111 |
| -/* |
112 |
| -** ROW |
113 |
| -*/ |
114 |
| - |
115 |
| -CSVrow::CSVrow(const std::vector<const std::string> &header) |
116 |
| - : _header(header) {} |
117 |
| - |
118 |
| -CSVrow::~CSVrow(void) {} |
119 |
| - |
120 |
| -void CSVrow::push(const std::string &value) |
121 |
| -{ |
| 164 | + for (auto it = _content.begin(); it != _content.end(); it++) |
| 165 | + f << **it << std::endl; |
| 166 | + f.close(); |
| 167 | + } |
| 168 | + |
| 169 | + /* |
| 170 | + ** ROW |
| 171 | + */ |
| 172 | + |
| 173 | + Row::Row(const std::vector<std::string> &header) |
| 174 | + : _header(header) {} |
| 175 | + |
| 176 | + Row::~Row(void) {} |
| 177 | + |
| 178 | + int Row::size(void) const |
| 179 | + { |
| 180 | + return _values.size(); |
| 181 | + } |
| 182 | + |
| 183 | + void Row::push(const std::string &value) |
| 184 | + { |
122 | 185 | _values.push_back(value);
|
123 |
| -} |
| 186 | + } |
124 | 187 |
|
125 |
| -const std::string CSVrow::operator[](unsigned int valuePosition) const |
126 |
| -{ |
127 |
| - if (valuePosition >= 0 && valuePosition < _values.size()) |
128 |
| - return _values[valuePosition]; |
129 |
| - throw CSVError("can't return this value (doesn't exist)"); |
130 |
| -} |
131 |
| - |
132 |
| -const std::string CSVrow::operator[](const std::string &valueName) const |
133 |
| -{ |
134 |
| - std::vector<const std::string>::const_iterator it; |
| 188 | + bool Row::set(const std::string &key, const std::string &value) |
| 189 | + { |
| 190 | + std::vector<std::string>::const_iterator it; |
135 | 191 | int pos = 0;
|
136 | 192 |
|
137 | 193 | for (it = _header.begin(); it != _header.end(); it++)
|
138 | 194 | {
|
139 |
| - if (valueName == *it) |
140 |
| - return _values[pos]; |
| 195 | + if (key == *it) |
| 196 | + { |
| 197 | + _values[pos] = value; |
| 198 | + return true; |
| 199 | + } |
141 | 200 | pos++;
|
142 | 201 | }
|
143 |
| - |
144 |
| - throw CSVError("can't return this value (doesn't exist)"); |
145 |
| -} |
146 |
| - |
147 |
| -std::ostream &operator<<(std::ostream &os, const CSVrow &row) |
148 |
| -{ |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + const std::string Row::operator[](unsigned int valuePosition) const |
| 206 | + { |
| 207 | + if (valuePosition >= 0 && valuePosition < _values.size()) |
| 208 | + return _values[valuePosition]; |
| 209 | + throw Error("can't return this value (doesn't exist)"); |
| 210 | + } |
| 211 | + |
| 212 | + const std::string Row::operator[](const std::string &key) const |
| 213 | + { |
| 214 | + std::vector<std::string>::const_iterator it; |
| 215 | + int pos = 0; |
| 216 | + |
| 217 | + for (it = _header.begin(); it != _header.end(); it++) |
| 218 | + { |
| 219 | + if (key == *it) |
| 220 | + return _values[pos]; |
| 221 | + pos++; |
| 222 | + } |
| 223 | + |
| 224 | + throw Error("can't return this value (doesn't exist)"); |
| 225 | + } |
| 226 | + |
| 227 | + std::ostream &operator<<(std::ostream &os, const Row &row) |
| 228 | + { |
| 229 | + for (int i = 0; i != row._values.size(); i++) |
| 230 | + os << row._values[i] << " | "; |
| 231 | + |
| 232 | + return os; |
| 233 | + } |
| 234 | + |
| 235 | + std::ofstream &operator<<(std::ofstream &os, const Row &row) |
| 236 | + { |
149 | 237 | for (int i = 0; i != row._values.size(); i++)
|
150 |
| - os << row._values[i] << " | "; |
151 |
| - |
| 238 | + { |
| 239 | + os << row._values[i]; |
| 240 | + if (i < row._values.size() - 1) |
| 241 | + os << ","; |
| 242 | + } |
152 | 243 | return os;
|
| 244 | + } |
153 | 245 | }
|
0 commit comments