diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b129ea..d2c94d7 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.16) +set(BLT_VERSION 5.1.17) set(BLT_TARGET BLT) diff --git a/include/blt/fs/file_writers.h b/include/blt/fs/file_writers.h new file mode 100644 index 0000000..d617de5 --- /dev/null +++ b/include/blt/fs/file_writers.h @@ -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 . + */ + +#ifndef BLT_FS_BOUNDED_WRITER_H +#define BLT_FS_BOUNDED_WRITER_H + +#include +#include +#include +#include +#include +#include + +namespace blt::fs +{ + inline auto basic_naming_function = [](const size_t invocation, const std::optional& 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 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 base_name, + const std::function)>& 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 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&)> m_naming_function; + }; +} + +#endif //BLT_FS_BOUNDED_WRITER_H diff --git a/include/blt/fs/filesystem.h b/include/blt/fs/filesystem.h index 4a7f847..096902c 100644 --- a/include/blt/fs/filesystem.h +++ b/include/blt/fs/filesystem.h @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include namespace blt::fs diff --git a/include/blt/fs/limited_writers.h b/include/blt/fs/limited_writers.h deleted file mode 100644 index 034a08b..0000000 --- a/include/blt/fs/limited_writers.h +++ /dev/null @@ -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 . - */ - -#ifndef BLT_FS_BOUNDED_WRITER_H -#define BLT_FS_BOUNDED_WRITER_H - -#include -#include -#include -#include -#icnlude - -namespace blt::fs -{ - inline auto basic_naming_function = [](size_t invocation, std::optional 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 base_name, std::function)> naming_function = basic_naming_function, size_t max_size = 1024 * 1024 * 10); - - private: - std::optional 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)> m_naming_function; - }; -} - -#endif //BLT_FS_BOUNDED_WRITER_H diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 7ef2e73..93201da 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 +Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 diff --git a/src/blt/fs/file_writers.cpp b/src/blt/fs/file_writers.cpp new file mode 100644 index 0000000..fb832a7 --- /dev/null +++ b/src/blt/fs/file_writers.cpp @@ -0,0 +1,80 @@ +/* + * + * 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 +#include +#include +#include + +namespace blt::fs +{ + i64 fwriter_t::write(const char* buffer, const size_t bytes) + { + return static_cast(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(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 base_name, + const std::function)>& 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); + } +} diff --git a/src/blt/fs/limited_writers.cpp b/src/blt/fs/limited_writers.cpp deleted file mode 100644 index 7b01c3d..0000000 --- a/src/blt/fs/limited_writers.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * - * 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 - -namespace blt::fs -{ - bounded_writer::bounded_writer(size_t max_size) - { - - } - -} \ No newline at end of file