Song: GetURI() returns std::string

This commit is contained in:
Max Kellermann 2013-10-17 01:01:15 +02:00
parent 67ae033de7
commit be8ceae6e6
10 changed files with 62 additions and 86 deletions

View File

@ -422,16 +422,13 @@ decoder_run(struct decoder_control *dc)
dc->ClearError();
const Song *song = dc->song;
char *uri;
assert(song != NULL);
if (song->IsFile())
uri = g_strdup(map_song_fs(song).c_str());
else
uri = song->GetURI();
const std::string uri = song->IsFile()
? std::string(map_song_fs(song).c_str())
: song->GetURI();
if (uri == NULL) {
if (uri.empty()) {
dc->state = DecoderState::ERROR;
dc->error.Set(decoder_domain, "Failed to map song");
@ -439,8 +436,7 @@ decoder_run(struct decoder_control *dc)
return;
}
decoder_run_song(dc, song, uri);
g_free(uri);
decoder_run_song(dc, song, uri.c_str());
}

View File

@ -461,12 +461,10 @@ Player::CheckDecoderStartup()
decoder_starting = false;
if (!paused && !OpenOutput()) {
char *uri = dc.song->GetURI();
const auto uri = dc.song->GetURI();
FormatError(player_domain,
"problems opening audio device "
"while playing \"%s\"", uri);
g_free(uri);
"while playing \"%s\"", uri.c_str());
return true;
}
@ -878,9 +876,10 @@ Player::SongBorder()
{
xfade_state = CrossFadeState::UNKNOWN;
char *uri = song->GetURI();
FormatInfo(player_domain, "played \"%s\"", uri);
g_free(uri);
{
const auto uri = song->GetURI();
FormatInfo(player_domain, "played \"%s\"", uri.c_str());
}
ReplacePipe(dc.pipe);

View File

@ -25,8 +25,6 @@
#include "Idle.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
void
@ -55,18 +53,17 @@ static void
playlist_queue_song_order(struct playlist *playlist, struct player_control *pc,
unsigned order)
{
char *uri;
assert(playlist->queue.IsValidOrder(order));
playlist->queued = order;
Song *song = playlist->queue.GetOrder(order)->DupDetached();
uri = song->GetURI();
FormatDebug(playlist_domain, "queue song %i:\"%s\"",
playlist->queued, uri);
g_free(uri);
{
const auto uri = song->GetURI();
FormatDebug(playlist_domain, "queue song %i:\"%s\"",
playlist->queued, uri.c_str());
}
pc->EnqueueSong(song);
}
@ -155,9 +152,11 @@ playlist::PlayOrder(player_control &pc, int order)
Song *song = queue.GetOrder(order)->DupDetached();
char *uri = song->GetURI();
FormatDebug(playlist_domain, "play %i:\"%s\"", order, uri);
g_free(uri);
{
const auto uri = song->GetURI();
FormatDebug(playlist_domain, "play %i:\"%s\"",
order, uri.c_str());
}
pc.Play(song);
current = order;

View File

@ -43,9 +43,8 @@ playlist_print_song(FILE *file, const Song *song)
if (!path.IsNull())
fprintf(file, "%s\n", path.c_str());
} else {
char *uri = song->GetURI();
const Path uri_fs = Path::FromUTF8(uri);
g_free(uri);
const auto uri_utf8 = song->GetURI();
const Path uri_fs = Path::FromUTF8(uri_utf8.c_str());
if (!uri_fs.IsNull())
fprintf(file, "%s\n", uri_fs.c_str());

View File

@ -40,10 +40,8 @@
static void
queue_save_database_song(FILE *fp, int idx, const Song *song)
{
char *uri = song->GetURI();
fprintf(fp, "%i:%s\n", idx, uri);
g_free(uri);
const auto uri = song->GetURI();
fprintf(fp, "%i:%s\n", idx, uri.c_str());
}
static void

View File

@ -89,9 +89,8 @@ Song::DupDetached() const
{
Song *song;
if (IsInDatabase()) {
char *new_uri = GetURI();
song = NewDetached(new_uri);
g_free(new_uri);
const auto new_uri = GetURI();
song = NewDetached(new_uri.c_str());
} else
song = song_alloc(uri, nullptr);
@ -138,28 +137,32 @@ song_equals(const Song *a, const Song *b)
(a->parent == &detached_root || b->parent == &detached_root)) {
/* must compare the full URI if one of the objects is
"detached" */
char *au = a->GetURI();
char *bu = b->GetURI();
const bool result = strcmp(au, bu) == 0;
g_free(bu);
g_free(au);
return result;
const auto au = a->GetURI();
const auto bu = b->GetURI();
return au == bu;
}
return directory_is_same(a->parent, b->parent) &&
strcmp(a->uri, b->uri) == 0;
}
char *
std::string
Song::GetURI() const
{
assert(*uri);
if (!IsInDatabase() || parent->IsRoot())
return g_strdup(uri);
else
return g_strconcat(parent->GetPath(),
"/", uri, nullptr);
return std::string(uri);
else {
const char *path = parent->GetPath();
std::string result;
result.reserve(strlen(path) + 1 + strlen(uri));
result.assign(path);
result.push_back('/');
result.append(uri);
return result;
}
}
double

View File

@ -23,6 +23,8 @@
#include "util/list.h"
#include "Compiler.h"
#include <string>
#include <assert.h>
#include <sys/time.h>
@ -126,12 +128,9 @@ struct Song {
/**
* Returns the URI of the song in UTF-8 encoding, including its
* location within the music directory.
*
* The return value is allocated on the heap, and must be freed by the
* caller.
*/
gcc_malloc
char *GetURI() const;
gcc_pure
std::string GetURI() const;
gcc_pure
double GetDuration() const;

View File

@ -123,9 +123,8 @@ bool
SongFilter::Item::Match(const Song &song) const
{
if (tag == LOCATE_TAG_FILE_TYPE || tag == LOCATE_TAG_ANY_TYPE) {
char *uri = song.GetURI();
const bool result = StringMatch(uri);
g_free(uri);
const auto uri = song.GetURI();
const bool result = StringMatch(uri.c_str());
if (result || tag == LOCATE_TAG_FILE_TYPE)
return result;

View File

@ -34,11 +34,8 @@ sticker_song_get_value(const Song *song, const char *name)
assert(song != NULL);
assert(song->IsInDatabase());
char *uri = song->GetURI();
char *value = sticker_load_value("song", uri, name);
g_free(uri);
return value;
const auto uri = song->GetURI();
return sticker_load_value("song", uri.c_str(), name);
}
bool
@ -48,11 +45,8 @@ sticker_song_set_value(const Song *song,
assert(song != NULL);
assert(song->IsInDatabase());
char *uri = song->GetURI();
bool ret = sticker_store_value("song", uri, name, value);
g_free(uri);
return ret;
const auto uri = song->GetURI();
return sticker_store_value("song", uri.c_str(), name, value);
}
bool
@ -61,11 +55,8 @@ sticker_song_delete(const Song *song)
assert(song != NULL);
assert(song->IsInDatabase());
char *uri = song->GetURI();
bool ret = sticker_delete("song", uri);
g_free(uri);
return ret;
const auto uri = song->GetURI();
return sticker_delete("song", uri.c_str());
}
bool
@ -74,11 +65,8 @@ sticker_song_delete_value(const Song *song, const char *name)
assert(song != NULL);
assert(song->IsInDatabase());
char *uri = song->GetURI();
bool success = sticker_delete_value("song", uri, name);
g_free(uri);
return success;
const auto uri = song->GetURI();
return sticker_delete_value("song", uri.c_str(), name);
}
struct sticker *
@ -87,11 +75,8 @@ sticker_song_get(const Song *song)
assert(song != NULL);
assert(song->IsInDatabase());
char *uri = song->GetURI();
struct sticker *sticker = sticker_load("song", uri);
g_free(uri);
return sticker;
const auto uri = song->GetURI();
return sticker_load("song", uri.c_str());
}
struct sticker_song_find_data {

View File

@ -50,13 +50,12 @@ static Cond remove_cond;
static void
song_remove_event(void)
{
char *uri;
assert(removed_song != NULL);
uri = removed_song->GetURI();
FormatInfo(update_domain, "removing %s", uri);
g_free(uri);
{
const auto uri = removed_song->GetURI();
FormatInfo(update_domain, "removing %s", uri.c_str());
}
#ifdef ENABLE_SQLITE
/* if the song has a sticker, remove it */