#pragma once #include #include #include #include #include #include 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; struct ParseError { std::size_t line; std::string message; }; // Parse from a string. Returns error on malformed input. [[nodiscard]] static std::optional 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 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 sections() const { std::vector 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 data_; }; } // namespace Void::Ini