core/tags/library: Replace std::string with gchar *

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-11 17:09:17 -05:00
parent 5cc4efb6ac
commit 62fd782710
5 changed files with 51 additions and 41 deletions

View File

@ -165,7 +165,7 @@ struct library *collection :: add(const std::string &dir)
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
return library;
library = library_find(dir);
library = library_find(dir.c_str());
if (library)
update(library);
return library;

View File

@ -6,15 +6,21 @@
static struct database library_db;
static struct db_entry *library_alloc(const gchar *path)
static struct library *__library_alloc(gchar *path, bool enabled)
{
struct library *library = new struct library;
dbe_init(&library->li_dbe, library);
library->li_size = 0;
library->li_enabled = true;
library->li_enabled = enabled;
library->li_path = path;
return &library->li_dbe;
return library;
}
static struct db_entry *library_alloc(const gchar *path)
{
return &__library_alloc(g_strdup(path), true)->li_dbe;
}
static void library_free(struct db_entry *dbe)
@ -24,32 +30,22 @@ static void library_free(struct db_entry *dbe)
static gchar *library_key(struct db_entry *dbe)
{
return g_strdup_printf(LIBRARY(dbe)->li_path.c_str());
return LIBRARY(dbe)->li_path;
}
static struct db_entry *library_read(struct file *file)
{
struct library *library = new struct library;
gchar *path;
int enabled;
gchar *path;
dbe_init(&library->li_dbe, library);
library->li_size = 0;
file_readf(file, "%d\n", &enabled);
library->li_enabled = enabled;
path = file_readl(file);
library->li_path = path;
g_free(path);
return &library->li_dbe;
file_readf(file, "%d %m[^\n]", &enabled, &path);
return &__library_alloc(path, enabled)->li_dbe;
}
static void library_write(struct file *file, struct db_entry *dbe)
{
file_writef(file, "%d %s", LIBRARY(dbe)->li_enabled,
LIBRARY(dbe)->li_path.c_str());
LIBRARY(dbe)->li_path);
}
@ -79,9 +75,9 @@ const struct database *library_db_get()
return &library_db;
}
struct library *library_find(const std::string &path)
struct library *library_find(const gchar *path)
{
return LIBRARY(db_find(&library_db, path.c_str()));
return LIBRARY(db_find(&library_db, path));
}
struct library *library_get(const unsigned int index)
@ -101,9 +97,9 @@ void library_set_enabled(struct library *library, bool enabled)
db_save(&library_db);
}
std::string library_file(struct library *library, const std::string &path)
gchar *library_file(struct library *library, const gchar *path)
{
return library->li_path + "/" + path;
return g_strdup_printf("%s/%s", library->li_path, path);
}
#ifdef CONFIG_TESTING

View File

@ -33,15 +33,14 @@ struct db_entry *track_alloc(const gchar *key)
unsigned int lib_id;
TagLib_File *file;
TagLib_Tag *tag;
std::string full;
char *path, *lower;
char *fullpath, *path, *lower;
sscanf(key, "%u/%m[^\n]", &lib_id, &path);
library = library_get(lib_id);
full = library_file(library, path);
file = taglib_file_new(full.c_str());
library = library_get(lib_id);
fullpath = library_file(library, path);
file = taglib_file_new(fullpath);
if (!file || !taglib_file_is_valid(file)) {
printf("WARNING: Could not read tags for: %s\n", full.c_str());
printf("WARNING: Could not read tags for: %s\n", fullpath);
goto out;
}
@ -68,6 +67,7 @@ struct db_entry *track_alloc(const gchar *key)
taglib_file_free(file);
g_free(lower);
out:
g_free(fullpath);
g_free(path);
return track;
}
@ -174,7 +174,7 @@ const struct database *track_db_get()
struct track *track_add(struct library *library, const std::string &filepath)
{
std::string path = filepath.substr(library->li_path.size() + 1);
std::string path = filepath.substr(strlen(library->li_path) + 1);
gchar *key = __track_key(library, path);
struct track *track = NULL;
@ -213,9 +213,14 @@ int track_compare(struct track *lhs, struct track *rhs)
const std::string track_path(struct track *track)
{
if (track->tr_library)
return library_file(track->tr_library, track->tr_path);
return "";
std::string ret = "";
if (track->tr_library) {
gchar *g_ret = library_file(track->tr_library, track->tr_path.c_str());
ret = g_ret;
g_free(g_ret);
}
return ret;
}
void track_played(struct track *track)

View File

@ -23,7 +23,7 @@ extern "C" {
struct library {
unsigned int li_size; /* This library's track count. */
bool li_enabled;/* True if this library is enabled. */
std::string li_path; /* This library's root path. */
gchar *li_path; /* This library's root path. */
struct db_entry li_dbe;
};
@ -40,7 +40,7 @@ void library_db_deinit();
const struct database *library_db_get();
/* Called to find a library tag by library path. */
struct library *library_find(const std::string &);
struct library *library_find(const gchar *);
/* Called to get a library tag with a specific index. */
struct library *library_get(const unsigned int);
@ -51,8 +51,11 @@ void library_remove(struct library *);
/* Called to configure if the library tag is enabled. */
void library_set_enabled(struct library *, bool);
/* Called to find the full path of files under the library directory. */
std::string library_file(struct library *, const std::string &);
/*
* Called to find the full path of files under the library directory.
* This function allocates a new string that MUST be freed with g_free().
*/
gchar *library_file(struct library *, const gchar *);
#ifdef CONFIG_TESTING
const struct db_ops *test_library_ops();

View File

@ -9,7 +9,7 @@ static void test_verify_zelda(struct library *library)
const struct db_ops *library_ops = test_library_ops();
test_equal(library->li_enabled, true);
test_equal(library->li_size, 0);
test_str_equal(library_ops->dbe_key(&library->li_dbe), "/home/Zelda/Music");
test_equal(library_ops->dbe_key(&library->li_dbe), "/home/Zelda/Music");
}
static void test_verify_link(struct library *library)
@ -17,7 +17,7 @@ static void test_verify_link(struct library *library)
const struct db_ops *library_ops = test_library_ops();
test_equal(library->li_enabled, false);
test_equal(library->li_size, 0);
test_str_equal(library_ops->dbe_key(&library->li_dbe), "/home/Link/Music");
test_equal(library_ops->dbe_key(&library->li_dbe), "/home/Link/Music");
}
static void test_library()
@ -46,14 +46,20 @@ static void test_library()
file_open(&f, OPEN_READ);
library = LIBRARY(library_ops->dbe_read(&f));
test_verify_link(library);
test_equal(library_file(library, "navi.mp3"), "/home/Link/Music/navi.mp3");
test_str_equal(library_file(library, "navi.mp3"),
"/home/Link/Music/navi.mp3");
g_free(library->li_path);
library_ops->dbe_free(&library->li_dbe);
library = LIBRARY(library_ops->dbe_read(&f));
file_close(&f);
test_verify_zelda(library);
test_equal(library_file(library, "impa.ogg"), "/home/Zelda/Music/impa.ogg");
test_str_equal(library_file(library, "impa.ogg"),
"/home/Zelda/Music/impa.ogg");
g_free(link->li_path);
g_free(zelda->li_path);
g_free(library->li_path);
library_ops->dbe_free(&link->li_dbe);
library_ops->dbe_free(&zelda->li_dbe);
library_ops->dbe_free(&library->li_dbe);