inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
songmap.hpp
Go to the documentation of this file.
1
15
16#ifndef SONG_MAP_HPP
17#define SONG_MAP_HPP
18
19#include "taglib_parser.h"
20#include <cereal/archives/binary.hpp>
21#include <cereal/types/map.hpp>
22#include <cereal/types/string.hpp>
23#include <cereal/types/vector.hpp>
24#include <fstream>
25#include <iostream>
26#include <map>
27#include <string>
28#include <vector>
29
37struct Song
38{
39 unsigned int inode;
41
48 Song(unsigned int inode, const Metadata& metadata) : inode(inode), metadata(metadata) {}
49
53 Song() : inode(0), metadata() {}
54
62 template <class Archive>
63 void serialize(Archive& ar)
64 {
65 ar(inode, metadata);
66 }
67};
68
78{
79private:
88 std::map<std::string, std::map<std::string, std::map<unsigned int, std::map<unsigned int, Song>>>> tree;
89
90public:
98 void addSong(const Song& song)
99 {
100 tree[song.metadata.artist][song.metadata.album][song.metadata.discNumber][song.metadata.track] = song;
101 }
102
109 void display() const
110 {
111 for (const auto& artistPair : tree)
112 {
113 std::cout << "Artist: " << artistPair.first << "\n";
114 for (const auto& albumPair : artistPair.second)
115 {
116 std::cout << " Album: " << albumPair.first << "\n";
117 for (const auto& discPair : albumPair.second)
118 {
119 std::cout << " Disc: " << discPair.first << "\n";
120 for (const auto& trackPair : discPair.second)
121 {
122 const auto& song = trackPair.second;
123 std::cout << " Track " << trackPair.first << ": " << song.metadata.title
124 << " (Inode: " << song.inode << ", Artist: " << song.metadata.artist
125 << ", Album: " << song.metadata.album << ", Genre: " << song.metadata.genre
126 << ")\n";
127 }
128 }
129 }
130 }
131 }
132
141 std::vector<Song> getSongsByArtist(const std::string& artist) const
142 {
143 std::vector<Song> result;
144 auto artistIt = tree.find(artist);
145 if (artistIt != tree.end())
146 {
147 for (const auto& albumPair : artistIt->second)
148 {
149 for (const auto& discPair : albumPair.second)
150 {
151 for (const auto& trackPair : discPair.second)
152 {
153 result.push_back(trackPair.second);
154 }
155 }
156 }
157 }
158 return result;
159 }
160
170 std::vector<Song> getSongsByAlbum(const std::string& artist, const std::string& album) const
171 {
172 std::vector<Song> result;
173 auto artistIt = tree.find(artist);
174 if (artistIt != tree.end())
175 {
176 auto albumIt = artistIt->second.find(album);
177 if (albumIt != artistIt->second.end())
178 {
179 for (const auto& discPair : albumIt->second)
180 {
181 for (const auto& trackPair : discPair.second)
182 {
183 result.push_back(trackPair.second);
184 }
185 }
186 }
187 }
188 return result;
189 }
190
198 std::map<std::string, std::map<std::string, std::map<unsigned int, std::map<unsigned int, Song>>>> returnSongMap()
199 {
200 return tree;
201 }
202
210 template <class Archive>
211 void serialize(Archive& ar)
212 {
213 ar(tree);
214 }
215
224 void saveToFile(const std::string& filename) const
225 {
226 std::ofstream file(filename, std::ios::binary);
227 if (!file)
228 {
229 throw std::runtime_error("Failed to open file for saving.");
230 }
231 cereal::BinaryOutputArchive archive(file);
232 archive(*this); // Serialize the SongTree
233 }
234
243 void loadFromFile(const std::string& filename)
244 {
245 std::ifstream file(filename, std::ios::binary);
246 if (!file)
247 {
248 throw std::runtime_error("Failed to open file for loading.");
249 }
250 cereal::BinaryInputArchive archive(file);
251 archive(*this); // Deserialize the SongTree
252 }
253};
254
255#endif
Represents a hierarchical tree structure to store songs by artist, album, disc number,...
Definition songmap.hpp:78
void serialize(Archive &ar)
Serializes the SongTree object.
Definition songmap.hpp:211
void display() const
Displays all songs in the tree hierarchically.
Definition songmap.hpp:109
void addSong(const Song &song)
Adds a song to the tree.
Definition songmap.hpp:98
std::vector< Song > getSongsByArtist(const std::string &artist) const
Retrieves all songs by a specific artist.
Definition songmap.hpp:141
std::map< std::string, std::map< std::string, std::map< unsigned int, std::map< unsigned int, Song > > > > returnSongMap()
Returns the internal song map.
Definition songmap.hpp:198
void saveToFile(const std::string &filename) const
Saves the SongTree to a file.
Definition songmap.hpp:224
void loadFromFile(const std::string &filename)
Loads a SongTree from a file.
Definition songmap.hpp:243
std::vector< Song > getSongsByAlbum(const std::string &artist, const std::string &album) const
Retrieves all songs from a specific album by a given artist.
Definition songmap.hpp:170
A structure to hold metadata information for a song.
Definition taglib_parser.h:39
unsigned int track
Definition taglib_parser.h:46
unsigned int discNumber
Definition taglib_parser.h:47
std::string artist
Definition taglib_parser.h:41
std::string album
Definition taglib_parser.h:42
Represents a song with associated metadata and inode.
Definition songmap.hpp:38
Song()
Default constructor for a Song, initializing with default values.
Definition songmap.hpp:53
unsigned int inode
Definition songmap.hpp:39
Metadata metadata
Definition songmap.hpp:40
void serialize(Archive &ar)
Serializes the Song object.
Definition songmap.hpp:63
Song(unsigned int inode, const Metadata &metadata)
Constructs a Song with the given inode and metadata.
Definition songmap.hpp:48
A header file for the TagLibParser class and Metadata structure, used to parse metadata from audio fi...