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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* -*- c++ -*- */
/*
* Copyright 2006,2013,2015 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gnuradio/constants.h>
#include <gnuradio/prefs.h>
#include <gnuradio/sys_paths.h>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <boost/program_options.hpp>
namespace fs = std::filesystem;
namespace po = boost::program_options;
typedef std::ifstream::char_type char_t;
namespace gr {
namespace {
void stolower(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
}
void stoupper(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
}
} // namespace
prefs* prefs::singleton()
{
static prefs instance;
return &instance;
}
prefs::prefs() { _read_files(_sys_prefs_filenames()); }
std::vector<std::string> prefs::_sys_prefs_filenames()
{
std::vector<std::string> fnames;
fs::path dir = prefsdir();
if (fs::is_directory(dir)) {
for (const auto& p : fs::directory_iterator(dir)) {
if (p.path().extension() == ".conf")
fnames.push_back(p.path());
}
std::sort(fnames.begin(), fnames.end());
}
// Find if there is a ~/.gnuradio/config.conf file and add this to
// the end of the file list to override any preferences in the
// installed path config files.
fs::path userconf = fs::path(gr::userconf_path()) / "config.conf";
if (fs::exists(userconf)) {
fnames.push_back(userconf.string());
}
return fnames;
}
void prefs::_read_files(const std::vector<std::string>& filenames)
{
for (const auto& fname : filenames) {
std::ifstream infile(fname.c_str());
if (!infile.good()) {
std::cerr << "WARNING: Config file '" << fname
<< "' could not be opened for reading." << std::endl;
continue;
}
try {
po::basic_parsed_options<char_t> parsed =
po::parse_config_file(infile, po::options_description(), true);
for (const auto& o : parsed.options) {
std::string okey = o.string_key;
size_t pos = okey.find(".");
std::string section, key;
if (pos != std::string::npos) {
section = okey.substr(0, pos);
key = okey.substr(pos + 1);
} else {
section = "default";
key = okey;
}
stolower(section);
stolower(key);
// value of a basic_option is always a std::vector<string>; we only
// allow single values, so:
std::string value = o.value[0];
std::lock_guard<std::mutex> lk(d_mutex);
d_config_map[section][key] = value;
}
} catch (std::exception& e) {
std::cerr << "WARNING: Config file '" << fname
<< "' failed to parse:" << std::endl;
std::cerr << e.what() << std::endl;
std::cerr << "Skipping it" << std::endl;
}
}
}
void prefs::add_config_file(const std::string& configfile)
{
_read_files(std::vector<std::string>{ configfile });
}
std::string prefs::to_string()
{
std::stringstream s;
std::lock_guard<std::mutex> lk(d_mutex);
for (const auto& sections : d_config_map) {
s << "[" << sections.first << "]" << std::endl;
for (const auto& options : sections.second) {
s << options.first << " = " << options.second << std::endl;
}
s << std::endl;
}
return s.str();
}
void prefs::save()
{
// TODO: write the settings in an error-safe way, by writing to a tempfile and
// atomically renaming.
std::string conf = to_string();
fs::path userconf = fs::path(gr::userconf_path()) / "config.conf";
std::ofstream fout(userconf);
fout << conf;
fout.close();
// TODO: throw on error?
}
char* prefs::option_to_env(std::string section, std::string option)
{
stoupper(section);
stoupper(option);
const auto envname = "GR_CONF_" + section + "_" + option;
return getenv(envname.c_str());
}
bool prefs::has_section(const std::string& section)
{
std::string s = section;
stolower(s);
std::lock_guard<std::mutex> lk(d_mutex);
return d_config_map.find(s) != d_config_map.end();
}
bool prefs::has_option(const std::string& section, const std::string& option)
{
if (option_to_env(section, option))
return true;
if (!has_section(section)) {
return false;
}
std::string s = section;
stolower(s);
std::string o = option;
stolower(o);
std::lock_guard<std::mutex> lk(d_mutex);
const auto& sec = d_config_map[s];
return sec.find(o) != sec.end();
}
// get_string needs specialization because it can have spaces.
const std::string prefs::get_string(const std::string& section,
const std::string& option,
const std::string& default_val)
{
char* env = option_to_env(section, option);
if (env)
return std::string(env);
if (!has_option(section, option)) {
return default_val;
}
std::string s = section;
stolower(s);
std::string o = option;
stolower(o);
std::lock_guard<std::mutex> lk(d_mutex);
return d_config_map[s][o];
}
// get_bool() needs specialization because there are multiple text strings for true/false
bool prefs::get_bool(const std::string& section,
const std::string& option,
bool default_val)
{
if (!has_option(section, option)) {
return default_val;
}
std::string str = get_string(section, option, "");
if (str.empty()) {
return default_val;
}
stolower(str);
if ((str == "true") || (str == "on") || (str == "1"))
return true;
if ((str == "false") || (str == "off") || (str == "0"))
return false;
return default_val;
}
template <typename T>
T prefs::get_general(const std::string& section,
const std::string& option,
const T& default_val)
{
if (!has_option(section, option)) {
return default_val;
}
std::string str = get_string(section, option, "");
if (str.empty()) {
return default_val;
}
std::stringstream sstr(str);
T n;
sstr >> n;
return n;
}
template <typename T>
void prefs::set_general(const std::string& section,
const std::string& option,
const T& val)
{
std::string s = section;
stolower(s);
std::string o = option;
stolower(o);
std::stringstream sstr;
sstr << val;
std::lock_guard<std::mutex> lk(d_mutex);
d_config_map[s][o] = sstr.str();
}
void prefs::set_bool(const std::string& section, const std::string& option, bool val)
{
return set_general(section, option, val);
}
long prefs::get_long(const std::string& section,
const std::string& option,
long default_val)
{
return get_general(section, option, default_val);
}
void prefs::set_long(const std::string& section, const std::string& option, long val)
{
return set_general(section, option, val);
}
double prefs::get_double(const std::string& section,
const std::string& option,
double default_val)
{
return get_general(section, option, default_val);
}
void prefs::set_double(const std::string& section, const std::string& option, double val)
{
return set_general(section, option, val);
}
void prefs::set_string(const std::string& section,
const std::string& option,
const std::string& val)
{
return set_general(section, option, val);
}
} /* namespace gr */
|