Vince's CSV Parser
Loading...
Searching...
No Matches
csv_format.cpp
Go to the documentation of this file.
1
5#include <algorithm>
6#include <cstdint>
7#include <limits>
8#include <set>
9
10#include "csv_format.hpp"
11
12namespace csv {
14 this->possible_delimiters = { delim };
15 this->assert_no_char_overlap();
16 return *this;
17 }
18
19 CSV_INLINE CSVFormat& CSVFormat::delimiter(const std::vector<char> & delim) {
20 this->possible_delimiters = delim;
21 this->assert_no_char_overlap();
22 return *this;
23 }
24
26 this->no_quote = false;
27 this->quote_char = quote;
28 this->assert_no_char_overlap();
29 return *this;
30 }
31
32 CSV_INLINE CSVFormat& CSVFormat::trim(const std::vector<char> & chars) {
33 this->trim_chars = chars;
34 this->assert_no_char_overlap();
35 return *this;
36 }
37
38 CSV_INLINE CSVFormat& CSVFormat::column_names(const std::vector<std::string>& names) {
39 this->col_names = names;
40 this->header = -1;
41 this->col_names_explicitly_set_ = true;
42 return *this;
43 }
44
46 if (row < 0) this->variable_column_policy = VariableColumnPolicy::KEEP;
47
48 this->header = row;
49 this->header_explicitly_set_ = true;
50 this->col_names = {};
51 this->col_names_explicitly_set_ = false;
52 return *this;
53 }
54
56 if (size < internals::CSV_CHUNK_SIZE_FLOOR) {
57 throw std::invalid_argument(internals::make_chunk_size_error(internals::CSV_CHUNK_SIZE_FLOOR, size));
58 }
59 const size_t max_chunk_size = (std::numeric_limits<std::uint32_t>::max)();
60 if (size > max_chunk_size) {
61 throw std::invalid_argument(internals::make_chunk_size_ceiling_error(max_chunk_size, size));
62 }
63 this->_chunk_size = size;
64 return *this;
65 }
66
67 CSV_INLINE void CSVFormat::assert_no_char_overlap()
68 {
69 const std::set<char> delims(this->possible_delimiters.begin(), this->possible_delimiters.end());
70 const std::set<char> trims(this->trim_chars.begin(), this->trim_chars.end());
71 std::set<char> offenders;
72
73 for (std::set<char>::const_iterator it = delims.begin(); it != delims.end(); ++it) {
74 if (trims.find(*it) != trims.end()) {
75 offenders.insert(*it);
76 }
77 }
78
79 // Make sure quote character is not contained in possible delimiters
80 // or whitespace characters.
81 if (delims.find(this->quote_char) != delims.end() ||
82 trims.find(this->quote_char) != trims.end()) {
83 offenders.insert(this->quote_char);
84 }
85
86 if (!offenders.empty()) {
87 throw std::runtime_error(internals::make_char_overlap_error(offenders));
88 }
89 }
90}
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:31
Defines an object used to store CSV format settings.
The all encompassing namespace.