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. --- .gitignore | 1 + CMakeLists.txt | 8 +++- README.org | 1 + cmake/warnings.cmake | 7 +--- include/fud_algorithm.hpp | 14 +++++++ include/fud_allocator.hpp | 46 ++++++++++++++++++--- include/fud_csv.hpp | 82 ++++++++++++++++++++++++++++++++++++ include/fud_file.hpp | 6 +++ include/fud_fud_type_traits.hpp | 80 ----------------------------------- include/fud_print.hpp | 40 ++++++++++++++++++ include/fud_text.hpp | 50 ++++++++++++++++++++++ include/fud_type_traits.hpp | 80 +++++++++++++++++++++++++++++++++++ source/fud_assert.cpp | 8 ++-- source/fud_csv.cpp | 92 +++++++++++++++++++++++++++++++++++++++++ source/fud_file.cpp | 76 ++++++++++++++++++++++++---------- source/fud_print.cpp | 52 +++++++++++++++++++++++ test/test_file.cpp | 28 ++++++++++--- 17 files changed, 550 insertions(+), 121 deletions(-) create mode 100644 include/fud_csv.hpp delete mode 100644 include/fud_fud_type_traits.hpp create mode 100644 include/fud_print.hpp create mode 100644 include/fud_text.hpp create mode 100644 include/fud_type_traits.hpp create mode 100644 source/fud_csv.cpp create mode 100644 source/fud_print.cpp diff --git a/.gitignore b/.gitignore index a7abdc1..2fd3d90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .cache *build/ +debug/ *release/ .ccls-cache compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index e40d7b2..465e35d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,12 @@ add_library(fud SHARED source/fud_allocator.cpp source/fud_assert.cpp source/fud_c_file.cpp + source/fud_csv.cpp source/fud_directory.cpp source/fud_file.cpp source/fud_format.cpp source/fud_memory.cpp + source/fud_print.cpp source/fud_sqlite.cpp source/fud_string_view.cpp source/fud_string_convert.cpp @@ -103,16 +105,19 @@ set(FUD_HEADERS "include/fud_algorithm.hpp" "include/fud_array.hpp" "include/fud_assert.hpp" + "include/fud_csv.hpp" "include/fud_c_file.hpp" "include/fud_c_string.hpp" "include/fud_directory.hpp" "include/fud_drain.hpp" "include/fud_file.hpp" "include/fud_fixed_vector.hpp" - "include/fud_fud_type_traits.hpp" + "include/fud_type_traits.hpp" + "include/fud_format.hpp" "include/fud_memory.hpp" "include/fud_option.hpp" "include/fud_permissions.hpp" + "include/fud_print.hpp" "include/fud_result.hpp" "include/fud_span.hpp" "include/fud_status.hpp" @@ -120,6 +125,7 @@ set(FUD_HEADERS "include/fud_string_convert.hpp" "include/fud_string_view.hpp" "include/fud_sqlite.hpp" + "include/fud_text.hpp" "include/fud_unique_array.hpp" "include/fud_utf8.hpp" "include/fud_utf8_iterator.hpp" diff --git a/README.org b/README.org index c693d68..4fe05c9 100644 --- a/README.org +++ b/README.org @@ -28,3 +28,4 @@ user controlled allocators. + Unicode support with UTF8 encoding + Formatting à la =std::format= + Wrappers around C files ++ CSV parsing diff --git a/cmake/warnings.cmake b/cmake/warnings.cmake index a2c5798..07c39b5 100644 --- a/cmake/warnings.cmake +++ b/cmake/warnings.cmake @@ -8,10 +8,6 @@ set(FUD_WARNINGS # -pedantic-errors -Wstack-usage=2048 # GCC specific -Wvla # variable modified types don't play nice in C++ - # types - -Wlong-long - -Wlong-long - # -Winline # memory / data / array / string -Wsizeof-pointer-memaccess -Wpacked @@ -81,7 +77,8 @@ set(FUD_WARNINGS # this plays badly with clangd # -Wzero-as-null-pointer-constant -Wlogical-op - -Wuseless-cast + # disabled because of dragonbox + # -Wuseless-cast -Wextra-semi -Wredundant-decls -Wmisleading-indentation diff --git a/include/fud_algorithm.hpp b/include/fud_algorithm.hpp index 0ad71d5..01cc5d3 100644 --- a/include/fud_algorithm.hpp +++ b/include/fud_algorithm.hpp @@ -27,6 +27,20 @@ namespace fud { +template +concept LessThanComparable = + requires(T lhs, T rhs) { + { lhs < rhs } -> std::convertible_to; +}; + +template +inline const T& min(const T& lhs, const T& rhs) { + if (lhs < rhs) { + return lhs; + } + return rhs; +} + template class Iota { public: diff --git a/include/fud_allocator.hpp b/include/fud_allocator.hpp index 9b90deb..95e1d5a 100644 --- a/include/fud_allocator.hpp +++ b/include/fud_allocator.hpp @@ -26,6 +26,12 @@ namespace fud { +/** \brief The default allocation function for globalFudAllocator. */ +extern std::byte* fudAlloc(size_t size); + +/** \brief The default deallocation function for globalFudAllocator. */ +extern void fudFree(std::byte* ptr); + class alignas(std::max_align_t) Allocator { public: virtual ~Allocator() = default; @@ -68,11 +74,41 @@ class NullAllocator : public Allocator { extern NullAllocator globalNullAllocator; -/** \brief The default allocation function for globalFudAllocator. */ -extern std::byte* fudAlloc(size_t size); - -/** \brief The default deallocation function for globalFudAllocator. */ -extern void fudFree(std::byte* ptr); +template +class SimpleStackAllocator final : public Allocator { +private: + std::byte m_memory[Size]{}; + size_t m_allocated{0}; + +public: + virtual ~SimpleStackAllocator() override final = default; + + virtual Result allocate(size_t bytes, size_t alignment = alignof(std::max_align_t)) override final { + using RetType = Result; + static_cast(alignment); + if (bytes > Size - m_allocated) { + return RetType::error(FudStatus::AllocFailure); + } + + auto* data = m_memory + m_allocated; + m_allocated += bytes; + + return RetType::okay(data); + } + + virtual void deallocate(std::byte* pointer, size_t bytes) override final + { + if (pointer + bytes != m_memory + m_allocated) { + m_allocated = Size; + return; + } + m_allocated -= bytes; + } + + virtual bool isEqual(const Allocator& rhs) const override final { + return &rhs == this; + } +}; } // namespace fud diff --git a/include/fud_csv.hpp b/include/fud_csv.hpp new file mode 100644 index 0000000..efd37e6 --- /dev/null +++ b/include/fud_csv.hpp @@ -0,0 +1,82 @@ +/* + * 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 diff --git a/include/fud_file.hpp b/include/fud_file.hpp index 82f8291..e7c485c 100644 --- a/include/fud_file.hpp +++ b/include/fud_file.hpp @@ -197,6 +197,10 @@ class BufferedRegularFile { FudStatus close(bool discardBuffer); + Result size() const { + return m_file.size(); + } + /** \brief Write from source to file as sink. */ DrainResult write(const std::byte* source, size_t length, Option maxExtraAttempts); @@ -217,6 +221,8 @@ class BufferedRegularFile { FudStatus seek(size_t position); + Result searchSubstring(StringView subString); + constexpr const RegularFile& file() const { return m_file; diff --git a/include/fud_fud_type_traits.hpp b/include/fud_fud_type_traits.hpp deleted file mode 100644 index 3fdff79..0000000 --- a/include/fud_fud_type_traits.hpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * libfud - * Copyright 2024 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_TYPE_TRAITS_HPP -#define FUD_TYPE_TRAITS_HPP - -#include -#include - -namespace fud { - -template