From 81351fff01e1f3d0b19d0b8007ec07d5edb16df7 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Thu, 29 Sep 2022 17:05:11 +0200 Subject: [PATCH] vector support for file reading --- src/file_io.cpp | 22 +++++++++++++++++++--- src/file_io.hpp | 11 +++++++++-- src/settings_manager.hpp | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/file_io.cpp b/src/file_io.cpp index 374d90c..95621e2 100644 --- a/src/file_io.cpp +++ b/src/file_io.cpp @@ -28,8 +28,20 @@ namespace gz { return success; } - std::unordered_map readKeyValueFile(const std::string& filepath, bool removeSpaces) { - std::unordered_map attr; + + using pairSS = std::pair; + using umapSS = std::unordered_map; + using mapSS = std::map; + using vecSS = std::vector; + + inline void insert(vecSS& t, pairSS&& p) { t.emplace_back(p); } + inline void insert(umapSS& t, pairSS&& p) { t.emplace(p); } + inline void insert(mapSS& t, pairSS&& p) { t.emplace(p); } + + + template + T readKeyValueFile(const std::string& filepath, bool removeSpaces) { + T attr; std::string line; int eqPos; std::ifstream file(filepath); @@ -50,7 +62,7 @@ namespace gz { line.erase(std::remove_if(line.begin(), line.begin() + eqPos + 2, [](unsigned char x) { return std::isspace(x); }), line.begin() + eqPos + 2); } eqPos = line.find("="); - attr[line.substr(0, eqPos)] = line.substr(eqPos+1, line.length()); + insert(attr, std::pair{ line.substr(0, eqPos), line.substr(eqPos+1, line.length()) }); } } file.close(); @@ -61,6 +73,10 @@ namespace gz { return attr; } + template umapSS readKeyValueFile(const std::string&, bool); + template mapSS readKeyValueFile(const std::string&, bool); + template vecSS readKeyValueFile(const std::string&, bool); + template bool writeKeyValueFile, std::equal_to>(const std::string&, const std::unordered_map, std::equal_to>&); template bool writeKeyValueFile>(const std::string&, const std::unordered_map>&); } diff --git a/src/file_io.hpp b/src/file_io.hpp index a05c521..6c5242b 100644 --- a/src/file_io.hpp +++ b/src/file_io.hpp @@ -16,12 +16,19 @@ namespace gz { template bool writeKeyValueFile(const std::string& filepath, const std::unordered_map& content); + template + concept ReadKeyValueFileImplemented = + std::same_as>> || + std::same_as> || + std::same_as> ; + /** * @brief Read a file that contains key = value pairs * @throws FileIOError * @see @ref fio_t_key_value "Key-Value filetype" */ - std::unordered_map readKeyValueFile(const std::string& filepath, bool removeSpaces=false); + template + T readKeyValueFile(const std::string& filepath, bool removeSpaces=false); } /** @@ -33,7 +40,7 @@ namespace gz { * @section fio_filetypes Filetypes * @subsection fio_t_key_value Simple Key-Value file * A file that contains key - value pairs in each line, separated with "=". - * Any number of whitespaces around the separator is allowed. + * Whitespaces around the separator are allowed. * If the first character of a line is "#", the whole line is a comment. * Example: * @code diff --git a/src/settings_manager.hpp b/src/settings_manager.hpp index ef47feb..a18f4d5 100644 --- a/src/settings_manager.hpp +++ b/src/settings_manager.hpp @@ -654,7 +654,7 @@ namespace gz { if (filepath.empty()) { throw InvalidArgument("filename is not set", "readFromFile"); } - std::unordered_map map = readKeyValueFile(filepath); + std::unordered_map map = readKeyValueFile>(filepath); settings.insert(map.begin(), map.end()); if (checkValidity) { // insert only valid values