From 29ffdaaacdb9e5cedec011ec43e735ce6fbed565 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 6 Mar 2025 15:33:35 -0500 Subject: [PATCH] write strings --- CMakeLists.txt | 2 +- include/blt/fs/filesystem.h | 77 ++++++++++++++++++++++++++++++++++++- include/blt/fs/fwddecl.h | 3 +- src/blt/fs/filesystem.cpp | 2 +- tests/logger_tests.cpp | 59 +++++++--------------------- 5 files changed, 94 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7c203c..782e4ac 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.13) +set(BLT_VERSION 5.1.14) set(BLT_TARGET BLT) diff --git a/include/blt/fs/filesystem.h b/include/blt/fs/filesystem.h index 8695f31..0ff2c62 100644 --- a/include/blt/fs/filesystem.h +++ b/include/blt/fs/filesystem.h @@ -20,6 +20,7 @@ #define BLT_FILESYSTEM_H #include +#include #include namespace blt::fs @@ -37,6 +38,7 @@ namespace blt::fs fstream_reader_t& operator=(const fstream_reader_t& copy) = delete; i64 read(char* buffer, size_t bytes) override; + private: std::istream* m_stream; }; @@ -50,7 +52,7 @@ namespace blt::fs fstream_writer_t& operator=(const fstream_writer_t& copy) = delete; - i64 write(char* buffer, size_t bytes) override; + i64 write(const char* buffer, size_t bytes) override; void flush() override; @@ -62,6 +64,79 @@ namespace blt::fs 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 df91679..a0c64c5 100644 --- a/include/blt/fs/fwddecl.h +++ b/include/blt/fs/fwddecl.h @@ -19,6 +19,7 @@ #ifndef BLT_FS_FWDDECL_H #define BLT_FS_FWDDECL_H +#include #include namespace blt::fs @@ -64,7 +65,7 @@ namespace blt::fs * @param bytes number of bytes to write * @return number of bytes, or negative value if error. Zero is also a valid return, not indicating error in itself but can be the result of one. */ - virtual i64 write(char* buffer, size_t bytes) = 0; + virtual i64 write(const char* buffer, size_t bytes) = 0; /** * Optional flush command which syncs the underlying objects diff --git a/src/blt/fs/filesystem.cpp b/src/blt/fs/filesystem.cpp index 03bba66..1ea5192 100644 --- a/src/blt/fs/filesystem.cpp +++ b/src/blt/fs/filesystem.cpp @@ -32,7 +32,7 @@ namespace blt::fs fstream_writer_t::fstream_writer_t(std::ostream& stream): m_stream{&stream} {} - i64 fstream_writer_t::write(char* buffer, size_t bytes) + i64 fstream_writer_t::write(const char* buffer, const size_t bytes) { m_stream->write(buffer, static_cast(bytes)); return static_cast(bytes); diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index cb159c7..527dcb2 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -15,15 +15,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include +#include #include #include +#include +#include #include #include #include #include -#include -#include -#include struct some_silly_type_t {}; @@ -89,43 +90,6 @@ std::pair compare_strings(const std::string& s1, const std::s return {true, ""}; } -struct seqbuf final : std::streambuf -{ - seqbuf(std::streambuf* destBuf, std::size_t& charCount) : m_dest(destBuf), m_count(charCount) - {} - -protected: - int_type overflow(int_type ch) override - { - if (traits_type::eq_int_type(ch, traits_type::eof())) - return traits_type::eof(); - ++m_count; - blt::logging::println("We are printing a character {:c}", ch); - return m_dest->sputc(static_cast(ch)); - } - - std::streamsize xsputn(const char* s, std::streamsize count) override - { - blt::logging::println("We are printing a series of characters {} {}", count, std::string_view{s, static_cast(count)}); - m_count += static_cast(count); - return m_dest->sputn(s, count); - } - - int_type underflow() override - { - return m_dest->sgetc(); - } - - int sync() override - { - return m_dest->pubsync(); - } - -private: - std::streambuf* m_dest; - std::size_t& m_count; -}; - int main() { std::stringstream ss; @@ -195,12 +159,17 @@ int main() std::ofstream os("test.txt"); + blt::fs::fstream_writer_t wtr(os); + blt::fs::writer_wrapper_t writer(wtr); + + writer.write("This is a println with a stream\n"); + writer.write("This is a mixed print"); + writer.write(std::to_string(25)); + writer.write(" with multiple types "); + writer.write(std::to_string(34.23340)); + writer.write('\n'); + writer.write("What about just a new line character?\n"); size_t charCount = 0; - seqbuf hi{os.rdbuf(), charCount}; - dynamic_cast(os).rdbuf(&hi); - os << "This is a println with a stream" << std::endl; - os << "This is a mixed print " << 25 << " with multiple types " << 34.23340 << std::endl; - os << "What about just a new line character?\n"; blt::logging::println("Logged {} characters", charCount);