push entire parse function into CONFIG macro

This commit is contained in:
jacekpoz 2024-04-19 09:31:32 +02:00
parent 0e05eee14a
commit f4490f2c6c
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -28,26 +28,43 @@ inline void toLower(std::string &str) {
}
#define CONFIG(name, ...) \
std::string line;\
\
name ret;\
std::string currentSection;\
std::string namePascalCased;\
\
while (std::getline(diffFile, line)) {\
/* remove \n*/\
line.pop_back();\
\
if (line.starts_with('[') && line.ends_with(']')) {\
currentSection = line.substr(1, line.length() - 2);\
toLower(currentSection);\
continue;\
name parse##name(fs::path configPath) {\
if (!fs::exists(configPath)) {\
std::cerr << #name << " file not found: " << configPath << "\n";\
std::exit(EXIT_FAILURE);\
}\
\
__VA_ARGS__\
}\
\
return ret;
\
if (!fs::is_regular_file(configPath)) {\
std::cerr << #name << " file isn't a regular file: " << configPath << "\n";\
std::exit(EXIT_FAILURE);\
}\
\
std::ifstream configFile(configPath);\
if (!configFile.is_open()) {\
std::cerr << "can't open " << #name << " file: " << configPath << "\n";\
std::exit(EXIT_FAILURE);\
}\
std::string line;\
\
name ret;\
std::string currentSection;\
std::string namePascalCased;\
\
while (std::getline(configFile, line)) {\
/* remove \n*/\
line.pop_back();\
\
if (line.starts_with('[') && line.ends_with(']')) {\
currentSection = line.substr(1, line.length() - 2);\
toLower(currentSection);\
continue;\
}\
\
__VA_ARGS__\
}\
\
return ret;\
}
#define SECTION(name, ...) \
if (currentSection == #name) {\
@ -189,22 +206,4 @@ inline void toLower(std::string &str) {
std::vector<type> value;\
)
Difficulty parseDifficulty(fs::path difficultyPath) {
if (!fs::exists(difficultyPath)) {
std::cerr << "difficulty file not found: " << difficultyPath << "\n";
std::exit(EXIT_FAILURE);
}
if (!fs::is_regular_file(difficultyPath)) {
std::cerr << "difficulty file isn't a regular file: " << difficultyPath << "\n";
std::exit(EXIT_FAILURE);
}
std::ifstream diffFile(difficultyPath);
if (!diffFile.is_open()) {
std::cerr << "can't open difficulty file: " << difficultyPath << "\n";
std::exit(EXIT_FAILURE);
}
#include <config_def.hpp>
}