18#if !defined(CSV_ENABLE_THREADS) || CSV_ENABLE_THREADS
31 inline void join_worker(std::thread& worker) {
32 if (worker.joinable()) worker.join();
35 #define JOIN_WORKER(worker) join_worker(worker)
37 #define JOIN_WORKER(worker) ((void)0)
81 #ifndef DOXYGEN_SHOULD_SKIP_THIS
83 using difference_type = std::ptrdiff_t;
85 using reference =
CSVRow & ;
86 using iterator_category = std::input_iterator_tag;
94 CONSTEXPR_14 reference
operator*() {
return this->row; }
95 CONSTEXPR_14 reference
operator*()
const {
return const_cast<reference
>(this->row); }
98 CONSTEXPR_14 pointer
operator->() {
return &(this->row); }
99 CONSTEXPR_14 pointer
operator->()
const {
return const_cast<pointer
>(&(this->row)); }
108 return (this->daddy == other.daddy) && (this->i == other.i);
135#if defined(__EMSCRIPTEN__)
137 new std::ifstream(std::string(filename), std::ios::binary)
141 throw std::runtime_error(
"Cannot open file " + std::string(filename));
149 CSV_MSVC_PUSH_DISABLE(4316)
150 this->init_parser(std::unique_ptr<internals::IBasicCSVParser>(
169 template<
typename TStream,
170 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
172 this->init_from_stream(source, format);
183 throw std::invalid_argument(
"CSVReader requires a non-null stream");
202 _format(std::move(other._format)),
204 parser(std::move(other.parser)),
205 records(std::move(other.records)),
209 header_trimmed(other.header_trimmed),
210 _chunk_size(other._chunk_size),
211 _read_requested(other._read_requested),
212 read_csv_exception(other.take_read_csv_exception()) {
213 JOIN_WORKER(other.read_csv_worker);
217 other.header_trimmed =
false;
218 other._read_requested =
false;
219 other._chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
227 if (
this == &other) {
231 JOIN_WORKER(this->read_csv_worker);
232 JOIN_WORKER(other.read_csv_worker);
234 this->_format = std::move(other._format);
235 this->
col_names = std::move(other.col_names);
236 this->
parser = std::move(other.parser);
237 this->
records = std::move(other.records);
239 this->
n_cols = other.n_cols;
241 this->header_trimmed = other.header_trimmed;
242 this->_chunk_size = other._chunk_size;
243 this->_read_requested = other._read_requested;
244 this->read_csv_exception = other.take_read_csv_exception();
248 other.header_trimmed =
false;
249 other._read_requested =
false;
250 other._chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
256 JOIN_WORKER(this->read_csv_worker);
263 CSV_CONST iterator
end() const noexcept;
266 bool eof() const noexcept {
return this->
parser->eof(); }
312 internals::ColNamesPtr
col_names = std::make_shared<internals::ColNames>();
315 std::unique_ptr<internals::IBasicCSVParser>
parser =
nullptr;
332 bool read_csv(
size_t bytes = internals::CSV_CHUNK_SIZE_DEFAULT);
339 bool header_trimmed =
false;
342 #if CSV_ENABLE_THREADS
343 std::thread read_csv_worker;
345 size_t _chunk_size = internals::CSV_CHUNK_SIZE_DEFAULT;
346 bool _read_requested =
false;
350 std::exception_ptr read_csv_exception =
nullptr;
351#if CSV_ENABLE_THREADS
352 std::mutex read_csv_exception_lock;
355 void set_read_csv_exception(std::exception_ptr eptr) {
356#if CSV_ENABLE_THREADS
357 std::lock_guard<std::mutex> lock(this->read_csv_exception_lock);
359 this->read_csv_exception = std::move(eptr);
362 std::exception_ptr take_read_csv_exception() {
363#if CSV_ENABLE_THREADS
364 std::lock_guard<std::mutex> lock(this->read_csv_exception_lock);
366 auto eptr = this->read_csv_exception;
367 this->read_csv_exception =
nullptr;
371 void rethrow_read_csv_exception_if_any() {
372 if (
auto eptr = this->take_read_csv_exception()) {
373 std::rethrow_exception(eptr);
380 void init_parser(std::unique_ptr<internals::IBasicCSVParser>
parser);
382 template<
typename TStream,
383 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
384 void init_from_stream(TStream& source, CSVFormat format) {
388 CSV_MSVC_PUSH_DISABLE(4316)
390 std::unique_ptr<internals::IBasicCSVParser>(
391 new internals::StreamParser<TStream>(source, format, this->
col_names)
398 void initial_read() {
399#if CSV_ENABLE_THREADS
401 this->read_csv_worker.join();
405 this->rethrow_read_csv_exception_if_any();
Contains the main CSV parsing algorithm and various utility functions.
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 utf8_bom() const noexcept
Whether or not CSV was prefixed with a UTF-8 bom.
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.
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.
CSVReader(CSVReader &&other) noexcept
Move constructor.
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.
CSVReader(TStream &source, CSVFormat format=CSVFormat::guess_csv())
Construct CSVReader from std::istream.
CSVReader(const CSVReader &)=delete
Not copyable.
iterator begin()
Return an iterator to the first row in the reader.
CSVReader(csv::string_view filename, const CSVFormat &format=CSVFormat::guess_csv())
Construct CSVReader from filename.
Data structure for representing CSV rows.
Parser for memory-mapped files.
A standalone header file containing shared code.
#define CONSTEXPR
Expands to constexpr in decent compilers and inline otherwise.
Implements data type parsing functionality.
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::IBasicCSVParser > 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.
nonstd::string_view string_view
The string_view class used by this library.