|
5 | 5 | #include "csv_reader.hpp" |
6 | 6 |
|
7 | 7 | namespace csv { |
8 | | - namespace internals { |
9 | | - CSV_INLINE std::string format_row(const std::vector<std::string>& row, csv::string_view delim) { |
10 | | - /** Print a CSV row */ |
11 | | - std::stringstream ret; |
12 | | - for (size_t i = 0; i < row.size(); i++) { |
13 | | - ret << row[i]; |
14 | | - if (i + 1 < row.size()) ret << delim; |
15 | | - else ret << '\n'; |
16 | | - } |
17 | | - ret.flush(); |
18 | | - |
19 | | - return ret.str(); |
20 | | - } |
21 | | - |
22 | | - /** Return the selected header row from a parsed head buffer. */ |
23 | | - CSV_INLINE std::vector<std::string> _get_col_names(csv::string_view head, CSVFormat format) { |
24 | | - // Parse the CSV |
25 | | - auto trim_chars = format.get_trim_chars(); |
26 | | - std::stringstream source(head.data()); |
27 | | - RowCollection rows; |
28 | | - |
29 | | - StreamParser<std::stringstream> parser(source, format); |
30 | | - parser.set_output(rows); |
31 | | - parser.next(); |
32 | | - |
33 | | - return CSVRow(std::move(rows[format.get_header()])); |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - /** Return a CSV's column names. */ |
38 | | - CSV_INLINE std::vector<std::string> get_col_names(csv::string_view filename, CSVFormat format) { |
39 | | - auto head = internals::get_csv_head(filename); |
40 | | - |
41 | | - /** Guess delimiter and header row */ |
42 | | - if (format.guess_delim()) { |
43 | | - auto guess_result = guess_format(filename, format.get_possible_delims()); |
44 | | - format.delimiter(guess_result.delim).header_row(guess_result.header_row); |
45 | | - } |
46 | | - |
47 | | - return internals::_get_col_names(head, format); |
48 | | - } |
49 | | - |
50 | 8 | /** Reads an arbitrarily large CSV file using memory-mapped IO. |
51 | 9 | * |
52 | 10 | * **Details:** Reads the first block of a CSV file synchronously to get information |
@@ -104,11 +62,8 @@ namespace csv { |
104 | 62 |
|
105 | 63 | /** Return the CSV's column names as a vector of strings. */ |
106 | 64 | CSV_INLINE std::vector<std::string> CSVReader::get_col_names() const { |
107 | | - if (this->col_names) { |
108 | | - return this->col_names->get_col_names(); |
109 | | - } |
110 | | - |
111 | | - return std::vector<std::string>(); |
| 65 | + return (this->col_names) ? this->col_names->get_col_names() : |
| 66 | + std::vector<std::string>(); |
112 | 67 | } |
113 | 68 |
|
114 | 69 | /** Return the index of the column name if found or |
@@ -263,9 +218,9 @@ namespace csv { |
263 | 218 |
|
264 | 219 | if (policy == VariableColumnPolicy::THROW) { |
265 | 220 | if (errored_row.size() < this->n_cols) |
266 | | - throw std::runtime_error("Line too short " + internals::format_row(errored_row)); |
| 221 | + throw std::runtime_error("Line too short " + std::string(errored_row.raw_str())); |
267 | 222 |
|
268 | | - throw std::runtime_error("Line too long " + internals::format_row(errored_row)); |
| 223 | + throw std::runtime_error("Line too long " + std::string(errored_row.raw_str())); |
269 | 224 | } |
270 | 225 |
|
271 | 226 | continue; |
|
0 commit comments