From f4490f2c6c30f7e1ef1a97b9e75f81d56b58bfda Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 19 Apr 2024 09:31:32 +0200 Subject: [PATCH] push entire parse function into CONFIG macro --- src/osuparser.cpp | 73 +++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/src/osuparser.cpp b/src/osuparser.cpp index 0aff48a..0135370 100644 --- a/src/osuparser.cpp +++ b/src/osuparser.cpp @@ -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 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 -}