54 lines
1.5 KiB
C++
54 lines
1.5 KiB
C++
#include "sicccPersister.H"
|
|
|
|
int
|
|
SicccPersister::write_sicccfile(const Sicccobject& so)
|
|
{
|
|
json_t *new_json = get_json_from_sicccobject(so);
|
|
if (json_object_update(root, new_json) == 0)
|
|
std::cout << "[DEBUG] update root json successfully" << std::endl;
|
|
else
|
|
std::cerr << "Error update root failed" << std::endl;
|
|
assert(root);
|
|
int ret = write_sicccfile();
|
|
return ret;
|
|
}
|
|
|
|
int
|
|
SicccPersister::write_sicccfile()
|
|
{
|
|
assert(!json_file_name.empty());
|
|
std::string store_str = merouterin.get_persist_path() + "/" + beautify_filename();
|
|
assert(root);
|
|
int ret = json_dump_file(root, store_str.c_str(), JSON_INDENT(4));
|
|
return ret;
|
|
}
|
|
|
|
Sicccobject
|
|
SicccPersister::read_json(const std::string &s)
|
|
{
|
|
root = load_json(s);
|
|
return Sicccobject(root);
|
|
}
|
|
|
|
std::vector<Sicccobject>
|
|
SicccPersister::read_sicccdir()
|
|
{
|
|
DIR *dir = nullptr;
|
|
struct dirent *entry = nullptr;
|
|
std::vector<Sicccobject> v;
|
|
json_error_t *error = nullptr;
|
|
if ((dir = opendir(merouterin.get_persist_path().c_str())) == NULL) {
|
|
std::cerr << "Error could not open directory" << std::endl;
|
|
return v; // should throw exception instea1d
|
|
}
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (std::strstr(entry->d_name, ".json") == NULL)
|
|
continue;
|
|
json_t *tmp = json_load_file(entry->d_name, JSON_DECODE_ANY, error);
|
|
assert(tmp);
|
|
Sicccobject so(tmp);
|
|
v.push_back(so);
|
|
}
|
|
return v;
|
|
}
|