From 5e9937e0f09ee4890663388f36cfaafcf1e8f86e Mon Sep 17 00:00:00 2001 From: Nomango Date: Sat, 17 Oct 2020 05:34:35 +0800 Subject: [PATCH] [deploy] add ConfigIni::LoadFromString & ConfigIni::SaveToString --- src/kiwano/utils/ConfigIni.cpp | 22 ++++++++++++++++++++-- src/kiwano/utils/ConfigIni.h | 10 ++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/kiwano/utils/ConfigIni.cpp b/src/kiwano/utils/ConfigIni.cpp index 118cca95..529d190c 100644 --- a/src/kiwano/utils/ConfigIni.cpp +++ b/src/kiwano/utils/ConfigIni.cpp @@ -34,9 +34,9 @@ StringView Trim(StringView str) if (!str.IsEmpty()) { std::size_t start = 0, end = str.GetLength(); - while (std::isspace(str[start])) + while (start < end && std::isspace(str[start])) ++start; - while (std::isspace(str[end - 1])) + while (end > 0&& std ::isspace(str[end - 1])) --end; if (end > start) @@ -132,6 +132,13 @@ bool ConfigIni::Load(std::istream& istream) return true; } +bool ConfigIni::LoadFromString(const String& content) +{ + StringStream ss; + ss.str(content); + return Load(ss); +} + bool ConfigIni::Save(const String& file_path) { std::ofstream ofs(file_path); @@ -176,6 +183,17 @@ bool ConfigIni::Save(std::ostream& os) return true; } +bool ConfigIni::SaveToString(String& content) +{ + StringStream ss; + if (Save(ss)) + { + content = ss.str(); + return true; + } + return false; +} + ConfigIni::SectionMap ConfigIni::GetSectionMap() const { return sections_; diff --git a/src/kiwano/utils/ConfigIni.h b/src/kiwano/utils/ConfigIni.h index b0a4f8b1..b8b662e1 100644 --- a/src/kiwano/utils/ConfigIni.h +++ b/src/kiwano/utils/ConfigIni.h @@ -57,6 +57,11 @@ public: /// @param is 输入流 bool Load(std::istream& is); + /// \~chinese + /// @brief 加载 ini 格式字符串 + /// @param content 输入内容 + bool LoadFromString(const String& content); + /// \~chinese /// @brief 保存至 ini 文件 /// @param file_path 文件路径 @@ -67,6 +72,11 @@ public: /// @param os 输出流 bool Save(std::ostream& os); + /// \~chinese + /// @brief 保存至字符串 + /// @param content 输出内容 + bool SaveToString(String& content); + /// \~chinese /// @brief 是否存在section /// @param section section的名称