From 78c219cc67f1fe6b3c7076e2c727f8f4cfffd859 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 11 Apr 2025 12:42:38 -0400 Subject: [PATCH] io changes --- CMakeLists.txt | 2 +- include/blt/fs/cstdio_wrappers.h | 56 ++++++++++++++++++++++++++++++++ include/blt/fs/fwddecl.h | 19 +++++++++++ include/blt/fs/stream_wrappers.h | 12 +++---- src/blt/fs/cstdio_wrappers.cpp | 42 ++++++++++++++++++++++++ src/blt/fs/stream_wrappers.cpp | 21 ++++++++++++ 6 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 include/blt/fs/cstdio_wrappers.h create mode 100644 src/blt/fs/cstdio_wrappers.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d15920..1552ff6 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.2.40) +set(BLT_VERSION 5.2.41) set(BLT_TARGET BLT) diff --git a/include/blt/fs/cstdio_wrappers.h b/include/blt/fs/cstdio_wrappers.h new file mode 100644 index 0000000..6425b9e --- /dev/null +++ b/include/blt/fs/cstdio_wrappers.h @@ -0,0 +1,56 @@ +#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_CSTDIO_WRAPPERS_H +#define BLT_FS_CSTDIO_WRAPPERS_H + +#include + +namespace blt::fs +{ + class file_reader_t final : public reader_t + { + public: + explicit file_reader_t(void* file): m_file(file) + { + } + + i64 read(char* buffer, size_t bytes) override; + + private: + void* m_file; + }; + + class file_writer_t final : public writer_t + { + public: + explicit file_writer_t(void* file): m_file(file) + { + } + + i64 write(const char* buffer, size_t bytes) override; + + i64 tell() override; + + void seek(i64 offset, seek_origin origin = seek_origin::seek_set) override; + private: + void* m_file; + }; +} + +#endif //BLT_FS_CSTDIO_WRAPPERS_H diff --git a/include/blt/fs/fwddecl.h b/include/blt/fs/fwddecl.h index bed0f77..32d395d 100644 --- a/include/blt/fs/fwddecl.h +++ b/include/blt/fs/fwddecl.h @@ -57,6 +57,16 @@ namespace blt::fs class writer_t { public: + enum class seek_origin + { + // Seek from current position + seek_cur = SEEK_CUR, + // Seek from end of file. Not valid on binary streams + seek_end = SEEK_END, + // Seek from start of file + seek_set = SEEK_SET + }; + virtual ~writer_t() = default; explicit writer_t() = default; @@ -71,6 +81,15 @@ namespace blt::fs */ virtual i64 write(const char* buffer, size_t bytes) = 0; + virtual i64 tell() + { + return 0; + } + + virtual void seek(i64 offset, seek_origin origin = seek_origin::seek_set) + { + } + i64 write(const void* buffer, const size_t bytes) { return this->write(static_cast(buffer), bytes); diff --git a/include/blt/fs/stream_wrappers.h b/include/blt/fs/stream_wrappers.h index dff7024..1bdf1a5 100644 --- a/include/blt/fs/stream_wrappers.h +++ b/include/blt/fs/stream_wrappers.h @@ -33,10 +33,6 @@ namespace blt::fs 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: @@ -48,14 +44,14 @@ namespace blt::fs 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; + i64 tell() override; + + void seek(i64 offset, seek_origin origin = seek_origin::seek_set) override; + virtual ~fstream_writer_t() override // NOLINT { flush(); diff --git a/src/blt/fs/cstdio_wrappers.cpp b/src/blt/fs/cstdio_wrappers.cpp new file mode 100644 index 0000000..799a2f7 --- /dev/null +++ b/src/blt/fs/cstdio_wrappers.cpp @@ -0,0 +1,42 @@ +/* + * + * 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 + +namespace blt::fs +{ + i64 file_reader_t::read(char* buffer, const size_t bytes) + { + return fread(buffer, bytes, 1, static_cast(m_file)); + } + + i64 file_writer_t::write(const char* buffer, const size_t bytes) + { + return fwrite(buffer, bytes, 1, static_cast(m_file)); + } + + i64 file_writer_t::tell() + { + return ftell(static_cast(m_file)); + } + + void file_writer_t::seek(const i64 offset, const seek_origin origin) + { + fseek(static_cast(m_file), offset, static_cast(origin)); + } +} diff --git a/src/blt/fs/stream_wrappers.cpp b/src/blt/fs/stream_wrappers.cpp index 144001c..8c64475 100644 --- a/src/blt/fs/stream_wrappers.cpp +++ b/src/blt/fs/stream_wrappers.cpp @@ -42,4 +42,25 @@ namespace blt::fs { m_stream->flush(); } + + i64 fstream_writer_t::tell() + { + return m_stream->tellp(); + } + + void fstream_writer_t::seek(const i64 offset, const seek_origin origin) + { + switch (origin) + { + case seek_origin::seek_cur: + m_stream->seekp(offset, std::ios_base::cur); + break; + case seek_origin::seek_end: + m_stream->seekp(offset, std::ios_base::end); + break; + case seek_origin::seek_set: + m_stream->seekp(offset, std::ios_base::beg); + break; + } + } }