Skip to content

Commit 43df70b

Browse files
committed
File: Add method to read into a fixed buffer until it's full or EOF happens
1 parent a9806d7 commit 43df70b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Libraries/File/File.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,21 @@ SC::Result SC::FileDescriptor::write(Span<const uint8_t> data)
590590
return write({reinterpret_cast<const char*>(data.data()), data.sizeInBytes()});
591591
}
592592

593+
SC::Result SC::FileDescriptor::readUntilFullOrEOF(Span<char> data, Span<char>& actuallyRead)
594+
{
595+
auto availableData = data;
596+
while (not availableData.empty())
597+
{
598+
Span<char> readData;
599+
SC_TRY(read(availableData, readData));
600+
if (readData.empty())
601+
break;
602+
availableData = {availableData.data(), availableData.sizeInBytes() - readData.sizeInBytes()};
603+
}
604+
actuallyRead = {data.data(), data.sizeInBytes() - availableData.sizeInBytes()};
605+
return Result(true);
606+
}
607+
593608
SC::Result SC::FileDescriptor::readUntilEOF(IGrowableBuffer&& adapter)
594609
{
595610
char buffer[1024];

Libraries/File/File.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescr
123123
/// @return Valid result if read succeeded
124124
Result read(Span<char> data, Span<char>& actuallyRead);
125125

126+
/// @brief Reads bytes from current position (FileDescriptor::seek) into Span, until full or EOF is reached
127+
/// @param data Span of bytes where data should be written to
128+
/// @param actuallyRead A sub-span of data of the actually read bytes. A zero sized span means EOF.
129+
/// @return Valid result if read succeeded
130+
Result readUntilFullOrEOF(Span<char> data, Span<char>& actuallyRead);
131+
126132
/// @brief Reads bytes from current position (FileDescriptor::seek) into user supplied Span
127133
/// @param data Span of bytes where data should be written to
128134
/// @param actuallyRead A sub-span of data of the actually read bytes. A zero sized span means EOF.

0 commit comments

Comments
 (0)