CommonLib
Toggle main menu visibility
Loading...
Searching...
No Matches
filesystem.inl
Go to the documentation of this file.
1
#
include
<
filesystem
>
2
3
#
include
"ut/string.h"
4
5
namespace
hedgedev::csl::ut::
filesystem
6
{
7
inline
bool
is_neighbour
(
const
std::filesystem::path& in_left,
const
std::filesystem::path& in_right)
8
{
9
return
string
::compare(in_left.parent_path().c_str(), in_right.parent_path().c_str(),
false
);
10
}
11
12
template
<
typename
T>
13
inline
std::error_code truncate_files(
const
std::filesystem::path& in_path, std::string_view in_extension, size_t in_max, T&& in_compare_func)
14
{
15
std::error_code result{};
16
17
if
(!std::filesystem::exists(in_path, result) || !std::filesystem::is_directory(in_path, result))
18
return
result;
19
20
std::vector<std::filesystem::directory_entry> files{};
21
22
for
(
const
auto
& entry : std::filesystem::directory_iterator(in_path))
23
{
24
if
(!entry.is_regular_file(result))
25
continue
;
26
27
if
(entry.path().extension() != in_extension)
28
continue
;
29
30
files.push_back(entry);
31
}
32
33
if
(files.size() <= in_max)
34
return
result;
35
36
std::sort(files.begin(), files.end(), in_compare_func);
37
38
auto
truncate_length = files.size() - in_max;
39
40
for
(size_t i = 0; i < truncate_length; i++)
41
std::filesystem::remove(files[i].path(), result);
42
43
return
result;
44
}
45
46
inline
std::error_code
truncate_files_by_age
(
const
std::filesystem::path& in_path, std::string_view in_extension, size_t in_max)
47
{
48
std::error_code result{};
49
50
// Sort files by oldest to newest.
51
return
truncate_files(in_path, in_extension, in_max, [&](
const
auto
& in_left,
const
auto
& in_right) ->
bool
52
{
53
return
std::filesystem::last_write_time(in_left, result) < std::filesystem::last_write_time(in_right, result);
54
});
55
}
56
}
hedgedev::csl::ut::filesystem
Definition
filesystem.h:6
hedgedev::csl::ut::filesystem::truncate_files_by_age
std::error_code truncate_files_by_age(const std::filesystem::path &in_path, std::string_view in_extension, size_t in_max=10)
Definition
filesystem.inl:46
hedgedev::csl::ut::filesystem::is_neighbour
bool is_neighbour(const std::filesystem::path &in_left, const std::filesystem::path &in_right)
Definition
filesystem.inl:7
hedgedev::csl::ut::string
Definition
string.h:11
include
ut
filesystem.inl
Generated by
1.18.0