9 std::unique_ptr<internals::IBasicCSVParser> parser_impl
11 auto resolved = parser_impl->get_resolved_format();
12 this->_format = resolved.format;
13 this->_chunk_size = this->_format.get_chunk_size();
14 this->
n_cols = resolved.n_cols;
16 if (!this->_format.col_names.empty()) {
20 this->
parser = std::move(parser_impl);
31 new_format.col_names = this->
col_names->get_col_names();
32 new_format.header = this->_format.header;
40 std::vector<std::string>();
47 return this->
col_names->index_of(col_name);
51 if (!this->header_trimmed) {
52 for (
int i = 0; i <= this->_format.header && !this->
records->empty(); i++) {
53 if (i == this->_format.header && this->col_names->empty()) {
61 this->header_trimmed =
true;
68 this->
col_names->set_policy(this->_format.get_column_name_policy());
70 this->
n_cols = names.size();
101 if (!this->header_trimmed) {
108 this->set_read_csv_exception(std::current_exception());
133#if CSV_ENABLE_THREADS
134 if (this->
records->is_waitable()) {
142 JOIN_WORKER(this->read_csv_worker);
145 this->rethrow_read_csv_exception_if_any();
157 if (this->_read_requested && this->
records->empty()) {
158 throw std::runtime_error(
159 "End of file not reached and no more records parsed. "
160 "This likely indicates a CSV row larger than the chunk size of " +
161 std::to_string(this->_chunk_size) +
" bytes. "
162 "Use CSVFormat::chunk_size() to increase the chunk size."
166#if CSV_ENABLE_THREADS
175 this->rethrow_read_csv_exception_if_any();
177 this->_read_requested =
true;
181 const auto policy = this->_format.variable_column_policy;
182 const size_t next_row_size = this->
records->front().size();
184 if (policy == VariableColumnPolicy::KEEP_NON_EMPTY && next_row_size == 0) {
189 if (next_row_size != this->
n_cols &&
190 (policy == VariableColumnPolicy::THROW || policy == VariableColumnPolicy::IGNORE_ROW)) {
191 auto errored_row = this->
records->pop_front();
193 if (policy == VariableColumnPolicy::THROW) {
194 if (errored_row.size() < this->n_cols)
195 throw std::runtime_error(
"Line too short " + std::string(errored_row.raw_str()));
197 throw std::runtime_error(
"Line too long " + std::string(errored_row.raw_str()));
203 row = this->
records->pop_front();
205 this->_read_requested =
false;
CSVFormat get_format() const
Return the format of the original raw CSV.
int index_of(csv::string_view col_name) const
Return the index of the column name if found or csv::CSV_NOT_FOUND otherwise.
bool read_row(CSVRow &row)
Retrieve rows as CSVRow objects, returning true if more rows are available.
std::vector< std::string > get_col_names() const
Return the CSV's column names as a vector of strings.
Data structure for representing CSV rows.
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Defines functionality needed for basic CSV parsing.
std::unique_ptr< RowCollection > records
Queue of parsed CSV rows.
size_t _n_rows
How many rows (minus header) have been read so far.
bool read_csv(size_t bytes=internals::CSV_CHUNK_SIZE_DEFAULT)
Read a chunk of CSV data.
internals::ColNamesPtr col_names
Pointer to a object containing column information.
void set_col_names(const std::vector< std::string > &)
Sets this reader's column names and associated data.
std::unique_ptr< internals::IBasicCSVParser > parser
Helper class which actually does the parsing.
size_t n_cols
The number of columns in this CSV.
The all encompassing namespace.
nonstd::string_view string_view
The string_view class used by this library.