Implement multiple values per config key (#1532)

This commit is contained in:
Stanislav Denisov
2026-02-01 17:46:07 +01:00
committed by GitHub
parent 7d2863cf56
commit 3354299255
2 changed files with 128 additions and 0 deletions
+115
View File
@@ -105,6 +105,88 @@ namespace wi::config
return it->second;
}
std::vector<std::string> Section::GetTextArray(const char* name) const
{
std::vector<std::string> result;
const auto it = values.find(name);
if (it == values.end())
{
return result;
}
const std::string& str = it->second;
if (str.empty())
{
return result;
}
size_t start = 0;
size_t end = str.find(',');
while (end != std::string::npos)
{
std::string token = str.substr(start, end - start);
// Trim leading/trailing whitespace
size_t first = token.find_first_not_of(" \t");
size_t last = token.find_last_not_of(" \t");
if (first != std::string::npos)
result.push_back(token.substr(first, last - first + 1));
else
result.push_back("");
start = end + 1;
end = str.find(',', start);
}
// Last token
std::string token = str.substr(start);
size_t first = token.find_first_not_of(" \t");
size_t last = token.find_last_not_of(" \t");
if (first != std::string::npos)
result.push_back(token.substr(first, last - first + 1));
else
result.push_back("");
return result;
}
std::vector<int> Section::GetIntArray(const char* name) const
{
std::vector<int> result;
std::vector<std::string> strings = GetTextArray(name);
result.reserve(strings.size());
for (const auto& s : strings)
{
if (!s.empty())
result.push_back(std::stoi(s));
else
result.push_back(0);
}
return result;
}
std::vector<float> Section::GetFloatArray(const char* name) const
{
std::vector<float> result;
std::vector<std::string> strings = GetTextArray(name);
result.reserve(strings.size());
for (const auto& s : strings)
{
if (!s.empty())
result.push_back(std::stof(s));
else
result.push_back(0.0f);
}
return result;
}
size_t Section::GetCount(const char* name) const
{
const auto it = values.find(name);
if (it == values.end() || it->second.empty())
{
return 0;
}
size_t count = 1;
for (char c : it->second)
{
if (c == ',')
count++;
}
return count;
}
void Section::Set(const char* name, bool value)
{
values[name] = value ? "true" : "false";
@@ -129,6 +211,39 @@ namespace wi::config
{
values[name] = value;
}
void Section::Set(const char* name, const std::vector<std::string>& arr)
{
std::string result;
for (size_t i = 0; i < arr.size(); ++i)
{
if (i > 0)
result += ", ";
result += arr[i];
}
values[name] = result;
}
void Section::Set(const char* name, const std::vector<int>& arr)
{
std::string result;
for (size_t i = 0; i < arr.size(); ++i)
{
if (i > 0)
result += ", ";
result += std::to_string(arr[i]);
}
values[name] = result;
}
void Section::Set(const char* name, const std::vector<float>& arr)
{
std::string result;
for (size_t i = 0; i < arr.size(); ++i)
{
if (i > 0)
result += ", ";
result += std::to_string(arr[i]);
}
values[name] = result;
}
bool File::Open(const char* filename)
{
+13
View File
@@ -25,6 +25,14 @@ namespace wi::config
float GetFloat(const char* name) const;
std::string GetText(const char* name) const;
// Get multiple values for the key (comma-separated in the file):
std::vector<std::string> GetTextArray(const char* name) const;
std::vector<int> GetIntArray(const char* name) const;
std::vector<float> GetFloatArray(const char* name) const;
// Get the number of values for the key:
size_t GetCount(const char* name) const;
// Set the associated value for the key:
void Set(const char* name, bool value);
void Set(const char* name, int value);
@@ -33,6 +41,11 @@ namespace wi::config
void Set(const char* name, const char* value);
void Set(const char* name, const std::string& value);
// Set multiple values for the key:
void Set(const char* name, const std::vector<std::string>& values);
void Set(const char* name, const std::vector<int>& values);
void Set(const char* name, const std::vector<float>& values);
std::unordered_map<std::string, std::string>::iterator begin() { return values.begin(); }
std::unordered_map<std::string, std::string>::const_iterator begin() const { return values.begin(); }
std::unordered_map<std::string, std::string>::iterator end() { return values.end(); }