22#include "parser/mmap.hpp"
23#include "parser/scheduler.hpp"
24#include "parser/stream.hpp"
67#if CSV_EXCEPTIONS_ENABLED
70 #ifndef DOXYGEN_SHOULD_SKIP_THIS
72 using difference_type = std::ptrdiff_t;
74 using reference =
CSVRow & ;
75 using iterator_category = std::input_iterator_tag;
83 CONSTEXPR_14 reference
operator*() {
return this->row; }
84 CONSTEXPR_14 reference
operator*()
const {
return const_cast<reference
>(this->row); }
87 CONSTEXPR_14 pointer
operator->() {
return &(this->row); }
88 CONSTEXPR_14 pointer
operator->()
const {
return const_cast<pointer
>(&(this->row)); }
97 return (this->daddy == other.daddy) && (this->i == other.i);
128 static std::unique_ptr<CSVReader>
try_open(
132 template<
typename TStream,
133 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
134 static std::unique_ptr<CSVReader>
try_open(
135 TStream& source, CSVError& error,
138 std::unique_ptr<CSVReader> reader(
new CSVReader(format, initialize_tag{}));
139 reader->init_from_stream(source, format);
140 error = reader->error_;
141 return error ? nullptr : std::move(reader);
144 static std::unique_ptr<CSVReader>
try_open(
145 std::unique_ptr<std::istream> source, CSVError& error,
148#ifdef CSV_HAS_STD_EXPECTED
149 template<
typename Source>
150 static std::expected<std::unique_ptr<CSVReader>, CSVError>
try_open(
153 auto reader =
try_open(std::forward<Source>(source), error, format);
154 if (error)
return std::unexpected(std::move(error));
159#if CSV_EXCEPTIONS_ENABLED
161 : CSVReader(format, initialize_tag{}) {
162 open_file(filename, format);
163 internals::throw_error(error_);
178 template<
typename TStream,
179 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
182 init_from_stream(source, format);
183 internals::throw_error(error_);
196 throw std::invalid_argument(internals::ERROR_READER_NULL_STREAM);
198 internals::throw_error(error_);
215 read_scheduler_(other._format.is_threading_enabled()) {
216 other.read_scheduler_.join();
217 this->move_state_from(other);
225 if (
this == &other) {
229 this->read_scheduler_.join();
230 other.read_scheduler_.join();
231 this->move_state_from(other);
237 this->read_scheduler_.join();
260#if CSV_EXCEPTIONS_ENABLED
289#if CSV_EXCEPTIONS_ENABLED
290 bool read_chunk(std::vector<CSVRow>& out,
size_t max_rows);
292 CSV_CONST iterator
end() const noexcept;
297 bool try_read_chunk(std::vector<CSVRow>& out,
size_t max_rows, CSVError& error);
300 bool eof() const noexcept {
return this->
parser->eof(); }
314 static const std::vector<std::string> empty_col_names;
325 return this->
col_names->index_of(col_name);
347 return this->
parser ? this->
parser->speculative_diagnostics()
348 : internals::SpeculativeParseDiagnostics();
353 return this->
parser ? this->
parser->parse_worker_count() : 1;
376 internals::ColNamesPtr
col_names = std::make_shared<internals::ColNames>();
379 std::unique_ptr<internals::parser::CSVParserDriverBase>
parser =
nullptr;
382 std::unique_ptr<RowCollection>
records{
new RowCollection(100)};
399 bool read_csv(
size_t bytes = internals::CSV_CHUNK_SIZE_DEFAULT);
405 struct initialize_tag {};
407 : _format(format), read_scheduler_(format.is_threading_enabled()) {}
412 bool header_trimmed =
false;
415 int header_rows_trimmed_ = 0;
419 size_t _chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
420 bool _read_requested =
false;
421 internals::CSVReadScheduler read_scheduler_;
424 void move_state_from(CSVReader& other)
noexcept {
425 this->_format = std::move(other._format);
426 this->error_ = std::move(other.error_);
427 this->col_names = std::move(other.col_names);
428 this->parser = std::move(other.parser);
429 this->
records = std::move(other.records);
430 this->owned_stream = std::move(other.owned_stream);
431 this->n_cols = other.n_cols;
432 this->_n_rows = other._n_rows;
433 this->header_trimmed = other.header_trimmed;
434 this->header_rows_trimmed_ = other.header_rows_trimmed_;
435 this->_chunk_size = other._chunk_size;
436 this->_read_requested = other._read_requested;
437 this->read_scheduler_.set_threading_enabled(this->_format.is_threading_enabled());
438 this->read_scheduler_.adopt_exception(other.read_scheduler_.take_exception());
439 other.reset_after_move();
442 void reset_after_move() noexcept {
445 this->header_trimmed =
false;
446 this->header_rows_trimmed_ = 0;
447 this->_read_requested =
false;
448 this->_chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
459 void init_parser(std::unique_ptr<internals::parser::CSVParserDriverBase>
parser);
461 template<
typename TStream,
462 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
463 void init_from_stream(TStream& source,
const CSVFormat& format) {
465 std::unique_ptr<internals::parser::CSVParserDriverBase>(
466 new internals::parser::StreamParser<TStream>(source, format, this->col_names)
483 bool accept_row(CSVRow&& candidate, CSVRow* single_row, std::vector<CSVRow>* batch_rows);
491 bool check_for_rows();
498 void drain_rows_into_chunk(std::vector<CSVRow>& out,
size_t max_rows);
502 #if defined(CSV_HAS_CXX20) && CSV_EXCEPTIONS_ENABLED
504 internals::csv_write_rows_input_range<CSVReader>,
505 "CSVReader must remain compatible with csv::DelimWriter::write_rows()."
An input iterator capable of handling large files.
CONSTEXPR bool operator==(const iterator &other) const noexcept
Returns true if iterators were constructed from the same CSVReader and point to the same row.
iterator & operator++()
Pre-increment iterator.
CONSTEXPR_14 reference operator*()
Access the CSVRow held by the iterator.
CONSTEXPR_14 pointer operator->()
Return a pointer to the CSVRow the iterator has stopped at.
Main class for parsing CSVs from files and in-memory sources.
CONSTEXPR bool empty() const noexcept
Whether or not the file or stream contains valid CSV rows, not including the header.
bool read_chunk(std::vector< CSVRow > &out, size_t max_rows)
Read up to max_rows rows into a caller-owned batch buffer.
bool utf8_bom() const noexcept
Whether or not CSV was prefixed with a UTF-8 bom.
bool try_read_chunk(std::vector< CSVRow > &out, size_t max_rows, CSVError &error)
Clear out and read a batch.
internals::SpeculativeParseDiagnostics speculative_diagnostics() const noexcept
Return speculative-parsing counters for filename-backed readers.
CSVFormat get_format() const
Return the resolved parsing format for this CSV source.
internals::ConstColNamesPtr col_names_ptr() const noexcept
Internal accessor for preserving resolved column-name lookup policy across helper types.
int index_of(csv::string_view col_name) const
Return the index of col_name, or csv::CSV_NOT_FOUND if absent.
size_t parse_worker_count() const noexcept
Return the number of parser worker threads used by the active parser.
const std::vector< std::string > & get_col_names() const
Return the active column names in CSV order.
CSVReader & operator=(const CSVReader &)=delete
Not copyable.
CSV_CONST iterator end() const noexcept
A placeholder for the imaginary past-the-end row in a CSV.
CONSTEXPR size_t n_rows() const noexcept
Retrieves the number of rows that have been read so far.
CSVReader(std::unique_ptr< std::istream > source, const CSVFormat &format=CSVFormat::guess_csv())
Construct CSVReader from an owned std::istream.
bool eof() const noexcept
Returns true if we have reached end of file.
CSVReader & operator=(CSVReader &&other) noexcept
Move assignment.
static std::unique_ptr< CSVReader > try_open(csv::string_view filename, CSVError &error, const CSVFormat &format=CSVFormat::guess_csv())
Construct CSVReader from filename.
CSVReader(CSVReader &&other) noexcept
Move constructor.
bool read_row(CSVRow &row)
Retrieve the next CSV row, returning true while more rows are available.
CSVReader(TStream &source, const CSVFormat &format=CSVFormat::guess_csv())
Construct CSVReader from std::istream.
bool try_read_row(CSVRow &row, CSVError &error)
Retrieve a row, distinguishing EOF (false, no error) from failure.
CSVReader(const CSVReader &)=delete
Not copyable.
iterator begin()
Return an iterator to the first row in the reader.
Data structure for representing CSV rows.
A standalone header file containing shared code.
#define CONSTEXPR
Expands to constexpr in decent compilers and inline otherwise.
Shared exception message templates and throw helpers.
CSV scalar type classification adapter.
std::unique_ptr< std::istream > owned_stream
Optional owned stream used by two paths: 1) Emscripten filename-constructor fallback to stream parsin...
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::parser::CSVParserDriverBase > parser
Helper class which actually does the parsing.
size_t n_cols
The number of columns in this CSV.
The all encompassing namespace.
std::string_view string_view
The string_view class used by this library.