YggdrasilWM  0.1.1
A tiny window manager coded in C++
ConfigFileHandler Class Reference

Handle file I/O and JSON parsing for the configuration file. If no path is provided, it will search for the configuration file in the default paths. More...

#include <ConfigFileHandler.hpp>

Collaboration diagram for ConfigFileHandler:
Collaboration graph

Public Member Functions

 ConfigFileHandler ()
 Construct a new ConfigFileHandler object without a path. More...
 
 ConfigFileHandler (const std::string &configPath)
 
 ~ConfigFileHandler ()
 
void readConfig ()
 Read the configuration file Store the JSon root object in Json::Value root_ Throws exceptions in case of file i/o error Catch Parsing Errors and log them with Logger Object. More...
 
void writeConfig (const Json::Value &root)
 Write the configuration file Store the JSon root object in the configPath file Throws exceptions in case of file i/o error. More...
 
const Json::Value & getRoot ()
 Get the root JSON object. More...
 
std::string getConfigPath ()
 

Static Private Member Functions

static std::string findConfigFile ()
 Find the configuration file in the default paths. More...
 
static bool fileExists (const std::string &path)
 Check if a file exists by trying to open it. More...
 
static std::string expandEnvironmentVariables (const std::string &path)
 Expand environment variables in a path. More...
 

Private Attributes

std::string configPath_
 
Json::Value root_
 

Static Private Attributes

static const std::vector< std::string > defaultPaths
 

Detailed Description

Handle file I/O and JSON parsing for the configuration file. If no path is provided, it will search for the configuration file in the default paths.

Constructor & Destructor Documentation

◆ ConfigFileHandler() [1/2]

ConfigFileHandler::ConfigFileHandler ( )

Construct a new ConfigFileHandler object without a path.

36  : configPath_(""),root_() {
37  this->configPath_ = findConfigFile();
38  if (this->configPath_.empty()) {
39  throw std::runtime_error("No config file found.");
40  }
41  Logger::GetInstance()->Log("Config file found at: " + this->configPath_ ,L_INFO);
42 }
@ L_INFO
Definition: Logger.hpp:49
Json::Value root_
Definition: ConfigFileHandler.hpp:81
std::string configPath_
Definition: ConfigFileHandler.hpp:80
static std::string findConfigFile()
Find the configuration file in the default paths.
Definition: ConfigFileHandler.cpp:59
virtual void Log(const std::string &message, LogLevel level) const
Log a message This method logs a message to the log file or stream. The message is only logged if the...
Definition: Logger.cpp:73
static Logger * GetInstance()
Get the Logger object.
Definition: Logger.cpp:41

References configPath_, findConfigFile(), Logger::GetInstance(), L_INFO, and Logger::Log().

Here is the call graph for this function:

◆ ConfigFileHandler() [2/2]

ConfigFileHandler::ConfigFileHandler ( const std::string &  configPath)
explicit
43  : configPath_("") {
44  if (fileExists(configPath)) {
45  this->configPath_ = configPath;
46  } else {
47  this->configPath_ = findConfigFile();
48  }
49  if (this->configPath_.empty()) {
50  throw std::runtime_error("No config file found.");
51  }
52  Logger::GetInstance()->Log("Using Config file : " + this->configPath_,L_INFO );
53 }
static bool fileExists(const std::string &path)
Check if a file exists by trying to open it.
Definition: ConfigFileHandler.cpp:55

References configPath_, fileExists(), findConfigFile(), Logger::GetInstance(), L_INFO, and Logger::Log().

Here is the call graph for this function:

◆ ~ConfigFileHandler()

ConfigFileHandler::~ConfigFileHandler ( )
54 {}

Member Function Documentation

◆ expandEnvironmentVariables()

std::string ConfigFileHandler::expandEnvironmentVariables ( const std::string &  path)
staticprivate

Expand environment variables in a path.

Parameters
path
Returns
Expanded path as a std::string
67  {
68  if (path.empty() || path[0] != '$') {
69  return path;
70  }
71  std::string variable = path.substr(1);
72  const char* value = std::getenv(variable.c_str());
73  return value ? value : "";
74 }

Referenced by findConfigFile().

Here is the caller graph for this function:

◆ fileExists()

bool ConfigFileHandler::fileExists ( const std::string &  path)
staticprivate

Check if a file exists by trying to open it.

Parameters
path
Returns
55  {
56  std::ifstream f(path.c_str());
57  return f.good();
58 }

Referenced by ConfigFileHandler(), and findConfigFile().

Here is the caller graph for this function:

◆ findConfigFile()

std::string ConfigFileHandler::findConfigFile ( )
staticprivate

Find the configuration file in the default paths.

Returns
The full path to the configuration file
59  {
60  for (const auto&path : defaultPaths) {
62  return path;
63  }
64  }
65  return "";
66 }
static std::string expandEnvironmentVariables(const std::string &path)
Expand environment variables in a path.
Definition: ConfigFileHandler.cpp:67
static const std::vector< std::string > defaultPaths
Definition: ConfigFileHandler.hpp:79

References defaultPaths, expandEnvironmentVariables(), and fileExists().

Referenced by ConfigFileHandler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConfigPath()

std::string ConfigFileHandler::getConfigPath ( )
103  {
104  return configPath_;
105 }

References configPath_.

◆ getRoot()

Json::Value ConfigFileHandler::getRoot ( )

Get the root JSON object.

Returns
Json::Value The root JSON object
101 { return root_; }

References root_.

◆ readConfig()

void ConfigFileHandler::readConfig ( )

Read the configuration file Store the JSon root object in Json::Value root_ Throws exceptions in case of file i/o error Catch Parsing Errors and log them with Logger Object.

75  {
76  std::ifstream file(configPath_);
77  if (!file.is_open())
78  throw std::runtime_error("Failed to open config file");
79  try {
80  Json::CharReaderBuilder readerBuilder;
81  Json::parseFromStream(readerBuilder, file, &root_, nullptr);
82  file.close();
83  } catch (Json::Exception &e) {
84  Logger::GetInstance()->Log("Failed to parse config file: " + std::string(e.what()),L_ERROR);
85  }
86 }
@ L_ERROR
Definition: Logger.hpp:51

References configPath_, Logger::GetInstance(), L_ERROR, Logger::Log(), and root_.

Here is the call graph for this function:

◆ writeConfig()

void ConfigFileHandler::writeConfig ( const Json::Value &  config)

Write the configuration file Store the JSon root object in the configPath file Throws exceptions in case of file i/o error.

Parameters
configThe root JSON object to write
87  {
88  std::ofstream file(configPath_);
89  if (!file.is_open())
90  throw std::runtime_error("Failed to open config file");
91  try {
92  Json::StreamWriterBuilder writer;
93  writer["indentation"] = "\t";
94  std::string output = Json::writeString(writer, root);
95  file << output;
96  file.close();
97  } catch (Json::Exception &e) {
98  Logger::GetInstance()->Log("Failed to write config file: " + std::string(e.what()),L_ERROR);
99  }
100 }

References configPath_, Logger::GetInstance(), L_ERROR, and Logger::Log().

Here is the call graph for this function:

Member Data Documentation

◆ configPath_

std::string ConfigFileHandler::configPath_
private

◆ defaultPaths

const std::vector< std::string > ConfigFileHandler::defaultPaths
staticprivate
Initial value:
= {
"config.json",
"$HOME/.config/YggdrasilWM/config.json",
"$HOME/.YggdrasilWM/config.json",
"/etc/YggdrasilWM/config.json"
}

Referenced by findConfigFile().

◆ root_

Json::Value ConfigFileHandler::root_
private

Referenced by getRoot(), and readConfig().


The documentation for this class was generated from the following files: