Skip to content

Commit

Permalink
refactor: extract config base class
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 18, 2023
1 parent 68d7ad7 commit fa12b11
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 75 deletions.
35 changes: 35 additions & 0 deletions source/cli/Config/Config.cpp
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
7 changes: 5 additions & 2 deletions source/cli/Config/Config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <filesystem>
#include <iostream>
#include <string>

#include "../meojson/json.hpp"
Expand All @@ -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:
Expand All @@ -24,4 +26,5 @@ class Config

protected:
std::string _target = "default";
std::filesystem::path _target_path = ".";
};
35 changes: 0 additions & 35 deletions source/cli/Config/ControlConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,13 @@ 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;
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;
Expand Down
2 changes: 0 additions & 2 deletions source/cli/Config/ControlConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 1 addition & 34 deletions source/cli/Config/DeviceConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
{
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions source/cli/Config/DeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fa12b11

Please sign in to comment.