vector support for file reading
This commit is contained in:
parent
ed39f1262c
commit
81351fff01
@ -28,8 +28,20 @@ namespace gz {
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> readKeyValueFile(const std::string& filepath, bool removeSpaces) {
|
|
||||||
std::unordered_map<std::string, std::string> attr;
|
using pairSS = std::pair<std::string, std::string>;
|
||||||
|
using umapSS = std::unordered_map<std::string, std::string>;
|
||||||
|
using mapSS = std::map<std::string, std::string>;
|
||||||
|
using vecSS = std::vector<pairSS>;
|
||||||
|
|
||||||
|
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<ReadKeyValueFileImplemented T>
|
||||||
|
T readKeyValueFile(const std::string& filepath, bool removeSpaces) {
|
||||||
|
T attr;
|
||||||
std::string line;
|
std::string line;
|
||||||
int eqPos;
|
int eqPos;
|
||||||
std::ifstream file(filepath);
|
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);
|
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("=");
|
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();
|
file.close();
|
||||||
@ -61,6 +73,10 @@ namespace gz {
|
|||||||
return attr;
|
return attr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template umapSS readKeyValueFile<umapSS>(const std::string&, bool);
|
||||||
|
template mapSS readKeyValueFile<mapSS>(const std::string&, bool);
|
||||||
|
template vecSS readKeyValueFile<vecSS>(const std::string&, bool);
|
||||||
|
|
||||||
template bool writeKeyValueFile<std::hash<std::string>, std::equal_to<std::string>>(const std::string&, const std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>>&);
|
template bool writeKeyValueFile<std::hash<std::string>, std::equal_to<std::string>>(const std::string&, const std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>>&);
|
||||||
template bool writeKeyValueFile<util::string_hash, std::equal_to<>>(const std::string&, const std::unordered_map<std::string, std::string, util::string_hash, std::equal_to<>>&);
|
template bool writeKeyValueFile<util::string_hash, std::equal_to<>>(const std::string&, const std::unordered_map<std::string, std::string, util::string_hash, std::equal_to<>>&);
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,19 @@ namespace gz {
|
|||||||
template<typename Hash, typename Pred>
|
template<typename Hash, typename Pred>
|
||||||
bool writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& content);
|
bool writeKeyValueFile(const std::string& filepath, const std::unordered_map<std::string, std::string, Hash, Pred>& content);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept ReadKeyValueFileImplemented =
|
||||||
|
std::same_as<T, std::vector<std::pair<std::string, std::string>>> ||
|
||||||
|
std::same_as<T, std::map<std::string, std::string>> ||
|
||||||
|
std::same_as<T, std::unordered_map<std::string, std::string>> ;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read a file that contains key = value pairs
|
* @brief Read a file that contains key = value pairs
|
||||||
* @throws FileIOError
|
* @throws FileIOError
|
||||||
* @see @ref fio_t_key_value "Key-Value filetype"
|
* @see @ref fio_t_key_value "Key-Value filetype"
|
||||||
*/
|
*/
|
||||||
std::unordered_map<std::string, std::string> readKeyValueFile(const std::string& filepath, bool removeSpaces=false);
|
template<ReadKeyValueFileImplemented T>
|
||||||
|
T readKeyValueFile(const std::string& filepath, bool removeSpaces=false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,7 +40,7 @@ namespace gz {
|
|||||||
* @section fio_filetypes Filetypes
|
* @section fio_filetypes Filetypes
|
||||||
* @subsection fio_t_key_value Simple Key-Value file
|
* @subsection fio_t_key_value Simple Key-Value file
|
||||||
* A file that contains key - value pairs in each line, separated with "=".
|
* 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.
|
* If the first character of a line is "#", the whole line is a comment.
|
||||||
* Example:
|
* Example:
|
||||||
* @code
|
* @code
|
||||||
|
@ -654,7 +654,7 @@ namespace gz {
|
|||||||
if (filepath.empty()) {
|
if (filepath.empty()) {
|
||||||
throw InvalidArgument("filename is not set", "readFromFile");
|
throw InvalidArgument("filename is not set", "readFromFile");
|
||||||
}
|
}
|
||||||
std::unordered_map<std::string, std::string> map = readKeyValueFile(filepath);
|
std::unordered_map<std::string, std::string> map = readKeyValueFile<std::unordered_map<std::string, std::string>>(filepath);
|
||||||
settings.insert(map.begin(), map.end());
|
settings.insert(map.begin(), map.end());
|
||||||
if (checkValidity) {
|
if (checkValidity) {
|
||||||
// insert only valid values
|
// insert only valid values
|
||||||
|
Loading…
Reference in New Issue
Block a user