25#ifndef PANGEA_INI_FILE_H
26#define PANGEA_INI_FILE_H
29#include <initializer_list>
39 std::map<std::string, std::string>
values_;
46 explicit IniFile(
const std::filesystem::path& path) {
47 std::ifstream input(path);
48 if (!input)
throw std::runtime_error(
"Cannot open INI: " + path.u8string());
49 std::string line, section;
50 bool firstLine =
true;
51 while (std::getline(input, line)) {
52 if (firstLine && line.compare(0, 3,
"\xEF\xBB\xBF") == 0) line.erase(0, 3);
54 line =
trim(line.substr(0, line.find_first_of(
";#")));
55 if (line.empty())
continue;
56 if (line.front() ==
'[' && line.back() ==
']') {
57 section =
trim(line.substr(1, line.size() - 2));
60 const auto equal = line.find(
'=');
61 if (section.empty() || equal == std::string::npos)
62 throw std::runtime_error(
"Expected section/key=value in " + path.u8string());
63 const auto key = section +
"." +
trim(line.substr(0, equal));
64 const auto value =
trim(line.substr(equal + 1));
65 if (value.empty() || !
values_.emplace(key, value).second)
66 throw std::runtime_error(
"Empty or duplicate INI key: " + key);
68 if (input.bad())
throw std::runtime_error(
"Cannot read INI: " + path.u8string());
75 void validate(std::initializer_list<const char*> allowed)
const {
76 for (
const auto& entry :
values_)
77 if (std::none_of(allowed.begin(), allowed.end(),
78 [&](
const char* key) { return entry.first == key; }))
79 throw std::runtime_error(
"Unknown INI key: " + entry.first);
82 bool has(
const std::string& key)
const {
return values_.count(key) != 0; }
84 std::string
text(
const std::string& key,
const std::string& fallback = {})
const {
85 const auto found =
values_.find(key);
86 return found ==
values_.end() ? fallback : found->second;
89 double numeric(
const std::string& key,
double fallback)
const {
93 double required(
const std::string& key)
const {
94 if (!
has(key))
throw std::runtime_error(
"Missing INI key: " + key);
98 bool boolean(
const std::string& key,
bool fallback =
false)
const {
99 const auto value =
numeric(key, fallback ? 1 : 0);
100 if (value != 0 && value != 1)
throw std::runtime_error(key +
" must be 0 or 1");
IniFile(const std::filesystem::path &path)
Parse an INI file into unique section-qualified keys.
std::map< std::string, std::string > values_
bool boolean(const std::string &key, bool fallback=false) const
Read a strict numeric boolean whose only accepted values are 0 and 1.
double required(const std::string &key) const
Read a required finite numeric value.
bool has(const std::string &key) const
Test whether a section-qualified key was present in the file.
double numeric(const std::string &key, double fallback) const
Read a finite numeric value, using fallback only when absent.
void validate(std::initializer_list< const char * > allowed) const
Reject keys not owned by the caller's configuration schema.
std::string text(const std::string &key, const std::string &fallback={}) const
Read text or return a fallback when the key is absent.
std::string trim(const std::string &text)
Strip the ASCII whitespace allowed around route INI tokens.
double number(const std::string &text, const std::string &name)
Read a complete finite numeric route field.
Parse capture-route settings and evaluate the simulated flight path.