28#include "std_filesystem.h"
32static fs::path UserPath() {
34 fs::path path(userdir);
36 if (!fs::exists(path)) fs::create_directories(path);
40static fs::path SystemPath() {
42 fs::path path(systemdir);
44 assert(fs::exists(path) &&
"System filters not found");
48namespace filters_on_disk {
50std::vector<std::string>
List(
bool include_system) {
51 std::vector<std::string> v;
52 for (
const auto& entry : fs::directory_iterator(UserPath()))
53 v.push_back(entry.path().filename().string());
55 for (
const auto& entry : fs::directory_iterator(SystemPath()))
56 v.push_back(entry.path().stem().string());
58 for (
auto& filter : v) filter = fs::path(filter).stem().string();
62bool Exists(
const std::string& name) {
63 const std::string filename = name +
".json";
64 if (fs::exists(UserPath() / filename))
return true;
65 return fs::exists(SystemPath() / filename);
68bool Remove(
const std::string& name) {
69 const std::string filename = name +
".json";
70 fs::path path(UserPath() / filename);
72 return !fs::exists(path);
75bool Rename(
const std::string& old_name,
const std::string& new_name) {
76 const std::string old_filename = old_name +
".json";
77 fs::path old_path(UserPath() / old_filename);
78 if (!fs::exists(old_path))
return false;
79 const std::string new_filename = new_name +
".json";
80 fs::path new_path(UserPath() / new_filename);
82 fs::rename(old_path, new_path);
83 }
catch (fs::filesystem_error& e) {
86 return !fs::exists(old_path) && fs::exists(new_path);
91 const std::string filename = name +
".json";
92 fs::path path(UserPath() / filename);
93 std::ofstream stream(path);
94 if (!stream.is_open())
return false;
100 const std::string filename = name +
".json";
101 fs::path path(UserPath() / filename);
102 if (!fs::exists(path)) path = SystemPath() / filename;
103 std::ifstream stream(path);
105 std::stringstream ss;
106 ss << stream.rdbuf();
std::string to_string() const
Output parsable JSON string representation.
static NavmsgFilter Parse(const std::string &s)
Parse text as created by to_string().
std::vector< std::string > List(bool include_system)
Return list of filters, possibly including also the system ones.
NavmsgFilter Read(const std::string &name)
Read filter with given name from disk.
bool Rename(const std::string &old_name, const std::string &new_name)
Rename old_name on disk to new.
bool Remove(const std::string &name)
Remove a filter, return ok if no errors.
bool Write(const NavmsgFilter &filter, const std::string &name)
Write contents for given filter to disk.
bool Exists(const std::string &name)
Return true iff filter with given name exists, either system or user defined.
Data Monitor filter storage routines.