2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-02-09 15:08:43 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-01-11 20:44:29 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-09-07 13:54:23 +00:00
|
|
|
#include <AK/StringView.h>
|
2019-08-06 18:04:12 +00:00
|
|
|
#include <Kernel/KBuffer.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
class KBufferBuilder {
|
2021-09-07 13:54:23 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(KBufferBuilder);
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
public:
|
|
|
|
using OutputType = KBuffer;
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
static ErrorOr<KBufferBuilder> try_create();
|
2021-09-07 13:54:23 +00:00
|
|
|
|
2020-12-18 13:10:10 +00:00
|
|
|
KBufferBuilder(KBufferBuilder&&) = default;
|
2021-09-07 13:54:23 +00:00
|
|
|
KBufferBuilder& operator=(KBufferBuilder&&) = default;
|
2021-02-28 13:42:08 +00:00
|
|
|
~KBufferBuilder() = default;
|
2019-08-06 18:04:12 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
ErrorOr<void> append(StringView);
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> append(char);
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> append(char const*, int);
|
2019-08-06 18:04:12 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
ErrorOr<void> append_escaped_for_json(StringView);
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> append_bytes(ReadonlyBytes);
|
2020-11-02 11:56:36 +00:00
|
|
|
|
2020-10-07 11:59:26 +00:00
|
|
|
template<typename... Parameters>
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
2020-10-07 11:59:26 +00:00
|
|
|
{
|
2021-07-20 11:58:38 +00:00
|
|
|
// FIXME: This really not ideal, but vformat expects StringBuilder.
|
2021-05-12 19:40:29 +00:00
|
|
|
StringBuilder builder;
|
2021-08-30 09:58:22 +00:00
|
|
|
AK::VariadicFormatParams variadic_format_params { parameters... };
|
2021-11-16 00:15:21 +00:00
|
|
|
TRY(vformat(builder, fmtstr.view(), variadic_format_params));
|
2021-09-06 16:24:13 +00:00
|
|
|
return append_bytes(builder.string_view().bytes());
|
2020-10-07 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
bool flush();
|
2020-12-18 13:10:10 +00:00
|
|
|
OwnPtr<KBuffer> build();
|
2019-08-06 18:04:12 +00:00
|
|
|
|
2021-09-06 16:24:13 +00:00
|
|
|
ReadonlyBytes bytes() const
|
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
return {};
|
2021-09-08 16:29:52 +00:00
|
|
|
return m_buffer->bytes();
|
2021-09-06 16:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 15:21:31 +00:00
|
|
|
size_t length() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
private:
|
2021-09-07 13:54:23 +00:00
|
|
|
explicit KBufferBuilder(NonnullOwnPtr<KBuffer>);
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
bool check_expand(size_t);
|
2020-12-18 13:10:10 +00:00
|
|
|
u8* insertion_ptr()
|
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
return nullptr;
|
|
|
|
return m_buffer->data() + m_size;
|
|
|
|
}
|
2019-08-06 18:04:12 +00:00
|
|
|
|
2021-09-07 13:44:45 +00:00
|
|
|
OwnPtr<KBuffer> m_buffer;
|
2019-08-06 18:04:12 +00:00
|
|
|
size_t m_size { 0 };
|
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|