| 12 | #include <Base64.h> |
| 13 | |
| 14 | void Settings::SettingsFileParser::loadSettingsFile( |
| 15 | const std::filesystem::path& file, CategorySettingValueMap& settings, bool base64Encoded, bool overrideExisting) |
| 16 | { |
| 17 | mFile = file; |
| 18 | std::ifstream fstream; |
| 19 | fstream.open(file); |
| 20 | auto stream = std::ref<std::istream>(fstream); |
| 21 | |
| 22 | std::istringstream decodedStream; |
| 23 | if (base64Encoded) |
| 24 | { |
| 25 | std::string base64String(std::istreambuf_iterator<char>(fstream), {}); |
| 26 | std::string decodedString; |
| 27 | auto result = Base64::Base64::Decode(base64String, decodedString); |
| 28 | if (!result.empty()) |
| 29 | fail("Could not decode Base64 file: " + result); |
| 30 | // Move won't do anything until C++20, but won't hurt to do it anyway. |
| 31 | decodedStream.str(std::move(decodedString)); |
| 32 | stream = std::ref<std::istream>(decodedStream); |
| 33 | } |
| 34 | |
| 35 | Log(Debug::Info) << "Loading settings file: " << file; |
| 36 | std::string currentCategory; |
| 37 | mLine = 0; |
| 38 | while (!stream.get().eof() && !stream.get().fail()) |
| 39 | { |
| 40 | ++mLine; |
| 41 | std::string line; |
| 42 | std::getline(stream.get(), line); |
| 43 | |
| 44 | size_t i = 0; |
| 45 | if (!skipWhiteSpace(i, line)) |
| 46 | continue; |
| 47 | |
| 48 | if (line[i] == '#') // skip comment |
| 49 | continue; |
| 50 | |
| 51 | if (line[i] == '[') |
| 52 | { |
| 53 | size_t end = line.find(']', i); |
| 54 | if (end == std::string::npos) |
| 55 | fail("unterminated category"); |
| 56 | |
| 57 | currentCategory = line.substr(i + 1, end - (i + 1)); |
| 58 | Misc::StringUtils::trim(currentCategory); |
| 59 | i = end + 1; |
| 60 | } |
| 61 | |
| 62 | if (!skipWhiteSpace(i, line)) |
| 63 | continue; |
| 64 | |
| 65 | if (currentCategory.empty()) |
| 66 | fail("empty category name"); |
| 67 | |
| 68 | size_t settingEnd = line.find('=', i); |
| 69 | if (settingEnd == std::string::npos) |
| 70 | fail("unterminated setting name"); |
| 71 | |