summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/prefs.cc
diff options
context:
space:
mode:
authorMarcus Müller <mmueller@gnuradio.org>2019-08-07 21:45:12 +0200
committerMarcus Müller <marcus@hostalia.de>2019-08-09 23:04:28 +0200
commitf7bbf2c1d8d780294f3e016aff239ca35eb6516e (patch)
treee09ab6112e02b2215b2d59ac24d3d6ea2edac745 /gnuradio-runtime/lib/prefs.cc
parent78431dc6941e3acc67c858277dfe4a0ed583643c (diff)
Tree: clang-format without the include sorting
Diffstat (limited to 'gnuradio-runtime/lib/prefs.cc')
-rw-r--r--gnuradio-runtime/lib/prefs.cc381
1 files changed, 182 insertions, 199 deletions
diff --git a/gnuradio-runtime/lib/prefs.cc b/gnuradio-runtime/lib/prefs.cc
index 426fbd33c2..218926d312 100644
--- a/gnuradio-runtime/lib/prefs.cc
+++ b/gnuradio-runtime/lib/prefs.cc
@@ -43,195 +43,187 @@ typedef std::ifstream::char_type char_t;
namespace gr {
- prefs *
- prefs::singleton()
- {
+prefs* prefs::singleton()
+{
static prefs instance; // Guaranteed to be destroyed.
- // Instantiated on first use.
+ // Instantiated on first use.
return &instance;
- }
+}
- prefs::prefs()
- {
- _read_files(_sys_prefs_filenames());
- }
+prefs::prefs() { _read_files(_sys_prefs_filenames()); }
- prefs::~prefs()
- {
+prefs::~prefs()
+{
// nop
- }
+}
- std::vector<std::string>
- prefs::_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)) {
- fs::directory_iterator diritr(dir);
- while(diritr != fs::directory_iterator()) {
- fs::path p = *diritr++;
- if(p.extension() == ".conf")
- fnames.push_back(p.string());
- }
- std::sort(fnames.begin(), fnames.end());
+ if (fs::is_directory(dir)) {
+ fs::directory_iterator diritr(dir);
+ while (diritr != fs::directory_iterator()) {
+ fs::path p = *diritr++;
+ if (p.extension() == ".conf")
+ fnames.push_back(p.string());
+ }
+ 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());
+ if (fs::exists(userconf)) {
+ fnames.push_back(userconf.string());
}
return fnames;
- }
-
- void
- prefs::_read_files(const std::vector<std::string> &filenames)
- {
- BOOST_FOREACH( std::string fname, filenames) {
- std::ifstream infile(fname.c_str());
- if(infile.good()) {
- try {
- po::basic_parsed_options<char_t> parsed = po::parse_config_file(infile, po::options_description(), true);
- BOOST_FOREACH(po::basic_option<char_t> 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;
+}
+
+void prefs::_read_files(const std::vector<std::string>& filenames)
+{
+ BOOST_FOREACH (std::string fname, filenames) {
+ std::ifstream infile(fname.c_str());
+ if (infile.good()) {
+ try {
+ po::basic_parsed_options<char_t> parsed =
+ po::parse_config_file(infile, po::options_description(), true);
+ BOOST_FOREACH (po::basic_option<char_t> 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;
+ }
+ std::transform(
+ section.begin(), section.end(), section.begin(), ::tolower);
+ std::transform(key.begin(), key.end(), key.begin(), ::tolower);
+ // value of a basic_option is always a std::vector<string>; we only
+ // allow single values, so:
+ std::string value = o.value[0];
+ 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;
}
- std::transform(section.begin(), section.end(), section.begin(), ::tolower);
- std::transform(key.begin(), key.end(), key.begin(), ::tolower);
- // value of a basic_option is always a std::vector<string>; we only allow single values, so:
- std::string value = o.value[0];
- 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;
+ } else { // infile.good();
+ std::cerr << "WARNING: Config file '" << fname
+ << "' could not be opened for reading." << std::endl;
}
- } else { // infile.good();
- std::cerr << "WARNING: Config file '" << fname << "' could not be opened for reading." << std::endl;
- }
}
- }
+}
- void
- prefs::add_config_file(const std::string &configfile)
- {
+void prefs::add_config_file(const std::string& configfile)
+{
std::vector<std::string> filenames;
filenames.push_back(configfile);
_read_files(filenames);
- }
+}
- std::string
- prefs::to_string()
- {
+std::string prefs::to_string()
+{
config_map_itr sections;
config_map_elem_itr options;
std::stringstream s;
- for(sections = d_config_map.begin(); sections != d_config_map.end(); sections++) {
- s << "[" << sections->first << "]" << std::endl;
- for(options = sections->second.begin(); options != sections->second.end(); options++) {
- s << options->first << " = " << options->second << std::endl;
- }
- s << std::endl;
+ for (sections = d_config_map.begin(); sections != d_config_map.end(); sections++) {
+ s << "[" << sections->first << "]" << std::endl;
+ for (options = sections->second.begin(); options != sections->second.end();
+ options++) {
+ s << options->first << " = " << options->second << std::endl;
+ }
+ s << std::endl;
}
return s.str();
- }
+}
- void
- prefs::save()
- {
+void prefs::save()
+{
std::string conf = to_string();
fs::path userconf = fs::path(gr::userconf_path()) / "config.conf";
fs::ofstream fout(userconf);
fout << conf;
fout.close();
- }
+}
- char *
- prefs::option_to_env(std::string section, std::string option)
- {
+char* prefs::option_to_env(std::string section, std::string option)
+{
std::stringstream envname;
- std::string secname=section, optname=option;
+ std::string secname = section, optname = option;
std::transform(section.begin(), section.end(), secname.begin(), ::toupper);
std::transform(option.begin(), option.end(), optname.begin(), ::toupper);
envname << "GR_CONF_" << secname << "_" << optname;
return getenv(envname.str().c_str());
- }
+}
- bool
- prefs::has_section(const std::string &section)
- {
+bool prefs::has_section(const std::string& section)
+{
std::string s = section;
std::transform(section.begin(), section.end(), s.begin(), ::tolower);
return d_config_map.count(s) > 0;
- }
+}
- bool
- prefs::has_option(const std::string &section, const std::string &option)
- {
- if(option_to_env(section, option))
- return true;
+bool prefs::has_option(const std::string& section, const std::string& option)
+{
+ if (option_to_env(section, option))
+ return true;
- if(has_section(section)) {
- std::string s = section;
- std::transform(section.begin(), section.end(), s.begin(), ::tolower);
+ if (has_section(section)) {
+ std::string s = section;
+ std::transform(section.begin(), section.end(), s.begin(), ::tolower);
- std::string o = option;
- std::transform(option.begin(), option.end(), o.begin(), ::tolower);
+ std::string o = option;
+ std::transform(option.begin(), option.end(), o.begin(), ::tolower);
- config_map_itr sec = d_config_map.find(s);
- return sec->second.count(o) > 0;
- }
- else {
- return false;
- }
- }
-
- 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)) {
- std::string s = section;
- std::transform(section.begin(), section.end(), s.begin(), ::tolower);
-
- std::string o = option;
- std::transform(option.begin(), option.end(), o.begin(), ::tolower);
-
- config_map_itr sec = d_config_map.find(s);
- config_map_elem_itr opt = sec->second.find(o);
- return opt->second;
+ config_map_itr sec = d_config_map.find(s);
+ return sec->second.count(o) > 0;
+ } else {
+ return false;
}
- else {
- return default_val;
+}
+
+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)) {
+ std::string s = section;
+ std::transform(section.begin(), section.end(), s.begin(), ::tolower);
+
+ std::string o = option;
+ std::transform(option.begin(), option.end(), o.begin(), ::tolower);
+
+ config_map_itr sec = d_config_map.find(s);
+ config_map_elem_itr opt = sec->second.find(o);
+ return opt->second;
+ } else {
+ return default_val;
}
- }
+}
- void
- prefs::set_string(const std::string &section, const std::string &option,
- const std::string &val)
- {
+void prefs::set_string(const std::string& section,
+ const std::string& option,
+ const std::string& val)
+{
std::string s = section;
std::transform(section.begin(), section.end(), s.begin(), ::tolower);
@@ -243,34 +235,31 @@ namespace gr {
opt_map[o] = val;
d_config_map[s] = opt_map;
- }
-
- bool
- prefs::get_bool(const std::string &section, const std::string &option,
- bool default_val)
- {
- if(has_option(section, option)) {
- std::string str = get_string(section, option, "");
- if(str == "") {
- return default_val;
- }
- std::transform(str.begin(), str.end(), str.begin(), ::tolower);
- if((str == "true") || (str == "on") || (str == "1"))
- return true;
- else if((str == "false") || (str == "off") || (str == "0"))
- return false;
- else
+}
+
+bool prefs::get_bool(const std::string& section,
+ const std::string& option,
+ bool default_val)
+{
+ if (has_option(section, option)) {
+ std::string str = get_string(section, option, "");
+ if (str == "") {
+ return default_val;
+ }
+ std::transform(str.begin(), str.end(), str.begin(), ::tolower);
+ if ((str == "true") || (str == "on") || (str == "1"))
+ return true;
+ else if ((str == "false") || (str == "off") || (str == "0"))
+ return false;
+ else
+ return default_val;
+ } else {
return default_val;
}
- else {
- return default_val;
- }
- }
+}
- void
- prefs::set_bool(const std::string &section, const std::string &option,
- bool val)
- {
+void prefs::set_bool(const std::string& section, const std::string& option, bool val)
+{
std::string s = section;
std::transform(section.begin(), section.end(), s.begin(), ::tolower);
@@ -284,31 +273,28 @@ namespace gr {
opt_map[o] = sstr.str();
d_config_map[s] = opt_map;
- }
-
- long
- prefs::get_long(const std::string &section, const std::string &option,
- long default_val)
- {
- if(has_option(section, option)) {
- std::string str = get_string(section, option, "");
- if(str == "") {
+}
+
+long prefs::get_long(const std::string& section,
+ const std::string& option,
+ long default_val)
+{
+ if (has_option(section, option)) {
+ std::string str = get_string(section, option, "");
+ if (str == "") {
+ return default_val;
+ }
+ std::stringstream sstr(str);
+ long n;
+ sstr >> n;
+ return n;
+ } else {
return default_val;
- }
- std::stringstream sstr(str);
- long n;
- sstr >> n;
- return n;
- }
- else {
- return default_val;
}
- }
+}
- void
- prefs::set_long(const std::string &section, const std::string &option,
- long val)
- {
+void prefs::set_long(const std::string& section, const std::string& option, long val)
+{
std::string s = section;
std::transform(section.begin(), section.end(), s.begin(), ::tolower);
@@ -322,31 +308,28 @@ namespace gr {
opt_map[o] = sstr.str();
d_config_map[s] = opt_map;
- }
-
- double
- prefs::get_double(const std::string &section, const std::string &option,
- double default_val)
- {
- if(has_option(section, option)) {
- std::string str = get_string(section, option, "");
- if(str == "") {
+}
+
+double prefs::get_double(const std::string& section,
+ const std::string& option,
+ double default_val)
+{
+ if (has_option(section, option)) {
+ std::string str = get_string(section, option, "");
+ if (str == "") {
+ return default_val;
+ }
+ std::stringstream sstr(str);
+ double n;
+ sstr >> n;
+ return n;
+ } else {
return default_val;
- }
- std::stringstream sstr(str);
- double n;
- sstr >> n;
- return n;
- }
- else {
- return default_val;
}
- }
+}
- void
- prefs::set_double(const std::string &section, const std::string &option,
- double val)
- {
+void prefs::set_double(const std::string& section, const std::string& option, double val)
+{
std::string s = section;
std::transform(section.begin(), section.end(), s.begin(), ::tolower);
@@ -360,6 +343,6 @@ namespace gr {
opt_map[o] = sstr.str();
d_config_map[s] = opt_map;
- }
+}
} /* namespace gr */