Skip to content
API Documentation
BastionGuard™
ClamdConfig Class Reference

#include <ClamdConfig.hpp>

Classes

struct  Line

Public Member Functions

 ClamdConfig ()=default
bool load (const std::string &path)
bool save (const std::string &path)
bool saveWithBackup (const std::string &path)
void setOption (const std::string &key, const std::string &value)
std::string getOption (const std::string &key) const
bool getOptionBool (const std::string &key) const
void setOptionBool (const std::string &key, bool enabled)
std::vector< std::string > getOnAccessPaths () const
void addOnAccessPath (const std::string &path)
void removeOnAccessPath (const std::string &path)
std::map< std::string, bool > getScanOptions () const
void setScanOption (const std::string &key, bool enabled)

Private Member Functions

bool backupFile (const std::string &path) const

Static Private Member Functions

static bool isYes (const std::string &v)

Private Attributes

std::map< std::string, std::string > options
std::vector< Line > rawLines

Detailed Description

Definition at line 26 of file ClamdConfig.hpp.

Constructor & Destructor Documentation

◆ ClamdConfig()

Member Function Documentation

◆ addOnAccessPath()

void ClamdConfig::addOnAccessPath ( const std::string & path)

Definition at line 143 of file ClamdConfig.cpp.

143 {
144 Line ln;
145 ln.key = "OnAccessIncludePath";
146 ln.value = path;
147 ln.text = ln.key + " " + ln.value;
148 ln.commented = false;
149
150 rawLines.push_back(ln);
151 options[ln.key] = ln.value;
152}
std::map< std::string, std::string > options
std::vector< Line > rawLines

References ClamdConfig::Line::commented, ClamdConfig::Line::key, options, rawLines, ClamdConfig::Line::text, and ClamdConfig::Line::value.

Referenced by ClamdConfig(), and SettingsPage::onAddPath().

◆ backupFile()

bool ClamdConfig::backupFile ( const std::string & path) const
private

Definition at line 90 of file ClamdConfig.cpp.

90 {
91 try {
92 std::filesystem::path p(path);
93 if (std::filesystem::exists(p)) {
94 auto backup = p;
95 backup += ".bak";
96 std::filesystem::copy_file(p, backup,
97 std::filesystem::copy_options::overwrite_existing);
98 }
99 return true;
100 } catch (std::exception& e) {
101 std::cerr << _("❌ Errore backup: ") << e.what() << std::endl;
102 return false;
103 }
104}
#define _(s)

References _.

Referenced by saveWithBackup().

◆ getOnAccessPaths()

std::vector< std::string > ClamdConfig::getOnAccessPaths ( ) const

Definition at line 133 of file ClamdConfig.cpp.

133 {
134 std::vector<std::string> paths;
135 for (auto& ln : rawLines) {
136 if (!ln.commented && ln.key == "OnAccessIncludePath") {
137 paths.push_back(ln.value);
138 }
139 }
140 return paths;
141}

References rawLines.

Referenced by ClamdConfig(), and SettingsPage::SettingsPage().

◆ getOption()

std::string ClamdConfig::getOption ( const std::string & key) const

Definition at line 118 of file ClamdConfig.cpp.

118 {
119 auto it = options.find(key);
120 return (it != options.end()) ? it->second : "";
121}

References options.

Referenced by ClamdConfig().

◆ getOptionBool()

bool ClamdConfig::getOptionBool ( const std::string & key) const

Definition at line 123 of file ClamdConfig.cpp.

123 {
124 auto it = options.find(key);
125 if (it == options.end()) return false;
126 return isYes(it->second);
127}
static bool isYes(const std::string &v)

References isYes(), and options.

Referenced by ClamdConfig().

◆ getScanOptions()

std::map< std::string, bool > ClamdConfig::getScanOptions ( ) const

Definition at line 162 of file ClamdConfig.cpp.

162 {
163 std::map<std::string, bool> scanOpts;
164 for (auto& ln : rawLines) {
165 if (!ln.key.empty()) {
166 // Consideriamo solo quelli che hanno valore yes/no
167 if (ln.value == "yes" || ln.value == "no") {
168 scanOpts[ln.key] = !ln.commented && isYes(ln.value);
169 }
170 }
171 }
172 return scanOpts;
173}

References isYes(), and rawLines.

Referenced by ClamdConfig(), and SettingsPage::SettingsPage().

◆ isYes()

bool ClamdConfig::isYes ( const std::string & v)
staticprivate

Definition at line 185 of file ClamdConfig.cpp.

185 {
186 std::string val = v;
187 std::transform(val.begin(), val.end(), val.begin(), ::tolower);
188 return (val == "yes" || val == "true" || val == "1");
189}

Referenced by getOptionBool(), and getScanOptions().

◆ load()

bool ClamdConfig::load ( const std::string & path)

Definition at line 33 of file ClamdConfig.cpp.

33 {
34 rawLines.clear();
35 options.clear();
36
37 ifstream file(path);
38 if (!file.is_open()) return false;
39
40 string line;
41 while (getline(file, line)) {
42 Line ln;
43 ln.text = line;
44
45 string trimmed = line;
46 bool commented = false;
47
48 if (!trimmed.empty() && trimmed[0] == '#') {
49 commented = true;
50 trimmed = trimmed.substr(1);
51 }
52
53 istringstream iss(trimmed);
54 string key, value;
55 if (iss >> key >> value) {
56 ln.key = key;
57 ln.value = value;
58 ln.commented = commented;
59
60 if (!commented) {
61 options[key] = value;
62 }
63 }
64 rawLines.push_back(ln);
65 }
66 return true;
67}

References ClamdConfig::Line::commented, ClamdConfig::Line::key, options, rawLines, ClamdConfig::Line::text, and ClamdConfig::Line::value.

Referenced by ClamdConfig(), and MainWindow::MainWindow().

◆ removeOnAccessPath()

void ClamdConfig::removeOnAccessPath ( const std::string & path)

Definition at line 154 of file ClamdConfig.cpp.

154 {
155 rawLines.erase(std::remove_if(rawLines.begin(), rawLines.end(),
156 [&](const Line& ln) {
157 return ln.key == "OnAccessIncludePath" && ln.value == path;
158 }),
159 rawLines.end());
160}

References rawLines.

Referenced by ClamdConfig(), and SettingsPage::onRemovePath().

◆ save()

bool ClamdConfig::save ( const std::string & path)

Definition at line 69 of file ClamdConfig.cpp.

69 {
70 ofstream file(path);
71 if (!file.is_open()) return false;
72
73 for (auto& ln : rawLines) {
74 if (!ln.key.empty()) {
75 // aggiorna il valore
76 auto it = options.find(ln.key);
77 if (it != options.end()) {
78 if (ln.commented) {
79 file << "#";
80 }
81 file << ln.key << " " << it->second << "\n";
82 continue;
83 }
84 }
85 file << ln.text << "\n";
86 }
87 return true;
88}

References options, and rawLines.

Referenced by ClamdConfig(), SettingsPage::onSave(), and saveWithBackup().

◆ saveWithBackup()

bool ClamdConfig::saveWithBackup ( const std::string & path)

Definition at line 106 of file ClamdConfig.cpp.

106 {
107 if (!backupFile(path)) {
108 std::cerr << _("❌ Impossibile creare il backup di ") << path << std::endl;
109 return false;
110 }
111 return save(path);
112}
bool backupFile(const std::string &path) const
bool save(const std::string &path)

References _, backupFile(), and save().

Referenced by ClamdConfig().

◆ setOption()

void ClamdConfig::setOption ( const std::string & key,
const std::string & value )

Definition at line 114 of file ClamdConfig.cpp.

114 {
115 options[key] = value;
116}

References options.

Referenced by ClamdConfig().

◆ setOptionBool()

void ClamdConfig::setOptionBool ( const std::string & key,
bool enabled )

Definition at line 129 of file ClamdConfig.cpp.

129 {
130 options[key] = enabled ? "yes" : "no";
131}

References options.

Referenced by ClamdConfig().

◆ setScanOption()

void ClamdConfig::setScanOption ( const std::string & key,
bool enabled )

Definition at line 175 of file ClamdConfig.cpp.

175 {
176 options[key] = enabled ? "yes" : "no";
177 for (auto& ln : rawLines) {
178 if (ln.key == key) {
179 ln.value = enabled ? "yes" : "no";
180 ln.text = (ln.commented ? "#" : "") + ln.key + " " + ln.value;
181 }
182 }
183}

References options, and rawLines.

Referenced by ClamdConfig(), and SettingsPage::onSave().

Member Data Documentation

◆ options

std::map<std::string, std::string> ClamdConfig::options
private

◆ rawLines

std::vector<Line> ClamdConfig::rawLines
private

The documentation for this class was generated from the following files: