diff --git a/src/io/BufferedOutputStream.cxx b/src/io/BufferedOutputStream.cxx index f30827259..9e7ae921f 100644 --- a/src/io/BufferedOutputStream.cxx +++ b/src/io/BufferedOutputStream.cxx @@ -118,6 +118,20 @@ BufferedOutputStream::Format(const char *fmt, ...) buffer.Append(size); } +void +BufferedOutputStream::VFmt(fmt::string_view format_str, fmt::format_args args) +{ + /* TODO format into this object's buffer instead of allocating + a new one */ + fmt::memory_buffer b; +#if FMT_VERSION >= 80000 + fmt::vformat_to(std::back_inserter(b), format_str, args); +#else + fmt::vformat_to(b, format_str, args); +#endif + return Write(b.data(), b.size()); +} + #ifdef _UNICODE void diff --git a/src/io/BufferedOutputStream.hxx b/src/io/BufferedOutputStream.hxx index 576aeedd7..cb3994584 100644 --- a/src/io/BufferedOutputStream.hxx +++ b/src/io/BufferedOutputStream.hxx @@ -33,6 +33,11 @@ #include "util/Compiler.h" #include "util/DynamicFifoBuffer.hxx" +#include +#if FMT_VERSION < 70000 || FMT_VERSION >= 80000 +#include +#endif + #include #ifdef _UNICODE @@ -92,6 +97,24 @@ public: gcc_printf(2,3) void Format(const char *fmt, ...); + void VFmt(fmt::string_view format_str, fmt::format_args args); + + template + void Fmt(const S &format_str, Args&&... args) { +#if FMT_VERSION >= 90000 + VFmt(format_str, + fmt::make_format_args(args...)); +#elif FMT_VERSION >= 70000 + VFmt(fmt::to_string_view(format_str), + fmt::make_args_checked(format_str, + args...)); +#else + /* expensive fallback for older libfmt versions */ + const auto result = fmt::format(format_str, args...); + Write(result.data(), result.size()); +#endif + } + #ifdef _UNICODE /** * Write one narrow character. diff --git a/src/io/meson.build b/src/io/meson.build index 3145bdd29..613f6b9dd 100644 --- a/src/io/meson.build +++ b/src/io/meson.build @@ -8,8 +8,14 @@ io = static_library( 'FileOutputStream.cxx', 'BufferedOutputStream.cxx', include_directories: inc, + dependencies: [ + fmt_dep, + ], ) io_dep = declare_dependency( link_with: io, + dependencies: [ + fmt_dep, + ], )