comparison src/library/library.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 886f66775f31
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
11 11
12 #include <iostream> 12 #include <iostream>
13 13
14 namespace Library { 14 namespace Library {
15 15
16 std::optional<std::filesystem::path> Database::GetAnimeFolder(int id) { 16 std::optional<std::filesystem::path> Database::GetAnimeFolder(int id)
17 {
17 // this function sucks, but it's the most I can really do for now. 18 // this function sucks, but it's the most I can really do for now.
18 // 19 //
19 // in the future the Refresh() function should look for directories 20 // in the future the Refresh() function should look for directories
20 // as well that fit the anime name and *also* have episodes in them. 21 // as well that fit the anime name and *also* have episodes in them.
21 // it should give each of these directories a rating by how many 22 // it should give each of these directories a rating by how many
22 // episodes are contained in them. whichever directory has more episodes 23 // episodes are contained in them. whichever directory has more episodes
23 // wins, or the first found if there is an equal amount. 24 // wins, or the first found if there is an equal amount.
24 25
25 for (const auto& [anime_id, episodes] : items) { 26 for (const auto &[anime_id, episodes] : items) {
26 if (id != anime_id) 27 if (id != anime_id)
27 continue; 28 continue;
28 29
29 for (const auto& [episode, path] : episodes) { 30 for (const auto &[episode, path] : episodes) {
30 return path.parent_path(); 31 return path.parent_path();
31 break; 32 break;
32 } 33 }
33 34
34 break; 35 break;
35 } 36 }
36 37
37 return std::nullopt; 38 return std::nullopt;
38 } 39 }
39 40
40 void Database::Refresh(std::optional<int> find_id) { 41 void Database::Refresh(std::optional<int> find_id)
42 {
41 items.clear(); 43 items.clear();
42 44
43 for (const auto& folder : session.config.library.paths) { 45 for (const auto &folder : session.config.library.paths) {
44 for (const auto& entry : std::filesystem::recursive_directory_iterator(folder)) { 46 for (const auto &entry : std::filesystem::recursive_directory_iterator(folder)) {
45 const std::filesystem::path path = entry.path(); 47 const std::filesystem::path path = entry.path();
46 if (!std::filesystem::is_regular_file(path)) 48 if (!std::filesystem::is_regular_file(path))
47 continue; 49 continue;
48 50
49 const std::string basename = path.filename().u8string(); 51 const std::string basename = path.filename().u8string();
50 52
51 anitomy::Anitomy anitomy; 53 anitomy::Anitomy anitomy;
52 anitomy.Parse(basename); 54 anitomy.Parse(basename);
53 55
54 const auto& elements = anitomy.elements(); 56 const auto &elements = anitomy.elements();
55 57
56 const std::string title = Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)); 58 const std::string title = Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle));
57 59
58 const int id = Anime::db.LookupAnimeTitle(title); 60 const int id = Anime::db.LookupAnimeTitle(title);
59 if (id <= 0 || (find_id && find_id.value() != id)) 61 if (id <= 0 || (find_id && find_id.value() != id))
60 continue; 62 continue;
61 63
62 const int episode = Strings::ToInt<int>(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber))); 64 const int episode =
65 Strings::ToInt<int>(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)));
63 66
64 // we have an ID now! 67 // we have an ID now!
65 items[id][episode] = path; 68 items[id][episode] = path;
66 } 69 }
67 } 70 }
68 } 71 }
69 72
70 void Database::Refresh() { 73 void Database::Refresh()
74 {
71 Refresh(std::nullopt); 75 Refresh(std::nullopt);
72 } 76 }
73 77
74 void Database::Refresh(int id) { 78 void Database::Refresh(int id)
79 {
75 Refresh(std::optional<int>(id)); 80 Refresh(std::optional<int>(id));
76 } 81 }
77 82
78 // TODO export to JSON 83 // TODO export to JSON
79 84