ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.09k stars 435 forks source link

Add ability to parse headers easier #108

Open ribtoks opened 4 years ago

ribtoks commented 4 years ago

What is this PR trying to accomplish?

When you don't know the header names in advance, you cannot use this great library. This PR introduces a helper that allows to use a workaround for setting column names after the header was read.

How is this PR trying to accomplish said thing?

I added an internal array of column names that gets filled when we read the header. After this we can use a trick to reuse this column names for actual reading using set_header_from_self().

How is this PR tested?

Manually and as part of a test suite in a different project.

Additional context

Illustration of how it is used:

io::CSVReader<COLUMNS_COUNT,
            io::trim_chars<' ', '\t'>,
            io::double_quote_escape<',', '\"'>,
            io::ignore_overflow,
            io::single_and_empty_line_comment<'%'>> csvReader(filepath);
// CsvCallHelper is just a C++ templates expander, it does not matter for this PR
CsvCallHelper<decltype(csvReader)> callHelper(csvReader);
std::vector<std::string> fakeColumns(COLUMNS_COUNT);
csvReader.next_line();
callHelper.ReadHeader<io::ignore_column, COLUMNS_COUNT>(io::ignore_missing_column | io::ignore_extra_column, fakeColumns);

csvReader.set_header_from_self();
std::vector<std::string> columns(COLUMNS_COUNT);
while (callHelper.ReadRow<COLUMNS_COUNT>(columns)) {
   // ...
}
ben-strasser commented 2 years ago

I do not understand what you are trying to accomplish. Have you tried calling next_line, parsing that and then using set_header?