summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2025-01-01 17:41:17 -0600
committerDominick Allen <djallen@librehumanitas.org>2025-01-01 17:41:17 -0600
commit16379362c02a2472f00fac49cad62788547c9519 (patch)
tree9b7f42acbba8dd259a536287a2b130e92ad2e2c7 /include
parent012df4bc38777c9053353ec2c4213bba67d63ab4 (diff)
Add CSV parsing, printing, fix buffered file reading.
Diffstat (limited to 'include')
-rw-r--r--include/fud_algorithm.hpp14
-rw-r--r--include/fud_allocator.hpp46
-rw-r--r--include/fud_csv.hpp82
-rw-r--r--include/fud_file.hpp6
-rw-r--r--include/fud_print.hpp40
-rw-r--r--include/fud_text.hpp50
-rw-r--r--include/fud_type_traits.hpp (renamed from include/fud_fud_type_traits.hpp)0
7 files changed, 233 insertions, 5 deletions
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<typename T>
+concept LessThanComparable =
+ requires(T lhs, T rhs) {
+ { lhs < rhs } -> std::convertible_to<bool>;
+};
+
+template <LessThanComparable T>
+inline const T& min(const T& lhs, const T& rhs) {
+ if (lhs < rhs) {
+ return lhs;
+ }
+ return rhs;
+}
+
template <std::integral T>
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 <size_t Size>
+class SimpleStackAllocator final : public Allocator {
+private:
+ std::byte m_memory[Size]{};
+ size_t m_allocated{0};
+
+public:
+ virtual ~SimpleStackAllocator() override final = default;
+
+ virtual Result<std::byte*, FudStatus> allocate(size_t bytes, size_t alignment = alignof(std::max_align_t)) override final {
+ using RetType = Result<std::byte*, FudStatus>;
+ static_cast<void>(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 <functional> // reference_wrapper
+
+namespace fud {
+
+using TextBuffer = Vector<std::byte>;
+using CsvBuffer = Vector<std::byte>;
+using CsvLine = Vector<StringView>;
+
+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<CsvLine> 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<TextBuffer&&> bufferOption,
+ StringView filename,
+ OpenFlags flags = OpenFlags{},
+ Option<int> 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_t, FudStatus> size() const {
+ return m_file.size();
+ }
+
/** \brief Write from source to file as sink. */
DrainResult write(const std::byte* source, size_t length, Option<size_t> maxExtraAttempts);
@@ -217,6 +221,8 @@ class BufferedRegularFile {
FudStatus seek(size_t position);
+ Result<size_t, FudStatus> searchSubstring(StringView subString);
+
constexpr const RegularFile& file() const
{
return m_file;
diff --git a/include/fud_print.hpp b/include/fud_print.hpp
new file mode 100644
index 0000000..592b106
--- /dev/null
+++ b/include/fud_print.hpp
@@ -0,0 +1,40 @@
+/*
+ * 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_PRINT_HPP
+#define FUD_PRINT_HPP
+
+#include "fud_format.hpp"
+
+namespace fud {
+
+struct StdOutSink {
+ DrainResult drain(StringView source);
+};
+
+FormatResult print(FormatString fmt);
+
+template <typename... Args>
+FormatResult print(FormatString fmt, Args&&... args)
+{
+ StdOutSink outSink{};
+ return format(outSink, FormatCharMode::Unchecked, fmt, std::forward<Args>(args)...);
+}
+
+} // namespace fud
+
+#endif
diff --git a/include/fud_text.hpp b/include/fud_text.hpp
new file mode 100644
index 0000000..683e911
--- /dev/null
+++ b/include/fud_text.hpp
@@ -0,0 +1,50 @@
+/*
+ * 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_TEXT_HPP
+#define FUD_TEXT_HPP
+
+#include "fud_string_view.hpp"
+#include <cstdint>
+
+namespace fud {
+
+enum class NewlineRepr : uint8_t {
+ Posix, // \n
+ MSDOS, // \r\n
+ C64, // \r (Commodore 64)
+ RiscOS, // \n\r
+};
+
+constexpr StringView newlineText(NewlineRepr repr) {
+ switch (repr) {
+ case NewlineRepr::Posix:
+ return StringView{u8"\n"};
+ case NewlineRepr::MSDOS:
+ return StringView{u8"\r\n"};
+ case NewlineRepr::C64:
+ return StringView{u8"\r"};
+ case NewlineRepr::RiscOS:
+ return StringView{u8"\n\r"};
+ default:
+ return StringView{u8"\n"};
+ }
+}
+
+} // namespace fud
+
+#endif
diff --git a/include/fud_fud_type_traits.hpp b/include/fud_type_traits.hpp
index 3fdff79..3fdff79 100644
--- a/include/fud_fud_type_traits.hpp
+++ b/include/fud_type_traits.hpp