lib45d
45Drives C++ Library Development Documentation
|
23 #include <45d/Quota.hpp>
24 #include <45d/config/ConfigNode.hpp>
25 #include <45d/config/Exceptions.hpp>
28 #include <unordered_map>
49 T
get(
const std::string &key,
50 const std::unordered_map<std::string, ffd::ConfigNode> *config_map) {
52 std::stringstream ss(node.
value_);
53 ss.exceptions(std::ios::failbit | std::ios::badbit);
105 T
get(
const std::string &key)
const {
120 T
get(
const std::string &key,
const T &fallback)
const noexcept {
123 }
catch (
const std::out_of_range &) {
125 }
catch (
const std::ios_base::failure &) {
127 report_error(
"Invalid configuration entry format: " + key +
" = "
129 }
catch (
const std::out_of_range &) {
130 report_error(
"Invalid configuration entry format for " + key);
134 }
catch (
const std::exception &e) {
135 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
137 report_error(
"Unexplained exception caught while getting " + key);
155 T
get(
const std::string &key,
bool *fail_flag)
const noexcept {
158 }
catch (
const std::ios_base::failure &) {
160 report_error(
"Invalid configuration entry format: " + key +
" = "
162 }
catch (
const std::out_of_range &) {
163 report_error(
"Invalid configuration entry format for " + key);
165 }
catch (
const std::out_of_range &) {
169 }
catch (
const std::exception &e) {
170 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
172 report_error(
"Unexplained exception caught while getting " + key);
175 if (std::is_fundamental<T>::value)
190 T
get_from(
const std::string §ion,
const std::string &key) {
193 "Cannot call get_from while ConfigSubsectionGuard is in scope"));
195 T result = get<T>(key);
213 T
get_from(
const std::string §ion,
const std::string &key,
const T &fallback) noexcept {
215 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
221 }
catch (
const std::out_of_range &) {
225 T result = get<T>(key, fallback);
240 T
get_from(
const std::string §ion,
const std::string &key,
bool *fail_flag) noexcept {
242 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
245 if (std::is_fundamental<T>::value)
252 }
catch (
const std::out_of_range &) {
253 std::cerr <<
"Section not in config: " << section << std::endl;
256 if (std::is_fundamental<T>::value)
261 T result = get<T>(key, fail_flag);
282 return Quota(max, get<std::string>(key));
296 }
catch (
const std::out_of_range &) {
298 }
catch (
const std::ios_base::failure &) {
300 report_error(
"Invalid configuration entry format: " + key +
" = "
302 }
catch (
const std::out_of_range &) {
303 report_error(
"Invalid configuration entry format for " + key);
307 }
catch (
const std::exception &e) {
308 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
310 report_error(
"Unexplained exception caught while getting " + key);
327 }
catch (
const std::ios_base::failure &) {
329 report_error(
"Invalid configuration entry format: " + key +
" = "
331 }
catch (
const std::out_of_range &) {
332 report_error(
"Invalid configuration entry format for " + key);
334 }
catch (
const std::out_of_range &) {
338 }
catch (
const std::exception &e) {
339 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
341 report_error(
"Unexplained exception caught while getting " + key);
358 "Cannot call get_from while ConfigSubsectionGuard is in scope"));
378 const std::string &key,
380 const Quota &fallback) noexcept {
382 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
388 }
catch (
const std::out_of_range &) {
407 const std::string &key,
409 bool *fail_flag) noexcept {
411 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
418 }
catch (
const std::out_of_range &) {
419 std::cerr <<
"Section not in config." << std::endl;
438 std::unordered_map<std::string, ConfigNode>
441 std::unordered_map<std::string, ConfigNode>
456 void parse(std::ifstream &file);
482 }
catch (
const std::out_of_range &) {
483 throw std::out_of_range(
"No subconfig with name `" + section +
"`");
486 throw std::out_of_range(
"`" + section +
"` is not a subsection.");
506 std::cerr << message << std::endl;
ConfigParser(std::string path)
Construct a new Config Parser object.
Definition: config_parser.cpp:25
Use this class for byte-formatted values. e.g.: "123 KiB".
Definition: Bytes.hpp:32
T get_from(const std::string §ion, const std::string &key)
Adapter for ffd::get(). Sets config_map_ptr_ to address of sub config with name section....
Definition: ConfigParser.hpp:190
Quota get_quota(const std::string &key, const Bytes &max, const Quota &fallback) const noexcept
Try to get Quota from config, default to fallback if fails. Guaranteed no-throw.
Definition: ConfigParser.hpp:293
This class extends ffd::Bytes to specify percents of an amount of bytes.
Definition: Quota.hpp:30
T get(const std::string &key, bool *fail_flag) const noexcept
Try to get value from config. If ffd::get fails, return T() or 0 and set fail_flag....
Definition: ConfigParser.hpp:155
std::string dump_str(void) const
Dump config to stdout as a string.
Definition: config_parser.cpp:79
Use this to switch to a certain config subsection to get a group of values.
Definition: ConfigSubsectionGuard.hpp:49
T get(const std::string &key, const std::unordered_map< std::string, ffd::ConfigNode > *config_map)
Get config entry as type T from configuration map.
Definition: ConfigParser.hpp:49
void parse_entry(const std::string &line)
Extract value from config line and insert ConfigNode into config_map_.
Definition: config_parser.cpp:57
T get_from(const std::string §ion, const std::string &key, bool *fail_flag) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,bool*) const noexcept,...
Definition: ConfigParser.hpp:240
T get(const std::string &key, const T &fallback) const noexcept
Try to get value from config, default to fallback if fails. Guaranteed no-throw.
Definition: ConfigParser.hpp:120
void parse(std::ifstream &file)
Iterate each line of config file and determine how to parse with l::check_record_type()
Definition: config_parser.cpp:37
T get(const std::string &key) const
Get value from config map using ffd::get(). This can throw. Use this in a try...catch block.
Definition: ConfigParser.hpp:105
45Drives namespace
Definition: Bytes.hpp:27
Quota get_quota(const std::string &key, const Bytes &max, bool *fail_flag) const noexcept
Try to get Quota from config. If ffd::get fails, return Quota(void) and set fail_flag....
Definition: ConfigParser.hpp:324
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max, bool *fail_flag) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,bool*) const noexcept,...
Definition: ConfigParser.hpp:406
const char * what(void) const noexcept
Return string containing explanation message.
Definition: Exceptions.hpp:44
Quota get_quota(const std::string &key, const Bytes &max) const
Get quota from config map using ffd::get(). This can throw. Use this in a try...catch block.
Definition: ConfigParser.hpp:281
std::unordered_map< std::string, ConfigNode > config_map_
Map of config keys (std::string) to values (ConfigNode)
Definition: ConfigParser.hpp:442
bool guarded_
true if a ConfigSubsectionGuard is in scope Set in ConfigSubsectionGuard::ConfigSubsectionGuard() Cle...
Definition: ConfigParser.hpp:437
General exception for all Config* related issues.
Definition: Exceptions.hpp:30
Throw this exception when a ConfigGuard is constructed or get_from() is called when the config is alr...
Definition: Exceptions.hpp:58
std::string current_section_
Name of current section, set by set_subsection()
Definition: ConfigParser.hpp:430
void set_subsection(const std::string §ion)
Update config_map_ptr_ to the subconfig map for section.
Definition: ConfigParser.hpp:479
T get_from(const std::string §ion, const std::string &key, const T &fallback) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,const T&) const noexcept,...
Definition: ConfigParser.hpp:213
std::string value_
string from config file after '='
Definition: ConfigNode.hpp:33
std::unordered_map< std::string, ConfigNode > * config_map_ptr_
Pointer to current config map.
Definition: ConfigParser.hpp:439
std::vector< ConfigNode * > sub_confs_
Vector of config subsections.
Definition: ConfigParser.hpp:429
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max, const Quota &fallback) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,const T&) const noexcept,...
Definition: ConfigParser.hpp:377
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max)
Adapter for ffd::get(). Sets config_map_ptr_ to address of sub config with name section....
Definition: ConfigParser.hpp:355
void reset_subsection(void) noexcept
Set config_map_ptr_ back to the address of config_map_.
Definition: ConfigParser.hpp:493
void report_error(const std::string &message) const noexcept
Print error message to stderr, conditionally prepended with current subsection name.
Definition: ConfigParser.hpp:503
void parse_heading(const std::string &line)
Create new subconfig.
Definition: config_parser.cpp:68
Main configuration parser class to inherit from in your code.
Definition: ConfigParser.hpp:68
Class for config_map_ entries.
Definition: ConfigNode.hpp:31
Namespace for internal use, not to be exposed to ffd.
Definition: ConfigParser.hpp:37
std::string path_
Path to config file.
Definition: ConfigParser.hpp:440