:github_url: https://github.com/srbhp/nrgplusplus .. _program_listing_file_utils_configPerser.hpp: Program Listing for File configPerser.hpp ========================================= |exhale_lsh| :ref:`Return to documentation for file ` (``utils/configPerser.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include class configPerser { // # start starts with comment // spaces are ignored // We need to specify the configFileName before // we call the constructor // configParser::configFileName = "config.txt" ; // // get the values of a key as // // std::cout << "key: " << configParser().get("key"); std::map config; public: const inline static std::string configFileName; configPerser() { // this function is only called once if (configFileName.empty()) { throw std::runtime_error("configParser::gfilename is empty"); } std::ifstream file(configFileName); if (!file.is_open()) { std::cout << configFileName << ":File not found" << std::endl; return; } std::string line; while (std::getline(file, line)) { if (line.empty() || line[0] == '#') { continue; } // remove empty spaces // line = std::regex_replace(line, std::regex("\\s+"), ""); std::string key; std::string value; std::string::size_type pos = line.find('='); if (pos == std::string::npos) { continue; } key = line.substr(0, pos); // remove spaces from the beginning and the end of the key key = std::regex_replace(key, std::regex("\\s+"), ""); value = line.substr(pos + 1); // remove spaces from the beginning and the end of the key value = std::regex_replace(value, std::regex("^ +| +$"), "$1"); config[key] = value; } } template T get(const std::string &key) { if (config.find(key) == config.end()) { throw std::runtime_error(key + ": key not found!" + "from the file " + configFileName); } std::cout << "configPerser: " << key << " " << config[key] << std::endl; // double if constexpr (std::is_same_v) { return std::stod(config[key]); } // int if constexpr (std::is_same_v) { return std::stoi(config[key]); } // bool if constexpr (std::is_same_v) { bool check = false; if (config[key] == "true") { check = true; return true; } if (config[key] == "false") { check = true; return false; } if (!check) { throw std::runtime_error(key + ": key not found!" + "from the file " + configFileName); } } // string if constexpr (std::is_same_v) { return config[key]; } // tuple // } void print() { for (auto [key, value] : config) { std::cout << key << ":\t" << value << std::endl; } } };