From f1027d33a414bdab711b78503372ea5f06a558e1 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 6 Mar 2025 20:41:18 -0500 Subject: [PATCH] starting on bounded writers --- CMakeLists.txt | 2 +- README.md | 39 ++++++--- include/blt/fs/filesystem.h | 113 +----------------------- include/blt/fs/fwddecl.h | 1 - include/blt/fs/limited_writers.h | 51 +++++++++++ include/blt/fs/stream_wrappers.h | 143 +++++++++++++++++++++++++++++++ include/blt/logging/logging.h | 1 - src/blt/fs/filesystem.cpp | 24 ------ src/blt/fs/limited_writers.cpp | 27 ++++++ src/blt/fs/stream_wrappers.cpp | 45 ++++++++++ 10 files changed, 295 insertions(+), 151 deletions(-) create mode 100644 include/blt/fs/limited_writers.h create mode 100644 include/blt/fs/stream_wrappers.h create mode 100644 src/blt/fs/limited_writers.cpp create mode 100644 src/blt/fs/stream_wrappers.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 366acb1..8b129ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 5.1.15) +set(BLT_VERSION 5.1.16) set(BLT_TARGET BLT) diff --git a/README.md b/README.md index 1d498f5..026d561 100644 --- a/README.md +++ b/README.md @@ -19,19 +19,32 @@ This module provides general string formatting utilities. *Note: this folder con - **format.h** Legacy library file containing various utilities for creating formatted output. Also includes methods for writing Java UTF8 strings. -- ## blt/fs - - ### loader.h - - `std::string blt::fs::getFile(std::string_view path)` - - Gets the entire file as a string. - - std::vector\ blt::fs::getLinesFromFile(std::string_view path) - - Gets the entire file as a string, then splits on the new line character. Then returns a vector of those lines - - std::vector\ blt::fs::recursiveInclude(std::string_view path, std::string include_header, std::vector guards); - - Recursively include in order based on the include string (include_header) marked by the include guards - - Defaults to C/C++/GLSL preprocessor style (Was designed for GLSL) - - std::string blt::fs::loadBrainFuckFile(const std::string& path) - - Load a brainfuck file recursively, uses ~ to mark the include path - - ### nbt.h - - probably needs to be remade (TODO) +## BLT Filesystem +This module provides helper classes for filesystem objects. It seeks to offer an interface that is simpler than the one provided by the standard library. +Specifically, the number of functions required to implement is significantly lower, +and the interface is generally cleaner. Eventually, this module aims to support various file formats, +such as Minecraft's NBT system. Currently, there is an existing NBT file, but it was written when I was first learning C++. + +### Files + +- **filesystem.h** + This is the base file which includes all other files. You should use the other options as this can be a heavy file to include + +- **path_helper.h** + This file provides functions for interfacing with paths. Specifically, as of this moment it only provides an interface for getting the base name of a file path. + +- **loader.h** + - `std::string blt::fs::getFile(std::string_view path)` + - Gets the entire file as a string. + - `std::vector\ blt::fs::getLinesFromFile(std::string_view path)` + - Gets the entire file as a string, then splits on the new line character. Then returns a vector of those lines + - `std::vector\ blt::fs::recursiveInclude(std::string_view path, std::string include_header, std::vector guards)` + - Recursively include in order based on the include string (include_header) marked by the include guards + - Defaults to C/C++/GLSL preprocessor style (Was designed for GLSL) + - `std::string blt::fs::loadBrainFuckFile(const std::string& path)` + - Load a brainfuck file recursively, uses ~ to mark the include path +- ### nbt.h + - probably needs to be remade (TODO) - ## blt/math - ### averages.h - blt::averagizer_o_matic diff --git a/include/blt/fs/filesystem.h b/include/blt/fs/filesystem.h index 0ff2c62..4a7f847 100644 --- a/include/blt/fs/filesystem.h +++ b/include/blt/fs/filesystem.h @@ -22,121 +22,12 @@ #include #include #include +#include +#include namespace blt::fs { - /** - * reader_t wrapper for fstream - */ - class fstream_reader_t final : public reader_t - { - public: - explicit fstream_reader_t(std::istream& stream); - explicit fstream_reader_t(fstream_reader_t& copy) = delete; - - fstream_reader_t& operator=(const fstream_reader_t& copy) = delete; - - i64 read(char* buffer, size_t bytes) override; - - private: - std::istream* m_stream; - }; - - class fstream_writer_t final : public writer_t - { - public: - explicit fstream_writer_t(std::ostream& stream); - - explicit fstream_writer_t(fstream_writer_t& copy) = delete; - - fstream_writer_t& operator=(const fstream_writer_t& copy) = delete; - - i64 write(const char* buffer, size_t bytes) override; - - void flush() override; - - virtual ~fstream_writer_t() override // NOLINT - { - flush(); - } - - private: - std::ostream* m_stream; - }; - - class reader_wrapper_t final - { - public: - explicit reader_wrapper_t(reader_t& reader): m_reader(&reader) - {} - - template - void read(T& out) - { - if (!m_reader->read(reinterpret_cast(&out), sizeof(T))) - throw std::runtime_error("Failed to read from reader"); - } - - template - friend reader_wrapper_t& operator>>(reader_wrapper_t& reader, T& t) - { - reader.read(t); - return reader; - } - - private: - reader_t* m_reader; - }; - - class writer_wrapper_t final - { - public: - explicit writer_wrapper_t(writer_t& writer): m_writer(&writer) - {} - - template - void write(const T& t) - { - m_writer->write(reinterpret_cast(&t), sizeof(T)); - } - - template - friend writer_wrapper_t& operator<<(writer_wrapper_t& writer, const T& t) - { - writer.write(t); - return writer; - } - - private: - writer_t* m_writer; - }; - - class writer_string_wrapper_t final - { - public: - explicit writer_string_wrapper_t(writer_t& writer): m_writer(&writer) - {} - - template - void write(const T& t) - { - std::stringstream ss; - ss << t; - const auto str = ss.str(); - m_writer->write(str.data(), str.size()); - } - - template - friend writer_string_wrapper_t& operator<<(writer_string_wrapper_t& writer, const T& t) - { - writer.write(t); - return writer; - } - - private: - writer_t* m_writer; - }; } #endif //BLT_FILESYSTEM_H diff --git a/include/blt/fs/fwddecl.h b/include/blt/fs/fwddecl.h index a0c64c5..06359be 100644 --- a/include/blt/fs/fwddecl.h +++ b/include/blt/fs/fwddecl.h @@ -19,7 +19,6 @@ #ifndef BLT_FS_FWDDECL_H #define BLT_FS_FWDDECL_H -#include #include namespace blt::fs diff --git a/include/blt/fs/limited_writers.h b/include/blt/fs/limited_writers.h new file mode 100644 index 0000000..034a08b --- /dev/null +++ b/include/blt/fs/limited_writers.h @@ -0,0 +1,51 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_FS_BOUNDED_WRITER_H +#define BLT_FS_BOUNDED_WRITER_H + +#include +#include +#include +#include +#icnlude + +namespace blt::fs +{ + inline auto basic_naming_function = [](size_t invocation, std::optional prefix) { + return prefix.value_or("") + "-" + std::to_string(invocation) + ".txt"; + }; + + /** + * Creates a bounded writer where after a specified number of bytes a new file will be opened and written to instead. + */ + class bounded_writer : public writer_t + { + public: + explicit bounded_writer(std::optional base_name, std::function)> naming_function = basic_naming_function, size_t max_size = 1024 * 1024 * 10); + + private: + std::optional m_base_name; + size_t m_current_invocation = 0; + // inputs: current invocation, optional string provided to the constructor + // returns: name of the file to write to + std::function)> m_naming_function; + }; +} + +#endif //BLT_FS_BOUNDED_WRITER_H diff --git a/include/blt/fs/stream_wrappers.h b/include/blt/fs/stream_wrappers.h new file mode 100644 index 0000000..2a5094d --- /dev/null +++ b/include/blt/fs/stream_wrappers.h @@ -0,0 +1,143 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_FS_STREAM_WRAPPERS_H +#define BLT_FS_STREAM_WRAPPERS_H + +#include +#include +#include + +namespace blt::fs +{ + /** + * reader_t wrapper for fstream + */ + class fstream_reader_t final : public reader_t + { + public: + explicit fstream_reader_t(std::istream& stream); + + explicit fstream_reader_t(fstream_reader_t& copy) = delete; + + fstream_reader_t& operator=(const fstream_reader_t& copy) = delete; + + i64 read(char* buffer, size_t bytes) override; + + private: + std::istream* m_stream; + }; + + class fstream_writer_t final : public writer_t + { + public: + explicit fstream_writer_t(std::ostream& stream); + + explicit fstream_writer_t(fstream_writer_t& copy) = delete; + + fstream_writer_t& operator=(const fstream_writer_t& copy) = delete; + + i64 write(const char* buffer, size_t bytes) override; + + void flush() override; + + virtual ~fstream_writer_t() override // NOLINT + { + flush(); + } + + private: + std::ostream* m_stream; + }; + + class reader_wrapper_t + { + public: + explicit reader_wrapper_t(reader_t& reader): m_reader(&reader) + {} + + template + void read(T& out) + { + if (!m_reader->read(reinterpret_cast(&out), sizeof(T))) + throw std::runtime_error("Failed to read from reader"); + } + + template + friend reader_wrapper_t& operator>>(reader_wrapper_t& reader, T& t) + { + reader.read(t); + return reader; + } + + private: + reader_t* m_reader; + }; + + class writer_wrapper_t + { + public: + explicit writer_wrapper_t(writer_t& writer): m_writer(&writer) + {} + + template + void write(const T& t) + { + static_assert(std::is_trivially_copyable_v); + m_writer->write(reinterpret_cast(&t), sizeof(T)); + } + + template + friend writer_wrapper_t& operator<<(writer_wrapper_t& writer, const T& t) + { + writer.write(t); + return writer; + } + + private: + writer_t* m_writer; + }; + + class writer_string_wrapper_t + { + public: + explicit writer_string_wrapper_t(writer_t& writer): m_writer(&writer) + {} + + template + void write(const T& t) + { + std::stringstream ss; + ss << t; + const auto str = ss.str(); + m_writer->write(str.data(), str.size()); + } + + template + friend writer_string_wrapper_t& operator<<(writer_string_wrapper_t& writer, const T& t) + { + writer.write(t); + return writer; + } + + private: + writer_t* m_writer; + }; +} + +#endif //BLT_FS_STREAM_WRAPPERS_H diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index b09059b..2c5441a 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -25,7 +25,6 @@ #include #include #include -#include #include #include diff --git a/src/blt/fs/filesystem.cpp b/src/blt/fs/filesystem.cpp index 1ea5192..43b9fb2 100644 --- a/src/blt/fs/filesystem.cpp +++ b/src/blt/fs/filesystem.cpp @@ -19,27 +19,3 @@ #include #include -namespace blt::fs -{ - fstream_reader_t::fstream_reader_t(std::istream& stream): m_stream{&stream} - {} - - i64 fstream_reader_t::read(char* buffer, const size_t bytes) - { - return m_stream->readsome(buffer, static_cast(bytes)); - } - - fstream_writer_t::fstream_writer_t(std::ostream& stream): m_stream{&stream} - {} - - i64 fstream_writer_t::write(const char* buffer, const size_t bytes) - { - m_stream->write(buffer, static_cast(bytes)); - return static_cast(bytes); - } - - void fstream_writer_t::flush() - { - m_stream->flush(); - } -} diff --git a/src/blt/fs/limited_writers.cpp b/src/blt/fs/limited_writers.cpp new file mode 100644 index 0000000..7b01c3d --- /dev/null +++ b/src/blt/fs/limited_writers.cpp @@ -0,0 +1,27 @@ +/* + * + * Copyright (C) 2025 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +namespace blt::fs +{ + bounded_writer::bounded_writer(size_t max_size) + { + + } + +} \ No newline at end of file diff --git a/src/blt/fs/stream_wrappers.cpp b/src/blt/fs/stream_wrappers.cpp new file mode 100644 index 0000000..144001c --- /dev/null +++ b/src/blt/fs/stream_wrappers.cpp @@ -0,0 +1,45 @@ +/* + * + * Copyright (C) 2025 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +namespace blt::fs +{ + fstream_reader_t::fstream_reader_t(std::istream& stream): m_stream{&stream} + {} + + i64 fstream_reader_t::read(char* buffer, const size_t bytes) + { + return m_stream->readsome(buffer, static_cast(bytes)); + } + + fstream_writer_t::fstream_writer_t(std::ostream& stream): m_stream{&stream} + {} + + i64 fstream_writer_t::write(const char* buffer, const size_t bytes) + { + m_stream->write(buffer, static_cast(bytes)); + return static_cast(bytes); + } + + void fstream_writer_t::flush() + { + m_stream->flush(); + } +}