From a25597223d06b14d42c0438a40b1eb9d7264b23e Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 28 May 2020 17:24:54 +0800 Subject: [PATCH] update ConfigIni --- src/kiwano/utils/ConfigIni.cpp | 39 ++++++++++++++++++++++------------ src/kiwano/utils/ConfigIni.h | 37 ++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/kiwano/utils/ConfigIni.cpp b/src/kiwano/utils/ConfigIni.cpp index 1b70c758..f6fd924c 100644 --- a/src/kiwano/utils/ConfigIni.cpp +++ b/src/kiwano/utils/ConfigIni.cpp @@ -39,7 +39,7 @@ StringView Trim(StringView str) while (std::isspace(str[end - 1])) --end; - if (end - start) + if (end > start) return StringView(str.Data() + start, end - start); } return StringView(); @@ -177,23 +177,20 @@ ConfigIni::SectionMap ConfigIni::GetSectionMap() const ConfigIni::ValueMap ConfigIni::GetSection(const String& section) const { - auto iter = sections_.find(section); - if (iter != sections_.end()) - return iter->second; + if (HasSection(section)) + { + return sections_.at(section); + } return ValueMap(); } -String ConfigIni::GetString(const String& section_name, const String& key) const +String ConfigIni::GetString(const String& section, const String& key, const String& default_value) const { - if (HasSection(section_name)) + if (HasKey(section, key)) { - const auto& section = sections_.at(section_name); - - auto iter_key = section.find(key); - if (iter_key != section.end()) - return iter_key->second; + return sections_.at(section).at(key); } - return String(); + return default_value; } float ConfigIni::GetFloat(const String& section, const String& key, float default_value) const @@ -274,7 +271,7 @@ bool ConfigIni::HasSection(const String& section) const return !!sections_.count(section); } -bool ConfigIni::HasValue(const String& section, const String& key) const +bool ConfigIni::HasKey(const String& section, const String& key) const { if (HasSection(section)) { @@ -324,6 +321,22 @@ void ConfigIni::SetBool(const String& section, const String& key, bool value) SetString(section, key, value ? "true" : "false"); } +void ConfigIni::DeleteSection(const String& section) +{ + if (HasSection(section)) + { + sections_.erase(section); + } +} + +void ConfigIni::DeleteKey(const String& section, const String& key) +{ + if (HasKey(section, key)) + { + sections_.at(section).erase(key); + } +} + ConfigIni::ValueMap& ConfigIni::operator[](const String& section) { if (!HasSection(section)) diff --git a/src/kiwano/utils/ConfigIni.h b/src/kiwano/utils/ConfigIni.h index e83434f2..07cc18a0 100644 --- a/src/kiwano/utils/ConfigIni.h +++ b/src/kiwano/utils/ConfigIni.h @@ -39,7 +39,7 @@ public: /// \~chinese /// @brief 键值字典 - typedef Map ValueMap; + typedef UnorderedMap ValueMap; /// \~chinese /// @brief Section字典 @@ -65,6 +65,17 @@ public: /// @param os 输出流 bool Save(std::ostream& os); + /// \~chinese + /// @brief 是否存在section + /// @param section section的名称 + bool HasSection(const String& section) const; + + /// \~chinese + /// @brief 是否存在值 + /// @param section section的名称 + /// @param key key的名称 + bool HasKey(const String& section, const String& key) const; + /// \~chinese /// @brief 获取所有section SectionMap GetSectionMap() const; @@ -78,7 +89,7 @@ public: /// @brief 获取值 /// @param section section的名称 /// @param key key的名称 - String GetString(const String& section, const String& key) const; + String GetString(const String& section, const String& key, const String& default_value = String()) const; /// \~chinese /// @brief 获取值 @@ -108,17 +119,6 @@ public: /// @param default_value 不存在时的默认值 bool GetBool(const String& section, const String& key, bool default_value = false) const; - /// \~chinese - /// @brief 是否存在section - /// @param section section的名称 - bool HasSection(const String& section) const; - - /// \~chinese - /// @brief 是否存在值 - /// @param section section的名称 - /// @param key key的名称 - bool HasValue(const String& section, const String& key) const; - /// \~chinese /// @brief 设置所有section /// @param sections section字典 @@ -165,6 +165,17 @@ public: /// @param value 值 void SetBool(const String& section, const String& key, bool value); + /// \~chinese + /// @brief 删除section + /// @param section section的名称 + void DeleteSection(const String& section); + + /// \~chinese + /// @brief 删除值 + /// @param section section的名称 + /// @param key key的名称 + void DeleteKey(const String& section, const String& key); + ValueMap& operator[](const String& section); const ValueMap& operator[](const String& section) const;