18namespace entropic::config {
29 return std::string(s.str, s.len);
39std::string
read_file(
const std::filesystem::path& path)
41 std::ifstream ifs(path);
43 s_log->error(
"Cannot open file: {}", path.string());
46 std::ostringstream ss;
48 auto content = ss.str();
49 s_log->info(
"Read file: {}, {} bytes", path.string(),
61std::filesystem::path
expand_home(
const std::filesystem::path& p)
63 auto str = p.string();
64 const char* home = std::getenv(
"HOME");
65 bool needs_expansion = !str.empty() && str[0] ==
'~' && home !=
nullptr;
67 if (!needs_expansion) {
71 return (str.size() == 1)
72 ? std::filesystem::path(home)
73 : std::filesystem::path(home) / str.substr(2);
85bool extract(ryml::ConstNodeRef node, c4::csubstr key, std::string& out)
87 if (!node.is_map() || !node.has_child(key)) {
90 auto child = node[key];
91 if (!child.has_val() || child.val_is_null()) {
107bool extract(ryml::ConstNodeRef node, c4::csubstr key,
int& out)
109 if (!node.is_map() || !node.has_child(key)) {
112 auto child = node[key];
113 if (!child.has_val() || child.val_is_null()) {
117 out = std::stoi(val);
130bool extract(ryml::ConstNodeRef node, c4::csubstr key,
float& out)
132 if (!node.is_map() || !node.has_child(key)) {
135 auto child = node[key];
136 if (!child.has_val() || child.val_is_null()) {
140 out = std::stof(val);
153bool extract(ryml::ConstNodeRef node, c4::csubstr key,
double& out)
155 if (!node.is_map() || !node.has_child(key)) {
158 auto child = node[key];
159 if (!child.has_val() || child.val_is_null()) {
163 out = std::stod(val);
176bool extract(ryml::ConstNodeRef node, c4::csubstr key,
bool& out)
178 if (!node.is_map() || !node.has_child(key)) {
181 auto child = node[key];
182 if (!child.has_val() || child.val_is_null()) {
186 out = (val ==
"true" || val ==
"True" || val ==
"TRUE"
187 || val ==
"yes" || val ==
"Yes" || val ==
"YES"
188 || val ==
"on" || val ==
"On" || val ==
"ON"
203 std::filesystem::path& out)
206 if (!
extract(node, key, val)) {
224 ryml::ConstNodeRef node, c4::csubstr key,
225 std::optional<std::filesystem::path>& out,
bool& disabled)
227 if (!node.is_map() || !node.has_child(key)) {
230 auto child = node[key];
231 bool is_null = !child.has_val() || child.val_is_null();
232 auto val = is_null ? std::string{} :
to_string(child.val());
233 bool is_false = (val ==
"false" || val ==
"False" || val ==
"FALSE");
236 out = (is_null || is_false)
238 : std::optional{
expand_home(std::filesystem::path(val))};
252 std::vector<std::string>& out)
254 if (!node.is_map() || !node.has_child(key)) {
257 auto child = node[key];
258 if (!child.is_seq()) {
262 for (
auto item : child) {
278 std::optional<std::vector<std::string>>& out)
280 if (!node.is_map() || !node.has_child(key)) {
283 auto child = node[key];
284 if (!child.is_seq()) {
285 bool is_null = !child.has_val() || child.val_is_null();
286 out = is_null ? std::nullopt : out;
289 std::vector<std::string> vec;
290 for (
auto item : child) {
293 out = std::move(vec);
307 std::unordered_map<std::string, std::string>& out)
309 if (!node.is_map() || !node.has_child(key)) {
312 auto child = node[key];
313 if (!child.is_map()) {
317 for (
auto item : child) {
333 ryml::ConstNodeRef node, c4::csubstr key,
334 std::unordered_map<std::string, std::vector<std::string>>& out)
336 if (!node.is_map() || !node.has_child(key)) {
339 auto child = node[key];
340 if (!child.is_map()) {
344 for (
auto item : child) {
345 std::vector<std::string> vec;
347 for (
auto elem : item) {
351 out[
to_string(item.key())] = std::move(vec);
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
std::filesystem::path expand_home(const std::filesystem::path &p)
Expand ~ to home directory in a path.
bool extract_string_list_map(ryml::ConstNodeRef node, c4::csubstr key, std::unordered_map< std::string, std::vector< std::string > > &out)
Extract a map of string to list of strings from a YAML mapping.
std::string read_file(const std::filesystem::path &path)
Read a file into a string.
bool extract_path(ryml::ConstNodeRef node, c4::csubstr key, std::filesystem::path &out)
Extract a filesystem path with ~ expansion.
bool extract_string_list(ryml::ConstNodeRef node, c4::csubstr key, std::vector< std::string > &out)
Extract a vector of strings from a YAML sequence node.
bool extract_string_map(ryml::ConstNodeRef node, c4::csubstr key, std::unordered_map< std::string, std::string > &out)
Extract a map of string to string from a YAML mapping node.
bool extract_string_list_opt(ryml::ConstNodeRef node, c4::csubstr key, std::optional< std::vector< std::string > > &out)
Extract an optional vector of strings.
bool extract_tri_state_path(ryml::ConstNodeRef node, c4::csubstr key, std::optional< std::filesystem::path > &out, bool &disabled)
Extract a tri-state path (null = default, false = disabled, string = path).
std::string to_string(c4::csubstr s)
Convert ryml csubstr to std::string.
bool extract(ryml::ConstNodeRef node, c4::csubstr key, std::string &out)
Extract a string value from a YAML node.
ryml extraction helpers for config parsing.