8#if defined(__EMSCRIPTEN__)
12 std::ifstream infile(std::string(filename), std::ios::binary);
13 if (!infile.is_open()) {
21#if !defined(__EMSCRIPTEN__)
22 std::pair<std::string, size_t> get_csv_head_mmap(
csv::string_view filename, CSVError& failure) {
25 std::error_code error;
26 auto mmap = mio::make_mmap_source(std::string(filename), 0, mio::map_entire_file, error);
30 std::ifstream input(std::string(filename), std::ios::binary | std::ios::ate);
31 if (input && input.tellg() == std::streampos(0))
return {};
33 failure.system = error;
36 const size_t file_size = mmap.size();
37 const size_t length = std::min(file_size, bytes);
38 return { std::string(mmap.begin(), mmap.begin() + length), file_size };
43#if defined(__EMSCRIPTEN__)
46 return get_csv_head_mmap(filename, error).first;
51 size_t get_bom_skip(
csv::string_view data,
bool& utf8_bom, CSVError& error) {
55 if (data.size() >= 4) {
56 const unsigned char b0 =
static_cast<unsigned char>(data[0]);
57 const unsigned char b1 =
static_cast<unsigned char>(data[1]);
58 const unsigned char b2 =
static_cast<unsigned char>(data[2]);
59 const unsigned char b3 =
static_cast<unsigned char>(data[3]);
61 if ((b0 == 0xFF && b1 == 0xFE && b2 == 0x00 && b3 == 0x00)
62 || (b0 == 0x00 && b1 == 0x00 && b2 == 0xFE && b3 == 0xFF)) {
68 if (data.size() >= 3) {
69 const unsigned char b0 =
static_cast<unsigned char>(data[0]);
70 const unsigned char b1 =
static_cast<unsigned char>(data[1]);
71 const unsigned char b2 =
static_cast<unsigned char>(data[2]);
73 if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF) {
79 if (data.size() >= 2) {
80 const unsigned char b0 =
static_cast<unsigned char>(data[0]);
81 const unsigned char b1 =
static_cast<unsigned char>(data[1]);
83 if ((b0 == 0xFF && b1 == 0xFE) || (b0 == 0xFE && b1 == 0xFF)) {
95#pragma region CSVParserDriverBase
98 CSVParserDriverBase::CSVParserDriverBase(
99 const CSVFormat& source_format,
100 const ColNamesPtr& col_names
101 ) : CSVParserCore<>(source_format, col_names) {}
103 void CSVParserDriverBase::resolve_format_from_head(
const CSVFormat& source_format) {
104 auto head = this->get_csv_head();
108 if (source_format.possible_delimiters.empty()) {
109 fail_static(error_, CSVErrorCode::InvalidFormat,
"At least one delimiter is required.");
113 ResolvedFormat resolved;
114 resolved.format = source_format;
116 const bool infer_delimiter = source_format.guess_delim();
117 const bool infer_header = !source_format.header_explicitly_set_
118 && (infer_delimiter || !source_format.col_names_explicitly_set_);
119 const bool infer_n_cols = (source_format.get_header() < 0 && source_format.get_col_names().empty());
121 if (infer_delimiter || infer_header || infer_n_cols) {
122 auto guess_result =
guess_format(head, source_format.get_possible_delims(), error_);
124 if (infer_delimiter) {
125 if (!resolved.format.try_delimiter(guess_result.delim, error_))
return;
130 resolved.format.header = guess_result.header_row;
133 resolved.n_cols = guess_result.n_cols;
137 if (resolved.format.no_quote) {
138 this->set_parse_flags(
145 this->set_parse_flags(
148 resolved.format.quote_char
152 this->format = resolved;
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...
CSV_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter)
Create a vector v where each index i corresponds to the ASCII number for a character and,...
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.
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.
@ UnsupportedEncoding
A UTF-16/UTF-32 BOM was found; transcode the input to UTF-8 first.
@ OpenFailed
A native file could not be opened, including initial input mapping.
std::string_view string_view
The string_view class used by this library.