FeatherMC/src/config/ServerProperties.cpp

91 lines
2.2 KiB
C++
Raw Normal View History

#include "ServerProperties.h"
#include <iostream>
#include <fstream>
#include <string_view>
#include <algorithm>
#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>(string_view key)
{
return properties[string(key)];
}
template <>
bool ServerProperties::Get<bool>(string_view key)
{
string value = Get<string>(key);
if (StringUtil::iequal(value, STRING_TRUE)) {
return true;
} else {
// TODO: complain about non-bool
return false;
}
}
template <>
int ServerProperties::Get<int>(string_view key)
{
return std::stoi(Get<string>(key));
}
template <>
long ServerProperties::Get<long>(string_view key)
{
return std::stol(Get<string>(key));
}
template <>
long long ServerProperties::Get<long long>(string_view key)
{
return std::stoll(Get<string>(key));
}
}