sticker: added "struct sticker"

The sticker struct can be used for enumerating values.  This will
replace the sticker_list_values() function.
This commit is contained in:
Max Kellermann 2009-03-14 14:20:01 +01:00
parent 5b687795c4
commit dd67992a0c
4 changed files with 131 additions and 0 deletions

View File

@ -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;
}

View File

@ -24,6 +24,7 @@
#include <glib.h>
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

View File

@ -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;
}

View File

@ -46,6 +46,8 @@
#include <stdbool.h>
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