Mercurial > minori
comparison src/config.cpp @ 1:1ae666fdf9e2
*: initial commit
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Tue, 08 Aug 2023 19:49:15 -0400 |
| parents | |
| children | 23d0d9319a00 |
comparison
equal
deleted
inserted
replaced
| 0:5a76e1b94163 | 1:1ae666fdf9e2 |
|---|---|
| 1 /** | |
| 2 * config.cpp: | |
| 3 * parses the config | |
| 4 * | |
| 5 * much of this is similar to the code used in | |
| 6 * wgsdk... | |
| 7 * maybe some of this will be C++-ified someday ;) | |
| 8 **/ | |
| 9 #include <filesystem> /* Sorry, C++17 is just sexy. if you have boost you can probably change this */ | |
| 10 #ifdef MACOSX | |
| 11 #include <NSSystemDirectories.h> | |
| 12 #endif | |
| 13 #include <limits.h> | |
| 14 #include <cstdlib> | |
| 15 #include <cstring> | |
| 16 #include <fstream> | |
| 17 #include "json.h" | |
| 18 #include "config.h" | |
| 19 #include "window.h" | |
| 20 #include "filesystem.h" | |
| 21 | |
| 22 std::map<std::string, enum Themes> StringToTheme = { | |
| 23 {"Default", OS}, | |
| 24 {"Light", LIGHT}, | |
| 25 {"Dark", DARK} | |
| 26 }; | |
| 27 | |
| 28 std::map<enum Themes, std::string> ThemeToString = { | |
| 29 {OS, "Default"}, | |
| 30 {LIGHT, "Light"}, | |
| 31 {DARK, "Dark"} | |
| 32 }; | |
| 33 | |
| 34 int Config::Load() { | |
| 35 std::filesystem::path cfg_path = get_config_path(); | |
| 36 if (!std::filesystem::exists(cfg_path)) | |
| 37 return 0; | |
| 38 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in); | |
| 39 auto config_js = nlohmann::json::parse(config_in); | |
| 40 /* this macro will make it easier to edit these in the future, if needed */ | |
| 41 #define GET_CONFIG_VALUE(pointer, location, struct, default) \ | |
| 42 struct = (config_js.contains(pointer)) ? (location) : (default) | |
| 43 GET_CONFIG_VALUE("/General/Service"_json_pointer, (enum AnimeListServices)config_js["General"]["Service"].get<int>(), service, NONE); | |
| 44 GET_CONFIG_VALUE("/Authorization/AniList/Auth Token"_json_pointer, config_js["Authorization"]["AniList"]["Auth Token"].get<std::string>(), anilist.auth_token, ""); | |
| 45 GET_CONFIG_VALUE("/Authorization/AniList/Username"_json_pointer, config_js["Authorization"]["AniList"]["Username"].get<std::string>(), anilist.username, ""); | |
| 46 GET_CONFIG_VALUE("/Authorization/AniList/User ID"_json_pointer, config_js["Authorization"]["AniList"]["User ID"].get<int>(), anilist.user_id, 0); | |
| 47 GET_CONFIG_VALUE("/Appearance/Theme"_json_pointer, StringToTheme[config_js["Appearance"]["Theme"].get<std::string>()], theme, OS); | |
| 48 #undef GET_CONFIG_VALUE | |
| 49 config_in.close(); | |
| 50 return 0; | |
| 51 } | |
| 52 | |
| 53 int Config::Save() { | |
| 54 std::filesystem::path cfg_path = get_config_path(); | |
| 55 if (!std::filesystem::exists(cfg_path.parent_path())) | |
| 56 std::filesystem::create_directories(cfg_path.parent_path()); | |
| 57 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc); | |
| 58 nlohmann::json config_js = { | |
| 59 {"General", { | |
| 60 {"Service", service} | |
| 61 }}, | |
| 62 {"Authorization", { | |
| 63 {"AniList", { | |
| 64 {"Auth Token", anilist.auth_token}, | |
| 65 {"Username", anilist.username}, | |
| 66 {"User ID", anilist.user_id} | |
| 67 }} | |
| 68 }}, | |
| 69 {"Appearance", { | |
| 70 {"Theme", ThemeToString[theme]} | |
| 71 }} | |
| 72 }; | |
| 73 config_out << std::setw(4) << config_js << std::endl; | |
| 74 config_out.close(); | |
| 75 return 0; | |
| 76 } |
