annotate src/core/config.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/core/config.cpp@6f7385bd334c
children c4bb49c2f6eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /**
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 * config.cpp:
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 * parses the config... lol
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 **/
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include "core/config.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include "core/anime.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include "core/filesystem.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include "core/json.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <cstdlib>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <cstring>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 #include <filesystem>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #include <fstream>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <limits.h>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 std::map<std::string, Themes> StringToTheme = {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
16 {"Default", Themes::OS },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
17 {"Light", Themes::LIGHT},
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
18 {"Dark", Themes::DARK }
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 std::map<Themes, std::string> ThemeToString = {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
22 {Themes::OS, "Default"},
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
23 {Themes::LIGHT, "Light" },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
24 {Themes::DARK, "Dark" }
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 std::map<Anime::Services, std::string> ServiceToString{
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
28 {Anime::Services::NONE, "None" },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
29 {Anime::Services::ANILIST, "AniList"}
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 std::map<std::string, Anime::Services> StringToService{
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
33 {"None", Anime::Services::NONE },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
34 {"AniList", Anime::Services::ANILIST}
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 std::map<Anime::TitleLanguage, std::string> AnimeTitleToStringMap = {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
38 {Anime::TitleLanguage::ROMAJI, "Romaji" },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
39 {Anime::TitleLanguage::NATIVE, "Native" },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
40 {Anime::TitleLanguage::ENGLISH, "English"}
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 std::map<std::string, Anime::TitleLanguage> StringToAnimeTitleMap = {
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
44 {"Romaji", Anime::TitleLanguage::ROMAJI },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
45 {"Native", Anime::TitleLanguage::NATIVE },
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
46 {"English", Anime::TitleLanguage::ENGLISH}
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 };
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 int Config::Load() {
61
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
50 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
51 if (!cfg_path.Exists())
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 return 0;
61
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
53 std::ifstream config(cfg_path.GetPath(), std::ifstream::in);
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
54 auto config_js = nlohmann::json::parse(config);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 61
diff changeset
56 anime_list.language =
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 61
diff changeset
57 StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")];
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 anime_list.display_aired_episodes =
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 anime_list.display_available_episodes =
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
61 JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 anime_list.highlight_anime_if_available =
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
63 JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 anime_list.highlighted_anime_above_others =
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
65 JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)];
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
70 config.close();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 return 0;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 int Config::Save() {
61
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
75 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
76 if (!cfg_path.GetParent().Exists())
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
77 cfg_path.GetParent().CreateDirectories();
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
78 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc);
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 61
diff changeset
79 /* clang-format off */
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 nlohmann::json config_js = {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 {"General", {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 {"Service", ServiceToString[service]}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 }},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 {"Anime List", {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 {"Title language", AnimeTitleToStringMap[anime_list.language]},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 {"Display only aired episodes", anime_list.display_aired_episodes},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 {"Display only available episodes in library", anime_list.display_available_episodes},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 {"Highlight anime if available", anime_list.highlight_anime_if_available},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 }},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 {"Authorization", {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 {"AniList", {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 {"Auth Token", anilist.auth_token},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 {"Username", anilist.username},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 {"User ID", anilist.user_id}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 }}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 }},
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 {"Appearance", {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 {"Theme", ThemeToString[theme]}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }}
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 };
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 61
diff changeset
102 /* clang-format on */
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
103 config << std::setw(4) << config_js << std::endl;
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
104 config.close();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 return 0;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 }