SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
config.cpp
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 20, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#include <string>
22#include <iostream>
23#include <fstream>
24#include <sstream>
25#include <vector>
26#include <map>
27
28#include "yaml-cpp/yaml.h"
29
30#include "config.h"
31
32namespace serif::config {
33
34 Config::Config() {}
35
36 Config::~Config() {}
37
38 Config& Config::getInstance() {
39 static Config instance;
40 return instance;
41 }
42
43 bool Config::loadConfig(const std::string& configFile) {
44 configFilePath = configFile;
45 try {
46 yamlRoot = YAML::LoadFile(configFile);
47 } catch (YAML::BadFile& e) {
48 std::cerr << "Error: " << e.what() << std::endl;
49 return false;
50 }
51 m_loaded = true;
52 return true;
53 }
54
55 bool Config::isKeyInCache(const std::string &key) {
56 return configMap.find(key) != configMap.end();
57 }
58
59 void Config::addToCache(const std::string &key, const YAML::Node &node) {
60 configMap[key] = node;
61 }
62
63 void Config::registerUnknownKey(const std::string &key) {
64 unknownKeys.push_back(key);
65 }
66
67 bool Config::has(const std::string &key) {
68 if (!m_loaded) {
69 throw std::runtime_error("Error! Config file not loaded");
70 }
71 if (isKeyInCache(key)) { return true; }
72
73 YAML::Node node = YAML::Clone(yamlRoot);
74 std::istringstream keyStream(key);
75 std::string subKey;
76 while (std::getline(keyStream, subKey, ':')) {
77 if (!node[subKey]) {
78 registerUnknownKey(key);
79 return false;
80 }
81 node = node[subKey]; // go deeper
82 }
83
84 // Key exists and is of the requested type
85 addToCache(key, node);
86 return true;
87 }
88
89 void recurse_keys(const YAML::Node& node, std::vector<std::string>& keyList, const std::string& path = "") {
90 if (node.IsMap()) {
91 for (const auto& it : node) {
92 auto key = it.first.as<std::string>();
93 auto new_path = path.empty() ? key : path + ":" + key;
94 recurse_keys(it.second, keyList, new_path);
95 }
96 } else {
97 keyList.push_back(path);
98 }
99
100 }
101
102 std::vector<std::string> Config::keys() const {
103 std::vector<std::string> keyList;
104 YAML::Node node = YAML::Clone(yamlRoot);
105 recurse_keys(node, keyList);
106 return keyList;
107 }
108
109}
void recurse_keys(const YAML::Node &node, std::vector< std::string > &keyList, const std::string &path="")
Definition config.cpp:89