2018-10-24 10:43:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ByteBuffer.h"
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class BufferStream {
|
|
|
|
public:
|
|
|
|
explicit BufferStream(ByteBuffer& buffer)
|
|
|
|
: m_buffer(buffer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator<<(byte value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:13:12 +00:00
|
|
|
void operator<<(char value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = (byte)value;
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:43:52 +00:00
|
|
|
void operator<<(word value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator<<(dword value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (byte)(value >> 16) & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu;
|
|
|
|
}
|
|
|
|
|
2018-11-12 23:17:30 +00:00
|
|
|
void operator<<(const char* str)
|
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t len = strlen(str);
|
|
|
|
ASSERT(len >= 0);
|
|
|
|
for (ssize_t i = 0; i < len; ++i)
|
2018-11-12 23:17:30 +00:00
|
|
|
m_buffer[m_offset++] = str[i];
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:43:52 +00:00
|
|
|
void operator<<(const String& value)
|
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
for (ssize_t i = 0; i < value.length(); ++i)
|
2018-10-24 10:43:52 +00:00
|
|
|
m_buffer[m_offset++] = value[i];
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:13:12 +00:00
|
|
|
void operator<<(const ByteBuffer& value)
|
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
for (ssize_t i = 0; i < value.size(); ++i)
|
2019-02-02 22:13:12 +00:00
|
|
|
m_buffer[m_offset++] = value[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool at_end() const
|
|
|
|
{
|
|
|
|
return m_offset == m_buffer.size();
|
|
|
|
}
|
|
|
|
|
2019-01-23 01:45:25 +00:00
|
|
|
void fill_to_end(byte ch)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-02-02 22:13:12 +00:00
|
|
|
while (!at_end())
|
2018-10-24 10:43:52 +00:00
|
|
|
m_buffer[m_offset++] = ch;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t offset() const { return m_offset; }
|
2018-10-24 10:43:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ByteBuffer& m_buffer;
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t m_offset { 0 };
|
2018-10-24 10:43:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::BufferStream;
|