Line | Branch | Exec | Source |
---|---|---|---|
1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
2 | // SPDX-License-Identifier: MIT | ||
3 | /*! | ||
4 | * \file | ||
5 | * \ingroup Common | ||
6 | * \brief Helper functions for operations on paths. | ||
7 | */ | ||
8 | #ifndef GRIDFORMAT_COMMON_PATH_HPP_ | ||
9 | #define GRIDFORMAT_COMMON_PATH_HPP_ | ||
10 | |||
11 | #include <ranges> | ||
12 | #include <iterator> | ||
13 | #include <string_view> | ||
14 | #include <filesystem> | ||
15 | #include <algorithm> | ||
16 | |||
17 | namespace GridFormat::Path { | ||
18 | |||
19 | /*! | ||
20 | * \ingroup Common | ||
21 | * \brief Return a range over the elements of a path. | ||
22 | */ | ||
23 | 229404 | std::ranges::range auto elements_of(std::string_view path, char delimiter = '/') { | |
24 |
1/2✓ Branch 1 taken 229404 times.
✗ Branch 2 not taken.
|
229404 | return std::views::split(path, delimiter) |
25 |
1/2✓ Branch 1 taken 229404 times.
✗ Branch 2 not taken.
|
458808 | | std::views::transform([] (const std::ranges::range auto& element) { |
26 | 400597 | std::string name; | |
27 |
2/4✓ Branch 1 taken 400597 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 400597 times.
✗ Branch 5 not taken.
|
400597 | std::ranges::copy(element, std::back_inserter(name)); |
28 | 400597 | return name; | |
29 |
1/2✓ Branch 1 taken 229404 times.
✗ Branch 2 not taken.
|
458808 | }); |
30 | } | ||
31 | |||
32 | //! Return true if the given path exists. | ||
33 | 1935 | bool exists(const std::filesystem::path& path) { | |
34 | 1935 | return std::filesystem::exists(path); | |
35 | } | ||
36 | |||
37 | //! Return true if the given path is a file. | ||
38 | 1934 | bool is_file(const std::filesystem::path& path) { | |
39 |
2/2✓ Branch 1 taken 1933 times.
✓ Branch 2 taken 1 times.
|
1934 | if (std::filesystem::is_regular_file(path)) |
40 | 1933 | return true; | |
41 | 3 | if (std::filesystem::is_symlink(path) | |
42 |
4/16✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
1 | && std::filesystem::is_regular_file(std::filesystem::read_symlink(path))) |
43 | ✗ | return true; | |
44 | 1 | return false; | |
45 | } | ||
46 | |||
47 | } // end namespace GridFormat::Path | ||
48 | |||
49 | #endif // GRIDFORMAT_COMMON_PATH_HPP_ | ||
50 |