From be8ceae6e6c2836233dd8acedd829b18943f820a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 17 Oct 2013 01:01:15 +0200 Subject: Song: GetURI() returns std::string --- src/DecoderThread.cxx | 14 +++++--------- src/PlayerThread.cxx | 13 ++++++------- src/Playlist.cxx | 21 ++++++++++----------- src/PlaylistSave.cxx | 5 ++--- src/QueueSave.cxx | 6 ++---- src/Song.cxx | 31 +++++++++++++++++-------------- src/Song.hxx | 9 ++++----- src/SongFilter.cxx | 5 ++--- src/SongSticker.cxx | 35 ++++++++++------------------------- src/UpdateRemove.cxx | 9 ++++----- 10 files changed, 62 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/DecoderThread.cxx b/src/DecoderThread.cxx index aacbb1fb7..4d0d008ca 100644 --- a/src/DecoderThread.cxx +++ b/src/DecoderThread.cxx @@ -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()); } diff --git a/src/PlayerThread.cxx b/src/PlayerThread.cxx index 9ad37eaee..94cfe2af1 100644 --- a/src/PlayerThread.cxx +++ b/src/PlayerThread.cxx @@ -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); diff --git a/src/Playlist.cxx b/src/Playlist.cxx index 9f3758c52..2932c2ca0 100644 --- a/src/Playlist.cxx +++ b/src/Playlist.cxx @@ -25,8 +25,6 @@ #include "Idle.hxx" #include "Log.hxx" -#include - #include 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; diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 7889f778a..5eb0e1180 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -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()); diff --git a/src/QueueSave.cxx b/src/QueueSave.cxx index 0c036d963..30c669538 100644 --- a/src/QueueSave.cxx +++ b/src/QueueSave.cxx @@ -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 diff --git a/src/Song.cxx b/src/Song.cxx index 5ee4c5545..c63dc8753 100644 --- a/src/Song.cxx +++ b/src/Song.cxx @@ -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 diff --git a/src/Song.hxx b/src/Song.hxx index e5bb28008..cafa17fd8 100644 --- a/src/Song.hxx +++ b/src/Song.hxx @@ -23,6 +23,8 @@ #include "util/list.h" #include "Compiler.h" +#include + #include #include @@ -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; diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx index 4b8ae20ba..8874b3448 100644 --- a/src/SongFilter.cxx +++ b/src/SongFilter.cxx @@ -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; diff --git a/src/SongSticker.cxx b/src/SongSticker.cxx index 589bc2a4a..141e286b5 100644 --- a/src/SongSticker.cxx +++ b/src/SongSticker.cxx @@ -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 { diff --git a/src/UpdateRemove.cxx b/src/UpdateRemove.cxx index d43ad92c7..6bccdafcc 100644 --- a/src/UpdateRemove.cxx +++ b/src/UpdateRemove.cxx @@ -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 */ -- cgit v1.2.3