From b52b0ac85a1dc079d09afa9542007ab358d5d3d7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 13 Jul 2022 12:54:58 +0200 Subject: [PATCH] *: use BufferedOutputStream::Fmt() --- src/PlaylistDatabase.cxx | 6 ++--- src/PlaylistSave.cxx | 4 +-- src/SongSave.cxx | 25 +++++++++--------- src/TagSave.cxx | 7 ++--- src/db/plugins/simple/DatabaseSave.cxx | 13 ++++----- src/db/plugins/simple/DirectorySave.cxx | 12 ++++----- src/mixer/Volume.cxx | 2 +- src/output/State.cxx | 4 +-- src/queue/PlaylistState.cxx | 35 ++++++++++++++----------- src/queue/Save.cxx | 4 +-- src/storage/StorageState.cxx | 10 +++---- 11 files changed, 64 insertions(+), 58 deletions(-) diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index d2ad94509..e785f9256 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -33,10 +33,10 @@ void playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv) { for (const PlaylistInfo &pi : pv) { - os.Format(PLAYLIST_META_BEGIN "%s\n", pi.name.c_str()); + os.Fmt(FMT_STRING(PLAYLIST_META_BEGIN "{}\n"), pi.name); if (!IsNegative(pi.mtime)) - os.Format("mtime: %li\n", - (long)std::chrono::system_clock::to_time_t(pi.mtime)); + os.Fmt(FMT_STRING("mtime: {}\n"), + std::chrono::system_clock::to_time_t(pi.mtime)); os.Write("playlist_end\n"); } } diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 953ddf2aa..64bffd7d0 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -40,11 +40,11 @@ playlist_print_path(BufferedOutputStream &os, const Path path) "narrow" charset (i.e. CP_ACP) is incapable of storing all Unicode paths */ try { - os.Format("%s\n", path.ToUTF8Throw().c_str()); + os.Fmt(FMT_STRING("{}\n"), path.ToUTF8Throw()); } catch (...) { } #else - os.Format("%s\n", path.c_str()); + os.Fmt(FMT_STRING("{}\n"), path.c_str()); #endif } diff --git a/src/SongSave.cxx b/src/SongSave.cxx index 842c633b3..2c0eebf61 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -22,6 +22,7 @@ #include "db/plugins/simple/Song.hxx" #include "song/DetachedSong.hxx" #include "TagSave.hxx" +#include "lib/fmt/AudioFormatFormatter.hxx" #include "io/LineReader.hxx" #include "io/BufferedOutputStream.hxx" #include "tag/ParseName.hxx" @@ -43,45 +44,45 @@ static void range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms) { if (end_ms > 0) - os.Format("Range: %u-%u\n", start_ms, end_ms); + os.Fmt(FMT_STRING("Range: {}-{}\n"), start_ms, end_ms); else if (start_ms > 0) - os.Format("Range: %u-\n", start_ms); + os.Fmt(FMT_STRING("Range: {}-\n"), start_ms); } void song_save(BufferedOutputStream &os, const Song &song) { - os.Format(SONG_BEGIN "%s\n", song.filename.c_str()); + os.Fmt(FMT_STRING(SONG_BEGIN "{}\n"), song.filename); if (!song.target.empty()) - os.Format("Target: %s\n", song.target.c_str()); + os.Fmt(FMT_STRING("Target: {}\n"), song.target); range_save(os, song.start_time.ToMS(), song.end_time.ToMS()); tag_save(os, song.tag); if (song.audio_format.IsDefined()) - os.Format("Format: %s\n", ToString(song.audio_format).c_str()); + os.Fmt(FMT_STRING("Format: {}\n"), song.audio_format); if (!IsNegative(song.mtime)) - os.Format(SONG_MTIME ": %li\n", - (long)std::chrono::system_clock::to_time_t(song.mtime)); - os.Format(SONG_END "\n"); + os.Fmt(FMT_STRING(SONG_MTIME ": {}\n"), + std::chrono::system_clock::to_time_t(song.mtime)); + os.Write(SONG_END "\n"); } void song_save(BufferedOutputStream &os, const DetachedSong &song) { - os.Format(SONG_BEGIN "%s\n", song.GetURI()); + os.Fmt(FMT_STRING(SONG_BEGIN "{}\n"), song.GetURI()); range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS()); tag_save(os, song.GetTag()); if (!IsNegative(song.GetLastModified())) - os.Format(SONG_MTIME ": %li\n", - (long)std::chrono::system_clock::to_time_t(song.GetLastModified())); - os.Format(SONG_END "\n"); + os.Fmt(FMT_STRING(SONG_MTIME ": {}\n"), + std::chrono::system_clock::to_time_t(song.GetLastModified())); + os.Write(SONG_END "\n"); } DetachedSong diff --git a/src/TagSave.cxx b/src/TagSave.cxx index 3ecc4e69c..749e19a56 100644 --- a/src/TagSave.cxx +++ b/src/TagSave.cxx @@ -27,11 +27,12 @@ void tag_save(BufferedOutputStream &os, const Tag &tag) { if (!tag.duration.IsNegative()) - os.Format(SONG_TIME "%f\n", tag.duration.ToDoubleS()); + os.Fmt(FMT_STRING(SONG_TIME "{}\n"), tag.duration.ToDoubleS()); if (tag.has_playlist) - os.Format("Playlist: yes\n"); + os.Write("Playlist: yes\n"); for (const auto &i : tag) - os.Format("%s: %s\n", tag_item_names[i.type], i.value); + os.Fmt(FMT_STRING("{}: {}\n"), + tag_item_names[i.type], i.value); } diff --git a/src/db/plugins/simple/DatabaseSave.cxx b/src/db/plugins/simple/DatabaseSave.cxx index 4e9bb503e..0862b2dc6 100644 --- a/src/db/plugins/simple/DatabaseSave.cxx +++ b/src/db/plugins/simple/DatabaseSave.cxx @@ -49,16 +49,17 @@ static constexpr unsigned OLDEST_DB_FORMAT = 1; void db_save_internal(BufferedOutputStream &os, const Directory &music_root) { - os.Format("%s\n", DIRECTORY_INFO_BEGIN); - os.Format(DB_FORMAT_PREFIX "%u\n", DB_FORMAT); - os.Format("%s%s\n", DIRECTORY_MPD_VERSION, VERSION); - os.Format("%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset()); + os.Write(DIRECTORY_INFO_BEGIN "\n"); + os.Fmt(FMT_STRING(DB_FORMAT_PREFIX "{}\n"), DB_FORMAT); + os.Write(DIRECTORY_MPD_VERSION VERSION "\n"); + os.Fmt(FMT_STRING(DIRECTORY_FS_CHARSET "{}\n"), GetFSCharset()); for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) if (IsTagEnabled(i)) - os.Format(DB_TAG_PREFIX "%s\n", tag_item_names[i]); + os.Fmt(FMT_STRING(DB_TAG_PREFIX "{}\n"), + tag_item_names[i]); - os.Format("%s\n", DIRECTORY_INFO_END); + os.Write(DIRECTORY_INFO_END "\n"); directory_save(os, music_root); } diff --git a/src/db/plugins/simple/DirectorySave.cxx b/src/db/plugins/simple/DirectorySave.cxx index e7648ac11..553ae4add 100644 --- a/src/db/plugins/simple/DirectorySave.cxx +++ b/src/db/plugins/simple/DirectorySave.cxx @@ -78,20 +78,20 @@ directory_save(BufferedOutputStream &os, const Directory &directory) if (!directory.IsRoot()) { const char *type = DeviceToTypeString(directory.device); if (type != nullptr) - os.Format(DIRECTORY_TYPE "%s\n", type); + os.Fmt(FMT_STRING(DIRECTORY_TYPE "{}\n"), type); if (!IsNegative(directory.mtime)) - os.Format(DIRECTORY_MTIME "%lu\n", - (unsigned long)std::chrono::system_clock::to_time_t(directory.mtime)); + os.Fmt(FMT_STRING(DIRECTORY_MTIME "{}\n"), + std::chrono::system_clock::to_time_t(directory.mtime)); - os.Format("%s%s\n", DIRECTORY_BEGIN, directory.GetPath()); + os.Fmt(FMT_STRING(DIRECTORY_BEGIN "{}\n"), directory.GetPath()); } for (const auto &child : directory.children) { if (child.IsMount()) continue; - os.Format(DIRECTORY_DIR "%s\n", child.GetName()); + os.Fmt(FMT_STRING(DIRECTORY_DIR "{}\n"), child.GetName()); directory_save(os, child); } @@ -101,7 +101,7 @@ directory_save(BufferedOutputStream &os, const Directory &directory) playlist_vector_save(os, directory.playlists); if (!directory.IsRoot()) - os.Format(DIRECTORY_END "%s\n", directory.GetPath()); + os.Fmt(FMT_STRING(DIRECTORY_END "{}\n"), directory.GetPath()); } static bool diff --git a/src/mixer/Volume.cxx b/src/mixer/Volume.cxx index 4afb97e5d..bfefb7327 100644 --- a/src/mixer/Volume.cxx +++ b/src/mixer/Volume.cxx @@ -114,7 +114,7 @@ read_sw_volume_state(const char *line, MultipleOutputs &outputs) void save_sw_volume_state(BufferedOutputStream &os) { - os.Format(SW_VOLUME_STATE "%u\n", volume_software_set); + os.Fmt(FMT_STRING(SW_VOLUME_STATE "{}\n"), volume_software_set); } unsigned diff --git a/src/output/State.cxx b/src/output/State.cxx index adce37ac0..28a9e2b64 100644 --- a/src/output/State.cxx +++ b/src/output/State.cxx @@ -43,8 +43,8 @@ audio_output_state_save(BufferedOutputStream &os, const auto &ao = outputs.Get(i); const std::scoped_lock lock(ao.mutex); - os.Format(AUDIO_DEVICE_STATE "%d:%s\n", - ao.IsEnabled(), ao.GetName()); + os.Fmt(FMT_STRING(AUDIO_DEVICE_STATE "{}:{}\n"), + (unsigned)ao.IsEnabled(), ao.GetName()); } } diff --git a/src/queue/PlaylistState.cxx b/src/queue/PlaylistState.cxx index 35569db4d..317cb6c45 100644 --- a/src/queue/PlaylistState.cxx +++ b/src/queue/PlaylistState.cxx @@ -73,29 +73,32 @@ playlist_state_save(BufferedOutputStream &os, const struct playlist &playlist, default: os.Write(PLAYLIST_STATE_FILE_STATE_PLAY "\n"); } - os.Format(PLAYLIST_STATE_FILE_CURRENT "%i\n", - playlist.queue.OrderToPosition(playlist.current)); - os.Format(PLAYLIST_STATE_FILE_TIME "%f\n", - player_status.elapsed_time.ToDoubleS()); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_CURRENT "{}\n"), + playlist.queue.OrderToPosition(playlist.current)); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_TIME "{}\n"), + player_status.elapsed_time.ToDoubleS()); } else { os.Write(PLAYLIST_STATE_FILE_STATE_STOP "\n"); if (playlist.current >= 0) - os.Format(PLAYLIST_STATE_FILE_CURRENT "%i\n", - playlist.queue.OrderToPosition(playlist.current)); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_CURRENT "{}\n"), + playlist.queue.OrderToPosition(playlist.current)); } - os.Format(PLAYLIST_STATE_FILE_RANDOM "%i\n", playlist.queue.random); - os.Format(PLAYLIST_STATE_FILE_REPEAT "%i\n", playlist.queue.repeat); - os.Format(PLAYLIST_STATE_FILE_SINGLE "%i\n", + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_RANDOM "{}\n"), + (unsigned)playlist.queue.random); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_REPEAT "{}\n"), + (unsigned)playlist.queue.repeat); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_SINGLE "{}\n"), (int)playlist.queue.single); - os.Format(PLAYLIST_STATE_FILE_CONSUME "%i\n", playlist.queue.consume); - os.Format(PLAYLIST_STATE_FILE_CROSSFADE "%i\n", - (int)pc.GetCrossFade().count()); - os.Format(PLAYLIST_STATE_FILE_MIXRAMPDB "%f\n", - (double)pc.GetMixRampDb()); - os.Format(PLAYLIST_STATE_FILE_MIXRAMPDELAY "%f\n", - pc.GetMixRampDelay().count()); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_CONSUME "{}\n"), + (unsigned)playlist.queue.consume); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_CROSSFADE "{}\n"), + pc.GetCrossFade().count()); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_MIXRAMPDB "{}\n"), + pc.GetMixRampDb()); + os.Fmt(FMT_STRING(PLAYLIST_STATE_FILE_MIXRAMPDELAY "{}\n"), + pc.GetMixRampDelay().count()); os.Write(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "\n"); queue_save(os, playlist.queue); os.Write(PLAYLIST_STATE_FILE_PLAYLIST_END "\n"); diff --git a/src/queue/Save.cxx b/src/queue/Save.cxx index 07b0458e0..6020e950e 100644 --- a/src/queue/Save.cxx +++ b/src/queue/Save.cxx @@ -38,7 +38,7 @@ static void queue_save_database_song(BufferedOutputStream &os, int idx, const DetachedSong &song) { - os.Format("%i:%s\n", idx, song.GetURI()); + os.Fmt(FMT_STRING("{}:{}\n"), idx, song.GetURI()); } static void @@ -67,7 +67,7 @@ queue_save(BufferedOutputStream &os, const Queue &queue) for (unsigned i = 0; i < queue.GetLength(); i++) { uint8_t prio = queue.GetPriorityAtPosition(i); if (prio != 0) - os.Format(PRIO_LABEL "%u\n", prio); + os.Fmt(FMT_STRING(PRIO_LABEL "{}\n"), prio); queue_save_song(os, i, queue.Get(i)); } diff --git a/src/storage/StorageState.cxx b/src/storage/StorageState.cxx index e15f2f9d8..130a4aeb1 100644 --- a/src/storage/StorageState.cxx +++ b/src/storage/StorageState.cxx @@ -62,11 +62,11 @@ storage_state_save(BufferedOutputStream &os, const Instance &instance) if (uri.empty() || StringIsEmpty(mount_uri)) return; - os.Format( - MOUNT_STATE_BEGIN "\n" - MOUNT_STATE_STORAGE_URI "%s\n" - MOUNT_STATE_MOUNTED_URL "%s\n" - MOUNT_STATE_END "\n", mount_uri, uri.c_str()); + os.Fmt(FMT_STRING(MOUNT_STATE_BEGIN "\n" + MOUNT_STATE_STORAGE_URI "{}\n" + MOUNT_STATE_MOUNTED_URL "{}\n" + MOUNT_STATE_END "\n"), + mount_uri, uri); }; ((CompositeStorage*)instance.storage)->VisitMounts(visitor);