diff options
author | pommicket <pommicket@gmail.com> | 2025-09-16 15:02:39 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-16 15:02:39 -0400 |
commit | 7ededc16f36099acbfac457f8e4e268879b0ce62 (patch) | |
tree | 35543f851383a5002566b3b0a8ea5f38d97e94c7 /cpp/pom.cpp | |
parent | 76f68d6c93b55f9cd96a369bf2e5785ed16fa184 (diff) |
Finish up implementation of C++ library
Diffstat (limited to 'cpp/pom.cpp')
-rw-r--r-- | cpp/pom.cpp | 167 |
1 files changed, 160 insertions, 7 deletions
diff --git a/cpp/pom.cpp b/cpp/pom.cpp index 4e4f7e0..7c2651c 100644 --- a/cpp/pom.cpp +++ b/cpp/pom.cpp @@ -41,10 +41,6 @@ std::string_view Error::message() const noexcept { return pom_error_message(static_cast<const pom_error *>(C)); } -const Error *Error::next() const noexcept { - return m_next.get(); -} - const char *Error::what() const noexcept { return m_is_original ? pom_error_to_string(static_cast<pom_error *>(C)) : pom_error_message(static_cast<const pom_error *>(C)); @@ -54,6 +50,10 @@ std::string_view Error::to_string() noexcept { return what(); } +std::ostream &operator<<(std::ostream &o, Error &e) { + return o << e.to_string(); +} + Configuration Configuration::section(std::string_view name) const { std::string name_str(name); const pom_conf *C_section = pom_conf_section( @@ -74,6 +74,12 @@ static void allocator_free(void *udata, void *ptr) { return static_cast<Allocator *>(udata)->free(ptr); } +void Settings::set_error_language(std::string_view lang) { + size_t len = std::min(sizeof m_error_lang - 1, lang.size()); + memcpy(m_error_lang, lang.data(), len); + m_error_lang[len] = 0; +} + void Settings::to_C(void *C) const { pom_settings &C_settings = *static_cast<pom_settings *>(C); strcpy(C_settings.error_lang, m_error_lang); @@ -158,7 +164,7 @@ void Configuration::merge(const Configuration &other) { pom_conf_merge(static_cast<pom_conf *>(C), static_cast<const pom_conf *>(other.C)); } -std::optional<std::string_view> Configuration::get(std::string_view key) const { +std::optional<std::string> Configuration::get(std::string_view key) const { if (!C) return {}; std::string key_str(key); const char *value = pom_conf_get(static_cast<const pom_conf *>(C), key_str.c_str()); @@ -168,8 +174,155 @@ std::optional<std::string_view> Configuration::get(std::string_view key) const { return value; } -std::string_view Configuration::get_or_default(std::string_view key, std::string_view dflt) const { - return get(key).value_or(dflt); +std::string Configuration::get_or_default(std::string_view key, std::string_view dflt) const { + auto value = get(key); + if (value.has_value()) + return value.value(); + else + return std::string(dflt); } +std::vector<std::string> Configuration::unread_keys() const { + std::vector<std::string> unread; + pom_unread_key_iter *iter = nullptr; + const char *key; + while ((key = pom_conf_next_unread_key(static_cast<const pom_conf *>(C), &iter))) { + unread.push_back(key); + } + return unread; +} + +std::vector<std::string> Configuration::keys() const { + std::vector<std::string> keys; + pom_key_iter *iter = nullptr; + const char *key; + while ((key = pom_conf_next_key(static_cast<const pom_conf *>(C), &iter))) { + keys.push_back(key); + } + return keys; +} + +class ItemImpl: public Item { +public: + ItemImpl(const pom_item *C) { + m_key = C->key; + m_value = C->value; + m_file = C->file; + m_line = C->line; + } + virtual ~ItemImpl() {} + virtual std::string_view key() const noexcept override { return m_key; } + virtual std::string_view value() const noexcept override { return m_value; } + virtual std::string_view file() const noexcept override { return m_file; } + virtual uint64_t line() const noexcept override { return m_line; } + std::string m_key, m_value, m_file; + uint64_t m_line; +}; + +std::vector<std::shared_ptr<Item>> Configuration::items() const { + std::vector<std::shared_ptr<Item>> items; + pom_item_iter *iter = nullptr; + const pom_item *item; + while ((item = pom_conf_next_item(static_cast<const pom_conf *>(C), &iter))) { + items.push_back(std::shared_ptr<Item>(new ItemImpl(item))); + } + return items; +} + +std::optional<int64_t> Configuration::get_int(std::string_view key) const { + std::string key_str(key); + if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { + return {}; + } + int64_t value; + pom_error *error = pom_conf_get_int(static_cast<const pom_conf *>(C), key_str.c_str(), &value); + if (error) + throw Error(error); + return value; +} + +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; +} + +std::optional<uint64_t> Configuration::get_uint(std::string_view key) const { + std::string key_str(key); + if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { + return {}; + } + uint64_t value; + pom_error *error = pom_conf_get_uint(static_cast<const pom_conf *>(C), key_str.c_str(), &value); + if (error) + throw Error(error); + return value; +} + +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; +} + +std::optional<double> Configuration::get_float(std::string_view key) const { + std::string key_str(key); + if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { + return {}; + } + double value; + pom_error *error = pom_conf_get_float(static_cast<const pom_conf *>(C), key_str.c_str(), &value); + if (error) + throw Error(error); + return value; +} + +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; +} + +std::optional<bool> Configuration::get_bool(std::string_view key) const { + std::string key_str(key); + if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) { + return {}; + } + bool value; + pom_error *error = pom_conf_get_bool(static_cast<const pom_conf *>(C), key_str.c_str(), &value); + if (error) + throw Error(error); + return value; +} + +bool Configuration::get_bool_or_default(std::string_view key, bool dflt) const { + 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; +} + +Configuration &Configuration::operator=(const Configuration &other) { + C = static_cast<void *>(pom_conf_copy(static_cast<const pom_conf *>(other.C))); + return *this; +} + +std::ostream &operator<<(std::ostream &o, const Configuration &conf) { + for (auto item: conf.items()) + o << item->key() << ": " << item->value() << "\n"; + return o; +} + + } // namespace pom |