mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 19:19:44 +03:00
LibCore: Add CFileStreamReader, a simple streaming CFile reader.
This is extremely barebones right now, but can be used to easily read binary data from a CFile piece by piece.
This commit is contained in:
parent
be1025c03f
commit
a292d8cd5a
Notes:
sideshowbarker
2024-07-19 13:01:41 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a292d8cd5a5
31
Libraries/LibCore/CFileStreamReader.h
Normal file
31
Libraries/LibCore/CFileStreamReader.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <LibCore/CFile.h>
|
||||
|
||||
class CFileStreamReader {
|
||||
public:
|
||||
CFileStreamReader(CFile& file)
|
||||
: m_file(file)
|
||||
{
|
||||
}
|
||||
|
||||
bool handle_read_failure()
|
||||
{
|
||||
return exchange(m_had_failure, false);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
CFileStreamReader& operator>>(T& value)
|
||||
{
|
||||
int nread = m_file.read((u8*)&value, sizeof(T));
|
||||
ASSERT(nread == sizeof(T));
|
||||
if (nread != sizeof(T))
|
||||
m_had_failure = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
CFile& m_file;
|
||||
bool m_had_failure { false };
|
||||
};
|
@ -22,6 +22,13 @@ const char* CIODevice::error_string() const
|
||||
return strerror(m_error);
|
||||
}
|
||||
|
||||
int CIODevice::read(u8* buffer, int length)
|
||||
{
|
||||
auto read_buffer = read(length);
|
||||
memcpy(buffer, read_buffer.data(), length);
|
||||
return read_buffer.size();
|
||||
}
|
||||
|
||||
ByteBuffer CIODevice::read(int max_size)
|
||||
{
|
||||
if (m_fd < 0)
|
||||
|
@ -28,6 +28,9 @@ public:
|
||||
|
||||
bool has_error() const { return m_error != 0; }
|
||||
|
||||
|
||||
int read(u8* buffer, int length);
|
||||
|
||||
ByteBuffer read(int max_size);
|
||||
ByteBuffer read_line(int max_size);
|
||||
ByteBuffer read_all();
|
||||
|
Loading…
Reference in New Issue
Block a user