#include "sicccPersister.H" /*TODO * check if path is valid */ void SicccPersister::set_persist_path(std::string &s) { assert(!s.empty()); perists_path = s; } void SicccPersister::set_json_filename(std::string &s) { assert(!s.empty()); json_file_name = s; } 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 = perists_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 SicccPersister::read_sicccdir() { DIR *dir = nullptr; struct dirent *entry = nullptr; std::vector v; json_error_t *error = nullptr; if ((dir = opendir(perists_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; }