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 this->col_names_explicitly_set_ = true;
40 return *this;
41 }
42
44 if (row < 0) this->variable_column_policy = VariableColumnPolicy::KEEP;
45
46 this->header = row;
47 this->header_explicitly_set_ = true;
48 this->col_names = {};
49 this->col_names_explicitly_set_ = false;
50 return *this;
51 }
52
54 if (size < internals::CSV_CHUNK_SIZE_FLOOR) {
55 throw std::invalid_argument(
56 "Chunk size must be at least " +
57 std::to_string(internals::CSV_CHUNK_SIZE_FLOOR) +
58 " bytes (500KB). Provided: " + std::to_string(size)
59 );
60 }
61 this->_chunk_size = size;
62 return *this;
63 }
64
65 CSV_INLINE void CSVFormat::assert_no_char_overlap()
66 {
67 const std::set<char> delims(this->possible_delimiters.begin(), this->possible_delimiters.end());
68 const std::set<char> trims(this->trim_chars.begin(), this->trim_chars.end());
69 std::set<char> offenders;
70
71 for (std::set<char>::const_iterator it = delims.begin(); it != delims.end(); ++it) {
72 if (trims.find(*it) != trims.end()) {
73 offenders.insert(*it);
74 }
75 }
76
77 // Make sure quote character is not contained in possible delimiters
78 // or whitespace characters.
79 if (delims.find(this->quote_char) != delims.end() ||
80 trims.find(this->quote_char) != trims.end()) {
81 offenders.insert(this->quote_char);
82 }
83
84 if (!offenders.empty()) {
85 std::string err_msg = "There should be no overlap between the quote character, "
86 "the set of possible delimiters "
87 "and the set of whitespace characters. Offending characters: ";
88
89 // Create a pretty error message with the list of overlapping
90 // characters
91 size_t i = 0;
92 for (std::set<char>::const_iterator it = offenders.begin(); it != offenders.end(); ++it, ++i) {
93 err_msg += "'";
94 err_msg += *it;
95 err_msg += "'";
96
97 if (i + 1 < offenders.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.
The all encompassing namespace.