updated build system

This commit is contained in:
2024-08-22 14:35:56 -05:00
parent 1236bf1829
commit 497b424840
36 changed files with 823 additions and 612 deletions

View File

@ -26,56 +26,6 @@
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace {
[[nodiscard]] auto remove_directory_recursively(std::string_view path) -> bool {
#if defined(_WIN32)
WIN32_FIND_DATAA fd{};
auto search = repertory::utils::path::combine(path, {"*.*"});
auto find = ::FindFirstFileA(search.c_str(), &fd);
if (find != INVALID_HANDLE_VALUE) {
auto res{true};
do {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if ((std::string(fd.cFileName) != ".") &&
(std::string(fd.cFileName) != "..")) {
res = remove_directory_recursively(
repertory::utils::path::combine(path, {fd.cFileName}));
}
} else {
res = repertory::utils::file::file(
repertory::utils::path::combine(path, {fd.cFileName}))
.remove();
}
} while (res && (::FindNextFileA(find, &fd) != 0));
::FindClose(find);
}
#else
auto *root = opendir(std::string{path}.c_str());
if (root != nullptr) {
auto res{true};
struct dirent *de{};
while (res && (de = readdir(root))) {
if (de->d_type == DT_DIR) {
if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
res = remove_directory_recursively(
repertory::utils::path::combine(path, {de->d_name}));
}
} else {
res = repertory::utils::file::file(
repertory::utils::path::combine(path, {de->d_name}))
.remove();
}
}
closedir(root);
}
#endif
return repertory::utils::file::remove_directory(path, false);
}
} // namespace
namespace repertory::utils::file {
auto i_file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
@ -107,131 +57,64 @@ auto i_file::read_all(data_buffer &data, std::uint64_t offset,
return false;
}
auto create_directories(std::string_view path) -> bool {
if (is_directory(path)) {
return true;
}
auto i_fs_item::get_time(time_types type) const -> std::uint64_t {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
return (::SHCreateDirectory(
nullptr,
utils::string::from_utf8(utils::path::absolute(path)).c_str()) ==
ERROR_SUCCESS);
#else // !defined(_WIN32)
auto ret{true};
auto paths = utils::string::split(utils::path::absolute(path),
utils::path::directory_seperator, false);
struct _stat64 st {};
_stat64(get_path().c_str(), &st);
#else // !defined(_WIN32)
struct stat st {};
stat(get_path().c_str(), &st);
#endif // defined(_WIN32)
std::string current_path;
for (std::size_t idx = 0U; ret && (idx < paths.size()); idx++) {
if (paths.at(idx).empty()) {
current_path = utils::path::directory_seperator;
continue;
switch (type) {
case time_types::access:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_atime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_atim.tv_nsec +
st.st_atim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::creation:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_ctime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_ctim.tv_nsec +
st.st_ctim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::modified:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::write:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
}
current_path = utils::path::combine(current_path, {paths.at(idx)});
auto status = mkdir(current_path.c_str(), S_IRWXU);
ret = ((status == 0) || (errno == EEXIST));
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return ret;
#endif
}
auto create_directories(std::wstring_view path) -> bool {
return create_directories(utils::string::to_utf8(path));
}
auto directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool {
return directory_exists_in_path_t<std::string>(path, sub_directory);
}
auto directory_exists_in_path(std::wstring_view path,
std::wstring_view sub_directory) -> bool {
return directory_exists_in_path_t<std::wstring>(path, sub_directory);
}
auto file_exists_in_path(std::string_view path,
std::string_view file_name) -> bool {
return file_exists_in_path_t<std::string>(path, file_name);
}
auto file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool {
return file_exists_in_path_t<std::wstring>(path, file_name);
}
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 {};
auto res = _stat64(std::string{path}.c_str(), &st);
if (res != 0) {
return false;
}
file_size = static_cast<std::uint64_t>(st.st_size);
return true;
#else // !defined(_WIN32)
std::error_code ec{};
file_size = std::filesystem::file_size(abs_path, ec);
return (ec.value() == 0);
#endif // defined(_WIN32)
}
auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool {
return get_file_size(utils::string::to_utf8(path), file_size);
}
auto is_directory(std::string_view path) -> bool {
auto abs_path = utils::path::absolute(path);
#if defined(_WIN32)
return ::PathIsDirectoryA(abs_path.c_str()) != 0;
#else // !defined(_WIN32)
struct stat st {};
return (stat(abs_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
#endif // defined(_WIN32)
}
auto is_directory(std::wstring_view path) -> bool {
return is_directory(utils::string::to_utf8(path));
}
auto is_file(std::string_view path) -> bool {
auto abs_path = utils::path::absolute(path);
#if defined(_WIN32)
return (::PathFileExistsA(abs_path.c_str()) &&
not ::PathIsDirectoryA(abs_path.c_str()));
#else // !defined(_WIN32)
struct stat st {};
return (stat(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode));
#endif // defined(_WIN32)
}
auto is_file(std::wstring_view path) -> bool {
return is_file(utils::string::to_utf8(path));
}
auto remove_directory(std::string_view path, bool recursive) -> bool {
auto abs_path = utils::path::absolute(path);
if (recursive) {
return remove_directory_recursively(abs_path);
}
#if defined(_WIN32)
return (not is_directory(abs_path) || ::RemoveDirectoryA(abs_path.c_str()));
#else // !defined(_WIN32)
return not is_directory(abs_path) || (rmdir(abs_path.c_str()) == 0);
#endif // defined(_WIN32)
}
auto remove_directory(std::wstring_view path, bool recursive) -> bool {
return remove_directory(utils::string::to_utf8(path), recursive);
return false;
}
#if defined(PROJECT_ENABLE_JSON)