core/library: Improve subdirectory handling for lookup and find
It's possible that the user may pass us a subdirectory of a path that has already been added to the library. We can use this to prevent double-adding tracks if the user does this. Additionally, this function can be used to help look up tracks using only a filepath. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
497ed57057
commit
e7ceed9b5d
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
* Copyright 2014 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <core/string.h>
|
||||
#include <core/tags/library.h>
|
||||
|
||||
#define LIBRARY_DB_MIN 0 /* Ocarina 6.0 */
|
||||
|
@ -82,12 +83,22 @@ const struct database *library_db_get()
|
|||
|
||||
struct library *library_find(const gchar *path)
|
||||
{
|
||||
return LIBRARY(db_find(&library_db, path));
|
||||
struct library *library = library_lookup(path);
|
||||
if (library)
|
||||
return library;
|
||||
return LIBRARY(db_insert(&library_db, path));
|
||||
}
|
||||
|
||||
struct library *library_lookup(const gchar *path)
|
||||
{
|
||||
return LIBRARY(db_get(&library_db, path));
|
||||
struct db_entry *dbe, *next;
|
||||
|
||||
db_for_each(dbe, next, &library_db) {
|
||||
if (string_is_subdir(path, LIBRARY(dbe)->li_path))
|
||||
return LIBRARY(dbe);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct library *library_get(const unsigned int index)
|
||||
|
|
|
@ -37,9 +37,11 @@ bool library_db_defrag();
|
|||
const struct database *library_db_get();
|
||||
|
||||
/*
|
||||
* Called to find a library tag by library path. The difference is that
|
||||
* Called to find a library tag by path. The difference is that
|
||||
* library_find() will allocate a new library struct if the requested one
|
||||
* doesn't exist yet, but library_lookup() will return NULL in this situation.
|
||||
*
|
||||
* Note that path may be a subdirectory of the returned library.
|
||||
*/
|
||||
struct library *library_find(const gchar *);
|
||||
struct library *library_lookup(const gchar *);
|
||||
|
|
|
@ -69,14 +69,15 @@ static void test_library_db()
|
|||
g_assert_cmpuint(library_db_get()->db_size, ==, 0);
|
||||
|
||||
library_db_init();
|
||||
library = library_lookup("/home/Zelda/Music");
|
||||
g_assert_null(library);
|
||||
g_assert_null(library_lookup("/home/Zelda/Music"));
|
||||
library = library_find("/home/Zelda/Music");
|
||||
test_verify_zelda(library);
|
||||
|
||||
g_assert_cmpuint(library_db_get()->db_size, ==, 1);
|
||||
g_assert(library_lookup("/home/Zelda/Music") == library);
|
||||
g_assert(library_lookup("/home/Zelda/Music/Ocarina") == library);
|
||||
g_assert(library_find("/home/Zelda/Music") == library);
|
||||
g_assert(library_find("/home/Zelda/Music/Ocarina") == library);
|
||||
g_assert(library_get(0) == library);
|
||||
g_assert_null(library_get(1));
|
||||
|
||||
|
|
Loading…
Reference in New Issue