OpenCPN Partial API docs
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
24#include <fstream>
25
27#include "std_filesystem.h"
28#include "model/base_platform.h"
29
30extern BasePlatform* g_BasePlatform;
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 Write(const NavmsgFilter& filter, const std::string& name) {
76 std::string json = filter.to_string();
77 const std::string filename = name + ".json";
78 fs::path path(UserPath() / filename);
79 std::ofstream stream(path);
80 if (!stream.is_open()) return false;
81 stream << json;
82 return true;
83}
84
85NavmsgFilter Read(const std::string& name) {
86 const std::string filename = name + ".json";
87 fs::path path(UserPath() / filename);
88 if (!fs::exists(path)) path = SystemPath() / filename;
89 std::ifstream stream(path);
90 if (stream.bad()) return NavmsgFilter();
91 std::stringstream ss;
92 ss << stream.rdbuf();
93 return NavmsgFilter::Parse(ss.str());
94}
95
96bool IsSystemFilter(std::string& name) { return true; }
97
98} // namespace filters_on_disk
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().
bool IsSystemFilter(std::string &name)
Return true iff name refers to a read-only system filter.
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 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.
Filter storage routines.