diff --git a/include/updater/csv_file_parser.hpp b/include/updater/csv_file_parser.hpp index a94df690a..685c30553 100644 --- a/include/updater/csv_file_parser.hpp +++ b/include/updater/csv_file_parser.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -92,32 +93,41 @@ template struct CSVFilesParser // Parse a single CSV file and return result as a vector auto ParseCSVFile(const std::string &filename, std::size_t file_id) const { - boost::iostreams::mapped_file_source mmap(filename); - auto first = mmap.begin(), last = mmap.end(); - - BOOST_ASSERT(file_id <= std::numeric_limits::max()); - ValueRule value_source = - value_rule[qi::_val = qi::_1, boost::phoenix::bind(&Value::source, qi::_val) = file_id]; - qi::rule()> csv_line = - (key_rule >> ',' >> value_source) >> -(',' >> *(qi::char_ - qi::eol)); - std::vector> result; - const auto ok = qi::parse(first, last, -(csv_line % qi::eol) >> *qi::eol, result); - - if (!ok || first != last) + try { - auto begin_of_line = first - 1; - while (begin_of_line >= mmap.begin() && *begin_of_line != '\n') - --begin_of_line; - auto line_number = std::count(mmap.begin(), first, '\n') + 1; - const auto message = boost::format("CSV file %1% malformed on line %2%: %3%") % - filename % std::to_string(line_number) % - std::string(begin_of_line + 1, std::find(first, last, '\n')); + boost::iostreams::mapped_file_source mmap(filename); + auto first = mmap.begin(), last = mmap.end(); + + BOOST_ASSERT(file_id <= std::numeric_limits::max()); + ValueRule value_source = + value_rule[qi::_val = qi::_1, boost::phoenix::bind(&Value::source, qi::_val) = file_id]; + qi::rule()> csv_line = + (key_rule >> ',' >> value_source) >> -(',' >> *(qi::char_ - qi::eol)); + std::vector> result; + const auto ok = qi::parse(first, last, -(csv_line % qi::eol) >> *qi::eol, result); + + if (!ok || first != last) + { + auto begin_of_line = first - 1; + while (begin_of_line >= mmap.begin() && *begin_of_line != '\n') + --begin_of_line; + auto line_number = std::count(mmap.begin(), first, '\n') + 1; + const auto message = boost::format("CSV file %1% malformed on line %2%: %3%") % + filename % std::to_string(line_number) % + std::string(begin_of_line + 1, std::find(first, last, '\n')); + throw util::exception(message.str() + SOURCE_REF); + } + + util::Log() << "Loaded " << filename << " with " << result.size() << "values"; + + return std::move(result); + } + catch (const boost::exception& e) + { + const auto message = boost::format("exception in loading %1%:\n %2%") % + filename % boost::diagnostic_information(e); throw util::exception(message.str() + SOURCE_REF); } - - util::Log() << "Loaded " << filename << " with " << result.size() << "values"; - - return std::move(result); } const std::size_t start_index;