From 87071200872c2450c947047350132aee493033c1 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 2 Jan 2025 15:11:51 -0600 Subject: Get basic CSV parser operating. --- include/fud_csv.hpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'include/fud_csv.hpp') diff --git a/include/fud_csv.hpp b/include/fud_csv.hpp index efd37e6..38b1b81 100644 --- a/include/fud_csv.hpp +++ b/include/fud_csv.hpp @@ -22,6 +22,7 @@ #include "fud_status.hpp" #include "fud_string_view.hpp" #include "fud_text.hpp" +#include "fud_utf8.hpp" #include "fud_vector.hpp" #include // reference_wrapper @@ -29,52 +30,59 @@ namespace fud { using TextBuffer = Vector; -using CsvBuffer = Vector; -using CsvLine = Vector; struct Csv { /** \brief The number of lines of data in the CSV. */ - size_t numLines; + size_t numLines{0}; /** \brief The number of columns in the CSV. */ - size_t numColumns; + size_t numColumns{0}; /** \brief Buffer for each line with numColumns of StringView. */ - Vector lines; + Vector entries; /** \brief Backing buffer for data. */ - CsvBuffer buffer; + Vector buffer; /** \separator for each column */ Utf8 columnDelimiter{Ascii{','}}; + Utf8 quoteCharacter{Ascii{'"'}}; + /** \separator for each line */ NewlineRepr newlineDelimiter{NewlineRepr::Posix}; - bool strict; + bool strictUtf8{true}; + + bool strictColumns{true}; + + bool strictQuote{false}; + + bool skipInitialSpace{false}; /** \brief Uses global Fud allocator for lines and backing buffer. */ static Csv makeDefault(); /** \brief Specify allocator to use for both lines and backing buffer. */ - static Csv makeSingleAllocator(Allocator& allocator); + static Csv makeWithSingleAllocator(Allocator& allocator); /** \brief Specify allocator. */ - static Csv make(Allocator& lineAllocator, Allocator& bufferAllocator); + static Csv make(Allocator& entryAllocator, Allocator& bufferAllocator); /** Consume and return the CSV. */ - static FudStatus parseCsvFromFilename( + static FudStatus parseFromFilename( Csv& csv, Option bufferOption, StringView filename, OpenFlags flags = OpenFlags{}, - Option dirFdOption = NullOpt); + Option dirFdOption = NullOpt, + Option maxExtraAttempts = NullOpt); // assumes file is at start - static FudStatus parseCsvFromUnbufferedFile(Csv& csv, RegularFile&& file); + static FudStatus parseFromUnbufferedFile(Csv& csv, RegularFile&& file, Option maxExtraAttempts); // assumes file is at start - static FudStatus parseCsvFromBufferedFile(Csv& csv, BufferedRegularFile& file); + static FudStatus parseFromBufferedFile(Csv& csv, BufferedRegularFile& file, Option maxExtraAttempts); }; } // namespace fud -- cgit v1.2.3