forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
be1025c
commit a292d8c
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters