updated build system
This commit is contained in:
@ -27,184 +27,12 @@
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::utils::file {
|
||||
auto file::open_file(std::filesystem::path path) -> file {
|
||||
path = utils::path::absolute(path.string());
|
||||
if (not is_file(path.string())) {
|
||||
throw std::runtime_error("file not found: " + path.string());
|
||||
}
|
||||
|
||||
auto stream = std::fstream{
|
||||
path,
|
||||
std::ios_base::binary | std::ios_base::in | std::ios_base::out,
|
||||
};
|
||||
return {
|
||||
std::move(stream),
|
||||
path,
|
||||
};
|
||||
}
|
||||
|
||||
auto file::open_or_create_file(std::filesystem::path path) -> file {
|
||||
path = utils::path::absolute(path.string());
|
||||
auto stream = std::fstream{
|
||||
path.string().c_str(),
|
||||
std::ios_base::binary | std::ios_base::trunc | std::ios_base::in |
|
||||
std::ios_base::out,
|
||||
};
|
||||
|
||||
return {
|
||||
std::move(stream),
|
||||
path,
|
||||
};
|
||||
}
|
||||
|
||||
void file::close() {
|
||||
if (stream_.is_open()) {
|
||||
stream_.close();
|
||||
}
|
||||
}
|
||||
|
||||
auto file::move_to(std::filesystem::path new_path) -> bool {
|
||||
new_path = utils::path::absolute(new_path.string());
|
||||
|
||||
auto reopen{false};
|
||||
if (stream_.is_open()) {
|
||||
reopen = true;
|
||||
close();
|
||||
}
|
||||
|
||||
std::filesystem::rename(path_, new_path, error_);
|
||||
if (not error_) {
|
||||
path_ = new_path;
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto file::read_(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
if (total_read != nullptr) {
|
||||
(*total_read) = 0U;
|
||||
}
|
||||
|
||||
try {
|
||||
stream_.seekg(static_cast<std::streamoff>(offset));
|
||||
|
||||
auto before = stream_.tellg();
|
||||
if (before == -1) {
|
||||
throw std::runtime_error("failed to tellg() before read");
|
||||
}
|
||||
|
||||
stream_.read(reinterpret_cast<char *>(data),
|
||||
static_cast<std::streamoff>(to_read));
|
||||
if (total_read != nullptr) {
|
||||
auto after = stream_.tellg();
|
||||
if (after >= 0) {
|
||||
(*total_read) = static_cast<std::size_t>(after - before);
|
||||
} else if (after == -1 && not stream_.eof()) {
|
||||
throw std::runtime_error("failed to tellg() after read");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto file::remove() -> bool {
|
||||
close();
|
||||
return std::filesystem::remove(path_, error_);
|
||||
}
|
||||
|
||||
auto file::truncate(std::size_t size) -> bool {
|
||||
auto reopen{false};
|
||||
if (stream_.is_open()) {
|
||||
reopen = true;
|
||||
close();
|
||||
}
|
||||
|
||||
std::filesystem::resize_file(path_, size, error_);
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
}
|
||||
|
||||
return not error_;
|
||||
}
|
||||
|
||||
auto file::write_(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset, std::size_t *total_written) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
if (total_written != nullptr) {
|
||||
(*total_written) = 0U;
|
||||
}
|
||||
|
||||
try {
|
||||
stream_.seekp(static_cast<std::streamoff>(offset));
|
||||
auto before = stream_.tellp();
|
||||
if (before == -1) {
|
||||
throw std::runtime_error("failed to tellp() before write");
|
||||
}
|
||||
|
||||
stream_.write(reinterpret_cast<const char *>(data),
|
||||
static_cast<std::streamoff>(to_write));
|
||||
|
||||
auto after = stream_.tellp();
|
||||
if (after == -1) {
|
||||
throw std::runtime_error("failed to tellp() after write");
|
||||
}
|
||||
|
||||
if (total_written != nullptr) {
|
||||
(*total_written) = static_cast<std::size_t>(after - before);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
|
||||
file_size = 0U;
|
||||
|
||||
#if defined(_WIN32)
|
||||
struct _stat64 st {};
|
||||
if (_stat64(abs_path.c_str(), &st) != 0) {
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
if (stat(abs_path.c_str(), &st) != 0) {
|
||||
#endif // defined(_WIN32)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (st.st_size >= 0) {
|
||||
file_size = static_cast<std::uint64_t>(st.st_size);
|
||||
}
|
||||
|
||||
return (st.st_size >= 0);
|
||||
std::error_code ec{};
|
||||
file_size = std::filesystem::file_size(abs_path, ec);
|
||||
return ec.value() == 0;
|
||||
}
|
||||
|
||||
auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool {
|
||||
@ -255,44 +83,42 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
|
||||
|
||||
try {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
if (not is_file(abs_path)) {
|
||||
return true;
|
||||
auto file = file::open_file(abs_path);
|
||||
if (not file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream file_stream{
|
||||
abs_path.c_str(),
|
||||
std::ios_base::binary | std::ios::in,
|
||||
};
|
||||
if (not file_stream.is_open()) {
|
||||
throw std::runtime_error("failed to open file: " + abs_path);
|
||||
}
|
||||
|
||||
auto ret{true};
|
||||
try {
|
||||
std::stringstream stream;
|
||||
stream << file_stream.rdbuf();
|
||||
data_buffer buffer{};
|
||||
if (not file.read_all(buffer, 0U)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto json_text = stream.str();
|
||||
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
if (password.has_value()) {
|
||||
auto decrypted_data =
|
||||
utils::encryption::decrypt_data(*password, json_text);
|
||||
json_text = {decrypted_data.begin(), decrypted_data.end()};
|
||||
data_buffer decrypted_data{};
|
||||
if (not utils::encryption::decrypt_data(*password, buffer,
|
||||
decrypted_data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer = decrypted_data;
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
if (not json_text.empty()) {
|
||||
data = nlohmann::json::parse(json_text.c_str());
|
||||
|
||||
std::string json_str(buffer.begin(), buffer.end());
|
||||
if (not json_str.empty()) {
|
||||
data = nlohmann::json::parse(json_str);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
ret = false;
|
||||
return false;
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
ret = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
file_stream.close();
|
||||
return ret;
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
@ -316,18 +142,22 @@ auto write_json_file(std::string_view path,
|
||||
try {
|
||||
auto file = file::open_or_create_file(path);
|
||||
if (not file.truncate()) {
|
||||
throw std::runtime_error("failed to truncate file: " +
|
||||
file.get_error_code().message());
|
||||
throw std::runtime_error("failed to truncate file");
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
if (password.has_value()) {
|
||||
return file.write(utils::encryption::encrypt_data(*password, data.dump()),
|
||||
0U);
|
||||
const auto str_data = data.dump();
|
||||
|
||||
data_buffer encrypted_data{};
|
||||
utils::encryption::encrypt_data(
|
||||
*password, reinterpret_cast<const unsigned char *>(str_data.c_str()),
|
||||
str_data.size(), encrypted_data);
|
||||
return file.write(encrypted_data, 0U);
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
|
||||
return file.write(data, 0U);
|
||||
return file.write(data.dump(), 0U);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
|
Reference in New Issue
Block a user