diff --git a/source/cli/Config/Config.cpp b/source/cli/Config/Config.cpp index 2a58c51..2a32bd6 100644 --- a/source/cli/Config/Config.cpp +++ b/source/cli/Config/Config.cpp @@ -1,5 +1,40 @@ #include "Config.h" +bool Config::load() +{ + auto config_opt = json::open(_target_path); + if (!config_opt) { + std::cerr << "Failed to open 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; +} + +bool Config::save(const json::value& root) +{ + std::filesystem::create_directories(_target_path.parent_path()); + std::ofstream ofs(_target_path, std::ios::out); + if (!ofs.is_open()) { + std::cerr << "Failed to open config file: " << _target_path << std::endl; + return false; + } + ofs << root; + ofs.close(); + return true; +} + bool Config::dump() { return save(to_json()); diff --git a/source/cli/Config/Config.h b/source/cli/Config/Config.h index e9714c7..4db5001 100644 --- a/source/cli/Config/Config.h +++ b/source/cli/Config/Config.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include "../meojson/json.hpp" @@ -11,9 +13,9 @@ class Config public: virtual bool parse(const json::value& config_json) = 0; - virtual bool load() = 0; + virtual bool load(); virtual json::value to_json() = 0; - virtual bool save(const json::value& root) = 0; + virtual bool save(const json::value& root); bool dump(); public: @@ -24,4 +26,5 @@ class Config protected: std::string _target = "default"; + std::filesystem::path _target_path = "."; }; \ No newline at end of file diff --git a/source/cli/Config/ControlConfig.cpp b/source/cli/Config/ControlConfig.cpp index b07f70c..2bf21c2 100644 --- a/source/cli/Config/ControlConfig.cpp +++ b/source/cli/Config/ControlConfig.cpp @@ -14,28 +14,6 @@ bool ControlConfig::parse(const json::value& config_opt) 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; @@ -43,19 +21,6 @@ json::value ControlConfig::to_json() 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; diff --git a/source/cli/Config/ControlConfig.h b/source/cli/Config/ControlConfig.h index 90dd803..d6ce309 100644 --- a/source/cli/Config/ControlConfig.h +++ b/source/cli/Config/ControlConfig.h @@ -18,9 +18,7 @@ class ControlConfig : public Config 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); diff --git a/source/cli/Config/DeviceConfig.cpp b/source/cli/Config/DeviceConfig.cpp index ffdd200..52f94af 100644 --- a/source/cli/Config/DeviceConfig.cpp +++ b/source/cli/Config/DeviceConfig.cpp @@ -4,6 +4,7 @@ DeviceConfig::DeviceConfig() { build_target_path(); } + bool DeviceConfig::parse(const json::value& config_opt) { _config_adb = config_opt.get("adb", _config_adb); @@ -12,27 +13,6 @@ bool DeviceConfig::parse(const json::value& config_opt) return true; } -bool DeviceConfig::load() -{ - auto config_opt = json::open(_target_path); - if (!config_opt) { - std::cerr << "Failed to open device 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 DeviceConfig::to_json() { @@ -43,19 +23,6 @@ json::value DeviceConfig::to_json() return root; } -bool DeviceConfig::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 device config file: " << _target_path << std::endl; - return false; - } - ofs << root; - ofs.close(); - return true; -} - void DeviceConfig::set_config_device_name(std::string name) { _config_device_name = name; diff --git a/source/cli/Config/DeviceConfig.h b/source/cli/Config/DeviceConfig.h index d6a8e61..906140c 100644 --- a/source/cli/Config/DeviceConfig.h +++ b/source/cli/Config/DeviceConfig.h @@ -18,9 +18,7 @@ class DeviceConfig : public Config public: bool parse(const json::value& config_opt) override; - bool load() override; json::value to_json() override; - bool save(const json::value& root) override; public: void set_config_device_name(std::string name);