Vince's CSV Parser
Loading...
Searching...
No Matches
driver.cpp
1#include "driver.hpp"
2
3#include <system_error>
4
5namespace csv {
6 namespace internals {
7 namespace parser {
8#if defined(__EMSCRIPTEN__)
9 // Opens the file and delegates to the template overload to avoid duplicating the read/resize logic.
10 std::string get_csv_head_stream(csv::string_view filename, CSVError& error) {
11 error.clear();
12 std::ifstream infile(std::string(filename), std::ios::binary);
13 if (!infile.is_open()) {
14 fail(error, CSVErrorCode::OpenFailed, make_prefixed_message(ERROR_CANNOT_OPEN_FILE, filename));
15 return {};
16 }
17 return get_csv_head_stream(infile, error);
18 }
19#endif
20
21#if !defined(__EMSCRIPTEN__)
22 std::pair<std::string, size_t> get_csv_head_mmap(csv::string_view filename, CSVError& failure) {
23 failure.clear();
24 const size_t bytes = CSV_HEAD_PREVIEW_SIZE;
25 std::error_code error;
26 auto mmap = mio::make_mmap_source(std::string(filename), 0, mio::map_entire_file, error);
27 if (error) {
28 // Native mapping APIs reject zero-length files. Confirm an empty,
29 // readable file before treating that failure as ordinary EOF.
30 std::ifstream input(std::string(filename), std::ios::binary | std::ios::ate);
31 if (input && input.tellg() == std::streampos(0)) return {};
32 fail(failure, CSVErrorCode::OpenFailed, make_prefixed_message(ERROR_CANNOT_OPEN_FILE, filename));
33 failure.system = error;
34 return {};
35 }
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 };
39 }
40#endif
41
42 std::string get_csv_head(csv::string_view filename, CSVError& error) {
43#if defined(__EMSCRIPTEN__)
44 return get_csv_head_stream(filename, error);
45#else
46 return get_csv_head_mmap(filename, error).first;
47#endif
48 }
49 }
50
51 size_t get_bom_skip(csv::string_view data, bool& utf8_bom, CSVError& error) {
52 error.clear();
53 utf8_bom = false;
54
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]);
60
61 if ((b0 == 0xFF && b1 == 0xFE && b2 == 0x00 && b3 == 0x00)
62 || (b0 == 0x00 && b1 == 0x00 && b2 == 0xFE && b3 == 0xFF)) {
63 fail_static(error, CSVErrorCode::UnsupportedEncoding, ERROR_UNSUPPORTED_UTF32);
64 return 0;
65 }
66 }
67
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]);
72
73 if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF) {
74 utf8_bom = true;
75 return 3;
76 }
77 }
78
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]);
82
83 if ((b0 == 0xFF && b1 == 0xFE) || (b0 == 0xFE && b1 == 0xFF)) {
84 fail_static(error, CSVErrorCode::UnsupportedEncoding, ERROR_UNSUPPORTED_UTF16);
85 return 0;
86 }
87 }
88
89 return 0;
90 }
91
92 namespace parser {
93
94#ifdef _MSC_VER
95#pragma region CSVParserDriverBase
96#endif
97
98 CSVParserDriverBase::CSVParserDriverBase(
99 const CSVFormat& source_format,
100 const ColNamesPtr& col_names
101 ) : CSVParserCore<>(source_format, col_names) {}
102
103 void CSVParserDriverBase::resolve_format_from_head(const CSVFormat& source_format) {
104 auto head = this->get_csv_head();
105 if (error_) return;
106 // Setters reject empty candidates, but moving a format can leave its
107 // source vector empty. Validate that boundary before using resolved access.
108 if (source_format.possible_delimiters.empty()) {
109 fail_static(error_, CSVErrorCode::InvalidFormat, "At least one delimiter is required.");
110 return;
111 }
112
113 ResolvedFormat resolved;
114 resolved.format = source_format;
115
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());
120
121 if (infer_delimiter || infer_header || infer_n_cols) {
122 auto guess_result = guess_format(head, source_format.get_possible_delims(), error_);
123 if (error_) return;
124 if (infer_delimiter) {
125 if (!resolved.format.try_delimiter(guess_result.delim, error_)) return;
126 }
127
128 if (infer_header) {
129 // Inferred header should not clear user-provided column names.
130 resolved.format.header = guess_result.header_row;
131 }
132
133 resolved.n_cols = guess_result.n_cols;
134 }
135
136 const char delimiter = resolved_delimiter(resolved.format);
137 if (resolved.format.no_quote) {
138 this->set_parse_flags(
139 make_parse_flags(delimiter),
140 delimiter,
141 delimiter
142 );
143 }
144 else {
145 this->set_parse_flags(
146 make_parse_flags(delimiter, resolved.format.quote_char),
147 delimiter,
148 resolved.format.quote_char
149 );
150 }
151
152 this->format = resolved;
153 }
154#ifdef _MSC_VER
155#pragma endregion
156#endif
157 }
158 }
159}
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...
Definition common.hpp:438
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,...
Definition core.hpp:40
char resolved_delimiter(const CSVFormat &format) noexcept
Read a delimiter after configuration has been validated and resolved to one candidate.
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.
Definition driver.hpp:55
CSVGuessResult guess_format(csv::string_view head, const std::vector< char > &delims, CSVError &error)
Guess the delimiter used by a delimiter-separated values file.
Definition guessing.cpp:73
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.
Definition common.hpp:182