updated build system

This commit is contained in:
2024-08-08 18:59:14 -05:00
parent 6e4ae2896b
commit 0b5efef569
30 changed files with 1162 additions and 474 deletions

View File

@ -24,104 +24,362 @@
#include "utils/config.hpp"
#include "utils/path.hpp"
#include <memory>
namespace repertory::utils::file {
class file final {
struct i_file {
virtual ~i_file() = default;
virtual void close() = 0;
[[nodiscard]] virtual auto exists() const -> bool = 0;
virtual void flush() const = 0;
[[nodiscard]] virtual auto get_handle() const -> native_handle = 0;
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
[[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0;
[[nodiscard]] virtual auto is_read_only() const -> bool = 0;
[[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0;
[[nodiscard]] virtual auto
read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool = 0;
[[nodiscard]] virtual auto
read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool = 0;
[[nodiscard]] virtual auto
read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool = 0;
[[nodiscard]] virtual auto remove() -> bool = 0;
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
[[nodiscard]] virtual auto size() const -> std::uint64_t = 0;
[[nodiscard]] virtual auto truncate() -> bool = 0;
[[nodiscard]] virtual auto truncate(std::size_t size) -> bool = 0;
[[nodiscard]] virtual auto
write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool = 0;
[[nodiscard]] virtual auto
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
std::size_t *total_written = nullptr) -> bool = 0;
public:
[[nodiscard]] static auto attach_file(native_handle handle,
bool read_only = false) -> file;
[[nodiscard]] virtual operator bool() const = 0;
[[nodiscard]] static auto open_file(std::filesystem::path path,
bool read_only = false) -> file;
protected:
i_file() noexcept = default;
[[nodiscard]] static auto open_or_create_file(std::filesystem::path path,
bool read_only = false) -> file;
i_file(const i_file &) noexcept = delete;
i_file(i_file &&) noexcept = delete;
auto operator=(i_file &&) noexcept -> i_file & = delete;
auto operator=(const i_file &) noexcept -> i_file & = delete;
};
class file final : public i_file {
public:
[[nodiscard]] static auto
attach_file(native_handle handle,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_file(std::string_view path,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_file(std::wstring_view path,
bool read_only = false) -> std::unique_ptr<i_file> {
return open_file(utils::string::to_utf8(path), read_only);
}
[[nodiscard]] static auto
open_or_create_file(std::string_view path,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_or_create_file(std::wstring_view path,
bool read_only = false) -> std::unique_ptr<i_file> {
return open_or_create_file(utils::string::to_utf8(path), read_only);
}
public:
file() noexcept = default;
protected:
file(file_t file_ptr, std::filesystem::path path)
: file_(std::move(file_ptr)), path_(std::move(path)) {}
file(std::string_view path)
: file_(nullptr), path_(utils::path::absolute(path)) {}
file(std::wstring_view path)
: file_(nullptr),
path_(utils::path::absolute(utils::string::to_utf8(path))) {}
private:
file(file_t file_ptr, std::string_view path, bool read_only)
: file_(std::move(file_ptr)), path_(path), read_only_(read_only) {}
public:
file(const file &) = delete;
file(file &&move_file) noexcept
: file_(std::move(move_file.file_)),
path_(std::move(move_file.path_))
path_(std::move(move_file.path_)),
read_only_(move_file.read_only_)
#if defined(_WIN32)
,
mtx_()
mtx_(std::move(move_file.mtx_))
#endif // defined(_WIN32)
{
}
~file() { close(); }
~file() override { close(); }
private:
file_t file_;
std::string path_;
bool read_only_{false};
#if defined(_WIN32)
mutable std::unique_ptr<std::recursive_mutex> mtx_{
new std::recursive_mutex(),
};
#endif // defined(_WIN32)
private:
std::atomic_uint32_t read_buffer_size{65536U};
private:
void open();
public:
void close() override;
[[nodiscard]] auto exists() const -> bool override;
void flush() const override;
[[nodiscard]] auto get_handle() const -> native_handle override;
[[nodiscard]] auto get_path() const -> std::string override { return path_; }
[[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override {
return read_buffer_size;
}
[[nodiscard]] auto is_read_only() const -> bool override {
return read_only_;
};
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto
read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto remove() -> bool override;
auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override {
read_buffer_size = size;
return read_buffer_size;
}
[[nodiscard]] auto size() const -> std::uint64_t override;
[[nodiscard]] auto truncate() -> bool override { return truncate(0U); }
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
[[nodiscard]] auto
write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool override;
[[nodiscard]] auto
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
std::size_t *total_written = nullptr) -> bool override;
public:
auto operator=(const file &) noexcept -> file & = delete;
auto operator=(file &&move_file) noexcept -> file & {
if (&move_file != this) {
file_ = std::move(move_file.file_);
path_ = std::move(move_file.path_);
read_only_ = move_file.read_only_;
#if defined(_WIN32)
mtx_ = std::move(move_file.mtx_);
#endif // defined(_WIN32)
}
return *this;
}
private:
file_t file_{nullptr};
std::filesystem::path path_;
#if defined(_WIN32)
mutable std::recursive_mutex mtx_{};
#endif // defined(_WIN32)
[[nodiscard]] operator bool() const override { return file_ != nullptr; }
};
class thread_file final : public i_file {
public:
[[nodiscard]] static auto
attach_file(native_handle handle,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_file(std::string_view path,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_file(std::wstring_view path,
bool read_only = false) -> std::unique_ptr<i_file> {
return open_file(utils::string::to_utf8(path), read_only);
}
[[nodiscard]] static auto
open_or_create_file(std::string_view path,
bool read_only = false) -> std::unique_ptr<i_file>;
[[nodiscard]] static auto
open_or_create_file(std::wstring_view path,
bool read_only = false) -> std::unique_ptr<i_file> {
return open_or_create_file(utils::string::to_utf8(path), read_only);
}
public:
void close();
thread_file() noexcept = default;
void flush();
thread_file(std::string_view path) : file_(new file(path)) {}
[[nodiscard]] auto get_handle() const -> native_handle;
thread_file(std::wstring_view path)
: file_(new file(utils::string::to_utf8(path))) {}
[[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; }
protected:
thread_file(std::unique_ptr<i_file> file);
[[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool;
public:
thread_file(const thread_file &) = delete;
thread_file(thread_file &&move_file) noexcept
: file_(std::move(move_file.file_)) {}
~thread_file() override { close(); }
private:
std::unique_ptr<i_file> file_;
public:
void close() override;
[[nodiscard]] auto exists() const -> bool override { return file_->exists(); }
void flush() const override;
[[nodiscard]] auto get_handle() const -> native_handle override {
return file_->get_handle();
}
[[nodiscard]] auto get_path() const -> std::string override {
return file_->get_path();
}
[[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override {
return file_->get_read_buffer_size();
}
[[nodiscard]] auto is_read_only() const -> bool override {
return file_->is_read_only();
};
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool;
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool;
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool;
[[nodiscard]] auto
read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool override;
[[nodiscard]] auto remove() -> bool;
[[nodiscard]] auto remove() -> bool override;
[[nodiscard]] auto size() const -> std::uint64_t;
auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override {
return file_->set_read_buffer_size(size);
}
[[nodiscard]] auto truncate() -> bool { return truncate(0U); }
[[nodiscard]] auto size() const -> std::uint64_t override;
[[nodiscard]] auto truncate(std::size_t size) -> bool;
[[nodiscard]] auto truncate() -> bool override { return truncate(0U); }
[[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool;
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
[[nodiscard]] auto write(const unsigned char *data, std::size_t to_write,
std::size_t offset,
std::size_t *total_written = nullptr) -> bool;
[[nodiscard]] auto
write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool override;
[[nodiscard]] auto
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
std::size_t *total_written = nullptr) -> bool override;
public:
[[nodiscard]] operator bool() const { return file_ != nullptr; }
[[nodiscard]] operator bool() const override {
return static_cast<bool>(*file_);
}
auto operator=(const file &) noexcept -> thread_file & = delete;
auto operator=(thread_file &&move_file) noexcept -> thread_file & {
if (&move_file != this) {
file_ = std::move(move_file.file_);
}
return *this;
}
};
[[nodiscard]] auto create_directories(std::string_view path) -> bool;
[[nodiscard]] auto create_directories(std::wstring_view path) -> bool;
[[nodiscard]] auto
directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool;
[[nodiscard]] auto
directory_exists_in_path(std::wstring_view path,
std::wstring_view sub_directory) -> bool;
[[nodiscard]] auto get_file_size(std::string_view path,
std::uint64_t &file_size) -> bool;
[[nodiscard]] auto get_file_size(std::wstring_view path,
std::uint64_t &file_size) -> bool;
[[nodiscard]] auto file_exists_in_path(std::string_view path,
std::string_view file_name) -> bool;
[[nodiscard]] auto file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool;
[[nodiscard]] auto is_directory(std::string_view path) -> bool;
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
@ -130,6 +388,12 @@ public:
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
[[nodiscard]] auto remove_directory(std::string_view path,
bool recursive = false) -> bool;
[[nodiscard]] auto remove_directory(std::wstring_view path,
bool recursive = false) -> bool;
#if defined(PROJECT_ENABLE_JSON)
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
[[nodiscard]] auto
@ -161,6 +425,21 @@ read_json_file(std::string_view path, nlohmann::json &data,
const nlohmann::json &data) -> bool;
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // defined(PROJECT_ENABLE_JSON)
template <typename string_t>
inline auto directory_exists_in_path_t(
std::basic_string_view<typename string_t::value_type> path,
std::basic_string_view<typename string_t::value_type> sub_directory)
-> bool {
return is_directory(utils::path::combine(path, {sub_directory}));
}
template <typename string_t>
inline auto file_exists_in_path_t(
std::basic_string_view<typename string_t::value_type> path,
std::basic_string_view<typename string_t::value_type> file_name) -> bool {
return is_file(utils::path::combine(path, {file_name}));
}
} // namespace repertory::utils::file
#endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_