2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2014-07-30 19:10:28 +02:00
|
|
|
|
|
|
|
#include "BufferedOutputStream.hxx"
|
|
|
|
#include "OutputStream.hxx"
|
|
|
|
|
2022-07-13 14:06:28 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-03-13 00:31:17 +01:00
|
|
|
#include <cstdarg>
|
|
|
|
|
2014-07-30 19:10:28 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-04 12:05:37 +02:00
|
|
|
#ifdef _UNICODE
|
|
|
|
#include "system/Error.hxx"
|
2021-05-19 17:24:02 +02:00
|
|
|
#include <stringapiset.h>
|
2016-09-04 12:05:37 +02:00
|
|
|
#endif
|
|
|
|
|
2014-07-30 19:10:28 +02:00
|
|
|
bool
|
2021-07-29 17:14:03 +02:00
|
|
|
BufferedOutputStream::AppendToBuffer(const void *data, std::size_t size) noexcept
|
2014-07-30 19:10:28 +02:00
|
|
|
{
|
|
|
|
auto r = buffer.Write();
|
2022-05-10 17:26:41 +02:00
|
|
|
if (r.size() < size)
|
2014-07-30 19:10:28 +02:00
|
|
|
return false;
|
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
memcpy(r.data(), data, size);
|
2014-07-30 19:10:28 +02:00
|
|
|
buffer.Append(size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-16 10:24:43 +01:00
|
|
|
void
|
2021-07-29 17:14:03 +02:00
|
|
|
BufferedOutputStream::Write(const void *data, std::size_t size)
|
2014-07-30 19:10:28 +02:00
|
|
|
{
|
2015-03-22 17:01:33 +01:00
|
|
|
/* try to append to the current buffer */
|
2014-07-30 19:10:28 +02:00
|
|
|
if (AppendToBuffer(data, size))
|
2015-12-16 10:24:43 +01:00
|
|
|
return;
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2015-03-22 17:01:33 +01:00
|
|
|
/* not enough room in the buffer - flush it */
|
2015-12-16 10:24:43 +01:00
|
|
|
Flush();
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2015-03-22 17:01:33 +01:00
|
|
|
/* see if there's now enough room */
|
2014-07-30 19:10:28 +02:00
|
|
|
if (AppendToBuffer(data, size))
|
2015-12-16 10:24:43 +01:00
|
|
|
return;
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2015-03-22 17:01:33 +01:00
|
|
|
/* too large for the buffer: direct write */
|
2015-12-16 10:24:43 +01:00
|
|
|
os.Write(data, size);
|
2014-07-30 19:10:28 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 12:49:16 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:05:37 +02:00
|
|
|
#ifdef _UNICODE
|
|
|
|
|
|
|
|
void
|
2023-05-15 11:11:41 +02:00
|
|
|
BufferedOutputStream::WriteWideToUTF8(std::wstring_view src)
|
2016-09-04 12:05:37 +02:00
|
|
|
{
|
2023-05-15 11:11:41 +02:00
|
|
|
if (src.empty())
|
2016-09-04 12:05:37 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto r = buffer.Write();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (r.empty()) {
|
2016-09-04 12:05:37 +02:00
|
|
|
Flush();
|
|
|
|
r = buffer.Write();
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:11:41 +02:00
|
|
|
int length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(),
|
2022-05-10 17:26:41 +02:00
|
|
|
(char *)r.data(), r.size(),
|
2021-01-26 22:27:18 +01:00
|
|
|
nullptr, nullptr);
|
2016-09-04 12:05:37 +02:00
|
|
|
if (length <= 0) {
|
|
|
|
const auto error = GetLastError();
|
|
|
|
if (error != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
throw MakeLastError(error, "UTF-8 conversion failed");
|
|
|
|
|
|
|
|
/* how much buffer do we need? */
|
2023-05-15 11:11:41 +02:00
|
|
|
length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(),
|
2016-09-04 12:05:37 +02:00
|
|
|
nullptr, 0, nullptr, nullptr);
|
|
|
|
if (length <= 0)
|
|
|
|
throw MakeLastError(error, "UTF-8 conversion failed");
|
|
|
|
|
|
|
|
/* grow the buffer and try again */
|
2023-05-15 11:11:41 +02:00
|
|
|
length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(),
|
2021-12-07 12:07:08 +01:00
|
|
|
(char *)buffer.Write(length), length,
|
2016-09-04 12:05:37 +02:00
|
|
|
nullptr, nullptr);
|
|
|
|
if (length <= 0)
|
|
|
|
throw MakeLastError(error, "UTF-8 conversion failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Append(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-12-16 10:24:43 +01:00
|
|
|
void
|
2014-07-30 19:10:28 +02:00
|
|
|
BufferedOutputStream::Flush()
|
|
|
|
{
|
|
|
|
auto r = buffer.Read();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (r.empty())
|
2015-12-16 10:24:43 +01:00
|
|
|
return;
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
os.Write(r.data(), r.size());
|
|
|
|
buffer.Consume(r.size());
|
2014-07-30 19:10:28 +02:00
|
|
|
}
|