-
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
3 changed files
with
114 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,71 @@ | ||
#include "TasksConfig.h" | ||
|
||
TasksConfig::TasksConfig() | ||
{ | ||
build_target_path(); | ||
} | ||
|
||
bool TasksConfig::parse(const json::value& config_opt) | ||
{ | ||
auto arr = config_opt.as_array(); | ||
for (auto& el : arr) { | ||
Task task; | ||
task.name = el.get("name", ""); | ||
task.status = el.get("status", true); | ||
task.param = el.get("param"); | ||
task.type = el.get("type", ""); | ||
_config_tasklist.emplace_back(task); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool TasksConfig::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_array()) { | ||
std::cerr << "Json is not an array: " << config << std::endl; | ||
return false; | ||
} | ||
|
||
if (!parse(config)) { | ||
std::cerr << "Failed to parse control: " << config << std::endl; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
json::value TasksConfig::to_json() | ||
{ | ||
json::value root = json::array(); | ||
for (auto& task : _config_tasklist) { | ||
json::object obj = { | ||
{ "name", task.name }, { "status", task.status }, { "param", task.param }, { "type", task.type } | ||
}; | ||
root.emplace(obj); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
void TasksConfig::set_config_tasklist(std::vector<Task> tasklist) | ||
{ | ||
_config_tasklist = tasklist; | ||
} | ||
|
||
std::vector<Task> TasksConfig::get_config_tasklist() | ||
{ | ||
return _config_tasklist; | ||
} | ||
|
||
void TasksConfig::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,41 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "Config.h" | ||
|
||
struct Task | ||
{ | ||
std::string name; | ||
bool status = true; | ||
std::string type; | ||
json::value param = json::object(); | ||
}; | ||
|
||
class TasksConfig : public Config | ||
{ | ||
public: | ||
TasksConfig(); | ||
virtual ~TasksConfig() = default; | ||
|
||
public: | ||
inline static const std::filesystem::path local_dir = "."; | ||
inline static const std::filesystem::path config_dir = local_dir / "config" / "tasks"; | ||
|
||
public: | ||
bool parse(const json::value& config_opt) override; | ||
bool load() override; | ||
json::value to_json() override; | ||
|
||
public: | ||
void set_config_tasklist(std::vector<Task> tasklist); | ||
|
||
public: | ||
std::vector<Task> get_config_tasklist(); | ||
|
||
private: | ||
void build_target_path() override; | ||
|
||
private: | ||
std::vector<Task> _config_tasklist; | ||
}; |