diff options
Diffstat (limited to 'cpp/pom.cpp')
-rw-r--r-- | cpp/pom.cpp | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/cpp/pom.cpp b/cpp/pom.cpp index 7c2651c..ffa70a2 100644 --- a/cpp/pom.cpp +++ b/cpp/pom.cpp @@ -5,6 +5,7 @@ #include <cstdlib> #include <string> #include <istream> +#include <iostream> namespace pom { @@ -55,6 +56,7 @@ std::ostream &operator<<(std::ostream &o, Error &e) { } Configuration Configuration::section(std::string_view name) const { + if (!C) return {}; std::string name_str(name); const pom_conf *C_section = pom_conf_section( static_cast<const pom_conf *>(C), name_str.c_str()); @@ -95,8 +97,8 @@ static size_t readable_read(void *udata, char *buf, size_t count) { return static_cast<Reader *>(udata)->read(buf, count); } -Configuration::Configuration(std::string_view filename, Reader &source, const Settings *settings) { - pom_settings C_settings = {}; +void Configuration::load(std::string_view filename, Reader &source, const Settings *settings) { + pom_settings C_settings = {}; if (settings) { settings->to_C(static_cast<void *>(&C_settings)); } @@ -108,8 +110,12 @@ Configuration::Configuration(std::string_view filename, Reader &source, const Se } } +Configuration::Configuration(std::string_view filename, Reader &source, const Settings *settings) { + load(filename, source, settings); +} + Configuration::Configuration(std::string_view path, const Settings *settings) { - pom_settings C_settings = {}; + pom_settings C_settings = {}; if (settings) { settings->to_C(static_cast<void *>(&C_settings)); } @@ -135,7 +141,7 @@ private: Configuration::Configuration(std::string_view filename, std::string_view string, const Settings *settings) { StringViewReader reader(string); - Configuration(filename, reader, settings); + load(filename, reader, settings); } class IStreamReader: public Reader { @@ -152,7 +158,7 @@ private: Configuration::Configuration(std::string_view filename, std::istream &stream, const Settings *settings) { IStreamReader reader(stream); - Configuration(filename, reader, settings); + load(filename, reader, settings); } void Configuration::merge(const Configuration &other) { @@ -183,6 +189,7 @@ std::string Configuration::get_or_default(std::string_view key, std::string_view } std::vector<std::string> Configuration::unread_keys() const { + if (!C) return {}; std::vector<std::string> unread; pom_unread_key_iter *iter = nullptr; const char *key; @@ -193,6 +200,7 @@ std::vector<std::string> Configuration::unread_keys() const { } std::vector<std::string> Configuration::keys() const { + if (!C) return {}; std::vector<std::string> keys; pom_key_iter *iter = nullptr; const char *key; @@ -220,6 +228,7 @@ public: }; std::vector<std::shared_ptr<Item>> Configuration::items() const { + if (!C) return {}; std::vector<std::shared_ptr<Item>> items; pom_item_iter *iter = nullptr; const pom_item *item; @@ -230,6 +239,7 @@ std::vector<std::shared_ptr<Item>> Configuration::items() const { } std::optional<int64_t> Configuration::get_int(std::string_view key) const { + if (!C) return {}; std::string key_str(key); if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { return {}; @@ -242,15 +252,11 @@ std::optional<int64_t> Configuration::get_int(std::string_view key) const { } int64_t Configuration::get_int_or_default(std::string_view key, int64_t dflt) const { - std::string key_str(key); - int64_t value; - pom_error *error = pom_conf_get_int_or_default(static_cast<const pom_conf *>(C), key_str.c_str(), &value, dflt); - if (error) - throw Error(error); - return value; + return get_int(key).value_or(dflt); } std::optional<uint64_t> Configuration::get_uint(std::string_view key) const { + if (!C) return {}; std::string key_str(key); if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { return {}; @@ -263,15 +269,11 @@ std::optional<uint64_t> Configuration::get_uint(std::string_view key) const { } uint64_t Configuration::get_uint_or_default(std::string_view key, uint64_t dflt) const { - std::string key_str(key); - uint64_t value; - pom_error *error = pom_conf_get_uint_or_default(static_cast<const pom_conf *>(C), key_str.c_str(), &value, dflt); - if (error) - throw Error(error); - return value; + return get_uint(key).value_or(dflt); } std::optional<double> Configuration::get_float(std::string_view key) const { + if (!C) return {}; std::string key_str(key); if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { return {}; @@ -284,15 +286,11 @@ std::optional<double> Configuration::get_float(std::string_view key) const { } double Configuration::get_float_or_default(std::string_view key, double dflt) const { - std::string key_str(key); - double value; - pom_error *error = pom_conf_get_float_or_default(static_cast<const pom_conf *>(C), key_str.c_str(), &value, dflt); - if (error) - throw Error(error); - return value; + return get_float(key).value_or(dflt); } std::optional<bool> Configuration::get_bool(std::string_view key) const { + if (!C) return {}; std::string key_str(key); if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { return {}; @@ -305,15 +303,30 @@ std::optional<bool> Configuration::get_bool(std::string_view key) const { } bool Configuration::get_bool_or_default(std::string_view key, bool dflt) const { + return get_bool(key).value_or(dflt); +} + +std::optional<std::vector<std::string>> Configuration::get_list(std::string_view key) const { + if (!C) return {}; std::string key_str(key); - bool value; - pom_error *error = pom_conf_get_bool_or_default(static_cast<const pom_conf *>(C), key_str.c_str(), &value, dflt); - if (error) - throw Error(error); - return value; + char **list = pom_conf_get_list(static_cast<const pom_conf *>(C), key_str.c_str()); + std::vector<std::string> vec; + for (size_t i = 0; list[i]; i++) + vec.emplace_back(list[i]); + free(list); + return vec; +} + +std::vector<std::string> Configuration::get_list_or_default(std::string_view key, const std::vector<std::string> &dflt) const { + auto list = get_list(key); + if (list.has_value()) + return list.value(); + else + return dflt; } Configuration &Configuration::operator=(const Configuration &other) { + pom_conf_free(static_cast<pom_conf *>(C)); C = static_cast<void *>(pom_conf_copy(static_cast<const pom_conf *>(other.C))); return *this; } @@ -324,5 +337,19 @@ std::ostream &operator<<(std::ostream &o, const Configuration &conf) { return o; } +std::optional<Location> Configuration::location(std::string_view key) const { + if (!C) return {}; + const char *file; + uint64_t line; + std::string key_str(key); + if (pom_conf_location(static_cast<const pom_conf *>(C), key_str.c_str(), &file, &line)) + return Location(file, line); + else + return {}; +} + +std::ostream &operator<<(std::ostream &o, const Location &location) { + return o << location.file() << ":" << location.line(); +} } // namespace pom |