180 lines
6.4 KiB
C++
180 lines
6.4 KiB
C++
/*
|
|
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#ifndef REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
|
#define REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
|
|
|
#include "utils/config.hpp"
|
|
|
|
namespace repertory::utils::file {
|
|
class file final {
|
|
public:
|
|
[[nodiscard]] static auto open_file(std::filesystem::path path,
|
|
bool read_only = false) -> file;
|
|
|
|
[[nodiscard]] static auto open_or_create_file(std::filesystem::path path,
|
|
bool read_only = false) -> file;
|
|
|
|
file() noexcept = default;
|
|
|
|
protected:
|
|
file(file_t file_ptr, std::filesystem::path path)
|
|
: file_(std::move(file_ptr)), path_(std::move(path)) {}
|
|
|
|
public:
|
|
file(const file &) = delete;
|
|
|
|
file(file &&move_file) noexcept
|
|
: file_(std::move(move_file.file_)),
|
|
path_(std::move(move_file.path_))
|
|
#if defined(_WIN32)
|
|
,
|
|
mtx_()
|
|
#endif // defined(_WIN32)
|
|
{
|
|
}
|
|
|
|
~file() { close(); }
|
|
|
|
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_);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
file_t file_{nullptr};
|
|
std::filesystem::path path_;
|
|
#if defined(_WIN32)
|
|
mutable std::recursive_mutex mtx_{};
|
|
#endif // defined(_WIN32)
|
|
|
|
public:
|
|
void close();
|
|
|
|
void flush();
|
|
|
|
[[nodiscard]] auto get_handle() const -> native_handle;
|
|
|
|
[[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; }
|
|
|
|
[[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool;
|
|
|
|
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool;
|
|
|
|
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
|
|
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;
|
|
|
|
[[nodiscard]] auto remove() -> bool;
|
|
|
|
[[nodiscard]] auto size() const -> std::uint64_t;
|
|
|
|
[[nodiscard]] auto truncate() -> bool { return truncate(0U); }
|
|
|
|
[[nodiscard]] auto truncate(std::size_t size) -> bool;
|
|
|
|
[[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool {
|
|
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
|
data.size() * sizeof(data_buffer::value_type), offset,
|
|
total_written);
|
|
}
|
|
|
|
[[nodiscard]] auto write(std::string_view data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool {
|
|
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
|
data.size(), offset, total_written);
|
|
}
|
|
|
|
[[nodiscard]] auto write(std::wstring_view data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool {
|
|
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
|
data.size() * sizeof(wchar_t), offset, total_written);
|
|
}
|
|
|
|
public:
|
|
[[nodiscard]] operator bool() const { return file_ != nullptr; }
|
|
|
|
private:
|
|
[[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write,
|
|
std::size_t offset,
|
|
std::size_t *total_written) -> 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 is_directory(std::string_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_file(std::string_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
|
|
|
|
#if defined(PROJECT_ENABLE_JSON)
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
[[nodiscard]] auto
|
|
read_json_file(std::string_view path, nlohmann::json &data,
|
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto read_json_file(
|
|
std::wstring_view path, nlohmann::json &data,
|
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(
|
|
std::string_view path, const nlohmann::json &data,
|
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(
|
|
std::wstring_view path, const nlohmann::json &data,
|
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
[[nodiscard]] auto read_json_file(std::string_view path,
|
|
nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto read_json_file(std::wstring_view path,
|
|
nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(std::string_view path,
|
|
const nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(std::wstring_view path,
|
|
const nlohmann::json &data) -> bool;
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
#endif // defined(PROJECT_ENABLE_JSON)
|
|
} // namespace repertory::utils::file
|
|
|
|
#endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_
|