annotate src/config.cpp @ 2:23d0d9319a00

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