Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
45 lines
1.6 KiB
C++
45 lines
1.6 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: 2026 Martino Ferrari
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace udpscope {
|
|
|
|
/// A source's settings: an ordered string key/value map. It is the stored form (top-level
|
|
/// `source_opt_<key>` entries of a session), the `--source-opt k=v` form, and what a config form
|
|
/// reads and writes. Order is insertion order, so a stored session stays diff-friendly.
|
|
class SourceConfig {
|
|
public:
|
|
using Items = std::vector<std::pair<std::string, std::string>>;
|
|
|
|
SourceConfig() = default;
|
|
explicit SourceConfig(Items items) : items_(std::move(items)) {}
|
|
|
|
/// Replaces the value in place if the key exists, else appends.
|
|
void set(const std::string& key, const std::string& value);
|
|
bool has(const std::string& key) const;
|
|
std::string get(const std::string& key, const std::string& def = {}) const;
|
|
long get_int(const std::string& key, long def) const;
|
|
double get_double(const std::string& key, double def) const;
|
|
/// 1/true/yes/on and 0/false/no/off (case-insensitive); anything else gives `def`.
|
|
bool get_bool(const std::string& key, bool def) const;
|
|
|
|
/// Parses `k=v`; false (and no change) if there is no `=` or the key is not `valid_key`.
|
|
bool parse_assignment(const std::string& kv);
|
|
|
|
const Items& items() const noexcept { return items_; }
|
|
bool empty() const noexcept { return items_.empty(); }
|
|
|
|
/// `[A-Za-z0-9_]+` -- the characters that survive as a TOML bare key suffix.
|
|
static bool valid_key(const std::string& key);
|
|
|
|
private:
|
|
Items items_;
|
|
};
|
|
|
|
} // namespace udpscope
|