[deploy] add ConfigIni::LoadFromString & ConfigIni::SaveToString

This commit is contained in:
Nomango 2020-10-17 05:34:35 +08:00
parent 4587e5a148
commit 5e9937e0f0
2 changed files with 30 additions and 2 deletions

View File

@ -34,9 +34,9 @@ StringView Trim(StringView str)
if (!str.IsEmpty()) if (!str.IsEmpty())
{ {
std::size_t start = 0, end = str.GetLength(); std::size_t start = 0, end = str.GetLength();
while (std::isspace(str[start])) while (start < end && std::isspace(str[start]))
++start; ++start;
while (std::isspace(str[end - 1])) while (end > 0&& std ::isspace(str[end - 1]))
--end; --end;
if (end > start) if (end > start)
@ -132,6 +132,13 @@ bool ConfigIni::Load(std::istream& istream)
return true; return true;
} }
bool ConfigIni::LoadFromString(const String& content)
{
StringStream ss;
ss.str(content);
return Load(ss);
}
bool ConfigIni::Save(const String& file_path) bool ConfigIni::Save(const String& file_path)
{ {
std::ofstream ofs(file_path); std::ofstream ofs(file_path);
@ -176,6 +183,17 @@ bool ConfigIni::Save(std::ostream& os)
return true; return true;
} }
bool ConfigIni::SaveToString(String& content)
{
StringStream ss;
if (Save(ss))
{
content = ss.str();
return true;
}
return false;
}
ConfigIni::SectionMap ConfigIni::GetSectionMap() const ConfigIni::SectionMap ConfigIni::GetSectionMap() const
{ {
return sections_; return sections_;

View File

@ -57,6 +57,11 @@ public:
/// @param is 输入流 /// @param is 输入流
bool Load(std::istream& is); bool Load(std::istream& is);
/// \~chinese
/// @brief 加载 ini 格式字符串
/// @param content 输入内容
bool LoadFromString(const String& content);
/// \~chinese /// \~chinese
/// @brief 保存至 ini 文件 /// @brief 保存至 ini 文件
/// @param file_path 文件路径 /// @param file_path 文件路径
@ -67,6 +72,11 @@ public:
/// @param os 输出流 /// @param os 输出流
bool Save(std::ostream& os); bool Save(std::ostream& os);
/// \~chinese
/// @brief 保存至字符串
/// @param content 输出内容
bool SaveToString(String& content);
/// \~chinese /// \~chinese
/// @brief 是否存在section /// @brief 是否存在section
/// @param section section的名称 /// @param section section的名称