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
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.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 {
|
|
|
|
public:
|
|
|
|
using OutputType = KBuffer;
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
explicit KBufferBuilder(bool can_expand = false);
|
|
|
|
explicit KBufferBuilder(RefPtr<KBufferImpl>&, bool can_expand = false);
|
2020-12-18 13:10:10 +00:00
|
|
|
KBufferBuilder(KBufferBuilder&&) = default;
|
2020-09-18 07:49:51 +00:00
|
|
|
~KBufferBuilder() { }
|
2019-08-06 18:04:12 +00:00
|
|
|
|
|
|
|
void append(const StringView&);
|
|
|
|
void append(char);
|
|
|
|
void append(const char*, int);
|
|
|
|
|
2020-11-02 11:56:36 +00:00
|
|
|
void append_escaped_for_json(const StringView&);
|
2020-09-17 19:51:09 +00:00
|
|
|
void append_bytes(ReadonlyBytes);
|
2020-11-02 11:56:36 +00:00
|
|
|
|
2020-10-07 11:59:26 +00:00
|
|
|
template<typename... Parameters>
|
2021-02-23 06:28:15 +00:00
|
|
|
void appendff(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&... parameters)
|
2020-10-07 11:59:26 +00:00
|
|
|
{
|
|
|
|
// FIXME: This is really not the way to go about it, but vformat expects a
|
|
|
|
// StringBuilder. Why does this class exist anyways?
|
2021-02-23 06:28:15 +00:00
|
|
|
append(String::formatted(fmtstr.view(), parameters...));
|
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
|
|
|
|
|
|
|
private:
|
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
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
RefPtr<KBufferImpl> m_buffer;
|
2019-08-06 18:04:12 +00:00
|
|
|
size_t m_size { 0 };
|
2020-09-17 19:51:09 +00:00
|
|
|
bool m_can_expand { false };
|
2019-08-06 18:04:12 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|