core/tags/track: Add a function to look up tracks using only the path

I'm struggling to come up with a good way to combine code for this with
code for track_add(), since the lookup mechanism is basically the same.
I'll keep them separate for now and think on it some more.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2017-04-11 14:54:59 -04:00
parent e7ceed9b5d
commit f9a573b6a3
3 changed files with 26 additions and 0 deletions

View File

@ -285,6 +285,23 @@ struct track *track_get(const unsigned int index)
return TRACK(db_at(&track_db, index)); return TRACK(db_at(&track_db, index));
} }
struct track *track_lookup(const gchar *filepath)
{
struct library *library = library_lookup(filepath);
unsigned int offset;
struct track *track;
gchar *key;
if (!library)
return NULL;
offset = strlen(library->li_path) + 1;
key = __track_key(library, g_strdup(filepath + offset));
track = TRACK(db_get(&track_db, key));
g_free(key);
return track;
}
int track_compare(struct track *lhs, struct track *rhs, enum compare_t compare) int track_compare(struct track *lhs, struct track *rhs, enum compare_t compare)
{ {
switch (compare) { switch (compare) {

View File

@ -99,6 +99,9 @@ void track_remove_all(struct library *);
/* Called to get a track tag with a specific index. */ /* Called to get a track tag with a specific index. */
struct track *track_get(const unsigned int); struct track *track_get(const unsigned int);
/* Called to look up a track tag using only a filepath. */
struct track *track_lookup(const gchar *);
/* Called to compare two track tags */ /* Called to compare two track tags */
int track_compare(struct track *, struct track *, enum compare_t); int track_compare(struct track *, struct track *, enum compare_t);

View File

@ -209,8 +209,14 @@ static void __test_track_db_subprocess()
g_assert_cmpuint(track_db_count_unplayed(), ==, 0); g_assert_cmpuint(track_db_count_unplayed(), ==, 0);
g_assert_cmpuint(track_db_count_plays(), ==, 0); g_assert_cmpuint(track_db_count_plays(), ==, 0);
g_assert_cmpuint(track_db_average_plays(), ==, 0); g_assert_cmpuint(track_db_average_plays(), ==, 0);
g_assert_null(track_lookup(NULL));
g_assert_null(track_lookup("/home/Zelda/Music/ocarina.ogg"));
g_assert_null(track_lookup(path));
track = track_add(library, path); track = track_add(library, path);
test_verify_track(track); test_verify_track(track);
g_assert(track_lookup(path) == track);
g_assert_cmpuint(track_db_count_unplayed(), ==, 1); g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
g_assert_null(track_add(library, path)); g_assert_null(track_add(library, path));