Small case namespaces

This commit is contained in:
2025-12-28 23:15:42 +00:00
parent 85295ae4d8
commit ae48f96fe8
64 changed files with 356 additions and 311 deletions
+64 -45
View File
@@ -3,56 +3,75 @@
#include <sstream>
#include <algorithm>
static std::string trim(std::string s)
namespace config
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c)
{ return !isspace(c); }));
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c)
{ return !isspace(c); })
.base(),
s.end());
return s;
}
bool IniParser::load(const std::string &path)
{
std::ifstream f(path);
if (!f)
return false;
std::string line, section;
while (std::getline(f, line))
std::string trim(std::string s)
{
line = trim(line);
if (line.empty() || line[0] == '#' || line[0] == ';')
continue;
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c)
{ return !isspace(c); }));
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c)
{ return !isspace(c); })
.base(),
s.end());
return s;
}
if (line.front() == '[' && line.back() == ']')
std::vector<std::string> splitList(const std::string &s, char delim)
{
std::vector<std::string> result;
std::string item;
std::istringstream ss(s);
while (std::getline(ss, item, delim))
{
section = line.substr(1, line.size() - 2);
continue;
item = trim(item);
if (!item.empty())
result.push_back(item);
}
auto pos = line.find('=');
if (pos == std::string::npos)
continue;
std::string key = trim(line.substr(0, pos));
std::string val = trim(line.substr(pos + 1));
data_[section][key] = val;
return result;
}
return true;
}
std::string IniParser::get(const std::string &section,
const std::string &key,
const std::string &def) const
{
auto s = data_.find(section);
if (s == data_.end())
return def;
auto k = s->second.find(key);
if (k == s->second.end())
return def;
return k->second;
}
bool IniParser::load(const std::string &path)
{
std::ifstream f(path);
if (!f)
return false;
std::string line, section;
while (std::getline(f, line))
{
line = trim(line);
if (line.empty() || line[0] == '#' || line[0] == ';')
continue;
if (line.front() == '[' && line.back() == ']')
{
section = line.substr(1, line.size() - 2);
continue;
}
auto pos = line.find('=');
if (pos == std::string::npos)
continue;
std::string key = trim(line.substr(0, pos));
std::string val = trim(line.substr(pos + 1));
data[section][key] = val;
}
return true;
}
std::string IniParser::get(const std::string &section,
const std::string &key,
const std::string &def) const
{
auto s = data.find(section);
if (s == data.end())
return def;
auto k = s->second.find(key);
if (k == s->second.end())
return def;
return k->second;
}
} // namespace config
+17 -10
View File
@@ -1,17 +1,24 @@
#pragma once
#include <vector>
#include <string>
#include <unordered_map>
class IniParser
namespace config
{
public:
bool load(const std::string &path);
std::string trim(std::string s);
std::vector<std::string> splitList(const std::string &s, char delim = ',');
std::string get(const std::string &section,
const std::string &key,
const std::string &def = "") const;
class IniParser
{
public:
bool load(const std::string &path);
private:
using Section = std::unordered_map<std::string, std::string>;
std::unordered_map<std::string, Section> data_;
};
std::string get(const std::string &section,
const std::string &key,
const std::string &def = "") const;
private:
using Section = std::unordered_map<std::string, std::string>;
std::unordered_map<std::string, Section> data;
};
} // namespace config