update ConfigIni

This commit is contained in:
Nomango 2020-05-28 17:24:54 +08:00
parent bd8b328d01
commit a25597223d
2 changed files with 50 additions and 26 deletions

View File

@ -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))

View File

@ -39,7 +39,7 @@ public:
/// \~chinese
/// @brief 键值字典
typedef Map<String, String> ValueMap;
typedef UnorderedMap<String, String> 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;