13#include <unordered_map>
14#include <unordered_set>
18#if !defined(__EMSCRIPTEN__)
19#include "../../external/mio.hpp"
21#include "../basic_csv_parser_simd.hpp"
22#include "../col_names.hpp"
23#include "../common.hpp"
24#include "../csv_exceptions.hpp"
25#include "../csv_format.hpp"
27#include "../csv_row.hpp"
28#include "../speculative/diagnostics.hpp"
42 size_t mode_row_length;
54 csv::enable_if_t<std::is_base_of<std::istream, TStream>::value,
int> = 0>
57#if !CSV_EXCEPTIONS_ENABLED
58 if (
source.exceptions() != std::ios::goodbit) {
60 "Explicit CSV reads require a non-throwing stream exception mask.");
69 CSV_CATCH((
const std::ios_base::failure&), {
71 fail_static(error, CSVErrorCode::ReadFailed, ERROR_STREAM_READ_FAILURE);
79 buf.resize(
static_cast<size_t>(source.gcount()));
83#if defined(__EMSCRIPTEN__)
88#if !defined(__EMSCRIPTEN__)
92 std::pair<std::string, size_t> get_csv_head_mmap(
csv::string_view filename, CSVError& error);
126 const std::vector<char>& delims = {
',',
'|',
'\t',
';',
'^',
'~' }) {
128 auto head = internals::parser::get_csv_head(filename, error);
129 if (error)
return {
',', 0, 0};
130 return internals::parser::guess_format(head, delims, error);
133#if CSV_EXCEPTIONS_ENABLED
135 const std::vector<char>& delims = {
',',
'|',
'\t',
';',
'^',
'~' }) {
138 internals::throw_error(error);
143 namespace internals {
150 virtual size_t worker_count()
const noexcept = 0;
152 virtual size_t read_window_size(
size_t chunk_size)
const noexcept = 0;
153 virtual bool utf8_bom()
const noexcept = 0;
159 std::shared_ptr<void> owner,
167 inline std::unique_ptr<ICSVParseOrchestrator> make_csv_parse_orchestrator(
172 const ColNamesPtr& col_names =
nullptr,
202 virtual void next(
size_t bytes) = 0;
205 return this->parse_orchestrator_
206 ? this->parse_orchestrator_->diagnostics()
211 return this->parse_orchestrator_
212 ? this->parse_orchestrator_->worker_count()
217 return this->parse_orchestrator_
218 ? this->parse_orchestrator_->utf8_bom()
228 ResolvedFormat format;
231 size_t source_size_ = 0;
234 std::unique_ptr<ICSVParseOrchestrator> parse_orchestrator_;
236 virtual std::string& get_csv_head() = 0;
238 void resolve_format_from_head(
const CSVFormat& format);
Borrow a row and error state for synchronous, single-pass field access.
A std::deque wrapper which allows multiple read and write threads to concurrently access it along wit...
Abstract base class for source adapters.
virtual void next(size_t bytes)=0
Parse the next block of data.
bool eof() const noexcept
Whether or not we have reached the end of source.
const CSVError & error() const noexcept
Source errors are read by the consumer only after joining its scheduler.
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.
constexpr size_t CSV_HEAD_PREVIEW_SIZE
Bytes read from the start of a source to resolve its format (header, delimiter guessing) before the f...
Focused CSV byte parser core.
std::string get_csv_head_stream(TStream &source, CSVError &error)
Read the first 500KB from a seekless stream source.
CSVGuessResult guess_format(csv::string_view head, const std::vector< char > &delims, CSVError &error)
Guess the delimiter used by a delimiter-separated values file.
The all encompassing namespace.
CSVGuessResult try_guess_format(csv::string_view filename, CSVError &error, const std::vector< char > &delims={ ',', '|', '\t', ';', '^', '~' })
Guess the delimiter, header row, and mode column count of a CSV file.
@ 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.
std::string_view string_view
The string_view class used by this library.
A diagnostic whose text remains valid for the lifetime of the error.
Stores the inferred format of a CSV file.
Explicit DFA state at a parse boundary.