162 lines
2.5 KiB
C++
162 lines
2.5 KiB
C++
#include "common.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
using std::endl;
|
|
using std::ifstream;
|
|
using std::istream;
|
|
using std::ostream;
|
|
using std::set;
|
|
using std::string;
|
|
using std::to_string;
|
|
using nlohmann::json;
|
|
|
|
bool read_config(json& cfg, ostream* log)
|
|
{
|
|
ifstream cfg_file("config.json");
|
|
|
|
if (!cfg_file.is_open())
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not open config.json" << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Parse with comments
|
|
try
|
|
{
|
|
cfg = json::parse(cfg_file, nullptr, true, true);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << e.what() << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool path_exists(const string& path)
|
|
{
|
|
struct stat info;
|
|
int error;
|
|
|
|
error = stat(path.c_str(), &info);
|
|
if (0 != error)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (S_ISDIR(info.st_mode))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
string build_fname(const string& main_path, const string& folder, const string& fname)
|
|
{
|
|
string path_to_file;
|
|
|
|
path_to_file = main_path;
|
|
if (!path_to_file.empty() && path_to_file[path_to_file.size() - 1] != '/')
|
|
{
|
|
path_to_file += '/';
|
|
}
|
|
|
|
path_to_file += folder;
|
|
if (!path_to_file.empty() && path_to_file[path_to_file.size() - 1] != '/')
|
|
{
|
|
path_to_file += '/';
|
|
}
|
|
|
|
path_to_file += fname;
|
|
|
|
return path_to_file;
|
|
}
|
|
|
|
string i_to_str(int i, int size, char fill)
|
|
{
|
|
string str;
|
|
|
|
str = to_string(i);
|
|
str.insert(str.begin(), size - str.size(), fill);
|
|
|
|
return str;
|
|
}
|
|
|
|
void json_extract(const json& j, const string& key, string& out)
|
|
{
|
|
if (j.contains(key) && j[key].is_string())
|
|
{
|
|
out = j[key];
|
|
}
|
|
}
|
|
|
|
void json_extract(const json& j, const string& key, uint16_t& out)
|
|
{
|
|
if (j.contains(key) && j[key].is_number_unsigned())
|
|
{
|
|
out = j[key];
|
|
}
|
|
}
|
|
|
|
void json_extract(const json& j, const string& key, bool& out)
|
|
{
|
|
if (j.contains(key) && j[key].is_boolean())
|
|
{
|
|
out = j[key];
|
|
}
|
|
}
|
|
|
|
bool ensure_folder(const string& main_path, const string& folder)
|
|
{
|
|
string full_path;
|
|
int error;
|
|
|
|
if (!path_exists(main_path))
|
|
{
|
|
error = mkdir(main_path.c_str(), 0775);
|
|
if (0 != error)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
full_path = build_fname(main_path, folder, "");
|
|
if (path_exists(full_path))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
error = mkdir(full_path.c_str(), 0775);
|
|
if (0 != error)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
string trim_whitespace(const string& s)
|
|
{
|
|
const string whitespace = " \t\r\n\f\v";
|
|
string result(s);
|
|
size_t pos;
|
|
|
|
pos = result.find_first_not_of(whitespace);
|
|
result.erase(0, pos);
|
|
pos = result.find_last_not_of(whitespace);
|
|
result.erase(pos + 1);
|
|
|
|
return result;
|
|
}
|