4#include "orchestrator.hpp"
9 constexpr size_t CSV_STREAM_WINDOW_SIZE_MAX = 256 * 1024 * 1024;
28 template<
typename TStream>
36 const ColNamesPtr& col_names =
nullptr
39 this->resolve_format_from_head(format);
40 if (this->error_)
return;
41 this->parse_orchestrator_ = make_csv_parse_orchestrator(
43 this->whitespace_flags(),
58 this->parse_orchestrator_ = make_csv_parse_orchestrator(
60 this->whitespace_flags(),
71 std::string& get_csv_head()
override {
73 return this->leftover_;
76 void next(
size_t bytes = CSV_CHUNK_SIZE_DEFAULT)
override {
77 if (this->
eof() || this->error_)
return;
86 chunk = std::move(leftover_);
90 : CSV_STREAM_WINDOW_SIZE_MAX;
96#if !CSV_EXCEPTIONS_ENABLED
97 if (source_.exceptions() != std::ios::goodbit) {
99 "Explicit CSV reads require a non-throwing stream exception mask.");
104 source_.read(
buf.get(),
static_cast<std::streamsize
>(
read_size));
106 CSV_CATCH((
const std::ios_base::failure&), {
107 if (!source_.eof() || source_.bad()) {
108 fail_static(this->error_, CSVErrorCode::ReadFailed, ERROR_STREAM_READ_FAILURE);
113 const size_t n =
static_cast<size_t>(source_.gcount());
115 if (
n > 0) chunk.append(
buf.get(),
n);
120 if (source_.bad() || (source_.fail() && !source_.eof())) {
132 this->output(), this->error_
134 if (this->error_)
return;
142 leftover_ = chunk.substr(
result.complete_prefix_length);
143 this->stream_pos_ +=
result.complete_prefix_length;
148 if (this->parse_orchestrator_) {
149 this->parse_orchestrator_->reset_with_initial_state(
state);
156 void reset_with_initial_state(
bool starts_in_quoted,
bool in_escape =
false) noexcept {
157 this->reset_with_initial_state(ParserDFAState{ starts_in_quoted, in_escape });
160 ParserDFAState ending_state() const noexcept {
161 return this->parse_orchestrator_
162 ? this->parse_orchestrator_->ending_state()
169 std::string leftover_;
170 size_t stream_pos_ = 0;
ParseFlagMap parse_flags_
An array where the (i + 128)th slot gives the ParseFlags for ASCII character i.
void reset_with_initial_state(ParserDFAState state) noexcept
Seed the DFA state for the next parse call.
ParserDFAState ending_state() const noexcept
Return the DFA state observed when the most recent parse call stopped.
Borrow a row and error state for synchronous, single-pass field access.
Abstract base class for source adapters.
bool eof() const noexcept
Whether or not we have reached the end of source.
A class for parsing CSV data from any std::istream, including non-seekable sources such as pipes and ...
void next(size_t bytes=CSV_CHUNK_SIZE_DEFAULT) override
Parse the next block of data.
std::array< ParseFlags, 256 > ParseFlagMap
An array which maps ASCII chars to a parsing flag.
std::array< bool, 256 > WhitespaceMap
An array which maps ASCII chars to a flag indicating if it is whitespace.
Contains CSV parser source-adapter declarations and format helpers.
std::string get_csv_head_stream(TStream &source, CSVError &error)
Read the first 500KB from a seekless stream source.
The all encompassing namespace.
@ InvalidArgument
Invalid operation input, such as a null stream or zero batch size.
@ ReadFailed
The input stream reported an I/O failure rather than ordinary EOF.
Explicit DFA state at a parse boundary.