Files
Library/include/Void/Data/Ini.hpp
T
faumarayandClaude Fable 5 ff6960a2af Fix -Werror build: drop dead variables, honor nodiscard
Surfaced by a -DVOID_WARNINGS_AS_ERRORS=ON build now that Json/Ini are
pulled into SHARED module TUs (Log, Fsm, Plugin, Configuration):
- Json::Detail::parse_number: removed the unused is_float tracking var.
- Ini::from_string: (void)-cast the nodiscard parse() result.
- Fsm::Machine::dispatch_event: removed the dead `source` variable
  (the transition already carries its source state).

Module targets now compile clean under -Wall -Wextra -Wpedantic -Werror;
967 tests green, ASan+UBSan clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-13 18:15:32 +03:00

164 lines
5.2 KiB
C++

#pragma once
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace Void::Ini
{
// ── IniFile ───────────────────────────────────────────────────────────────────
// Parses .ini / .cfg files with sections ([section]) and key=value pairs.
// Comments start with ';' or '#'. Inline comments are NOT stripped.
// Values may be quoted ("hello world") — quotes are stripped.
class IniFile
{
public:
using Section = std::unordered_map<std::string, std::string>;
struct ParseError
{
std::size_t line;
std::string message;
};
// Parse from a string. Returns error on malformed input.
[[nodiscard]] static std::optional<ParseError>
parse(IniFile& out, std::string_view text)
{
out.data_.clear();
std::string current_section;
std::size_t lineno = 0;
auto sv_trim = [](std::string_view s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) s.remove_prefix(1);
while (!s.empty() && (s.back() == ' ' || s.back() == '\t' ||
s.back() == '\r')) s.remove_suffix(1);
return s;
};
std::string_view remaining = text;
while (!remaining.empty()) {
++lineno;
const auto nl = remaining.find('\n');
std::string_view line = sv_trim(
nl == std::string_view::npos ? remaining
: remaining.substr(0, nl));
remaining = nl == std::string_view::npos
? std::string_view{}
: remaining.substr(nl + 1);
if (line.empty() || line[0] == ';' || line[0] == '#') continue;
if (line[0] == '[') {
const auto close = line.find(']');
if (close == std::string_view::npos) {
return ParseError{lineno, "unclosed '['"};
}
current_section = std::string{line.substr(1, close - 1)};
out.data_[current_section]; // ensure section exists
continue;
}
const auto eq = line.find('=');
if (eq == std::string_view::npos) {
if (!line.empty()) {
return ParseError{lineno, std::string{"expected '=' in: "} + std::string{line}};
}
continue;
}
std::string key{sv_trim(line.substr(0, eq))};
std::string_view val_sv = sv_trim(line.substr(eq + 1));
// Strip optional quotes
std::string val;
if (val_sv.size() >= 2 && val_sv.front() == '"' && val_sv.back() == '"') {
val = std::string{val_sv.substr(1, val_sv.size() - 2)};
} else {
val = std::string{val_sv};
}
out.data_[current_section][std::move(key)] = std::move(val);
}
return std::nullopt;
}
[[nodiscard]] static IniFile from_string(std::string_view text)
{
IniFile f;
(void)parse(f, text);
return f;
}
[[nodiscard]] std::optional<std::string>
get(std::string_view section, std::string_view key) const
{
auto sec_it = data_.find(std::string{section});
if (sec_it == data_.end()) return std::nullopt;
auto key_it = sec_it->second.find(std::string{key});
if (key_it == sec_it->second.end()) return std::nullopt;
return key_it->second;
}
[[nodiscard]] std::string
get_or(std::string_view section, std::string_view key,
std::string_view fallback) const
{
return get(section, key).value_or(std::string{fallback});
}
void set(std::string_view section, std::string_view key, std::string value)
{
data_[std::string{section}][std::string{key}] = std::move(value);
}
[[nodiscard]] bool has_section(std::string_view section) const
{
return data_.contains(std::string{section});
}
[[nodiscard]] bool has_key(std::string_view section,
std::string_view key) const
{
return get(section, key).has_value();
}
[[nodiscard]] std::vector<std::string> sections() const
{
std::vector<std::string> result;
result.reserve(data_.size());
for (const auto& [k, _] : data_) result.push_back(k);
return result;
}
[[nodiscard]] const Section* section(std::string_view name) const
{
auto it = data_.find(std::string{name});
return it == data_.end() ? nullptr : &it->second;
}
// Serialize back to INI text.
[[nodiscard]] std::string serialize() const
{
std::string out;
for (const auto& [sec, kv] : data_) {
out += '['; out += sec; out += "]\n";
for (const auto& [k, v] : kv) {
out += k; out += " = "; out += v; out += '\n';
}
out += '\n';
}
return out;
}
private:
std::unordered_map<std::string, Section> data_;
};
} // namespace Void::Ini