Vince's CSV Parser
Loading...
Searching...
No Matches
csv_format.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <iterator>
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11#include "common.hpp"
12
13namespace csv {
14 namespace internals {
15 class IBasicCSVParser;
16 }
17
18 class CSVReader;
19
22 THROW = -1,
23 IGNORE_ROW = 0,
24 KEEP = 1
25 };
26
29 char delim;
30 int header_row;
31 };
32
36 class CSVFormat {
37 public:
39 CSVFormat() = default;
40
45 CSVFormat& delimiter(char delim);
46
52 CSVFormat& delimiter(const std::vector<char> & delim);
53
59 CSVFormat& trim(const std::vector<char> & ws);
60
65 CSVFormat& quote(char quote);
66
71 CSVFormat& column_names(const std::vector<std::string>& names);
72
77 CSVFormat& header_row(int row);
78
85 this->header_row(-1);
86 return *this;
87 }
88
90 CSVFormat& quote(bool use_quote) {
91 this->no_quote = !use_quote;
92 return *this;
93 }
94
96 CONSTEXPR_14 CSVFormat& variable_columns(VariableColumnPolicy policy = VariableColumnPolicy::IGNORE_ROW) {
97 this->variable_column_policy = policy;
98 return *this;
99 }
100
102 CONSTEXPR_14 CSVFormat& variable_columns(bool policy) {
103 this->variable_column_policy = (VariableColumnPolicy)policy;
104 return *this;
105 }
106
116 CSVFormat& chunk_size(size_t size);
117
118 #ifndef DOXYGEN_SHOULD_SKIP_THIS
119 char get_delim() const {
120 // This error should never be received by end users.
121 if (this->possible_delimiters.size() > 1) {
122 throw std::runtime_error("There is more than one possible delimiter.");
123 }
124
125 return this->possible_delimiters.at(0);
126 }
127
128 CONSTEXPR bool is_quoting_enabled() const { return !this->no_quote; }
129 CONSTEXPR char get_quote_char() const { return this->quote_char; }
130 CONSTEXPR int get_header() const { return this->header; }
131 std::vector<char> get_possible_delims() const { return this->possible_delimiters; }
132 std::vector<char> get_trim_chars() const { return this->trim_chars; }
133 CONSTEXPR VariableColumnPolicy get_variable_column_policy() const { return this->variable_column_policy; }
134 CONSTEXPR size_t get_chunk_size() const { return this->_chunk_size; }
135 #endif
136
139 CSVFormat format;
140 format.delimiter({ ',', '|', '\t', ';', '^' })
141 .quote('"')
142 .header_row(0);
143
144 return format;
145 }
146
147 bool guess_delim() {
148 return this->possible_delimiters.size() > 1;
149 }
150
151 friend CSVReader;
152 friend internals::IBasicCSVParser;
153
154 private:
156 void assert_no_char_overlap();
157
159 std::vector<char> possible_delimiters = { ',' };
160
162 std::vector<char> trim_chars = {};
163
165 int header = 0;
166
168 bool no_quote = false;
169
171 char quote_char = '"';
172
174 std::vector<std::string> col_names = {};
175
177 VariableColumnPolicy variable_column_policy = VariableColumnPolicy::IGNORE_ROW;
178
180 size_t _chunk_size = internals::ITERATION_CHUNK_SIZE;
181 };
182}
Stores information about how to parse a CSV file.
CSVFormat & column_names(const std::vector< std::string > &names)
Sets the column names.
CONSTEXPR_14 CSVFormat & variable_columns(VariableColumnPolicy policy=VariableColumnPolicy::IGNORE_ROW)
Tells the parser how to handle columns of a different length than the others.
CSVFormat()=default
Settings for parsing a RFC 4180 CSV file.
static CSVFormat guess_csv()
CSVFormat for guessing the delimiter.
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 & quote(bool use_quote)
Turn quoting on or off.
CONSTEXPR_14 CSVFormat & variable_columns(bool policy)
Tells the parser how to handle columns of a different length than the others.
CSVFormat & no_header()
Tells the parser that this CSV has no header row.
CSVFormat & header_row(int row)
Sets the header row.
CSVFormat & quote(char quote)
Sets the quote character.
A standalone header file containing shared code.
#define CONSTEXPR
Expands to constexpr in decent compilers and inline otherwise.
Definition common.hpp:149
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Definition common.hpp:26
constexpr size_t ITERATION_CHUNK_SIZE
Chunk size for lazy-loading large CSV files.
Definition common.hpp:190
The all encompassing namespace.
VariableColumnPolicy
Determines how to handle rows that are shorter or longer than the majority.
Stores the inferred format of a CSV file.