-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "Config.h" | ||
|
||
bool Config::dump() | ||
{ | ||
return save(to_json()); | ||
} | ||
|
||
void Config::set_target(std::string target) | ||
{ | ||
_target = target; | ||
if (!_target.ends_with(".json")) { | ||
_target += ".json"; | ||
} | ||
build_target_path(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "../meojson/json.hpp" | ||
|
||
class Config | ||
{ | ||
public: | ||
virtual ~Config() = default; | ||
|
||
public: | ||
virtual bool parse(const json::value& config_json) = 0; | ||
virtual bool load() = 0; | ||
virtual json::value to_json() = 0; | ||
virtual bool save(const json::value& root) = 0; | ||
bool dump(); | ||
|
||
public: | ||
virtual void set_target(const std::string target); | ||
|
||
private: | ||
virtual void build_target_path() = 0; | ||
|
||
protected: | ||
std::string _target = "default"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "ControlConfig.h" | ||
|
||
ControlConfig::ControlConfig() | ||
{ | ||
build_target_path(); | ||
} | ||
|
||
bool ControlConfig::parse(const json::value& config_opt) | ||
{ | ||
_config_server = config_opt.get("server", _config_server); | ||
_config_screencap = config_opt.get("screencap", _config_screencap); | ||
_config_touch = config_opt.get("touch", _config_touch); | ||
|
||
return true; | ||
} | ||
|
||
bool ControlConfig::load() | ||
{ | ||
auto config_opt = json::open(_target_path); | ||
if (!config_opt) { | ||
std::cerr << "Failed to open control config file: " << _target_path << std::endl; | ||
return false; | ||
} | ||
|
||
auto& config = *config_opt; | ||
if (!config.is_object()) { | ||
std::cerr << "Json is not an object: " << config << std::endl; | ||
return false; | ||
} | ||
|
||
if (!parse(config)) { | ||
std::cerr << "Failed to parse control: " << config << std::endl; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
json::value ControlConfig::to_json() | ||
{ | ||
json::value root; | ||
root = { { "server", _config_server }, { "screencap", _config_screencap }, { "touch", _config_touch } }; | ||
return root; | ||
} | ||
|
||
bool ControlConfig::save(const json::value& root) | ||
{ | ||
std::filesystem::create_directories(config_dir); | ||
std::ofstream ofs(_target_path, std::ios::out); | ||
if (!ofs.is_open()) { | ||
std::cerr << "Failed to open control config file: " << _target_path << std::endl; | ||
return false; | ||
} | ||
ofs << root; | ||
ofs.close(); | ||
return true; | ||
} | ||
|
||
void ControlConfig::set_config_server(int server) | ||
{ | ||
_config_server = server; | ||
} | ||
|
||
void ControlConfig::set_config_screencap(int screencap) | ||
{ | ||
_config_screencap = screencap; | ||
} | ||
|
||
void ControlConfig::set_config_touch(int touch) | ||
{ | ||
_config_touch = touch; | ||
} | ||
|
||
int ControlConfig::get_config_server() | ||
{ | ||
return _config_server; | ||
} | ||
|
||
int ControlConfig::get_config_screencap() | ||
{ | ||
return _config_screencap; | ||
} | ||
|
||
int ControlConfig::get_config_touch() | ||
{ | ||
return _config_touch; | ||
} | ||
|
||
void ControlConfig::build_target_path() | ||
{ | ||
_target_path = config_dir / _target; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <fstream> | ||
|
||
#include "Config.h" | ||
|
||
class ControlConfig : public Config | ||
{ | ||
public: | ||
ControlConfig(); | ||
virtual ~ControlConfig() = default; | ||
|
||
public: | ||
inline static const std::filesystem::path local_dir = "."; | ||
inline static const std::filesystem::path config_dir = local_dir / "config" / "control"; | ||
|
||
public: | ||
bool parse(const json::value& config_json) override; | ||
bool load() override; | ||
json::value to_json() override; | ||
bool save(const json::value& root) override; | ||
|
||
public: | ||
void set_config_server(int server); | ||
void set_config_screencap(int screencap); | ||
void set_config_touch(int touch); | ||
|
||
public: | ||
int get_config_server(); | ||
int get_config_screencap(); | ||
int get_config_touch(); | ||
|
||
private: | ||
void build_target_path() override; | ||
|
||
private: | ||
int _config_server = 1; | ||
int _config_screencap = 3; | ||
int _config_touch = 3; | ||
|
||
private: | ||
std::filesystem::path _target_path; | ||
}; |