Repository navigation
Expand file tree
/
Copy pathsettings.cpp
More file actions
166 lines (136 loc) · 4.66 KB
/
Copy pathsettings.cpp
File metadata and controls
166 lines (136 loc) · 4.66 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
#include "settings.hpp"
#include <array>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <system_error>
#if defined(_WIN32)
#include <Windows.h>
#endif
namespace dynamic_file_binder {
namespace {
std::filesystem::path GetEnvironmentPath(const char* name)
{
#if defined(_WIN32)
DWORD requiredSize = GetEnvironmentVariableA(name, nullptr, 0);
if (requiredSize == 0) {
return {};
}
std::string buffer(requiredSize, '\0');
DWORD writtenSize = GetEnvironmentVariableA(name, buffer.data(), requiredSize);
if (writtenSize == 0 || writtenSize >= requiredSize) {
return {};
}
buffer.resize(writtenSize);
return std::filesystem::path(buffer);
#else
if (const char* value = std::getenv(name)) {
return std::filesystem::path(value);
}
return {};
#endif
}
std::string Trim(std::string value)
{
const auto first = value.find_first_not_of(" \t\r\n");
if (first == std::string::npos) {
return {};
}
const auto last = value.find_last_not_of(" \t\r\n");
return value.substr(first, last - first + 1);
}
} // namespace
Settings::Settings()
{
LoadSettings();
}
std::filesystem::path Settings::GetDefaultBasePath()
{
if (const std::filesystem::path userProfile = GetEnvironmentPath("USERPROFILE"); !userProfile.empty()) {
return userProfile / "DynamicFileBinder";
}
if (const std::filesystem::path home = GetEnvironmentPath("HOME"); !home.empty()) {
return home / "DynamicFileBinder";
}
return std::filesystem::current_path() / "DynamicFileBinder";
}
void Settings::LoadIni(const std::filesystem::path& configPath)
{
std::ifstream input(configPath);
std::string line;
while (std::getline(input, line)) {
line = Trim(line);
if (line.empty() || line.front() == ';' || line.front() == '#') {
continue;
}
const auto separator = line.find('=');
if (separator == std::string::npos) {
continue;
}
const std::string key = Trim(line.substr(0, separator));
const std::string value = Trim(line.substr(separator + 1));
if (key == "SyncBasePath") {
syncBasePath_ = std::filesystem::path(value);
} else if (key == "MaxLocalStorageMB") {
maxLocalStorageMB_ = static_cast<std::size_t>(std::stoull(value));
}
}
}
void Settings::LoadJson(const std::filesystem::path& configPath)
{
std::ifstream input(configPath);
std::ostringstream buffer;
buffer << input.rdbuf();
const std::string content = buffer.str();
const auto syncKey = content.find("SyncBasePath");
if (syncKey != std::string::npos) {
const auto colon = content.find(':', syncKey);
const auto firstQuote = content.find('"', colon);
const auto secondQuote = content.find('"', firstQuote + 1);
if (colon != std::string::npos && firstQuote != std::string::npos && secondQuote != std::string::npos) {
syncBasePath_ = std::filesystem::path(content.substr(firstQuote + 1, secondQuote - firstQuote - 1));
}
}
const auto storageKey = content.find("MaxLocalStorageMB");
if (storageKey != std::string::npos) {
const auto colon = content.find(':', storageKey);
if (colon != std::string::npos) {
const auto numberStart = content.find_first_of("0123456789", colon);
const auto numberEnd = content.find_first_not_of("0123456789", numberStart);
if (numberStart != std::string::npos) {
maxLocalStorageMB_ = static_cast<std::size_t>(std::stoull(content.substr(numberStart, numberEnd - numberStart)));
}
}
}
}
void Settings::LoadSettings()
{
const std::filesystem::path configDir = std::filesystem::current_path() / "config";
const std::filesystem::path iniPath = configDir / "settings.ini";
const std::filesystem::path jsonPath = configDir / "settings.json";
syncBasePath_ = GetDefaultBasePath();
if (std::filesystem::exists(iniPath)) {
LoadIni(iniPath);
} else if (std::filesystem::exists(jsonPath)) {
LoadJson(jsonPath);
}
std::error_code errorCode;
std::filesystem::create_directories(syncBasePath_, errorCode);
}
std::filesystem::path Settings::GetSyncBasePath() const
{
return syncBasePath_;
}
std::filesystem::path Settings::GetCustomSyncPath(const std::filesystem::path& filePath) const
{
if (filePath.extension() == ".pdf") {
return syncBasePath_ / "PDFs" / filePath.filename();
}
return syncBasePath_ / filePath.filename();
}
std::size_t Settings::GetMaxLocalStorageMB() const
{
return maxLocalStorageMB_;
}
} // namespace dynamic_file_binder