From dd67992a0c403ff23b071fe9b1e312cfaa4bd924 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 14 Mar 2009 14:20:01 +0100 Subject: sticker: added "struct sticker" The sticker struct can be used for enumerating values. This will replace the sticker_list_values() function. --- src/song_sticker.c | 16 ++++++++++++++ src/song_sticker.h | 10 +++++++++ src/sticker.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sticker.h | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) diff --git a/src/song_sticker.c b/src/song_sticker.c index 46673e737..951b28871 100644 --- a/src/song_sticker.c +++ b/src/song_sticker.c @@ -88,3 +88,19 @@ sticker_song_delete(const struct song *song) return ret; } + +struct sticker * +sticker_song_get(const struct song *song) +{ + char *uri; + struct sticker *sticker; + + assert(song != NULL); + assert(song_in_database(song)); + + uri = song_get_uri(song); + sticker = sticker_load("song", uri); + g_free(uri); + + return sticker; +} diff --git a/src/song_sticker.h b/src/song_sticker.h index df5b092db..3b27e91b6 100644 --- a/src/song_sticker.h +++ b/src/song_sticker.h @@ -24,6 +24,7 @@ #include struct song; +struct sticker; /** * Returns one value from a song's sticker record. The caller must @@ -53,4 +54,13 @@ sticker_song_list_values(const struct song *song); bool sticker_song_delete(const struct song *song); +/** + * Loads the sticker for the specified song. + * + * @param song the song object + * @return a sticker object, or NULL on error or if there is no sticker + */ +struct sticker * +sticker_song_get(const struct song *song); + #endif diff --git a/src/sticker.c b/src/sticker.c index ce8b1d68b..cad4e1c97 100644 --- a/src/sticker.c +++ b/src/sticker.c @@ -27,6 +27,10 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "sticker" +struct sticker { + GHashTable *table; +}; + static const char sticker_sql_create[] = "CREATE TABLE IF NOT EXISTS sticker(" " type VARCHAR NOT NULL, " @@ -433,3 +437,61 @@ sticker_delete(const char *type, const char *uri) idle_add(IDLE_STICKER); return true; } + +void +sticker_free(struct sticker *sticker) +{ + assert(sticker != NULL); + assert(sticker->table != NULL); + + g_hash_table_destroy(sticker->table); + g_free(sticker); +} + +const char * +sticker_get_value(struct sticker *sticker, const char *name) +{ + return g_hash_table_lookup(sticker->table, name); +} + +struct sticker_foreach_data { + void (*func)(const char *name, const char *value, + gpointer user_data); + gpointer user_data; +}; + +static void +sticker_foreach_func(gpointer key, gpointer value, gpointer user_data) +{ + struct sticker_foreach_data *data = user_data; + + data->func(key, value, data->user_data); +} + +void +sticker_foreach(struct sticker *sticker, + void (*func)(const char *name, const char *value, + gpointer user_data), + gpointer user_data) +{ + struct sticker_foreach_data data = { + .func = func, + .user_data = user_data, + }; + + g_hash_table_foreach(sticker->table, sticker_foreach_func, &data); +} + +struct sticker * +sticker_load(const char *type, const char *uri) +{ + struct sticker *sticker = g_new(struct sticker, 1); + + sticker->table = sticker_list_values(type, uri); + if (sticker->table == NULL) { + g_free(sticker); + return NULL; + } + + return sticker; +} diff --git a/src/sticker.h b/src/sticker.h index 5e9ba55f7..9ca740d45 100644 --- a/src/sticker.h +++ b/src/sticker.h @@ -46,6 +46,8 @@ #include +struct sticker; + /** * Opens the sticker database (if path is not NULL). */ @@ -94,4 +96,45 @@ sticker_store_value(const char *type, const char *uri, bool sticker_delete(const char *type, const char *uri); +/** + * Frees resources held by the sticker object. + * + * @param sticker the sticker object to be freed + */ +void +sticker_free(struct sticker *sticker); + +/** + * Determines a single value in a sticker. + * + * @param sticker the sticker object + * @param name the name of the sticker + * @return the sticker value, or NULL if none was found + */ +const char * +sticker_get_value(struct sticker *sticker, const char *name); + +/** + * Iterates over all sticker items in a sticker. + * + * @param sticker the sticker object + * @param func a callback function + * @param user_data an opaque pointer for the callback function + */ +void +sticker_foreach(struct sticker *sticker, + void (*func)(const char *name, const char *value, + gpointer user_data), + gpointer user_data); + +/** + * Loads the sticker for the specified resource. + * + * @param type the resource type, e.g. "song" + * @param uri the URI of the resource, e.g. the song path + * @return a sticker object, or NULL on error or if there is no sticker + */ +struct sticker * +sticker_load(const char *type, const char *uri); + #endif -- cgit v1.2.3