library: Lookup a song by id

If the id is valid, I fill out a song structure and return true.
If the id is invalid, I return false.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-11-17 14:41:22 -05:00 committed by Anna Schumaker
parent e314248f10
commit 4fc09978dc
3 changed files with 62 additions and 0 deletions

View File

@ -117,10 +117,20 @@ namespace library
};
struct Song {
library :: Album *album;
library :: Artist *artist;
library :: Genre *genre;
library :: Library *library;
library :: Track *track;
};
void init();
bool add_path(const std::string &);
void del_path(unsigned int);
void update_path(unsigned int);
bool lookup(unsigned int, library :: Song *);
#ifdef CONFIG_DEBUG
void print_db(DB_Type);
void reset();

View File

@ -336,6 +336,18 @@ void library :: update_path(unsigned int id)
do_update(id, library_db[id].root_path);
}
bool library :: lookup(unsigned int id, library :: Song *song)
{
if (id >= track_db.num_rows())
return false;
song->track = &track_db[id];
song->artist = &artist_db[song->track->artist_id];
song->album = &album_db[song->track->album_id];
song->genre = &genre_db[song->track->genre_id];
song->library = &library_db[song->track->library_id];
return true;
}
#ifdef CONFIG_DEBUG
void library :: print_db(DB_Type type)
{

View File

@ -29,6 +29,28 @@ void test_del_dir(const std::string &test, const unsigned int path_id)
library :: print_db(library :: DB_LIBRARY);
}
void test_lookup(const std::string &test, const unsigned int track_id, bool expected)
{
library :: Song song;
bool res;
print("Test %s (track_id == %u): ", test.c_str(), track_id);
res = library :: lookup(track_id, &song);
if (res == expected)
print("PASSED\n");
else
print("FAILED\n");
if (res == true) {
print(" %s by %s from %s\n", song.track->title.c_str(),
song.artist->name.c_str(), song.album->name.c_str());
print(" Genre: %s, Library: %s\n", song.genre->name.c_str(),
song.library->root_path.c_str());
}
}
void test_print_dbs(const std::string &test)
{
print("Test %s\n", test.c_str());
@ -97,6 +119,23 @@ void test_4()
library :: print_db(library :: DB_GENRE);
print("\n");
library :: print_db(library :: DB_TRACK);
print("\n");
}
/* Test lookup() */
void test_5()
{
library :: reset();
/* Lookup on empty DB */
test_lookup("5a", 0, false);
test_add_dir("5b", "/tmp/library/0", true);
/* Lookup on DB[0] */
test_lookup("5c", 0, true);
/* Lookup on DB[10] */
test_lookup("5d", 42, true);
/* Lookup beyond db */
test_lookup("5e", 100000, false);
}
int main(int argc, char **argv)
@ -108,5 +147,6 @@ int main(int argc, char **argv)
test_2();
test_3();
test_4();
test_5();
return 0;
}