#include "ServerProperties.h" #include #include #include #include #include "../util/StringUtil.h" static const string STRING_TRUE("true"); static const string STRING_FALSE("false"); namespace Feather { ServerProperties::ServerProperties(const char* path) { std::ifstream file; file.open(path); printf("Reading %s...\n", path); if (file.is_open()) { string line; while(std::getline(file, line)) { // trip whitespace StringUtil::trim(line); // skip comments if (line[0] == '#') continue; // key=value size_t pos = line.find('='); // java also accepts key:value if (pos == string::npos) pos = line.find(':'); if (pos == string::npos) continue; string key = line.substr(0, pos); StringUtil::trim(key); string value = line.substr(pos + 1); StringUtil::trim(value); properties.insert( {key, value} ); } file.close(); } //for ( auto const& pair : properties ) // std::cout << "[" << pair.first << "] = [" << pair.second << "]\n"; } template <> string ServerProperties::Get(string_view key) { return properties[string(key)]; } template <> bool ServerProperties::Get(string_view key) { string value = Get(key); if (StringUtil::iequal(value, STRING_TRUE)) { return true; } else { // TODO: complain about non-bool return false; } } template <> int ServerProperties::Get(string_view key) { return std::stoi(Get(key)); } template <> long ServerProperties::Get(string_view key) { return std::stol(Get(key)); } template <> long long ServerProperties::Get(string_view key) { return std::stoll(Get(key)); } }