inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
toml_parser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "toml.hpp"
4#include <cstdlib>
5#include <filesystem>
6#include <iostream>
7
8using namespace std;
9namespace fs = std::filesystem;
10
16#define PARENT_LIB "library"
17#define PARENT_LIB_FIELD_NAME "name"
18#define PARENT_LIB_FIELD_DIR "directory"
19
20#define PARENT_FTP "ftp"
21#define PARENT_FTP_FIELD_USER "username"
22#define PARENT_FTP_FIELD_SALT "salt"
23#define PARENT_FTP_FIELD_PWD_HASH "password_hash"
24
25#define PARENT_DBG "debug"
26#define PARENT_DBG_FIELD_TAGLIB_PARSER_LOG \
27 "taglib_parser_log"
28#define PARENT_DBG_FIELD_COLORS_PARSER_LOG "colors_parser_log"
29#define PARENT_DBG_FIELD_KEYBINDS_PARSER_LOG "keybinds_parser_log"
30
31/* SPECIAL KEYBINDS MACROS */
32#define PARENT_KEYBINDS "keybinds"
33#define SPECIAL_KEYBIND_ENTER_STR "Enter"
34#define SPECIAL_KEYBIND_TAB_STR "Tab"
35#define SPECIAL_KEYBIND_SPACE_STR "Space"
36
37#define PARENT_COLORS "colors"
38
39#define PARENT_UI "ui"
40
41#define CUSTOM_CONFIG_MACRO "INLIMBO_CONFIG_HOME"
42
56{
57 const char* customConfigHome = getenv(CUSTOM_CONFIG_MACRO);
58 if (customConfigHome)
59 {
60 return string(customConfigHome);
61 }
62
63 const char* homeDir = getenv("HOME");
64 if (!homeDir)
65 {
66 cerr << "ERROR: HOME environment variable not found." << endl;
67 exit(EXIT_FAILURE);
68 }
69
70 return string(homeDir) + "/.config/inLimbo/";
71}
72
81string getConfigPath(string fileName) { return getBaseConfigPath() + fileName; }
82
84{
85 const char* homeDir = std::getenv("HOME");
86 if (!homeDir)
87 {
88 cerr << "ERROR: HOME environment variable not found." << endl;
89 exit(EXIT_FAILURE);
90 }
91
92 string cacheFilePath = string(homeDir) + "/.cache/inLimbo/";
93
94 return cacheFilePath;
95}
96
105bool configFileExists(const string& filePath) { return fs::exists(filePath); }
106
117{
118 string configFilePath = getConfigPath("config.toml");
119
120 if (!configFileExists(configFilePath))
121 {
122 cerr << "ERROR: config.toml not found in " << configFilePath << endl;
123 exit(EXIT_FAILURE);
124 }
125
126 cout << "-- CONFIG: Loading config.toml file: " << configFilePath << endl;
127
128 return toml::parse_file(configFilePath);
129}
130
133
144auto parseTOMLField(string parent, string field) -> string_view
145{
146 return config[parent][field].value_or(
147 ""sv);
148}
149
161auto parseTOMLFieldCustom(const toml::parse_result& custom_config, string parent,
162 string field) -> string_view
163{
164 return custom_config[parent][field].value_or(
165 ""sv);
166}
167
178auto parseTOMLFieldInt(string parent, string field) -> int64_t
179{
180 return config[parent][field].value_or(
181 -1);
182}
183
195auto parseTOMLFieldIntCustom(const toml::parse_result& custom_config, string parent,
196 string field) -> int64_t
197{
198 return custom_config[parent][field].value_or(
199 -1);
200}
201
202auto parseTOMLFieldBool(const string& parent, const string& field) -> bool
203{
204 if (string(parseTOMLField(parent, field)) == "true")
205 return true;
206 return false;
207}
string getBaseConfigPath()
Retrieves the path to the configuration directory.
Definition toml_parser.hpp:55
#define CUSTOM_CONFIG_MACRO
Definition toml_parser.hpp:41
auto parseTOMLFieldInt(string parent, string field) -> int64_t
Parses an integer field from the TOML configuration.
Definition toml_parser.hpp:178
string getCachePath()
Definition toml_parser.hpp:83
auto parseTOMLFieldCustom(const toml::parse_result &custom_config, string parent, string field) -> string_view
Parses a string field from a custom TOML configuration that is called by the INLIMBO_CONFIG_HOME macr...
Definition toml_parser.hpp:161
auto parseTOMLFieldBool(const string &parent, const string &field) -> bool
Definition toml_parser.hpp:202
auto config
Definition toml_parser.hpp:132
auto parseTOMLFieldIntCustom(const toml::parse_result &custom_config, string parent, string field) -> int64_t
Parses an integer field from a custom TOML configuration set by the INLIMBO_CONFIG_HOME macro at runt...
Definition toml_parser.hpp:195
auto parseTOMLField(string parent, string field) -> string_view
Parses a string field from the TOML configuration.
Definition toml_parser.hpp:144
bool configFileExists(const string &filePath)
Checks if the configuration file exists.
Definition toml_parser.hpp:105
auto loadConfig()
Loads the configuration file.
Definition toml_parser.hpp:116
string getConfigPath(string fileName)
Retrieves the full path to the configuration file.
Definition toml_parser.hpp:81