starting on bounded writers

main
Brett 2025-03-06 20:41:18 -05:00
parent f15ce17215
commit f1027d33a4
10 changed files with 295 additions and 151 deletions

View File

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

View File

@ -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\<std::string> 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\<std::string> blt::fs::recursiveInclude(std::string_view path, std::string include_header, std::vector<include_guard> 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\<std::string> 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\<std::string> blt::fs::recursiveInclude(std::string_view path, std::string include_header, std::vector<include_guard> 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

View File

@ -22,121 +22,12 @@
#include <iosfwd>
#include <sstream>
#include <blt/fs/fwddecl.h>
#include <blt/fs/limited_writers.h>
#include <blt/fs/stream_wrappers.h>
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 <typename T>
void read(T& out)
{
if (!m_reader->read(reinterpret_cast<char*>(&out), sizeof(T)))
throw std::runtime_error("Failed to read from reader");
}
template <typename T>
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 <typename T>
void write(const T& t)
{
m_writer->write(reinterpret_cast<char*>(&t), sizeof(T));
}
template <typename T>
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 <typename T>
void write(const T& t)
{
std::stringstream ss;
ss << t;
const auto str = ss.str();
m_writer->write(str.data(), str.size());
}
template <typename T>
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

View File

@ -19,7 +19,6 @@
#ifndef BLT_FS_FWDDECL_H
#define BLT_FS_FWDDECL_H
#include <stdexcept>
#include <blt/std/types.h>
namespace blt::fs

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_FS_BOUNDED_WRITER_H
#define BLT_FS_BOUNDED_WRITER_H
#include <blt/fs/fwddecl.h>
#include <functional>
#include <string>
#include <blt/std/types.h>
#icnlude <optional>
namespace blt::fs
{
inline auto basic_naming_function = [](size_t invocation, std::optional<std::string> 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<std::string> base_name, std::function<std::string(size_t, std::optional<std::string>)> naming_function = basic_naming_function, size_t max_size = 1024 * 1024 * 10);
private:
std::optional<std::string> 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<std::string(size_t, std::optional<std::string>)> m_naming_function;
};
}
#endif //BLT_FS_BOUNDED_WRITER_H

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_FS_STREAM_WRAPPERS_H
#define BLT_FS_STREAM_WRAPPERS_H
#include <iosfwd>
#include <sstream>
#include <blt/fs/fwddecl.h>
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 <typename T>
void read(T& out)
{
if (!m_reader->read(reinterpret_cast<char*>(&out), sizeof(T)))
throw std::runtime_error("Failed to read from reader");
}
template <typename T>
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 <typename T>
void write(const T& t)
{
static_assert(std::is_trivially_copyable_v<T>);
m_writer->write(reinterpret_cast<char*>(&t), sizeof(T));
}
template <typename T>
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 <typename T>
void write(const T& t)
{
std::stringstream ss;
ss << t;
const auto str = ss.str();
m_writer->write(str.data(), str.size());
}
template <typename T>
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

View File

@ -25,7 +25,6 @@
#include <string_view>
#include <vector>
#include <blt/logging/fmt_tokenizer.h>
#include <blt/logging/fwddecl.h>
#include <blt/meta/is_streamable.h>
#include <blt/std/utility.h>

View File

@ -19,27 +19,3 @@
#include <cstring>
#include <blt/std/logging.h>
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<std::streamsize>(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<std::streamsize>(bytes));
return static_cast<i64>(bytes);
}
void fstream_writer_t::flush()
{
m_stream->flush();
}
}

View File

@ -0,0 +1,27 @@
/*
* <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 <blt/fs/bounded_writers.h>
namespace blt::fs
{
bounded_writer::bounded_writer(size_t max_size)
{
}
}

View File

@ -0,0 +1,45 @@
/*
* <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 <istream>
#include <ostream>
#include <blt/fs/stream_wrappers.h>
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<std::streamsize>(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<std::streamsize>(bytes));
return static_cast<i64>(bytes);
}
void fstream_writer_t::flush()
{
m_stream->flush();
}
}