Skip to content

Commit

Permalink
Add ReadAll, overloads for WriteAll
Browse files Browse the repository at this point in the history
Also, rename old ReadAll to ReadToEOF.
  • Loading branch information
AlexMax committed Dec 29, 2023
1 parent 603ada6 commit 2d2de63
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
67 changes: 63 additions & 4 deletions include/lexio/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,41 @@ inline size_t Seek(SEEKABLE &seekable, const ptrdiff_t offset, const Whence when
//
//******************************************************************************

template <typename READER, typename = std::enable_if_t<IsReaderV<READER>>>
inline void ReadAll(uint8_t *outDest, const size_t count, READER &reader)
{
size_t offset = 0, remain = count;
while (offset != count)
{
const size_t written = Read<READER>(outDest + offset, remain, reader);
offset += written;
remain -= written;
}
}

template <typename READER, size_t N, typename = std::enable_if_t<IsReaderV<READER>>>
inline void ReadAll(uint8_t (&outArray)[N], READER &reader)
{
size_t offset = 0, remain = N;
while (offset != N)
{
const size_t written = Read<READER>(&outArray[offset], remain, reader);
offset += written;
remain -= written;
}
}

template <typename READER, typename IT, typename = std::enable_if_t<IsReaderV<READER>>>
inline size_t ReadAll(IT outStart, IT outEnd, READER &reader)
{
IT iter = outStart;
while (iter != outEnd)
{
const size_t written = Read<READER>(outStart, outEnd, reader);
iter += written;
}
}

/**
* @brief Get the current contents of the buffer.
*
Expand All @@ -499,7 +534,7 @@ inline BufferView GetBuffer(BUFFERED_READER &bufReader)
* @return Total number of bytes read.
*/
template <typename BUFFERED_READER, typename OUT_ITER, typename = std::enable_if_t<IsBufferedReaderV<BUFFERED_READER>>>
inline size_t ReadAll(OUT_ITER outIt, BUFFERED_READER &bufReader, const size_t bufSize = 8192)
inline size_t ReadToEOF(OUT_ITER outIt, BUFFERED_READER &bufReader, const size_t bufSize = 8192)
{
size_t total = 0;
for (;;)
Expand Down Expand Up @@ -579,11 +614,35 @@ inline size_t ReadUntil(OUT_ITER outIt, BUFFERED_READER &bufReader, const uint8_
template <typename WRITER, typename = std::enable_if_t<IsWriterV<WRITER>>>
inline void WriteAll(WRITER &writer, const uint8_t *src, const size_t count)
{
size_t offset = 0;
while (offset < count)
size_t offset = 0, remain = count;
while (offset != count)
{
const size_t written = Write<WRITER>(writer, src + offset, remain);
offset += written;
remain -= written;
}
}

template <typename WRITER, size_t N, typename = std::enable_if_t<IsWriterV<WRITER>>>
inline void WriteAll(WRITER &writer, const uint8_t (&array)[N])
{
size_t offset = 0, remain = N;
while (offset != N)
{
const size_t written = Write<WRITER>(writer, src + offset, count);
const size_t written = Write<WRITER>(writer, array[offset], remain);
offset += written;
remain -= written;
}
}

template <typename WRITER, typename IT, typename = std::enable_if_t<IsWriterV<WRITER>>>
inline void WriteAll(WRITER &writer, IT start, IT end)
{
IT iter = start;
while (iter != end)
{
const size_t written = Write<WRITER>(writer, iter, end);
iter += written;
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,25 @@ TEST(Core, ReadIterator)
EXPECT_EQ(LexIO::Read(&buffer[0], &buffer[5], basic), 5);
}

TEST(Core, ReadAll)
TEST(Core, ReadToEOF)
{
LexIO::VectorStream basic = GetStream();
auto buffer = VectorBufReader(std::move(basic));

std::vector<uint8_t> data;
const size_t bytes = LexIO::ReadAll(std::back_inserter(data), buffer);
const size_t bytes = LexIO::ReadToEOF(std::back_inserter(data), buffer);
EXPECT_EQ(bytes, 45);
EXPECT_EQ(data.size(), 45);
EXPECT_EQ(*(data.end() - 1), '\n');
}

TEST(Core, ReadAllSmallBuffer)
TEST(Core, ReadToEOFSmallBuffer)
{
LexIO::VectorStream basic = GetStream();
auto buffer = VectorBufReader(std::move(basic));

std::vector<uint8_t> data;
const size_t bytes = LexIO::ReadAll(std::back_inserter(data), buffer, 4);
const size_t bytes = LexIO::ReadToEOF(std::back_inserter(data), buffer, 4);
EXPECT_EQ(data[4], 'q'); // Check the buffer boundary.
EXPECT_EQ(data[8], 'k');
EXPECT_EQ(bytes, 45);
Expand Down

0 comments on commit 2d2de63

Please sign in to comment.