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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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