io changes

main
Brett 2025-04-11 12:42:38 -04:00
parent 09d1a82268
commit 78c219cc67
6 changed files with 143 additions and 9 deletions

View File

@ -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)

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_FS_CSTDIO_WRAPPERS_H
#define BLT_FS_CSTDIO_WRAPPERS_H
#include <blt/fs/fwddecl.h>
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

View File

@ -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<const char*>(buffer), bytes);

View File

@ -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();

View File

@ -0,0 +1,42 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <blt/fs/cstdio_wrappers.h>
namespace blt::fs
{
i64 file_reader_t::read(char* buffer, const size_t bytes)
{
return fread(buffer, bytes, 1, static_cast<FILE*>(m_file));
}
i64 file_writer_t::write(const char* buffer, const size_t bytes)
{
return fwrite(buffer, bytes, 1, static_cast<FILE*>(m_file));
}
i64 file_writer_t::tell()
{
return ftell(static_cast<FILE*>(m_file));
}
void file_writer_t::seek(const i64 offset, const seek_origin origin)
{
fseek(static_cast<FILE*>(m_file), offset, static_cast<int>(origin));
}
}

View File

@ -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;
}
}
}