Skip to content

Commit

Permalink
use more libfmt instead of sprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Mar 6, 2023
1 parent 02d1087 commit 415de49
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 115 deletions.
6 changes: 3 additions & 3 deletions src/db/update/Playlist.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#include "storage/FileInfo.hxx"
#include "storage/StorageInterface.hxx"
#include "fs/Traits.hxx"
#include "util/StringFormat.hxx"
#include "Log.hxx"

#include <fmt/core.h>

inline void
UpdateWalk::UpdatePlaylistFile(Directory &directory,
SongEnumerator &contents) noexcept
Expand All @@ -41,8 +42,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &directory,
the virtual directory (DEVICE_PLAYLIST) to
the containing directory */
: "../" + db_song->filename;
db_song->filename = StringFormat<64>("track%04u",
++track);
db_song->filename = fmt::format("track{:04}", ++track);

{
const ScopeDatabaseLock protect;
Expand Down
6 changes: 3 additions & 3 deletions src/decoder/Thread.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DecoderUriDecode(const DecoderPlugin &plugin,
{
const ScopeUnlock unlock(bridge.dc.mutex);

FormatThreadName("decoder:%s", plugin.name);
FmtThreadName("decoder:{}", plugin.name);

plugin.UriDecode(bridge, uri);

Expand Down Expand Up @@ -98,7 +98,7 @@ decoder_stream_decode(const DecoderPlugin &plugin,
{
const ScopeUnlock unlock(bridge.dc.mutex);

FormatThreadName("decoder:%s", plugin.name);
FmtThreadName("decoder:{}", plugin.name);

plugin.StreamDecode(bridge, input_stream);

Expand Down Expand Up @@ -135,7 +135,7 @@ decoder_file_decode(const DecoderPlugin &plugin,
{
const ScopeUnlock unlock(bridge.dc.mutex);

FormatThreadName("decoder:%s", plugin.name);
FmtThreadName("decoder:{}", plugin.name);

plugin.FileDecode(bridge, path);

Expand Down
23 changes: 12 additions & 11 deletions src/decoder/plugins/GmeDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "fs/NarrowPath.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "util/ScopeExit.hxx"
#include "util/StringCompare.hxx"
#include "util/StringFormat.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"

#include <gme/gme.h>

#include <fmt/format.h>

#include <cassert>

#include <stdlib.h>
Expand Down Expand Up @@ -228,16 +230,16 @@ ScanGmeInfo(const gme_info_t &info, unsigned song_num, int track_count,
));

if (track_count > 1)
handler.OnTag(TAG_TRACK, StringFormat<16>("%u", song_num + 1).c_str());
handler.OnTag(TAG_TRACK, fmt::format_int{song_num + 1}.c_str());

if (!StringIsEmpty(info.song)) {
if (track_count > 1) {
/* start numbering subtunes from 1 */
const auto tag_title =
StringFormat<1024>("%s (%u/%d)",
info.song, song_num + 1,
track_count);
handler.OnTag(TAG_TITLE, tag_title.c_str());
fmt::format("{} ({}/{})",
info.song, song_num + 1,
track_count);
handler.OnTag(TAG_TITLE, tag_title);
} else
handler.OnTag(TAG_TITLE, info.song);
}
Expand Down Expand Up @@ -306,7 +308,7 @@ gme_container_scan(Path path_fs)
if (num_songs < 2)
return list;

const auto *subtune_suffix = path_fs.GetExtension();
const Path subtune_suffix = Path::FromFS(path_fs.GetExtension());

TagBuilder tag_builder;

Expand All @@ -315,10 +317,9 @@ gme_container_scan(Path path_fs)
AddTagHandler h(tag_builder);
ScanMusicEmu(emu, i, h);

const auto track_name =
StringFormat<64>(SUBTUNE_PREFIX "%03u.%s", i+1,
subtune_suffix);
tail = list.emplace_after(tail, track_name,
auto track_name = fmt::format(SUBTUNE_PREFIX "{:03}.{}",
i + 1, subtune_suffix);
tail = list.emplace_after(tail, std::move(track_name),
tag_builder.Commit());
}

Expand Down
11 changes: 6 additions & 5 deletions src/decoder/plugins/SidplayDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#ifdef HAVE_SIDPLAYFP
#include "io/FileReader.hxx"
#endif
#include "util/StringFormat.hxx"
#include "util/Domain.hxx"
#include "util/AllocatedString.hxx"
#include "util/CharUtil.hxx"
Expand All @@ -38,6 +37,8 @@
#include <sidplay/utils/SidDatabase.h>
#endif

#include <fmt/format.h>

#include <iterator>
#include <memory>

Expand Down Expand Up @@ -510,8 +511,8 @@ ScanSidTuneInfo(const SidTuneInfo &info, unsigned track, unsigned n_tracks,

if (n_tracks > 1) {
const auto tag_title =
StringFormat<1024>("%s (%u/%u)",
album.c_str(), track, n_tracks);
fmt::format("{} ({}/{})",
album.c_str(), track, n_tracks);
handler.OnTag(TAG_TITLE, tag_title.c_str());
} else
handler.OnTag(TAG_TITLE, album.c_str());
Expand All @@ -532,7 +533,7 @@ ScanSidTuneInfo(const SidTuneInfo &info, unsigned track, unsigned n_tracks,
handler.OnTag(TAG_DATE, date.c_str());

/* track */
handler.OnTag(TAG_TRACK, StringFormat<16>("%u", track).c_str());
handler.OnTag(TAG_TRACK, fmt::format_int{track}.c_str());
}

static bool
Expand Down Expand Up @@ -611,7 +612,7 @@ sidplay_container_scan(Path path_fs)
/* Construct container/tune path names, eg.
Delta.sid/tune_001.sid */
tail = list.emplace_after(tail,
StringFormat<32>(SUBTUNE_PREFIX "%03u.sid", i),
fmt::format(SUBTUNE_PREFIX "{:03}.sid", i),
tag_builder.Commit());
}

Expand Down
11 changes: 5 additions & 6 deletions src/decoder/plugins/WildmidiDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#include "../DecoderAPI.hxx"
#include "tag/Handler.hxx"
#include "util/ScopeExit.hxx"
#include "util/StringFormat.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "fs/Path.hxx"
#include "fs/NarrowPath.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "PluginUnavailable.hxx"

#ifdef _WIN32
Expand All @@ -31,11 +32,9 @@ wildmidi_init(const ConfigBlock &block)
block.GetPath("config_file",
"/etc/timidity/timidity.cfg");

if (!FileExists(path)) {
const auto utf8 = path.ToUTF8();
throw PluginUnavailable(StringFormat<1024>("configuration file does not exist: %s",
utf8.c_str()));
}
if (!FileExists(path))
throw PluginUnavailable{FmtBuffer<1024>("configuration file does not exist: {}",
path)};

#ifdef LIBWILDMIDI_VERSION
/* WildMidi_ClearError() requires libwildmidi 0.4 */
Expand Down
2 changes: 1 addition & 1 deletion src/input/ThreadInputStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ThreadInputStream::Start()
inline void
ThreadInputStream::ThreadFunc() noexcept
{
FormatThreadName("input:%s", plugin);
FmtThreadName("input:{}", plugin);

std::unique_lock<Mutex> lock(mutex);

Expand Down
15 changes: 8 additions & 7 deletions src/input/plugins/CurlInputPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include "config/Block.hxx"
#include "tag/Builder.hxx"
#include "tag/Tag.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "event/Call.hxx"
#include "event/Loop.hxx"
#include "util/ASCII.hxx"
#include "util/StringFormat.hxx"
#include "util/NumberParser.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
Expand All @@ -33,6 +33,8 @@
#include "util/UriQueryParser.hxx"
#endif

#include <fmt/format.h>

#include <cassert>
#include <cinttypes>

Expand Down Expand Up @@ -261,8 +263,8 @@ CurlInputStream::OnHeaders(unsigned status,

if (status < 200 || status >= 300)
throw HttpStatusError(status,
StringFormat<40>("got HTTP status %u",
status).c_str());
FmtBuffer<40>("got HTTP status {}",
status).c_str());

const std::scoped_lock<Mutex> protect(mutex);

Expand Down Expand Up @@ -475,8 +477,8 @@ CurlInputStream::InitEasy()

if (proxy_user != nullptr && proxy_password != nullptr)
request->SetOption(CURLOPT_PROXYUSERPWD,
StringFormat<1024>("%s:%s", proxy_user,
proxy_password).c_str());
FmtBuffer<1024>("{}:{}", proxy_user,
proxy_password).c_str());

if (cacert != nullptr)
request->SetOption(CURLOPT_CAINFO, cacert);
Expand Down Expand Up @@ -532,8 +534,7 @@ CurlInputStream::SeekInternal(offset_type new_offset)

if (offset > 0)
request->SetOption(CURLOPT_RANGE,
StringFormat<40>("%" PRIoffset "-",
offset).c_str());
fmt::format_int{offset}.c_str());

StartRequest();
}
Expand Down
4 changes: 2 additions & 2 deletions src/io/FileOutputStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "FileOutputStream.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/SystemError.hxx"
#include "util/StringFormat.hxx"
#include "lib/fmt/ToBuffer.hxx"

#ifdef _WIN32
#include <tchar.h>
Expand Down Expand Up @@ -282,7 +282,7 @@ try {

/* hard-link the temporary file to the final path */
if (linkat(AT_FDCWD,
StringFormat<64>("/proc/self/fd/%d", fd.Get()),
FmtBuffer<64>("/proc/self/fd/{}", fd.Get()),
directory_fd.Get(), path.c_str(),
AT_SYMLINK_FOLLOW) < 0)
throw FmtErrno("Failed to commit {}", path);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/ffmpeg/LogCallback.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "LogCallback.hxx"
#include "Domain.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "util/Domain.hxx"
#include "util/StringFormat.hxx"
#include "Log.hxx"

extern "C" {
Expand Down Expand Up @@ -40,9 +40,9 @@ FfmpegLogCallback(void *ptr, int level, const char *fmt, std::va_list vl)

if (cls != nullptr) {
const auto domain =
StringFormat<64>("%s/%s",
ffmpeg_domain.GetName(),
cls->item_name(ptr));
FmtBuffer<64>("{}/{}",
ffmpeg_domain.GetName(),
cls->item_name(ptr));
const Domain d(domain);

char msg[1024];
Expand Down
10 changes: 5 additions & 5 deletions src/lib/nfs/Error.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// author: Max Kellermann <mk@cm4all.com>

#include "Error.hxx"
#include "util/StringFormat.hxx"
#include "lib/fmt/ToBuffer.hxx"

extern "C" {
#include <nfsc/libnfs.h>
Expand All @@ -13,20 +13,20 @@ extern "C" {

#include <string.h>

static StringBuffer<256>
static auto
FormatNfsClientError(struct nfs_context *nfs, const char *msg) noexcept
{
assert(msg != nullptr);

const char *msg2 = nfs_get_error(nfs);
return StringFormat<256>("%s: %s", msg, msg2);
return FmtBuffer<256>("{}: {}", msg, msg2);
}

NfsClientError::NfsClientError(struct nfs_context *nfs, const char *msg) noexcept
:std::runtime_error(FormatNfsClientError(nfs, msg).c_str()),
code(0) {}

static StringBuffer<256>
static auto
FormatNfsClientError(int err, struct nfs_context *nfs, void *data,
const char *msg) noexcept
{
Expand All @@ -40,7 +40,7 @@ FormatNfsClientError(int err, struct nfs_context *nfs, void *data,
msg2 = strerror(-err);
}

return StringFormat<256>("%s: %s", msg, msg2);
return FmtBuffer<256>("{}: {}", msg, msg2);
}

NfsClientError::NfsClientError(int err, struct nfs_context *nfs, void *data,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/sqlite/Database.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "Database.hxx"
#include "Error.hxx"
#include "util/StringFormat.hxx"
#include "lib/fmt/ToBuffer.hxx"

namespace Sqlite {

Expand All @@ -12,8 +12,8 @@ Database::Database(const char *path)
int result = sqlite3_open(path, &db);
if (result != SQLITE_OK)
throw SqliteError(db, result,
StringFormat<1024>("Failed to open sqlite database '%s'",
path));
FmtBuffer<1024>("Failed to open sqlite database '{}'",
path));
}

} // namespace Sqlite
5 changes: 3 additions & 2 deletions src/output/Init.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
#include "filter/plugins/VolumeFilterPlugin.hxx"
#include "filter/plugins/NormalizeFilterPlugin.hxx"
#include "util/StringAPI.hxx"
#include "util/StringFormat.hxx"
#include "Log.hxx"

#include <fmt/core.h>

#include <cassert>
#include <stdexcept>

Expand Down Expand Up @@ -153,7 +154,7 @@ FilteredAudioOutput::Configure(const ConfigBlock &block,
config_audio_format.Clear();
}

log_name = StringFormat<256>("\"%s\" (%s)", name, plugin_name);
log_name = fmt::format("\"{}\" ({})", name, plugin_name);

/* create the normalization filter (if configured) */

Expand Down
2 changes: 1 addition & 1 deletion src/output/Thread.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ AudioOutputControl::InternalDrain() noexcept
void
AudioOutputControl::Task() noexcept
{
FormatThreadName("output:%s", GetName().c_str());
FmtThreadName("output:{}", GetName());

try {
SetThreadRealtime();
Expand Down
Loading

0 comments on commit 415de49

Please sign in to comment.