Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
ini_file.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file ini_file.h
20 * @brief Parse strict section-qualified INI settings for the survey mapper.
21 * @details Parsing and allow-list validation are separate so each owning
22 * configuration schema can reject unknown keys. This catches misspellings
23 * instead of quietly running with a default that looks like an accepted value.
24 */
25#ifndef PANGEA_INI_FILE_H
26#define PANGEA_INI_FILE_H
27
28#include "route.h"
29#include <initializer_list>
30
31namespace pangea_survey {
32/**
33 * @brief Read scalar values from a bounded, section-based INI file.
34 * @details Keys are stored as `section.name`; duplicate and empty settings are
35 * rejected during construction. Call @ref validate with the owning schema's
36 * allow-list before consuming values.
37 */
38class IniFile {
39 std::map<std::string, std::string> values_;
40public:
41 /**
42 * @brief Parse an INI file into unique section-qualified keys.
43 * @param[in] path Input file path.
44 * @throws std::runtime_error If the file is unreadable or malformed.
45 */
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);
53 firstLine = false;
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));
58 continue;
59 }
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);
67 }
68 if (input.bad()) throw std::runtime_error("Cannot read INI: " + path.u8string());
69 }
70 /**
71 * @brief Reject keys not owned by the caller's configuration schema.
72 * @param[in] allowed Exact `section.name` keys accepted by the schema.
73 * @throws std::runtime_error On the first unknown key.
74 */
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);
80 }
81 /** @brief Test whether a section-qualified key was present in the file. */
82 bool has(const std::string& key) const { return values_.count(key) != 0; }
83 /** @brief Read text or return a fallback when the key is absent. */
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;
87 }
88 /** @brief Read a finite numeric value, using @p fallback only when absent. */
89 double numeric(const std::string& key, double fallback) const {
90 return has(key) ? number(text(key), key) : fallback;
91 }
92 /** @brief Read a required finite numeric value. */
93 double required(const std::string& key) const {
94 if (!has(key)) throw std::runtime_error("Missing INI key: " + key);
95 return number(text(key), key);
96 }
97 /** @brief Read a strict numeric boolean whose only accepted values are 0 and 1. */
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");
101 return value == 1;
102 }
103};
104}
105
106#endif
IniFile(const std::filesystem::path &path)
Parse an INI file into unique section-qualified keys.
Definition ini_file.h:46
std::map< std::string, std::string > values_
Definition ini_file.h:39
bool boolean(const std::string &key, bool fallback=false) const
Read a strict numeric boolean whose only accepted values are 0 and 1.
Definition ini_file.h:98
double required(const std::string &key) const
Read a required finite numeric value.
Definition ini_file.h:93
bool has(const std::string &key) const
Test whether a section-qualified key was present in the file.
Definition ini_file.h:82
double numeric(const std::string &key, double fallback) const
Read a finite numeric value, using fallback only when absent.
Definition ini_file.h:89
void validate(std::initializer_list< const char * > allowed) const
Reject keys not owned by the caller's configuration schema.
Definition ini_file.h:75
std::string text(const std::string &key, const std::string &fallback={}) const
Read text or return a fallback when the key is absent.
Definition ini_file.h:84
std::string trim(const std::string &text)
Strip the ASCII whitespace allowed around route INI tokens.
Definition route.h:130
double number(const std::string &text, const std::string &name)
Read a complete finite numeric route field.
Definition route.h:141
Parse capture-route settings and evaluate the simulated flight path.