file writers

main
Brett 2025-03-07 00:43:54 -05:00
parent f1027d33a4
commit 83df06f1c0
7 changed files with 178 additions and 81 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 5.1.16)
set(BLT_VERSION 5.1.17)
set(BLT_TARGET BLT)

View File

@ -0,0 +1,95 @@
#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 <functional>
#include <optional>
#include <string>
#include <utility>
#include <blt/fs/fwddecl.h>
#include <blt/std/types.h>
namespace blt::fs
{
inline auto basic_naming_function = [](const size_t invocation, const std::optional<std::string>& prefix) {
return prefix.value_or("") + "-" + std::to_string(invocation) + ".txt";
};
// ReSharper disable once CppClassCanBeFinal
class fwriter_t : public writer_t
{
public:
explicit fwriter_t(const std::string& name, std::string mode = "ab"): m_mode(std::move(mode))
{
fwriter_t::newfile(name);
}
i64 write(const char* buffer, size_t bytes) override;
virtual void newfile(const std::string& new_name);
void flush() override;
protected:
std::string m_mode;
FILE* m_file = nullptr;
};
// ReSharper disable once CppClassCanBeFinal
class buffered_writer : public fwriter_t
{
public:
explicit buffered_writer(const std::string& name, size_t buffer_size = 1024 * 128);
i64 write(const char* buffer, size_t bytes) override;
void flush() override;
protected:
size_t m_current_pos = 0;
std::vector<char> m_buffer;
};
/**
* Creates a bounded writer where after a specified number of bytes a new file will be opened and written to instead.
*/
// ReSharper disable once CppClassCanBeFinal
class bounded_writer : public fwriter_t
{
public:
explicit bounded_writer(fwriter_t& writer, std::optional<std::string> base_name,
const std::function<std::string(size_t, std::optional<std::string>)>& naming_function = basic_naming_function,
size_t max_size = 1024 * 1024 * 10);
i64 write(const char* buffer, size_t bytes) override;
private:
fwriter_t* m_writer = nullptr;
std::optional<std::string> m_base_name;
size_t m_current_invocation = 0;
size_t m_max_size = 0;
size_t m_currently_written = 0;
// inputs: current invocation, optional string provided to the constructor
// returns: name of the file to write to
std::function<std::string(size_t, const std::optional<std::string>&)> m_naming_function;
};
}
#endif //BLT_FS_BOUNDED_WRITER_H

View File

@ -22,7 +22,7 @@
#include <iosfwd>
#include <sstream>
#include <blt/fs/fwddecl.h>
#include <blt/fs/limited_writers.h>
#include <blt/fs/file_writers.h>
#include <blt/fs/stream_wrappers.h>
namespace blt::fs

View File

@ -1,51 +0,0 @@
#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

@ -1 +1 @@
Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5
Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3

View File

@ -0,0 +1,80 @@
/*
* <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/file_writers.h>
#include <cstdio>
#include <utility>
#include <cstring>
#include <stdexcept>
namespace blt::fs
{
i64 fwriter_t::write(const char* buffer, const size_t bytes)
{
return static_cast<i64>(std::fwrite(buffer, 1, bytes, m_file));
}
void fwriter_t::flush()
{
writer_t::flush();
std::fflush(m_file);
}
void fwriter_t::newfile(const std::string& new_name)
{
if (m_file != nullptr)
std::fclose(m_file);
m_file = std::fopen(new_name.c_str(), m_mode.c_str());
if (!m_file)
throw std::runtime_error("Failed to open file for writing");
}
buffered_writer::buffered_writer(const std::string& name, const size_t buffer_size): fwriter_t{name}
{
m_buffer.resize(buffer_size);
}
i64 buffered_writer::write(const char* buffer, const size_t bytes)
{
if (bytes > m_buffer.size())
return fwriter_t::write(buffer, bytes);
if (bytes + m_current_pos > m_buffer.size())
flush();
std::memcpy(m_buffer.data() + m_current_pos, buffer, bytes);
m_current_pos += bytes;
return static_cast<i64>(bytes);
}
void buffered_writer::flush()
{
fwriter_t::flush();
fwriter_t::write(m_buffer.data(), m_current_pos);
m_current_pos = 0;
}
bounded_writer::bounded_writer(fwriter_t& writer, std::optional<std::string> base_name,
const std::function<std::string(size_t, std::optional<std::string>)>& naming_function,
const size_t max_size): fwriter_t{naming_function(0, base_name)}, m_writer(&writer),
m_base_name(std::move(base_name)), m_max_size(max_size),
m_naming_function(naming_function)
{}
i64 bounded_writer::write(const char* buffer, size_t bytes)
{
return fwriter_t::write(buffer, bytes);
}
}

View File

@ -1,27 +0,0 @@
/*
* <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)
{
}
}