diff src/core/anime_db.cc @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents b5d6c27c308f
children
line wrap: on
line diff
--- a/src/core/anime_db.cc	Fri Jul 25 10:05:23 2025 -0400
+++ b/src/core/anime_db.cc	Fri Jul 25 10:16:02 2025 -0400
@@ -12,40 +12,43 @@
 
 #include <fstream>
 
+#include <cstdlib>
 #include <exception>
-#include <cstdlib>
 #include <iostream>
 #include <random>
 
 namespace Anime {
 
-size_t Database::GetTotalAnimeAmount() const {
+size_t Database::GetTotalAnimeAmount() const
+{
 	size_t total = 0;
 
-	for (const auto& [id, anime] : items)
+	for (const auto &[id, anime] : items)
 		if (anime.IsInUserList())
 			total++;
 
 	return total;
 }
 
-size_t Database::GetListsAnimeAmount(ListStatus status) const {
+size_t Database::GetListsAnimeAmount(ListStatus status) const
+{
 	if (status == ListStatus::NotInList)
 		return 0;
 
 	size_t total = 0;
 
-	for (const auto& [id, anime] : items)
+	for (const auto &[id, anime] : items)
 		if (anime.IsInUserList() && anime.GetUserStatus() == status)
 			total++;
 
 	return total;
 }
 
-size_t Database::GetTotalEpisodeAmount() const {
+size_t Database::GetTotalEpisodeAmount() const
+{
 	size_t total = 0;
 
-	for (const auto& [id, anime] : items)
+	for (const auto &[id, anime] : items)
 		if (anime.IsInUserList())
 			total += anime.GetUserRewatchedTimes() * anime.GetEpisodes() + anime.GetUserProgress();
 
@@ -53,10 +56,11 @@
 }
 
 /* Returns the total watched amount in minutes. */
-size_t Database::GetTotalWatchedAmount() const {
+size_t Database::GetTotalWatchedAmount() const
+{
 	size_t total = 0;
 
-	for (const auto& [id, anime] : items)
+	for (const auto &[id, anime] : items)
 		if (anime.IsInUserList())
 			total += anime.GetDuration() * anime.GetUserProgress() +
 			         anime.GetEpisodes() * anime.GetDuration() * anime.GetUserRewatchedTimes();
@@ -69,10 +73,11 @@
    amount of episodes, as AniList will let you
    set episode counts up to 32768. But that should
    rather be handled elsewhere. */
-size_t Database::GetTotalPlannedAmount() const {
+size_t Database::GetTotalPlannedAmount() const
+{
 	size_t total = 0;
 
-	for (const auto& [id, anime] : items)
+	for (const auto &[id, anime] : items)
 		if (anime.IsInUserList())
 			total += anime.GetDuration() * (anime.GetEpisodes() - anime.GetUserProgress());
 
@@ -82,11 +87,12 @@
 /* In Taiga this is called the mean, but "average" is
    what's primarily used in conversation, at least
    in the U.S. */
-double Database::GetAverageScore() const {
+double Database::GetAverageScore() const
+{
 	double avg = 0;
 	size_t amt = 0;
 
-	for (const auto& [id, anime] : items) {
+	for (const auto &[id, anime] : items) {
 		if (anime.IsInUserList() && anime.GetUserScore()) {
 			avg += anime.GetUserScore();
 			amt++;
@@ -95,11 +101,12 @@
 	return avg / amt;
 }
 
-double Database::GetScoreDeviation() const {
+double Database::GetScoreDeviation() const
+{
 	double squares_sum = 0, avg = GetAverageScore();
 	size_t amt = 0;
 
-	for (const auto& [id, anime] : items) {
+	for (const auto &[id, anime] : items) {
 		if (anime.IsInUserList() && anime.GetUserScore()) {
 			squares_sum += std::pow(static_cast<double>(anime.GetUserScore()) - avg, 2);
 			amt++;
@@ -109,18 +116,19 @@
 	return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
 }
 
-int Database::LookupAnimeTitle(const std::string& title) const {
+int Database::LookupAnimeTitle(const std::string &title) const
+{
 	if (title.empty())
 		return 0;
 
 	std::string title_n(title);
 	Strings::NormalizeAnimeTitle(title_n);
 
-	for (const auto& [id, anime] : items) {
+	for (const auto &[id, anime] : items) {
 		std::vector<std::string> synonyms(anime.GetTitleSynonyms());
 		synonyms.push_back(anime.GetUserPreferredTitle());
 
-		for (auto& synonym : synonyms) {
+		for (auto &synonym : synonyms) {
 			Strings::NormalizeAnimeTitle(synonym);
 			if (synonym == title_n)
 				return id;
@@ -130,7 +138,8 @@
 	return 0;
 }
 
-static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) {
+static bool GetListDataAsJSON(const Anime &anime, nlohmann::json &json)
+{
 	if (!anime.IsInUserList())
 		return false;
 
@@ -152,7 +161,8 @@
 	return true;
 }
 
-static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) {
+static bool GetAnimeAsJSON(const Anime &anime, nlohmann::json &json)
+{
 	// clang-format off
 	json = {
 		{"id", anime.GetId()},
@@ -173,13 +183,13 @@
 	// clang-format on
 
 	/* now for dynamically-filled stuff */
-	for (const auto& lang : TitleLanguages) {
+	for (const auto &lang : TitleLanguages) {
 		std::optional<std::string> title = anime.GetTitle(lang);
 		if (title.has_value())
 			json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value();
 	}
 
-	for (const auto& service : Services) {
+	for (const auto &service : Services) {
 		std::optional<std::string> id = anime.GetServiceId(service);
 		if (id.has_value())
 			json["ids"][Strings::ToLower(Translate::ToString(service))] = id.value();
@@ -192,8 +202,9 @@
 	return true;
 }
 
-bool Database::GetDatabaseAsJSON(nlohmann::json& json) const {
-	for (const auto& [id, anime] : items) {
+bool Database::GetDatabaseAsJSON(nlohmann::json &json) const
+{
+	for (const auto &[id, anime] : items) {
 		nlohmann::json anime_json = {};
 		GetAnimeAsJSON(anime, anime_json);
 		json.push_back(anime_json);
@@ -202,7 +213,8 @@
 	return true;
 }
 
-bool Database::SaveDatabaseToDisk() const {
+bool Database::SaveDatabaseToDisk() const
+{
 	std::filesystem::path db_path = Filesystem::GetAnimeDBPath();
 	Filesystem::CreateDirectories(db_path);
 
@@ -218,7 +230,8 @@
 	return true;
 }
 
-static bool ParseAnimeUserInfoJSON(const nlohmann::json& json, Anime& anime) {
+static bool ParseAnimeUserInfoJSON(const nlohmann::json &json, Anime &anime)
+{
 	if (!anime.IsInUserList())
 		anime.AddToUserList();
 
@@ -236,22 +249,23 @@
 	return true;
 }
 
-static bool ParseAnimeInfoJSON(const nlohmann::json& json, Database& database) {
+static bool ParseAnimeInfoJSON(const nlohmann::json &json, Database &database)
+{
 	int id = JSON::GetNumber(json, "/id"_json_pointer, 0);
 	if (!id)
 		return false;
 
-	Anime& anime = database.items[id];
+	Anime &anime = database.items[id];
 
 	anime.SetId(id);
-	for (const auto& service : Services) {
+	for (const auto &service : Services) {
 		nlohmann::json::json_pointer p("/ids/" + Strings::ToLower(Translate::ToString(service)));
 
 		if (json.contains(p) && json[p].is_string())
 			anime.SetServiceId(service, json[p].get<std::string>());
 	}
 
-	for (const auto& lang : TitleLanguages) {
+	for (const auto &lang : TitleLanguages) {
 		nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang)));
 
 		if (json.contains(p) && json[p].is_string())
@@ -278,14 +292,16 @@
 	return true;
 }
 
-bool Database::ParseDatabaseJSON(const nlohmann::json& json) {
-	for (const auto& anime_json : json)
+bool Database::ParseDatabaseJSON(const nlohmann::json &json)
+{
+	for (const auto &anime_json : json)
 		ParseAnimeInfoJSON(anime_json, *this);
 
 	return true;
 }
 
-bool Database::LoadDatabaseFromDisk() {
+bool Database::LoadDatabaseFromDisk()
+{
 	std::filesystem::path db_path = Filesystem::GetAnimeDBPath();
 	Filesystem::CreateDirectories(db_path);
 
@@ -297,7 +313,7 @@
 	nlohmann::json json;
 	try {
 		json = json.parse(db_file);
-	} catch (std::exception const& ex) {
+	} catch (std::exception const &ex) {
 		std::cerr << "[anime/db] Failed to parse JSON! " << ex.what() << std::endl;
 		return false;
 	}
@@ -308,7 +324,8 @@
 	return true;
 }
 
-int Database::GetUnusedId() const {
+int Database::GetUnusedId() const
+{
 	std::uniform_int_distribution<int> distrib(1, INT_MAX);
 	int res;
 
@@ -319,8 +336,9 @@
 	return res;
 }
 
-int Database::LookupServiceId(Service service, const std::string& id_to_find) const {
-	for (const auto& [id, anime] : items) {
+int Database::LookupServiceId(Service service, const std::string &id_to_find) const
+{
+	for (const auto &[id, anime] : items) {
 		std::optional<std::string> service_id = anime.GetServiceId(service);
 		if (!service_id)
 			continue;
@@ -332,7 +350,8 @@
 	return 0;
 }
 
-int Database::LookupServiceIdOrUnused(Service service, const std::string& id_to_find) const {
+int Database::LookupServiceIdOrUnused(Service service, const std::string &id_to_find) const
+{
 	int id = LookupServiceId(service, id_to_find);
 	if (id)
 		return id;
@@ -340,17 +359,19 @@
 	return GetUnusedId();
 }
 
-void Database::RemoveAllUserData() {
-	for (auto& [id, anime] : items) {
+void Database::RemoveAllUserData()
+{
+	for (auto &[id, anime] : items) {
 		if (anime.IsInUserList())
 			anime.RemoveFromUserList();
 	}
 }
 
-std::vector<int> Database::GetAllAnimeForSeason(Season season) {
+std::vector<int> Database::GetAllAnimeForSeason(Season season)
+{
 	std::vector<int> res;
 
-	for (const auto& [id, anime] : items) {
+	for (const auto &[id, anime] : items) {
 		if (anime.GetSeason() == season)
 			res.push_back(id);
 	}