/* * libfud * Copyright 2025 Dominick Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FUD_CSV_HPP #define FUD_CSV_HPP #include "fud_file.hpp" #include "fud_status.hpp" #include "fud_string_view.hpp" #include "fud_text.hpp" #include "fud_vector.hpp" #include // reference_wrapper 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; /** \brief The number of columns in the CSV. */ size_t numColumns; /** \brief Buffer for each line with numColumns of StringView. */ Vector lines; /** \brief Backing buffer for data. */ CsvBuffer buffer; /** \separator for each column */ Utf8 columnDelimiter{Ascii{','}}; /** \separator for each line */ NewlineRepr newlineDelimiter{NewlineRepr::Posix}; bool strict; /** \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); /** \brief Specify allocator. */ static Csv make(Allocator& lineAllocator, Allocator& bufferAllocator); /** Consume and return the CSV. */ static FudStatus parseCsvFromFilename( Csv& csv, Option bufferOption, StringView filename, OpenFlags flags = OpenFlags{}, Option dirFdOption = NullOpt); // assumes file is at start static FudStatus parseCsvFromUnbufferedFile(Csv& csv, RegularFile&& file); // assumes file is at start static FudStatus parseCsvFromBufferedFile(Csv& csv, BufferedRegularFile& file); }; } // namespace fud #endif