-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreferences_linux.cpp
More file actions
193 lines (148 loc) · 4.4 KB
/
Copy pathpreferences_linux.cpp
File metadata and controls
193 lines (148 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include "../../preferences.h"
namespace nativeapi {
class Preferences::Impl {
public:
explicit Impl(const std::string& scope) : scope_(scope) {
// Use XDG Base Directory Specification
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
std::string config_dir;
if (xdg_config_home) {
config_dir = std::string(xdg_config_home);
} else {
const char* home = getenv("HOME");
if (!home) {
struct passwd* pw = getpwuid(getuid());
home = pw->pw_dir;
}
config_dir = std::string(home) + "/.config";
}
config_dir += "/nativeapi";
// Create directory if it doesn't exist
mkdir(config_dir.c_str(), 0755);
config_file_ = config_dir + "/preferences_" + scope + ".conf";
// Load existing preferences
LoadFromFile();
}
~Impl() { SaveToFile(); }
bool Set(const std::string& key, const std::string& value) {
data_[key] = value;
return SaveToFile();
}
std::string Get(const std::string& key, const std::string& default_value) const {
auto it = data_.find(key);
return (it != data_.end()) ? it->second : default_value;
}
bool Remove(const std::string& key) {
auto it = data_.find(key);
if (it != data_.end()) {
data_.erase(it);
return SaveToFile();
}
return false;
}
bool Clear() {
data_.clear();
return SaveToFile();
}
bool Contains(const std::string& key) const { return data_.find(key) != data_.end(); }
std::vector<std::string> GetKeys() const {
std::vector<std::string> keys;
keys.reserve(data_.size());
for (const auto& pair : data_) {
keys.push_back(pair.first);
}
return keys;
}
size_t GetSize() const { return data_.size(); }
std::map<std::string, std::string> GetAll() const { return data_; }
const std::string& GetScope() const { return scope_; }
private:
bool LoadFromFile() {
std::ifstream file(config_file_);
if (!file.is_open()) {
return false;
}
std::string line;
while (std::getline(file, line)) {
// Skip empty lines and comments
if (line.empty() || line[0] == '#') {
continue;
}
// Parse key=value
size_t pos = line.find('=');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
// Unescape newlines
size_t escape_pos = 0;
while ((escape_pos = value.find("\\n", escape_pos)) != std::string::npos) {
value.replace(escape_pos, 2, "\n");
escape_pos += 1;
}
data_[key] = value;
}
}
file.close();
return true;
}
bool SaveToFile() const {
std::ofstream file(config_file_);
if (!file.is_open()) {
return false;
}
file << "# NativeAPI Preferences - " << scope_ << std::endl;
for (const auto& pair : data_) {
std::string value = pair.second;
// Escape newlines
size_t pos = 0;
while ((pos = value.find('\n', pos)) != std::string::npos) {
value.replace(pos, 1, "\\n");
pos += 2;
}
file << pair.first << "=" << value << std::endl;
}
file.close();
return true;
}
std::string scope_;
std::string config_file_;
mutable std::map<std::string, std::string> data_;
};
// Constructor implementations
Preferences::Preferences() : Preferences("default") {}
Preferences::Preferences(const std::string& scope) : pimpl_(std::make_unique<Impl>(scope)) {}
Preferences::~Preferences() = default;
// Interface implementation
bool Preferences::Set(const std::string& key, const std::string& value) {
return pimpl_->Set(key, value);
}
std::string Preferences::Get(const std::string& key, const std::string& default_value) const {
return pimpl_->Get(key, default_value);
}
bool Preferences::Remove(const std::string& key) {
return pimpl_->Remove(key);
}
bool Preferences::Clear() {
return pimpl_->Clear();
}
bool Preferences::Contains(const std::string& key) const {
return pimpl_->Contains(key);
}
std::vector<std::string> Preferences::GetKeys() const {
return pimpl_->GetKeys();
}
size_t Preferences::GetSize() const {
return pimpl_->GetSize();
}
std::map<std::string, std::string> Preferences::GetAll() const {
return pimpl_->GetAll();
}
std::string Preferences::GetScope() const {
return pimpl_->GetScope();
}
} // namespace nativeapi