aboutsummaryrefslogtreecommitdiffstats
path: root/src/StickerDatabase.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/StickerDatabase.cxx (renamed from src/sticker.c)95
1 files changed, 33 insertions, 62 deletions
diff --git a/src/sticker.c b/src/StickerDatabase.cxx
index 346a827a5..2d77e4b63 100644
--- a/src/sticker.c
+++ b/src/StickerDatabase.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -18,8 +18,11 @@
*/
#include "config.h"
-#include "sticker.h"
-#include "idle.h"
+#include "StickerDatabase.hxx"
+#include "Idle.hxx"
+
+#include <string>
+#include <map>
#include <glib.h>
#include <sqlite3.h>
@@ -33,7 +36,7 @@
#endif
struct sticker {
- GHashTable *table;
+ std::map<std::string, std::string> table;
};
enum sticker_sql {
@@ -47,19 +50,19 @@ enum sticker_sql {
};
static const char *const sticker_sql[] = {
- [STICKER_SQL_GET] =
+ //[STICKER_SQL_GET] =
"SELECT value FROM sticker WHERE type=? AND uri=? AND name=?",
- [STICKER_SQL_LIST] =
+ //[STICKER_SQL_LIST] =
"SELECT name,value FROM sticker WHERE type=? AND uri=?",
- [STICKER_SQL_UPDATE] =
+ //[STICKER_SQL_UPDATE] =
"UPDATE sticker SET value=? WHERE type=? AND uri=? AND name=?",
- [STICKER_SQL_INSERT] =
+ //[STICKER_SQL_INSERT] =
"INSERT INTO sticker(type,uri,name,value) VALUES(?, ?, ?, ?)",
- [STICKER_SQL_DELETE] =
+ //[STICKER_SQL_DELETE] =
"DELETE FROM sticker WHERE type=? AND uri=?",
- [STICKER_SQL_DELETE_VALUE] =
+ //[STICKER_SQL_DELETE_VALUE] =
"DELETE FROM sticker WHERE type=? AND uri=? AND name=?",
- [STICKER_SQL_FIND] =
+ //[STICKER_SQL_FIND] =
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=?",
};
@@ -226,13 +229,12 @@ sticker_load_value(const char *type, const char *uri, const char *name)
}
static bool
-sticker_list_values(GHashTable *hash, const char *type, const char *uri)
+sticker_list_values(std::map<std::string, std::string> &table,
+ const char *type, const char *uri)
{
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_LIST];
int ret;
- char *name, *value;
- assert(hash != NULL);
assert(type != NULL);
assert(uri != NULL);
assert(sticker_enabled());
@@ -256,10 +258,13 @@ sticker_list_values(GHashTable *hash, const char *type, const char *uri)
do {
ret = sqlite3_step(stmt);
switch (ret) {
+ const char *name, *value;
+
case SQLITE_ROW:
- name = g_strdup((const char*)sqlite3_column_text(stmt, 0));
- value = g_strdup((const char*)sqlite3_column_text(stmt, 1));
- g_hash_table_insert(hash, name, value);
+ name = (const char*)sqlite3_column_text(stmt, 0);
+ value = (const char*)sqlite3_column_text(stmt, 1);
+
+ table.insert(std::make_pair(name, value));
break;
case SQLITE_DONE:
break;
@@ -518,44 +523,20 @@ sticker_delete_value(const char *type, const char *uri, const char *name)
return ret > 0;
}
-static struct sticker *
-sticker_new(void)
-{
- struct sticker *sticker = g_new(struct sticker, 1);
-
- sticker->table = g_hash_table_new_full(g_str_hash, g_str_equal,
- g_free, g_free);
- return sticker;
-}
-
void
sticker_free(struct sticker *sticker)
{
- assert(sticker != NULL);
- assert(sticker->table != NULL);
-
- g_hash_table_destroy(sticker->table);
- g_free(sticker);
+ delete sticker;
}
const char *
sticker_get_value(const 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;
+ auto i = sticker->table.find(name);
+ if (i == sticker->table.end())
+ return nullptr;
- data->func(key, value, data->user_data);
+ return i->second.c_str();
}
void
@@ -564,33 +545,23 @@ sticker_foreach(const struct sticker *sticker,
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);
+ for (const auto &i : sticker->table)
+ func(i.first.c_str(), i.second.c_str(), user_data);
}
struct sticker *
sticker_load(const char *type, const char *uri)
{
- struct sticker *sticker = sticker_new();
- bool success;
+ sticker s;
- success = sticker_list_values(sticker->table, type, uri);
- if (!success) {
- sticker_free(sticker);
+ if (!sticker_list_values(s.table, type, uri))
return NULL;
- }
- if (g_hash_table_size(sticker->table) == 0) {
+ if (s.table.empty())
/* don't return empty sticker objects */
- sticker_free(sticker);
return NULL;
- }
- return sticker;
+ return new sticker(std::move(s));
}
bool