From 78771f50f6c97101928e938b59ee6bef18ec62c1 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Thu, 5 Dec 2024 07:15:30 +0200 Subject: [PATCH] Added missing file --- src/details/os_filesystem.cpp | 73 +++++++++++++++++++++++++++++++++++ src/details/os_windows.cpp | 1 - 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/details/os_filesystem.cpp diff --git a/src/details/os_filesystem.cpp b/src/details/os_filesystem.cpp new file mode 100644 index 00000000..f811cb54 --- /dev/null +++ b/src/details/os_filesystem.cpp @@ -0,0 +1,73 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#include +#include + +#include "spdlog/common.h" +#include "spdlog/details/os.h" + +namespace spdlog { +namespace details { +namespace os { + +bool remove(const filename_t &filename) { + return std::filesystem::remove(filename); +} + +bool remove_if_exists(const filename_t &filename) { + if (path_exists(filename)) { + return os::remove(filename); + } + return false; +} + +// Rename if regular file +bool rename(const filename_t &filename1, const filename_t &filename2) noexcept { + if (!std::filesystem::is_regular_file(filename1)) { + return false; + } + std::error_code ec; + std::filesystem::rename(filename1, filename2, ec); + return !ec; +} + +// Return true if path exists (file or directory) +bool path_exists(const filename_t &filename) noexcept { return std::filesystem::exists(filename); } + +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc//" +filename_t dir_name(const filename_t &path) { return path.parent_path(); } + + +// Create the given directory - and all directories leading to it +// return true on success or if the directory already exists +bool create_dir(const filename_t &path) { + std::error_code ec; + return std::filesystem::create_directories(path, ec) || !ec; +} + + +// Return file path and its extension: +// +// "mylog.txt" => ("mylog", ".txt") +// "mylog" => ("mylog", "") +// "mylog." => ("mylog", ".") +// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") +// +// the starting dot in filenames is ignored (hidden files): +// +// ".mylog" => (".mylog". "") +// "my_folder/.mylog" => ("my_folder/.mylog", "") +// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") +std::tuple split_by_extension(const filename_t &fname) { + const auto ext = fname.extension(); + auto without_ext = filename_t(fname).replace_extension(); + return std::make_tuple(without_ext, ext); +} +} // namespace os +} // namespace details +} // namespace spdlog diff --git a/src/details/os_windows.cpp b/src/details/os_windows.cpp index b101fc87..c827a897 100644 --- a/src/details/os_windows.cpp +++ b/src/details/os_windows.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include "spdlog/common.h"