Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/internal/basic_csv_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ namespace csv {

CSV_INLINE std::string get_csv_head(csv::string_view filename);

template<typename TStream,
csv::enable_if_t<std::is_base_of<std::istream, TStream>::value, int> = 0>
std::string get_csv_head(TStream &source) {
auto tellg = source.tellg();
std::string head;
std::getline(source, head);
source.seekg(tellg);
return head;
}

/** Read the first 500KB of a CSV file */
CSV_INLINE std::string get_csv_head(csv::string_view filename, size_t file_size);

Expand Down
10 changes: 9 additions & 1 deletion include/internal/csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ namespace csv {
*/
template<typename TStream,
csv::enable_if_t<std::is_base_of<std::istream, TStream>::value, int> = 0>
CSVReader(TStream& source, CSVFormat format = CSVFormat()) : _format(format) {
CSVReader(TStream &source, CSVFormat format = CSVFormat::guess_csv()) : _format(format) {
auto head = internals::get_csv_head(source);
using Parser = internals::StreamParser<TStream>;

if (format.guess_delim()) {
auto guess_result = internals::_guess_format(head, format.possible_delimiters);
format.delimiter(guess_result.delim);
format.header = guess_result.header_row;
this->_format = format;
}

if (!format.col_names.empty())
this->set_col_names(format.col_names);

Expand Down
Loading
Loading