mpd/src/util/FormatString.cxx

36 lines
713 B
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#include "FormatString.hxx"
#include "AllocatedString.hxx"
#include <stdio.h>
#include <stdlib.h>
AllocatedString
FormatStringV(const char *fmt, std::va_list args) noexcept
{
std::va_list tmp;
va_copy(tmp, args);
2020-02-01 13:49:19 +01:00
const int length = vsnprintf(nullptr, 0, fmt, tmp);
va_end(tmp);
if (length <= 0)
/* wtf.. */
abort();
char *buffer = new char[length + 1];
vsnprintf(buffer, length + 1, fmt, args);
return AllocatedString::Donate(buffer);
}
AllocatedString
2017-05-16 10:22:52 +02:00
FormatString(const char *fmt, ...) noexcept
{
std::va_list args;
va_start(args, fmt);
auto p = FormatStringV(fmt, args);
va_end(args);
return p;
}