diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-21 02:36:45 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-21 02:36:45 +0500 |
| commit | 744b36aefee654530158f06d682e9e330ffc06c9 (patch) | |
| tree | 3e88ff8e693d99840fd7bb9f17923e360a4e2155 /src/config.cpp | |
| parent | aa21312f18ef86a335dd28272e317da959ceb9b7 (diff) | |
feat: .env config parser
Diffstat (limited to 'src/config.cpp')
| -rw-r--r-- | src/config.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..2d1b876 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,45 @@ +#include "config.hpp" + +#include <cctype> +#include <fstream> +#include <iostream> +#include <optional> +#include <sstream> +#include <string> + +namespace bot { + std::optional<Configuration> parse_configuration_from_file( + const std::string &file_path) { + std::ifstream ifs(file_path); + + if (!ifs.is_open()) { + std::cerr << "*** Failed to open the configuration file: " << file_path + << "!\n"; + return std::nullopt; + } + + Configuration cfg; + + std::string line; + while (std::getline(ifs, line, '\n')) { + std::istringstream iss(line); + std::string key; + std::string value; + + std::getline(iss, key, '='); + std::getline(iss, value); + + for (char &c : key) { + c = tolower(c); + } + + if (key == "bot_username") { + cfg.bot_username = value; + } else if (key == "bot_password") { + cfg.bot_password = value; + } + } + + return cfg; + } +} |
