From 938bbc92f2c76e98fce2a16605adb052c87a7544 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 9 May 2016 10:09:59 -0400 Subject: [PATCH] core/tags/library: Add a library_lookup() function I want a way to lookup library paths without allocating new ones, so let's add this now. Signed-off-by: Anna Schumaker --- core/tags/library.c | 5 +++++ include/core/tags/library.h | 7 ++++++- tests/core/tags/library.c | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/tags/library.c b/core/tags/library.c index 0fa806dd..f513e004 100644 --- a/core/tags/library.c +++ b/core/tags/library.c @@ -81,6 +81,11 @@ struct library *library_find(const gchar *path) return LIBRARY(db_find(&library_db, path)); } +struct library *library_lookup(const gchar *path) +{ + return LIBRARY(db_get(&library_db, path)); +} + struct library *library_get(const unsigned int index) { return LIBRARY(db_at(&library_db, index)); diff --git a/include/core/tags/library.h b/include/core/tags/library.h index 0421d31f..273dc3d9 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -35,8 +35,13 @@ void library_db_deinit(); /* Called to access the library database. */ const struct database *library_db_get(); -/* Called to find a library tag by library path. */ +/* + * Called to find a library tag by library 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. + */ struct library *library_find(const gchar *); +struct library *library_lookup(const gchar *); /* Called to get a library tag with a specific index. */ struct library *library_get(const unsigned int); diff --git a/tests/core/tags/library.c b/tests/core/tags/library.c index 1e1a809c..d59e778a 100644 --- a/tests/core/tags/library.c +++ b/tests/core/tags/library.c @@ -77,10 +77,13 @@ static void test_library_db() test_equal(library_db_get()->db_size, 0); library_db_init(); + library = library_lookup("/home/Zelda/Music"); + test_equal((void *)library, NULL); library = library_find("/home/Zelda/Music"); test_verify_zelda(library); test_equal(library_db_get()->db_size, 1); + test_equal((void *)library_lookup("/home/Zelda/Music"), (void *)library); test_equal((void *)library_find("/home/Zelda/Music"), (void *)library); test_equal((void *)library_get(0), (void *)library); test_equal((void *)library_get(1), NULL);