18 #include "../external/mio.hpp"
48 const std::vector<char>& delims = {
',',
'|',
'\t',
';',
'^',
'~' });
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; }
87 CONSTEXPR_14 pointer
operator->() {
return &(this->row); }
96 return (this->daddy == other.daddy) && (this->i == other.i);
118 template<
typename TStream,
119 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
123 if (!format.col_names.empty())
126 this->
parser = std::unique_ptr<Parser>(
128 this->initial_read();
137 if (this->read_csv_worker.joinable()) {
138 this->read_csv_worker.join();
146 HEDLEY_CONST iterator
end() const noexcept;
149 bool eof() const noexcept {
return this->
parser->eof(); };
195 internals::ColNamesPtr
col_names = std::make_shared<internals::ColNames>();
198 std::unique_ptr<internals::IBasicCSVParser>
parser =
nullptr;
215 bool header_trimmed =
false;
219 std::thread read_csv_worker;
223 void initial_read() {
225 this->read_csv_worker.join();
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.
HEDLEY_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.
bool eof() const noexcept
Returns true if we have reached end of file.
CSVReader(TStream &source, CSVFormat format=CSVFormat())
Allows parsing stream sources such as std::stringstream or std::ifstream
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.
iterator begin()
Return an iterator to the first row in the reader.
CSVReader(csv::string_view filename, CSVFormat format=CSVFormat::guess_csv())
Reads an arbitrarily large CSV file using memory-mapped IO.
Data structure for representing CSV rows.
A class for parsing CSV data from a std::stringstream or an std::ifstream
A standalone header file containing shared code.
#define CONSTEXPR
Expands to constexpr in decent compilers and inline otherwise.
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Implements data type parsing functionality.
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::ITERATION_CHUNK_SIZE)
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.
constexpr size_t ITERATION_CHUNK_SIZE
For functions that lazy load a large CSV, this determines how many bytes are read at a time.
std::vector< std::string > _get_col_names(csv::string_view head, CSVFormat format)
Return a CSV's column names.
std::string format_row(const std::vector< std::string > &row, csv::string_view delim)
CSVGuessResult _guess_format(csv::string_view head, const std::vector< char > &delims)
Guess the delimiter used by a delimiter-separated values file.
The all encompassing namespace.
std::vector< std::string > get_col_names(csv::string_view filename, CSVFormat format)
Return a CSV's column names.
internals::ThreadSafeDeque< CSVRow > RowCollection
Standard type for storing collection of rows.
CSVGuessResult guess_format(csv::string_view filename, const std::vector< char > &delims)
Guess the delimiter used by a delimiter-separated values file.
nonstd::string_view string_view
The string_view class used by this library.
Stores the inferred format of a CSV file.