From 16379362c02a2472f00fac49cad62788547c9519 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 1 Jan 2025 17:41:17 -0600 Subject: Add CSV parsing, printing, fix buffered file reading. --- source/fud_file.cpp | 76 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 21 deletions(-) (limited to 'source/fud_file.cpp') diff --git a/source/fud_file.cpp b/source/fud_file.cpp index 55f8dbc..caf0f5a 100644 --- a/source/fud_file.cpp +++ b/source/fud_file.cpp @@ -17,6 +17,8 @@ #include "fud_file.hpp" +#include "fud_algorithm.hpp" + #include #include #include @@ -625,6 +627,7 @@ DrainResult BufferedRegularFile::write(const std::byte* source, size_t length, O DrainResult BufferedRegularFile::read(std::byte* sink, size_t length, Option maxExtraAttempts) { + auto extraAttempts = maxExtraAttempts.valueOr(0); DrainResult result{0, FudStatus::Success}; if (sink == nullptr) { @@ -637,15 +640,6 @@ DrainResult BufferedRegularFile::read(std::byte* sink, size_t length, Option 0) { result.status = FudStatus::OperationInvalid; return result; @@ -653,13 +647,18 @@ DrainResult BufferedRegularFile::read(std::byte* sink, size_t length, Option 0 && m_bufferPosition < m_bufferLength) { - auto count = m_bufferLength - m_bufferPosition; - if (count > length) { - count = length; - } + auto remainingLength = m_bufferLength - m_bufferPosition; + auto count = min(length, remainingLength); + auto copyStatus = copyMem(sink, length, m_buffer.data() + m_bufferPosition, count); fudAssert(copyStatus == FudStatus::Success); @@ -677,23 +676,52 @@ DrainResult BufferedRegularFile::read(std::byte* sink, size_t length, Option m_buffer.size()) { - auto drainResult = m_file.read(sink, length, maxExtraAttempts.valueOr(0)); + auto drainResult = m_file.read(sink, length, extraAttempts); + + sink += drainResult.bytesDrained; + length -= drainResult.bytesDrained; + result.status = drainResult.status; result.bytesDrained += drainResult.bytesDrained; + return result; } - if (length == 0 || result.status != FudStatus::Success) { + fudAssert(length > 0); + + if (result.status != FudStatus::Success) { return result; } - fudAssert(m_bufferLength == 0 && m_bufferPosition == 0); - auto drainResult = m_file.read(m_buffer.data(), m_buffer.size(), maxExtraAttempts.valueOr(0)); - if (drainResult.status != FudStatus::Success && drainResult.status != FudStatus::Partial) { - result.status = drainResult.status; - } else { - result.status = FudStatus::Success; + fudAssert(m_bufferLength == 0); + fudAssert(m_bufferPosition == 0); + + auto drainResult = m_file.read(m_buffer.data(), m_buffer.size(), extraAttempts); + result.status = drainResult.status; + if (drainResult.status == FudStatus::Success || drainResult.status == FudStatus::Partial) { m_bufferLength = drainResult.bytesDrained; + + auto count = min(length, m_bufferLength); + + auto copyStatus = copyMem(sink, count, m_buffer.data(), count); + fudAssert(copyStatus == FudStatus::Success); + + sink += count; + length -= count; + + if (drainResult.status == FudStatus::Partial && length == 0) { + drainResult.status = FudStatus::Success; + } + + m_bufferPosition = count; + result.bytesDrained += count; } return result; @@ -713,6 +741,12 @@ DrainResult BufferedRegularFile::flush(size_t maxExtraAttempts) return {0, FudStatus::Success}; } + if (m_lastOperation != Operation::Write) { + m_bufferLength = 0; + m_bufferPosition = 0; + return {0, FudStatus::Success}; + } + auto drainResult = m_file.write(m_buffer.data(), m_bufferLength, maxExtraAttempts); if (drainResult.status == FudStatus::Success) { m_bufferLength = 0; -- cgit v1.2.3