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