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))};
258 ryml::ConstNodeRef node, c4::csubstr key, std::optional<std::string>& out)
260 bool matched =
false;
261 if (node.is_map() && node.has_child(key)) {
262 auto child = node[key];
263 if (child.is_map() && child.has_child(
"content")) {
264 auto content = child[
"content"];
265 if (content.has_val()) {
284 std::vector<std::string>& out)
286 if (!node.is_map() || !node.has_child(key)) {
289 auto child = node[key];
290 if (!child.is_seq()) {
294 for (
auto item : child) {
310 std::optional<std::vector<std::string>>& out)
312 if (!node.is_map() || !node.has_child(key)) {
315 auto child = node[key];
316 if (!child.is_seq()) {
317 bool is_null = !child.has_val() || child.val_is_null();
318 out = is_null ? std::nullopt : out;
321 std::vector<std::string> vec;
322 for (
auto item : child) {
325 out = std::move(vec);
339 std::unordered_map<std::string, std::string>& out)
341 if (!node.is_map() || !node.has_child(key)) {
344 auto child = node[key];
345 if (!child.is_map()) {
349 for (
auto item : child) {
365 ryml::ConstNodeRef node, c4::csubstr key,
366 std::unordered_map<std::string, std::vector<std::string>>& out)
368 if (!node.is_map() || !node.has_child(key)) {
371 auto child = node[key];
372 if (!child.is_map()) {
376 for (
auto item : child) {
377 std::vector<std::string> vec;
379 for (
auto elem : item) {
383 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.
bool extract_inline_content(ryml::ConstNodeRef node, c4::csubstr key, std::optional< std::string > &out)
Extract inline text from an object-form value (gh#141).
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.