OpenCPN Partial API docs
Loading...
Searching...
No Matches
filters_on_disk.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2025 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <fstream>
25
27#include "model/base_platform.h"
28#include "std_filesystem.h"
29
31
32static fs::path UserPath() {
33 auto userdir = g_BasePlatform->GetPrivateDataDir().ToStdString();
34 fs::path path(userdir);
35 path /= "filters";
36 if (!fs::exists(path)) fs::create_directories(path);
37 return path;
38}
39
40static fs::path SystemPath() {
41 auto systemdir = g_BasePlatform->GetSharedDataDir().ToStdString();
42 fs::path path(systemdir);
43 path /= "filters";
44 assert(fs::exists(path) && "System filters not found");
45 return path;
46}
47
48namespace filters_on_disk {
49
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());
54 if (include_system) {
55 for (const auto& entry : fs::directory_iterator(SystemPath()))
56 v.push_back(entry.path().stem().string());
57 }
58 for (auto& filter : v) filter = fs::path(filter).stem().string();
59 return v;
60}
61
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);
66}
67
68bool Remove(const std::string& name) {
69 const std::string filename = name + ".json";
70 fs::path path(UserPath() / filename);
71 fs::remove(path);
72 return !fs::exists(path);
73}
74
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);
81 try {
82 fs::rename(old_path, new_path);
83 } catch (fs::filesystem_error& e) {
84 return false;
85 }
86 return !fs::exists(old_path) && fs::exists(new_path);
87}
88
89bool Write(const NavmsgFilter& filter, const std::string& name) {
90 std::string json = filter.to_string();
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;
95 stream << json;
96 return true;
97}
98
99NavmsgFilter Read(const std::string& name) {
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);
104 if (stream.bad()) return NavmsgFilter();
105 std::stringstream ss;
106 ss << stream.rdbuf();
107 return NavmsgFilter::Parse(ss.str());
108}
109
110} // namespace filters_on_disk
BasePlatform * g_BasePlatform
points to g_platform, handles brain-dead MS linker.
Basic platform specific support utilities without GUI deps.
wxString & GetPrivateDataDir()
Return dir path for opencpn.log, etc., respecting -c cli option.
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.