1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-30 02:47:35 +00:00

GIODevice: Add a read_all() that returns a ByteBuffer with all we can read.

Use this to implement file opening in TextEditor.
This commit is contained in:
Andreas Kling 2019-03-18 14:38:30 +01:00
parent 8e3d0a23d5
commit 9ad076178a
6 changed files with 58 additions and 25 deletions

View file

@ -100,6 +100,7 @@ public:
}
ByteBuffer to_byte_buffer() const;
static String from_byte_buffer(const ByteBuffer&);
static String format(const char*, ...);

View file

@ -38,6 +38,7 @@ public:
byte* offset_pointer(int offset) { return m_data + offset; }
const byte* offset_pointer(int offset) const { return m_data + offset; }
void* end_pointer() { return m_data + m_size; }
const void* end_pointer() const { return m_data + m_size; }
// NOTE: trim() does not reallocate.
@ -109,6 +110,7 @@ public:
byte* offset_pointer(ssize_t offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const byte* offset_pointer(ssize_t offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
void* end_pointer() { return m_impl ? m_impl->end_pointer() : nullptr; }
const void* end_pointer() const { return m_impl ? m_impl->end_pointer() : nullptr; }
// NOTE: trim() does not reallocate.
@ -137,6 +139,13 @@ public:
m_impl->grow(size);
}
void append(const void* data, int data_size)
{
int old_size = size();
grow(size() + data_size);
memcpy(pointer() + old_size, data, data_size);
}
private:
explicit ByteBuffer(RetainPtr<ByteBufferImpl>&& impl)
: m_impl(move(impl))

View file

@ -92,6 +92,15 @@ ByteBuffer String::to_byte_buffer() const
return ByteBuffer::copy(reinterpret_cast<const byte*>(characters()), length());
}
String String::from_byte_buffer(const ByteBuffer& buffer)
{
if (buffer.is_null())
return nullptr;
if (buffer.is_empty())
return empty();
return String((const char*)buffer.pointer(), buffer.size());
}
unsigned String::to_uint(bool& ok) const
{
unsigned value = 0;