inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
toml_parser.hpp
Go to the documentation of this file.
1#ifndef TOML_PARSER_HPP
2#define TOML_PARSER_HPP
3
4#include "toml.hpp"
5#include <cstdlib>
6#include <filesystem>
7#include <iostream>
8
9using namespace std;
10namespace fs = std::filesystem;
11
17#define PARENT_LIB "library"
18#define PARENT_LIB_FIELD_NAME "name"
19#define PARENT_LIB_FIELD_DIR "directory"
20
21#define PARENT_FTP "ftp"
22#define PARENT_FTP_FIELD_USER "username"
23#define PARENT_FTP_FIELD_SALT "salt"
24#define PARENT_FTP_FIELD_PWD_HASH "password_hash"
25
26#define PARENT_DBG "debug"
27#define PARENT_DBG_FIELD_PARSER_LOG "parser_log"
28
29/* SPECIAL KEYBINDS MACROS */
30#define PARENT_KEYBINDS "keybinds"
31#define SPECIAL_KEYBIND_ENTER_STR "Enter"
32#define SPECIAL_KEYBIND_TAB_STR "Tab"
33#define SPECIAL_KEYBIND_SPACE_STR "Space"
34
35#define PARENT_COLORS "colors"
36
48string getConfigPath(string fileName)
49{
50 const char* homeDir = std::getenv("HOME");
51 if (!homeDir)
52 {
53 cerr << "ERROR: HOME environment variable not found." << endl;
54 exit(EXIT_FAILURE);
55 }
56
57 // Construct the path to the config.toml in $HOME/.config/inLimbo/
58 string configFilePath = string(homeDir) + "/.config/inLimbo/" + fileName;
59 return configFilePath;
60}
61
63{
64 const char* homeDir = std::getenv("HOME");
65 if (!homeDir)
66 {
67 cerr << "ERROR: HOME environment variable not found." << endl;
68 exit(EXIT_FAILURE);
69 }
70
71 string cacheFilePath = string(homeDir) + "/.cache/inLimbo/";
72
73 return cacheFilePath;
74}
75
84bool configFileExists(const string& filePath) { return fs::exists(filePath); }
85
96{
97 string configFilePath = getConfigPath("config.toml");
98
99 if (!configFileExists(configFilePath))
100 {
101 cerr << "ERROR: config.toml not found in " << configFilePath << endl;
102 exit(EXIT_FAILURE); // Exit gracefully if the file is not found
103 }
104
105 return toml::parse_file(configFilePath);
106}
107
110
121string_view parseTOMLField(string parent, string field)
122{
123 return config[parent][field].value_or(
124 ""sv);
125}
126
137int64_t parseTOMLFieldInt(string parent, string field)
138{
139 return config[parent][field].value_or(
140 -1);
141}
142
143#endif
int64_t parseTOMLFieldInt(string parent, string field)
Parses an integer field from the TOML configuration.
Definition toml_parser.hpp:137
string_view parseTOMLField(string parent, string field)
Parses a string field from the TOML configuration.
Definition toml_parser.hpp:121
string getCachePath()
Definition toml_parser.hpp:62
auto config
Definition toml_parser.hpp:109
bool configFileExists(const string &filePath)
Checks if the configuration file exists.
Definition toml_parser.hpp:84
auto loadConfig()
Loads the configuration file.
Definition toml_parser.hpp:95
string getConfigPath(string fileName)
Retrieves the path to the configuration file.
Definition toml_parser.hpp:48