Skip to content

Commit

Permalink
Fix for csv with different count of fields in header and row
Browse files Browse the repository at this point in the history
  • Loading branch information
mesozoic-drones committed Apr 30, 2020
1 parent b0bc895 commit 7f6f960
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions include/just_gtfs/just_gtfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,21 @@ inline Result CsvParser::read_header(const std::string & csv_filename)

inline Result CsvParser::read_row(std::map<std::string, std::string> & obj)
{
obj = {};
std::string row;
if (!getline(csv_stream, row))
{
obj = {};
return {ResultCode::END_OF_FILE, {}};
}

if (row == "\r")
{
obj = {};
return {ResultCode::OK, {}};
}

std::vector<std::string> fields_values = split_record(row);
const std::vector<std::string> fields_values = split_record(row);

// Different count of fields in row and in the header of csv.
// Typical approach to skip not required fields.
if (fields_values.size() != field_sequence.size())
obj = {};
// Different count of fields in the row and in the header of csv.
// Typical approach is to skip not required fields.
const size_t fields_count = std::min(field_sequence.size(), fields_values.size());

for (size_t i = 0; i < field_sequence.size(); ++i)
for (size_t i = 0; i < fields_count; ++i)
obj[field_sequence[i]] = fields_values[i];

return {ResultCode::OK, {}};
Expand Down

0 comments on commit 7f6f960

Please sign in to comment.