comparison src/core/config.cpp @ 11:fc1bf97c528b

*: use C++11 standard I've been meaning to do this for a while, but I didn't want to reimplement the filesystem code. Now we are on C++11 and most compilers from the past 5 centuries should support this now
author Paper <mrpapersonic@gmail.com>
date Sun, 17 Sep 2023 06:14:30 -0400
parents 5c0397762b53
children cde8f67a7c7d
comparison
equal deleted inserted replaced
10:4b198a111713 11:fc1bf97c528b
45 {"Native", Anime::TitleLanguage::NATIVE }, 45 {"Native", Anime::TitleLanguage::NATIVE },
46 {"English", Anime::TitleLanguage::ENGLISH} 46 {"English", Anime::TitleLanguage::ENGLISH}
47 }; 47 };
48 48
49 int Config::Load() { 49 int Config::Load() {
50 std::filesystem::path cfg_path = get_config_path(); 50 std::string cfg_path = Filesystem::GetConfigPath();
51 if (!std::filesystem::exists(cfg_path)) 51 if (!Filesystem::Exists(cfg_path))
52 return 0; 52 return 0;
53 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in); 53 std::ifstream config(cfg_path.c_str(), std::ifstream::in);
54 auto config_js = nlohmann::json::parse(config_in); 54 auto config_js = nlohmann::json::parse(config);
55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)]; 55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
56 anime_list.language = StringToAnimeTitleMap[JSON::GetString( 56 anime_list.language = StringToAnimeTitleMap[JSON::GetString(
57 config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")]; 57 config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")];
58 anime_list.display_aired_episodes = 58 anime_list.display_aired_episodes =
59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); 59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
65 JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); 65 JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer);
66 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); 66 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer);
67 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); 67 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer);
68 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); 68 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer);
69 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)]; 69 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)];
70 config_in.close(); 70 config.close();
71 return 0; 71 return 0;
72 } 72 }
73 73
74 int Config::Save() { 74 int Config::Save() {
75 std::filesystem::path cfg_path = get_config_path(); 75 std::string cfg_path = Filesystem::GetConfigPath();
76 if (!std::filesystem::exists(cfg_path.parent_path())) 76 if (!Filesystem::Exists(Filesystem::GetParentDirectory(cfg_path)))
77 std::filesystem::create_directories(cfg_path.parent_path()); 77 Filesystem::CreateDirectories(Filesystem::GetParentDirectory(cfg_path));
78 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc); 78 std::ofstream config(cfg_path.c_str(), std::ofstream::out | std::ofstream::trunc);
79 // clang-format off 79 // clang-format off
80 nlohmann::json config_js = { 80 nlohmann::json config_js = {
81 {"General", { 81 {"General", {
82 {"Service", ServiceToString[service]} 82 {"Service", ServiceToString[service]}
83 }}, 83 }},
98 {"Appearance", { 98 {"Appearance", {
99 {"Theme", ThemeToString[theme]} 99 {"Theme", ThemeToString[theme]}
100 }} 100 }}
101 }; 101 };
102 // clang-format on 102 // clang-format on
103 config_out << std::setw(4) << config_js << std::endl; 103 config << std::setw(4) << config_js << std::endl;
104 config_out.close(); 104 config.close();
105 return 0; 105 return 0;
106 } 106 }