Vince's CSV Parser
Loading...
Searching...
No Matches
csv_format.cpp
Go to the documentation of this file.
1
5#include <algorithm>
6#include <set>
7
8#include "csv_format.hpp"
9
10namespace csv {
12 this->possible_delimiters = { delim };
13 this->assert_no_char_overlap();
14 return *this;
15 }
16
17 CSV_INLINE CSVFormat& CSVFormat::delimiter(const std::vector<char> & delim) {
18 this->possible_delimiters = delim;
19 this->assert_no_char_overlap();
20 return *this;
21 }
22
24 this->no_quote = false;
25 this->quote_char = quote;
26 this->assert_no_char_overlap();
27 return *this;
28 }
29
30 CSV_INLINE CSVFormat& CSVFormat::trim(const std::vector<char> & chars) {
31 this->trim_chars = chars;
32 this->assert_no_char_overlap();
33 return *this;
34 }
35
36 CSV_INLINE CSVFormat& CSVFormat::column_names(const std::vector<std::string>& names) {
37 this->col_names = names;
38 this->header = -1;
39 return *this;
40 }
41
43 if (row < 0) this->variable_column_policy = VariableColumnPolicy::KEEP;
44
45 this->header = row;
46 this->col_names = {};
47 return *this;
48 }
49
52 throw std::invalid_argument(
53 "Chunk size must be at least " +
54 std::to_string(internals::ITERATION_CHUNK_SIZE) +
55 " bytes (10MB). Provided: " + std::to_string(size)
56 );
57 }
58 this->_chunk_size = size;
59 return *this;
60 }
61
62 CSV_INLINE void CSVFormat::assert_no_char_overlap()
63 {
64 auto delims = std::set<char>(
65 this->possible_delimiters.begin(), this->possible_delimiters.end()),
66 trims = std::set<char>(
67 this->trim_chars.begin(), this->trim_chars.end());
68
69 // Stores intersection of possible delimiters and trim characters
70 std::vector<char> intersection = {};
71
72 // Find which characters overlap, if any
73 std::set_intersection(
74 delims.begin(), delims.end(),
75 trims.begin(), trims.end(),
76 std::back_inserter(intersection));
77
78 // Make sure quote character is not contained in possible delimiters
79 // or whitespace characters
80 if (delims.find(this->quote_char) != delims.end() ||
81 trims.find(this->quote_char) != trims.end()) {
82 intersection.push_back(this->quote_char);
83 }
84
85 if (!intersection.empty()) {
86 std::string err_msg = "There should be no overlap between the quote character, "
87 "the set of possible delimiters "
88 "and the set of whitespace characters. Offending characters: ";
89
90 // Create a pretty error message with the list of overlapping
91 // characters
92 for (size_t i = 0; i < intersection.size(); i++) {
93 err_msg += "'";
94 err_msg += intersection[i];
95 err_msg += "'";
96
97 if (i + 1 < intersection.size())
98 err_msg += ", ";
99 }
100
101 throw std::runtime_error(err_msg + '.');
102 }
103 }
104}
Stores information about how to parse a CSV file.
CSVFormat & column_names(const std::vector< std::string > &names)
Sets the column names.
CSVFormat & chunk_size(size_t size)
Sets the chunk size used when reading the CSV.
CSVFormat & trim(const std::vector< char > &ws)
Sets the whitespace characters to be trimmed.
CSVFormat & delimiter(char delim)
Sets the delimiter of the CSV file.
CSVFormat & header_row(int row)
Sets the header row.
CSVFormat & quote(char quote)
Sets the quote character.
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Definition common.hpp:26
Defines an object used to store CSV format settings.
constexpr size_t ITERATION_CHUNK_SIZE
Chunk size for lazy-loading large CSV files.
Definition common.hpp:190
The all encompassing namespace.