-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.cpp
160 lines (134 loc) · 6.02 KB
/
file.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
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
// Copyright (C) High-Performance Computing Center Stuttgart (https://www.hlrs.de/)
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "file.h"
#include "value.h"
#include "array.h"
#include "detail/manager.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
#include "detail/toml/toml.hpp"
#ifdef CONFIG_NAMESPACE
namespace CONFIG_NAMESPACE {
#endif
namespace config {
using namespace detail;
namespace {
const std::string sep("/");
const toml::table *table_for_section(const toml::table &root, const std::string §ion)
{
if (section.empty())
return &root;
return root[section].as_table();
return nullptr;
}
} // namespace
File::File(const std::string &path, detail::Manager *mgr)
: Logger("File"), m_manager(mgr ? mgr : Manager::the()), m_config(m_manager->registerPath(path))
{}
File::~File() = default;
bool File::exists() const
{
return m_config.exists;
}
std::string File::pathname() const
{
return m_config.base + sep + m_config.path + ".toml";
}
bool File::save()
{
return m_manager->save(m_config.path);
}
void File::setSaveOnExit(bool enable)
{
m_config.autosave = enable;
}
bool File::isSaveOnExit() const
{
return m_config.autosave;
}
std::vector<std::string> File::sections()
{
return subsections("");
}
std::vector<std::string> File::subsections(const std::string §ion)
{
const std::string prefix = section.empty() ? "" : section + ".";
std::vector<std::string> sections;
if (const auto *tbl = table_for_section(m_config.config, section)) {
for (auto it = tbl->begin(); it != tbl->end(); ++it) {
if (it->second.is_table()) {
sections.emplace_back(prefix + std::string(it->first.str()));
}
}
}
return sections;
}
std::vector<std::string> File::entries(const std::string §ion)
{
std::vector<std::string> entries;
if (const auto *tbl = table_for_section(m_config.config, section)) {
debug("File::entries") << "entries for section " + section + ":" << std::endl;
for (auto it = tbl->begin(); it != tbl->end(); ++it) {
debug("File::entries") << " " + std::string(it->first.str()) << std::endl;
if (!it->second.is_table())
entries.emplace_back(it->first.str());
}
} else {
debug("no entries for section " + section);
}
return entries;
}
template<class V>
std::unique_ptr<Value<V>> File::value(const std::string §ion, const std::string &name)
{
const auto &path = m_config.path;
return std::make_unique<Value<V>>(path, section, name, m_manager);
}
template<class V>
std::unique_ptr<Value<V>> File::value(const std::string §ion, const std::string &name, const V &def, Flag flags)
{
const auto &path = m_config.path;
return std::make_unique<Value<V>>(path, section, name, def, m_manager, flags);
}
template<class V>
std::unique_ptr<Array<V>> File::array(const std::string §ion, const std::string &name)
{
const auto &path = m_config.path;
return std::make_unique<Array<V>>(path, section, name, m_manager);
}
template<class V>
std::unique_ptr<Array<V>> File::array(const std::string §ion, const std::string &name, const std::vector<V> &def,
Flag flags)
{
const auto &path = m_config.path;
return std::make_unique<Array<V>>(path, section, name, def, m_manager, flags);
}
template std::unique_ptr<Value<bool>> COVEXPORT File::value(const std::string §ion, const std::string &name);
template std::unique_ptr<Value<int64_t>> COVEXPORT File::value(const std::string §ion, const std::string &name);
template std::unique_ptr<Value<double>> COVEXPORT File::value(const std::string §ion, const std::string &name);
template std::unique_ptr<Value<std::string>> COVEXPORT File::value(const std::string §ion, const std::string &name);
template std::unique_ptr<Value<bool>> COVEXPORT File::value(const std::string §ion, const std::string &name,
const bool &def, Flag flags);
template std::unique_ptr<Value<int64_t>> COVEXPORT File::value(const std::string §ion, const std::string &name,
const int64_t &def, Flag flags);
template std::unique_ptr<Value<double>> COVEXPORT File::value(const std::string §ion, const std::string &name,
const double &def, Flag flags);
template std::unique_ptr<Value<std::string>> COVEXPORT File::value(const std::string §ion, const std::string &name,
const std::string &def, Flag flags);
template std::unique_ptr<Array<bool>> COVEXPORT File::array(const std::string §ion, const std::string &name);
template std::unique_ptr<Array<int64_t>> COVEXPORT File::array(const std::string §ion, const std::string &name);
template std::unique_ptr<Array<double>> COVEXPORT File::array(const std::string §ion, const std::string &name);
template std::unique_ptr<Array<std::string>> COVEXPORT File::array(const std::string §ion, const std::string &name);
template std::unique_ptr<Array<bool>> COVEXPORT File::array(const std::string §ion, const std::string &name,
const std::vector<bool> &def, Flag flags);
template std::unique_ptr<Array<int64_t>> COVEXPORT File::array(const std::string §ion, const std::string &name,
const std::vector<int64_t> &def, Flag flags);
template std::unique_ptr<Array<double>> COVEXPORT File::array(const std::string §ion, const std::string &name,
const std::vector<double> &def, Flag flags);
template std::unique_ptr<Array<std::string>> COVEXPORT File::array(const std::string §ion, const std::string &name,
const std::vector<std::string> &def, Flag flags);
} // namespace config
#ifdef CONFIG_NAMESPACE
}
#endif