284 lines
6.4 KiB
C++
284 lines
6.4 KiB
C++
#include "dl_file_ini.h"
|
|
|
|
#include "dl_file.h"
|
|
|
|
namespace dl
|
|
{
|
|
bool FileIniSectionNode::Set(std::string_view key, std::string_view value)
|
|
{
|
|
std::string str{ key };
|
|
FileIniLine sec;
|
|
sec.value = std::string{ value };
|
|
sec.key = std::string{ key };
|
|
auto iter = _mapKeyIndex.find(str);
|
|
if (iter == _mapKeyIndex.end())
|
|
{
|
|
_vecData.push_back(sec);
|
|
_mapKeyIndex[str] = _vecData.size();
|
|
}
|
|
else
|
|
_vecData[iter->second - 1] = sec;
|
|
return true;
|
|
}
|
|
|
|
bool FileIniSectionNode::Add(std::string_view key, std::string_view value)
|
|
{
|
|
std::string str{ key };
|
|
auto iter = _mapKeyIndex.find(str);
|
|
if (iter != _mapKeyIndex.end())
|
|
{
|
|
log_err("已存在");
|
|
return false;
|
|
}
|
|
FileIniLine sec;
|
|
sec.value = std::string{ value };
|
|
sec.key = str;
|
|
_vecData.push_back(sec);
|
|
_mapKeyIndex[str] = _vecData.size();
|
|
return true;
|
|
}
|
|
|
|
bool FileIniSectionNode::Change(std::string_view key, std::string_view value)
|
|
{
|
|
std::string str{ key };
|
|
auto iter = _mapKeyIndex.find(str);
|
|
if (iter == _mapKeyIndex.end())
|
|
return false;
|
|
_vecData[iter->second - 1].value = std::string{ value };
|
|
return true;
|
|
}
|
|
|
|
bool FileIniSectionNode::Del(std::string_view key)
|
|
{
|
|
auto iter0 = _mapKeyIndex.find(std::string{ key });
|
|
if (iter0 == _mapKeyIndex.end())
|
|
{
|
|
log_err("要删除的值不存在");
|
|
return false;
|
|
}
|
|
if (iter0->second > _vecData.size())
|
|
{
|
|
log_err("要删除的值不存在");
|
|
return false;
|
|
}
|
|
_vecData.erase(_vecData.begin() + (iter0->second - 1));
|
|
_mapKeyIndex.erase(iter0);
|
|
return true;
|
|
}
|
|
|
|
bool FileIniSectionNode::Get(std::string_view key, FileIniLine& ret)
|
|
{
|
|
auto iter0 = _mapKeyIndex.find(std::string{ key });
|
|
if (iter0 == _mapKeyIndex.end())
|
|
{
|
|
log_err("查找的键不存在");
|
|
return false;
|
|
}
|
|
size_t size = _vecData.size();
|
|
if (size < iter0->second)
|
|
{
|
|
log_err("查找的键不存在");
|
|
return false;
|
|
}
|
|
ret = _vecData[iter0->second - 1];
|
|
return true;
|
|
}
|
|
|
|
|
|
void FileIniSection::Set(std::string_view key, std::string_view value)
|
|
{
|
|
size_t idx = _allSectionNode.back();
|
|
_ini->_allSection[idx].Set(key, value);
|
|
}
|
|
|
|
void FileIniSection::Add(std::string_view key, std::string_view value)
|
|
{
|
|
size_t idx = _allSectionNode.back();
|
|
_ini->_allSection[idx].Add(key, value);
|
|
}
|
|
|
|
bool FileIniSection::Change(std::string_view key, std::string_view value)
|
|
{
|
|
size_t idx = _allSectionNode.back();
|
|
if (_ini->_allSection[idx].Change(key, value))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
bool FileIniSection::Del(std::string_view key)
|
|
{
|
|
for (auto i : _allSectionNode)
|
|
{
|
|
if (_ini->_allSection[i].Del(key))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::string FileIniSection::Get(std::string_view key)
|
|
{
|
|
FileIniLine ret;
|
|
for (auto i : _allSectionNode)
|
|
{
|
|
if (_ini->_allSection[i].Get(key, ret))
|
|
return ret.value;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
FileIni::FileIni()
|
|
:_secNull{ this }
|
|
{
|
|
|
|
}
|
|
|
|
bool FileIni::Open(std::string_view path_name)
|
|
{
|
|
auto fun_add_sec = [&](FileIniSectionNode& sec_node, const std::string& str)
|
|
{
|
|
_allSection.push_back(sec_node);
|
|
sec_node._vecData.clear();
|
|
sec_node._mapKeyIndex.clear();
|
|
auto iter = _mapSection.find(str);
|
|
if (iter != _mapSection.end())
|
|
{
|
|
iter->second._allSectionNode.push_back(_allSection.size() - 1);
|
|
}
|
|
else
|
|
{
|
|
FileIniSection sec{ this };
|
|
_mapSection[str] = sec;
|
|
_mapSection[str]._allSectionNode.push_back(_allSection.size() - 1);
|
|
}
|
|
};
|
|
|
|
std::string txt = File::GetString(path_name);
|
|
if (txt.empty())
|
|
{
|
|
log_err("文件加载失败:{}", path_name);
|
|
return false;
|
|
}
|
|
std::string filter{ ";" };
|
|
// 先添加一个空section
|
|
FileIniSectionNode cur_sec;
|
|
size_t i = 0;
|
|
std::string cur_key;
|
|
std::string last_key;
|
|
std::vector<std::string> all_line =
|
|
String::Split(txt, '\n');
|
|
for (std::string& line : all_line)
|
|
{
|
|
FileIniLine iline;
|
|
std::vector<std::string> one_line;
|
|
if (String::IsBlank(line))
|
|
iline._vtype = FileIniLine::Type::BLANK;
|
|
else if (line[0] == ';')
|
|
iline._vtype = FileIniLine::Type::COMMENT;
|
|
else if (line[0] == '[')
|
|
{
|
|
cur_key = Sub::Range(line, '[', ']');
|
|
iline._vtype = FileIniLine::Type::SECTION;
|
|
}
|
|
else
|
|
{
|
|
iline._vtype = FileIniLine::Type::VALUE;
|
|
one_line = String::Split(line, '=');
|
|
if (one_line.size() >= 2)
|
|
line = one_line[1];
|
|
else
|
|
line = "";
|
|
iline.key = one_line[0];
|
|
}
|
|
iline.value = line;
|
|
if (cur_key != last_key)
|
|
fun_add_sec(cur_sec, last_key);
|
|
cur_sec._vecData.push_back(iline);
|
|
if (iline._vtype == FileIniLine::Type::VALUE && one_line.size() > 0)
|
|
{
|
|
cur_sec._mapKeyIndex[one_line[0]] = cur_sec._vecData.size();
|
|
}
|
|
else
|
|
cur_sec._mapKeyIndex[line] = cur_sec._vecData.size();
|
|
last_key = cur_key;
|
|
i++;
|
|
if (i == all_line.size())
|
|
fun_add_sec(cur_sec, cur_key);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool FileIni::Save(std::string_view path_name)
|
|
{
|
|
std::string data;
|
|
for (auto& sec : _allSection)
|
|
{
|
|
for (auto& iter : sec._vecData)
|
|
{
|
|
if (iter._vtype == FileIniLine::Type::BLANK)
|
|
{
|
|
data += '\n';
|
|
continue;
|
|
}
|
|
else if (iter._vtype == FileIniLine::Type::COMMENT
|
|
|| iter._vtype == FileIniLine::Type::SECTION)
|
|
{
|
|
data += fmt::format("{}\n", iter.value);
|
|
continue;
|
|
}
|
|
data += fmt::format("{}={}\n", iter.key, iter.value);
|
|
continue;
|
|
}
|
|
}
|
|
return File::SaveString(data, path_name);
|
|
}
|
|
|
|
bool FileIni::DelSection(std::string_view key)
|
|
{
|
|
auto iter = _mapSection.find(std::string{ key });
|
|
if (iter == _mapSection.end())
|
|
{
|
|
log_err("删除键不存在");
|
|
return false;
|
|
}
|
|
for (size_t i : iter->second._allSectionNode)
|
|
{
|
|
if (i > _allSection.size())
|
|
continue;
|
|
iter->second._allSectionNode.erase(iter->second._allSectionNode.begin() + (i - 1));
|
|
}
|
|
_mapSection.erase(iter);
|
|
return true;
|
|
}
|
|
|
|
FileIniSection& FileIni::AddSection(std::string_view name)
|
|
{
|
|
std::string str{ name };
|
|
auto iter = _mapSection.find(str);
|
|
if (iter != _mapSection.end())
|
|
{
|
|
log_err("已存在");
|
|
return iter->second;
|
|
}
|
|
FileIniSection sec{ this };
|
|
_mapSection[str] = sec;
|
|
return iter->second;
|
|
}
|
|
|
|
FileIniSection& FileIni::GetSection(std::string_view key)
|
|
{
|
|
auto iter = _mapSection.find(std::string{ key });
|
|
if (iter == _mapSection.end())
|
|
return _secNull;
|
|
return iter->second;
|
|
}
|
|
|
|
|
|
std::vector<std::string> FileIni::GetAllSectionName()
|
|
{
|
|
std::vector<std::string> ret_vec;
|
|
for (auto it : _mapSection)
|
|
ret_vec.push_back(it.first);
|
|
return ret_vec;
|
|
}
|
|
|
|
} |