First server executable
This commit is contained in:
101
helpers.cpp
Normal file
101
helpers.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "helpers.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
using std::endl;
|
||||
using std::ifstream;
|
||||
using std::istream;
|
||||
using std::ostream;
|
||||
using std::string;
|
||||
|
||||
bool read_config_json(json& cfg, const string& filename, ostream* log)
|
||||
{
|
||||
ifstream cfg_file(filename);
|
||||
|
||||
if (!cfg_file.is_open())
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << "Could not open config file" << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return read_config_json(cfg, cfg_file, log);
|
||||
}
|
||||
|
||||
bool read_config_json(json& cfg, istream& in, ostream* log)
|
||||
{
|
||||
// Parse with comments
|
||||
try
|
||||
{
|
||||
cfg = json::parse(in, nullptr, true, true);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << e.what() << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool json_extract(const json& j, const string& key, string& out)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (j.contains(key) && j[key].is_string())
|
||||
{
|
||||
out = j[key];
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool json_extract(const json& j, const string& key, int& out)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (j.contains(key) && j[key].is_number_integer())
|
||||
{
|
||||
out = j[key];
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool json_extract(const json& j, const string& key, bool& out)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (j.contains(key) && j[key].is_boolean())
|
||||
{
|
||||
out = j[key];
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool json_extract(const json& j, const string& key, uint16_t& out)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (j.contains(key) && j[key].is_number_integer())
|
||||
{
|
||||
if ((j[key] >= 0) && (j[key] <= UINT16_MAX))
|
||||
{
|
||||
out = j[key];
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user