From d93307d810b3f4ee8044f7308e360d9ea9c7cf22 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Fri, 3 Jan 2025 00:08:58 -0600 Subject: More fixups to csv logic. --- source/fud_csv.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'source/fud_csv.cpp') diff --git a/source/fud_csv.cpp b/source/fud_csv.cpp index bcef925..260a4ff 100644 --- a/source/fud_csv.cpp +++ b/source/fud_csv.cpp @@ -144,7 +144,10 @@ FudStatus Csv::parseFromUnbufferedFile(Csv& csv, RegularFile&& file, size_t maxE static_cast(csv); constexpr size_t BufferSize = 256; SimpleStackAllocator stackAllocator{}; - auto bufferedFile{BufferedRegularFile::make(std::move(file), TextBuffer{stackAllocator})}; + TextBuffer textBuffer{stackAllocator}; + auto resizeResult = textBuffer.resize(BufferSize); + fudAssert(resizeResult == FudStatus::Success); + auto bufferedFile{BufferedRegularFile::make(std::move(file), std::move(textBuffer))}; return parseFromBufferedFile(csv, bufferedFile, maxExtraAttempts); } @@ -222,6 +225,7 @@ DrainResult scanLine(const Csv& csv, File& file, size_t maxExtraAttempts, size_t bool sawQuote{false}; bool addToSize{}; size_t numColumns{0}; + size_t lineLength{0}; while (not endOfLine) { addToSize = false; @@ -239,7 +243,7 @@ DrainResult scanLine(const Csv& csv, File& file, size_t maxExtraAttempts, size_t } if (maybeNewline and utf8Char != Utf8{Ascii{lineEnding[1]}}) { - rawSize += 2; + lineLength += 2; maybeNewline = false; } else if (inQuote and utf8Char == csv.quoteCharacter) { inQuote = false; @@ -270,7 +274,7 @@ DrainResult scanLine(const Csv& csv, File& file, size_t maxExtraAttempts, size_t } if (addToSize and numColumns < csv.numColumns) { - rawSize += utf8Char.size(); + lineLength += utf8Char.size(); } if (numColumns > csv.numColumns and csv.strictColumns) { @@ -279,6 +283,11 @@ DrainResult scanLine(const Csv& csv, File& file, size_t maxExtraAttempts, size_t } } + rawSize += lineLength; + if (numColumns > 0 && lineLength == 0) { + numColumns = 0; + } + if (numColumns == 0) { readResult.status = FudStatus::Empty; return readResult; @@ -297,6 +306,11 @@ FudStatus fillBuffer(Csv& csv, File& file, size_t maxExtraAttempts, size_t rawSi { static_cast(rawSize); + auto flushResult = file.flush(); + if (flushResult.status != FudStatus::Success) { + return flushResult.status; + } + auto seekStatus = file.seekStart(); if (seekStatus != FudStatus::Success) { return seekStatus; -- cgit v1.2.3