-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.cpp
75 lines (55 loc) · 1.61 KB
/
settings.cpp
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
#include "settings.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <filesystem>
namespace fs = std::filesystem;
#include <fstream>
const std::string CONFIG_DIRECTORIES_KEY = "directories";
const std::string CONFIG_RECURSIVE_KEY = "recursive";
Settings SettingsProvider::getSettings() const
{
std::lock_guard<std::mutex> lock(mtx_);
return settings_;
}
void SettingsProvider::setSettings(Settings settings)
{
std::lock_guard<std::mutex> lock(mtx_);
settings_ = std::move(settings);
}
void Settings::load(std::string const& fileName)
{
using boost::property_tree::ptree;
using namespace boost::property_tree::json_parser;
if (!fs::exists(fileName))
{
save(fileName);
return;
}
ptree tree;
read_json(fileName, tree);
ptree::assoc_iterator itDirs = tree.find(CONFIG_DIRECTORIES_KEY);
if (itDirs != tree.not_found())
{
for (auto dir : itDirs->second)
{
Settings::Directory dirSettings;
dirSettings.recursive = dir.second.get(CONFIG_RECURSIVE_KEY, true);
directories[dir.first] = std::move(dirSettings);
}
}
}
void Settings::save(std::string const& fileName)
{
using boost::property_tree::ptree;
using namespace boost::property_tree::json_parser;
ptree treeMain;
ptree::iterator itDirs = treeMain.push_back(
std::make_pair(CONFIG_DIRECTORIES_KEY, ptree()));
for (auto dir : directories)
{
ptree::iterator itDir = itDirs->second.push_back(
std::make_pair(dir.first, ptree()));
itDir->second.put(CONFIG_RECURSIVE_KEY, dir.second.recursive);
}
write_json(fileName, treeMain);
}