diff src/core/config.cc @ 102:b315f3759c56

*: big patch 1. use a wrapper for mINI that enables case sensitivity (personal preference) 2. rename dark_theme.cc to theme.cc and change it to be a class 3. include the "dep" folder so we don't have stupidity in json.h or ini.h 4. I think the graph was also tweaked a lot in this, nothing is constexpr and size is found at runtime...
author Paper <mrpapersonic@gmail.com>
date Fri, 03 Nov 2023 21:32:52 -0400
parents c537996cf67b
children 2004b41d4a59
line wrap: on
line diff
--- a/src/core/config.cc	Fri Nov 03 14:06:02 2023 -0400
+++ b/src/core/config.cc	Fri Nov 03 21:32:52 2023 -0400
@@ -5,11 +5,11 @@
 #include "core/config.h"
 #include "core/strings.h"
 #include "core/anime.h"
+#include "core/ini.h"
 #include "core/filesystem.h"
 #include "core/json.h"
 #include "gui/translate/anime.h"
 #include "gui/translate/config.h"
-#include "ini.h" // mINI
 #include <algorithm>
 #include <cstdlib>
 #include <cstring>
@@ -17,27 +17,25 @@
 #include <fstream>
 #include <limits.h>
 
-/* I'm not exactly fond of using JSON for a config file, but it's better than
-   no config I guess. I'd like to have something more readable, e.g. YAML or
-   even INI. */
-
+/* Move these to strings.cc or the translation stuff, please. */
 static bool string_to_bool(const std::string& s, bool def = false) {
-	bool b;
-    std::istringstream is(Strings::ToLower(s));
-    is >> std::boolalpha >> b;
-    return b;
+	if (s.length() < 4)
+		return def;
+	std::string l = Strings::ToLower(s);
+	if (Strings::BeginningMatchesSubstring(l, "true"))
+		return true;
+	else if (Strings::BeginningMatchesSubstring(l, "false"))
+		return false;
+	return def;
 }
 
 static std::string bool_to_string(bool b) {
-	std::ostringstream stream;
-	stream << std::boolalpha << b;
-	return stream.str();
+	return b ? "true" : "false";
 }
 
 int Config::Load() {
 	Filesystem::Path cfg_path = Filesystem::GetConfigPath();
-	if (!cfg_path.Exists())
-		return 0;
+
 	mINI::INIFile file(cfg_path.GetPath());
 	mINI::INIStructure ini;
 	file.read(ini);
@@ -50,7 +48,7 @@
 	anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others"));
 	anilist.auth_token = ini.get("AniList").get("Auth Token");
 	anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID"));
-	theme = Translate::ToTheme(ini.get("Appearance").get("Theme"));
+	theme.SetTheme(Translate::ToTheme(ini.get("Appearance").get("Theme")));
 
 	return 0;
 }
@@ -71,9 +69,9 @@
 	ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others);
 	ini["AniList"]["Auth Token"] = anilist.auth_token;
 	ini["AniList"]["User ID"] = std::to_string(anilist.user_id);
-	ini["Appearance"]["Theme"] = Translate::ToString(theme);
+	ini["Appearance"]["Theme"] = Translate::ToString(theme.GetTheme());
 
-	file.generate(ini);
+	file.write(ini);
 
 	return 0;
 }