lib/icuConverter, ...: use FmtBuffer() and FmtErrno()

This commit is contained in:
Max Kellermann
2022-11-29 06:36:55 +01:00
parent 4b5c8d1f3e
commit 8ad0d919b1
6 changed files with 29 additions and 23 deletions

View File

@@ -21,6 +21,7 @@
#include "Error.hxx"
#include "Format.hxx"
#include "lib/fmt/AudioFormatFormatter.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "util/ByteOrder.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
@@ -198,16 +199,16 @@ SetupHw(snd_pcm_t *pcm,
audio_format.format, params);
if (err < 0)
throw Alsa::MakeError(err,
fmt::format("Failed to configure format {}",
audio_format.format).c_str());
FmtBuffer<256>("Failed to configure format {}",
audio_format.format));
unsigned int channels = audio_format.channels;
err = snd_pcm_hw_params_set_channels_near(pcm, hwparams,
&channels);
if (err < 0)
throw Alsa::MakeError(err,
fmt::format("Failed to configure {} channels",
audio_format.channels).c_str());
FmtBuffer<256>("Failed to configure {} channels",
audio_format.channels));
audio_format.channels = (int8_t)channels;
@@ -219,8 +220,8 @@ SetupHw(snd_pcm_t *pcm,
&output_sample_rate, nullptr);
if (err < 0)
throw Alsa::MakeError(err,
fmt::format("Failed to configure sample rate {} Hz",
requested_sample_rate).c_str());
FmtBuffer<256>("Failed to configure sample rate {} Hz",
requested_sample_rate));
if (output_sample_rate == 0)
throw FormatRuntimeError("Failed to configure sample rate %u Hz",

View File

@@ -19,6 +19,7 @@
#include "Converter.hxx"
#include "util/AllocatedString.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "config.h"
#include <fmt/format.h>
@@ -33,7 +34,7 @@
#include "util/AllocatedArray.hxx"
#include <unicode/ucnv.h>
#elif defined(HAVE_ICONV)
#include "system/Error.hxx"
#include "lib/fmt/SystemError.hxx"
#endif
#ifdef HAVE_ICU
@@ -54,8 +55,8 @@ IcuConverter::Create(const char *charset)
UErrorCode code = U_ZERO_ERROR;
UConverter *converter = ucnv_open(charset, &code);
if (converter == nullptr)
throw std::runtime_error(fmt::format(FMT_STRING("Failed to initialize charset '{}': {}"),
charset, u_errorName(code)));
throw std::runtime_error(FmtBuffer<256>(FMT_STRING("Failed to initialize charset '{}': {}"),
charset, u_errorName(code)));
return std::unique_ptr<IcuConverter>(new IcuConverter(converter));
#elif defined(HAVE_ICONV)
@@ -67,8 +68,8 @@ IcuConverter::Create(const char *charset)
iconv_close(to);
if (from != (iconv_t)-1)
iconv_close(from);
throw MakeErrno(e, fmt::format(FMT_STRING("Failed to initialize charset '{}'"),
charset).c_str());
throw FmtErrno(e, FMT_STRING("Failed to initialize charset '{}'"),
charset);
}
return std::unique_ptr<IcuConverter>(new IcuConverter(to, from));
@@ -119,8 +120,8 @@ IcuConverter::ToUTF8(std::string_view s) const
&source, source + s.size(),
nullptr, true, &code);
if (code != U_ZERO_ERROR)
throw std::runtime_error(fmt::format(FMT_STRING("Failed to convert to Unicode: {}"),
u_errorName(code)));
throw std::runtime_error(FmtBuffer<256>(FMT_STRING("Failed to convert to Unicode: {}"),
u_errorName(code)));
const size_t target_length = target - buffer;
return UCharToUTF8({buffer, target_length});
@@ -149,8 +150,8 @@ IcuConverter::FromUTF8(std::string_view s) const
nullptr, true, &code);
if (code != U_ZERO_ERROR)
throw std::runtime_error(fmt::format(FMT_STRING("Failed to convert from Unicode: {}"),
u_errorName(code)));
throw std::runtime_error(FmtBuffer<256>(FMT_STRING("Failed to convert from Unicode: {}"),
u_errorName(code)));
return {{buffer, size_t(target - buffer)}};