22#include "parser/mmap.hpp"
24#include "parser/stream.hpp"
68#if CSV_EXCEPTIONS_ENABLED
71 #ifndef DOXYGEN_SHOULD_SKIP_THIS
73 using difference_type = std::ptrdiff_t;
75 using reference =
CSVRow & ;
76 using iterator_category = std::input_iterator_tag;
84 CONSTEXPR_14 reference
operator*() {
return this->row; }
85 CONSTEXPR_14 reference
operator*()
const {
return const_cast<reference
>(this->row); }
88 CONSTEXPR_14 pointer
operator->() {
return &(this->row); }
89 CONSTEXPR_14 pointer
operator->()
const {
return const_cast<pointer
>(&(this->row)); }
98 return (this->daddy == other.daddy) && (this->i == other.i);
129 static std::unique_ptr<CSVReader>
try_open(
133 template<
typename TStream,
134 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
135 static std::unique_ptr<CSVReader>
try_open(
136 TStream& source, CSVError& error,
139 std::unique_ptr<CSVReader> reader(
new CSVReader(format, initialize_tag{}));
140 reader->init_from_stream(source, format);
141 error = reader->error_;
142 return error ? nullptr : std::move(reader);
145 static std::unique_ptr<CSVReader>
try_open(
146 std::unique_ptr<std::istream> source, CSVError& error,
149#ifdef CSV_HAS_STD_EXPECTED
150 template<
typename Source>
151 static std::expected<std::unique_ptr<CSVReader>, CSVError>
try_open(
154 auto reader =
try_open(std::forward<Source>(source), error, format);
155 if (error)
return std::unexpected(std::move(error));
160#if CSV_EXCEPTIONS_ENABLED
162 : CSVReader(format, initialize_tag{}) {
163 open_file(filename, format);
164 internals::throw_error(error_);
179 template<
typename TStream,
180 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
183 init_from_stream(source, format);
184 internals::throw_error(error_);
197 throw std::invalid_argument(internals::ERROR_READER_NULL_STREAM);
199 internals::throw_error(error_);
216 read_scheduler_(other._format.is_threading_enabled()) {
217 other.read_scheduler_.join();
218 this->move_state_from(other);
226 if (
this == &other) {
230 this->read_scheduler_.join();
231 other.read_scheduler_.join();
232 this->move_state_from(other);
238 this->read_scheduler_.join();
261#if CSV_EXCEPTIONS_ENABLED
281#if CSV_EXCEPTIONS_ENABLED
311#if CSV_EXCEPTIONS_ENABLED
312 bool read_chunk(std::vector<CSVRow>& out,
size_t max_rows);
314 CSV_CONST iterator
end() const noexcept;
319 bool try_read_chunk(std::vector<CSVRow>& out,
size_t max_rows, CSVError& error);
334 static const std::vector<std::string> empty_col_names;
345 return this->
col_names->index_of(col_name);
367 return this->
parser ? this->
parser->speculative_diagnostics()
373 return this->
parser ? this->
parser->parse_worker_count() : 1;
396 internals::ColNamesPtr
col_names = std::make_shared<internals::ColNames>();
399 std::unique_ptr<internals::parser::CSVParserDriverBase>
parser =
nullptr;
419 bool read_csv(
size_t bytes = internals::CSV_CHUNK_SIZE_DEFAULT);
425 struct initialize_tag {};
427 : _format(format), read_scheduler_(format.is_threading_enabled()) {}
432 bool header_trimmed =
false;
435 int header_rows_trimmed_ = 0;
439 size_t _chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
440 bool _read_requested =
false;
441 internals::CSVReadScheduler read_scheduler_;
444 void move_state_from(CSVReader& other)
noexcept {
445 this->_format = std::move(other._format);
446 this->error_ = std::move(other.error_);
447 this->col_names = std::move(other.col_names);
448 this->parser = std::move(other.parser);
449 this->
records = std::move(other.records);
450 this->owned_stream = std::move(other.owned_stream);
451 this->n_cols = other.n_cols;
452 this->_n_rows = other._n_rows;
453 this->header_trimmed = other.header_trimmed;
454 this->header_rows_trimmed_ = other.header_rows_trimmed_;
455 this->_chunk_size = other._chunk_size;
456 this->_read_requested = other._read_requested;
457 this->read_scheduler_.set_threading_enabled(this->_format.is_threading_enabled());
458 this->read_scheduler_.adopt_exception(other.read_scheduler_.take_exception());
459 other.reset_after_move();
462 void reset_after_move() noexcept {
465 this->header_trimmed =
false;
466 this->header_rows_trimmed_ = 0;
467 this->_read_requested =
false;
468 this->_chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
479 void init_parser(std::unique_ptr<internals::parser::CSVParserDriverBase>
parser);
481 template<
typename TStream,
482 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
483 void init_from_stream(TStream& source,
const CSVFormat& format) {
485 std::unique_ptr<internals::parser::CSVParserDriverBase>(
486 new internals::parser::StreamParser<TStream>(source, format, this->col_names)
505 bool row_is_returnable(
const CSVRow& candidate);
508 bool accept_row(CSVRow&& candidate, CSVRow* single_row, std::vector<CSVRow>* batch_rows);
516 bool check_for_rows();
523 void drain_rows_into_chunk(std::vector<CSVRow>& out,
size_t max_rows);
527 #if defined(CSV_HAS_CXX20) && CSV_EXCEPTIONS_ENABLED
529 internals::csv_write_rows_input_range<CSVReader>,
530 "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 no data rows have been successfully returned yet.
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.
bool has_next_row()
Check whether a logical next row is available without consuming it.
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.
CSVReader & operator=(CSVReader &&other) noexcept
Move assignment.
bool try_has_next_row(CSVError &error)
Check for a logical next row, distinguishing exhaustion from failure.
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.
internals::ThreadSafeDeque< CSVRow > RowCollection
Standard type for storing collection of rows.
std::string_view string_view
The string_view class used by this library.
Runtime read scheduling for CSVReader.