Consolix
Loading...
Searching...
No Matches
ConfigComponent.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_CONFIG_COMPONENT_HPP_INCLUDED
3#define _CONSOLIX_CONFIG_COMPONENT_HPP_INCLUDED
4
8
9#if CONSOLIX_USE_JSON == 1
10
11#include <fstream>
12#include <nlohmann/json.hpp>
13
14namespace consolix {
15
23 template <typename ConfigType>
24 class ConfigComponent : public IAppComponent {
25 public:
29 explicit ConfigComponent(
30 const std::string& default_file = "config.json",
31 const std::string& cli_flag = "--config")
32 : m_default_file(default_file), m_cli_flag(cli_flag) {
33 }
34
36 void reload() {
37 load_config();
38 }
39
40 protected:
41
44 bool initialize() override {
45 if (!has_service<CliOptions>()) {
46 load_config();
47 m_is_init = true;
48 return true;
49 }
50 if (!has_service<CliArguments>()) return false;
51 load_config();
52 m_is_init = true;
53 return true;
54 }
55
58 bool is_initialized() const override {
59 return m_is_init;
60 }
61
63 void process() override {}
64
65 private:
66 std::string m_default_file;
67 std::string m_cli_flag;
68 ConfigType m_config_data;
69 std::atomic<bool> m_is_init{false};
70 std::mutex m_mutex;
71
73 void load_config() {
74 std::lock_guard<std::mutex> lock(m_mutex);
75 std::string config_path = resolve_file_path();
76# if defined(_WIN32) || defined(_WIN64)
77 std::ifstream file(utf8_to_ansi(config_path));
78# else
79 std::ifstream file(config_path);
80# endif
81
82 if (!file) {
83# if CONSOLIX_USE_LOGIT == 1
84 LOGIT_PRINT_ERROR("Failed to open config file: ", config_path);
85# endif
88 "Failed to open config file: " << config_path;
89 throw std::runtime_error("Failed to open config file: " + config_path);
90 }
91
92 try {
93 // Read file content
94 std::string json_content((std::istreambuf_iterator<char>(file)),
95 std::istreambuf_iterator<char>());
96
97 // Remove comments from the JSON string
98 json_content = strip_json_comments(json_content);
99
100 // Parse JSON
101 nlohmann::json json_data = nlohmann::json::parse(json_content);
102
103 m_config_data = json_data.get<ConfigType>();
104
105 // Register the configuration in the ServiceLocator
106 register_service<ConfigType>([this]() {
107 return std::make_shared<ConfigType>(m_config_data);
108 });
109 } catch (const std::exception& e) {
110# if CONSOLIX_USE_LOGIT == 1
111 LOGIT_PRINT_ERROR("Failed to parse config file: ", e.what());
112# endif
113 CONSOLIX_STREAM() <<
115 "Failed to parse config file: " << e.what();
116 throw;
117 }
118 }
119
122 std::string resolve_file_path() {
123 std::string config_path = m_default_file;
124 try {
125 // Check if CLI arguments are available
126 auto args = get_service<CliArguments>();
127 if (args.count(m_cli_flag)) {
128 config_path = args[m_cli_flag].as<std::string>();
129 }
130# if defined(_WIN32) || defined(_WIN64)
131 return get_exec_dir() + "\\" + config_path;
132# else
133 return get_exec_dir() + "/" + config_path;
134# endif
135 } catch (...) {
136# if defined(_WIN32) || defined(_WIN64)
137 return get_exec_dir() + "\\" + m_default_file;
138# else
139 return get_exec_dir() + "/" + m_default_file;
140# endif
141 }
142 }
143 };
144
145} // namespace consolix
146
147#endif // #if CONSOLIX_USE_JSON == 1
148
149#endif // _CONSOLIX_CONFIG_COMPONENT_HPP_INCLUDED
#define CONSOLIX_STREAM()
Fallback for general logging.
< Utility modules and helpers.
ColorManipulator color(TextColor color)
Creates a color manipulator for use in output streams.