Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Path.tpp
Go to the documentation of this file.
1
21// IWYU pragma: private, include "ElementsKernel/Path.h"
22
23#ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_
24#error "This file should not be included directly! Use ElementsKernel/Path.h instead"
25#else
26
27#include <algorithm> // IWYU pragma: keep
28#include <iosfwd> // for ptrdiff_t
29#include <string> // for string
30#include <unordered_set> // for unordered_set
31#include <utility> // for forward, pair
32#include <vector> // for vector
33
34#include <boost/algorithm/string.hpp> // for join
35#include <boost/filesystem/operations.hpp> // for exists
36#include <boost/filesystem/path.hpp> // for operator/, path
37
38namespace Elements {
39inline namespace Kernel {
40namespace Path {
41
42template <typename T, typename U>
43Item getPathFromLocations(const T& file_name, const std::vector<U>& locations) {
44
45 Item found_path{};
46 Item file_path{file_name};
47
48 auto found_pos = std::find_if(locations.cbegin(), locations.cend(), [file_path](const U& l) {
49 return boost::filesystem::exists(Item{l} / file_path);
50 });
51
52 if (found_pos != locations.cend()) {
53 found_path = Item{*found_pos} / file_path;
54 }
55
56 return found_path;
57}
58
59template <typename T, typename U>
60std::vector<Item> getAllPathFromLocations(const T& file_name, const std::vector<U>& locations) {
61
62 std::vector<Item> file_list(locations.size());
63 Item file_path{file_name};
64
65 std::transform(locations.cbegin(), locations.cend(), file_list.begin(), [file_path](const U& l) {
66 return Item{l} / file_path;
67 });
68
69 auto found_pos = std::remove_if(file_list.begin(), file_list.end(), [](const Item& p) {
70 return not boost::filesystem::exists(p);
71 });
72
73 file_list.erase(found_pos, file_list.end());
74
75 return removeDuplicates(file_list);
76}
77
78template <typename T>
79Item getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
80
81 using std::vector;
82
83 vector<Item> location_list = getLocationsFromEnv(path_variable);
84
85 return getPathFromLocations(file_name, location_list);
86}
87
88template <typename T>
89std::string joinPath(const std::vector<T>& path_list) {
90
91 using std::string;
92 using std::vector;
93
94 vector<string> elems(path_list.size());
95
96 std::transform(path_list.cbegin(), path_list.cend(), elems.begin(), [](const T& s) {
97 return Item{s}.string();
98 });
99
100 string result = boost::algorithm::join(elems, PATH_SEP);
101
102 return result;
103}
104
105template <typename... Args>
106auto join(Args&&... args) -> decltype(joinPath(std::forward<Args>(args)...)) {
107 return joinPath(std::forward<Args>(args)...);
108}
109
110template <typename... Args>
111auto split(Args&&... args) -> decltype(splitPath(std::forward<Args>(args)...)) {
112 return splitPath(std::forward<Args>(args)...);
113}
114
115template <typename T, typename U>
116std::vector<Item> multiPathAppend(const std::vector<T>& initial_locations, const std::vector<U>& suffixes) {
117
118 using std::vector;
119
120 vector<Item> result(initial_locations.size() * suffixes.size());
121
122 auto pos = result.begin();
123
124 std::for_each(initial_locations.cbegin(), initial_locations.cend(), [&pos, &suffixes](const T& l) {
125 std::transform(suffixes.cbegin(), suffixes.cend(), pos, [l](const U& s) {
126 return Item{l} / s;
127 });
128 pos += static_cast<std::ptrdiff_t>(suffixes.size());
129 });
130
131 return result;
132}
133
134template <typename T>
136
138
139 std::vector<Item> output(path_list.size());
140
141 auto end = std::copy_if(path_list.cbegin(), path_list.cend(), output.begin(), [&s](const T& i) {
142 return s.insert(Item{i}.string()).second;
143 });
144
145 output.erase(end, output.end());
146
147 return output;
148}
149
150} // namespace Path
151} // namespace Kernel
152} // namespace Elements
153
154#endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_
T cbegin(T... args)
T copy_if(T... args)
T cend(T... args)
T find_if(T... args)
T for_each(T... args)
T forward(T... args)
ELEMENTS_API std::vector< Item > getAllPathFromLocations(const T &file_name, const std::vector< U > &locations)
retrieve all the paths from a file name and a set of location to look into
ELEMENTS_API std::vector< Item > getLocationsFromEnv(const std::string &path_variable, bool exist_only=false)
function to get the locations from an environment variable
Definition Path.cpp:82
ELEMENTS_API auto join(Args &&... args) -> decltype(joinPath(std::forward< Args >(args)...))
alias for the joinPath function
ELEMENTS_API std::vector< Item > removeDuplicates(const std::vector< T > &path_list)
remove duplicated paths keeping the order
ELEMENTS_API std::string joinPath(const std::vector< T > &path_list)
collate a vector of path into a string using PATH_SEP
ELEMENTS_API auto split(Args &&... args) -> decltype(splitPath(std::forward< Args >(args)...))
alias for the splitPath function
ELEMENTS_API Item getPathFromLocations(const T &file_name, const std::vector< U > &locations)
retrieve path from a file name and a set of location to look into
ELEMENTS_API std::vector< Item > splitPath(const std::string &path_string)
split a string into a vector of path using PATH_SEP
Definition Path.cpp:104
ELEMENTS_API Item getPathFromEnvVariable(const T &file_name, const std::string &path_variable)
retrieve path from a file name and an environment variable to look into
ELEMENTS_API std::vector< Item > multiPathAppend(const std::vector< T > &initial_locations, const std::vector< U > &suffixes)
path join each suffix to each initial locations
boost::filesystem::path Item
Definition Path.h:57
T remove_if(T... args)
T size(T... args)
T transform(T... args)