diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2025-01-03 00:08:58 -0600 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2025-01-03 00:08:58 -0600 |
commit | d93307d810b3f4ee8044f7308e360d9ea9c7cf22 (patch) | |
tree | 08cfe9ae27c2a9d5bd11881089b9a3333a3c11b8 /source/fud_csv.cpp | |
parent | 1ac94c8aff47b549f30b370be2191bcc0157826c (diff) |
More fixups to csv logic.
Diffstat (limited to 'source/fud_csv.cpp')
-rw-r--r-- | source/fud_csv.cpp | 20 |
1 files changed, 17 insertions, 3 deletions
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<void>(csv); constexpr size_t BufferSize = 256; SimpleStackAllocator<BufferSize> 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<void>(rawSize); + auto flushResult = file.flush(); + if (flushResult.status != FudStatus::Success) { + return flushResult.status; + } + auto seekStatus = file.seekStart(); if (seekStatus != FudStatus::Success) { return seekStatus; |