Elements  6.0.1
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Path.tpp
Go to the documentation of this file.
1 
21 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_
22 #error "This file should not be included directly! Use ElementsKernel/Path.h instead"
23 #else
24 
25 #include <algorithm> // for find_if, transform, for_each
26 #include <string> // for string
27 #include <unordered_set> // for unordered_set
28 #include <vector> // for vector
29 
30 #include <boost/algorithm/string/join.hpp> // for join
31 #include <boost/filesystem/operations.hpp> // for exists
32 
33 namespace Elements {
34 inline namespace Kernel {
35 namespace Path {
36 
37 template <typename T, typename U>
38 Item getPathFromLocations(const T& file_name, const std::vector<U>& locations) {
39 
40  Item found_path{};
41  Item file_path{file_name};
42 
43  auto found_pos = std::find_if(locations.cbegin(), locations.cend(), [file_path](const U& l) {
44  return boost::filesystem::exists(Item{l} / file_path);
45  });
46 
47  if (found_pos != locations.cend()) {
48  found_path = Item{*found_pos} / file_path;
49  }
50 
51  return found_path;
52 }
53 
54 template <typename T, typename U>
55 std::vector<Item> getAllPathFromLocations(const T& file_name, const std::vector<U>& locations) {
56 
57  std::vector<Item> file_list(locations.size());
58  Item file_path{file_name};
59 
60  std::transform(locations.cbegin(), locations.cend(), file_list.begin(), [file_path](const U& l) {
61  return Item{l} / file_path;
62  });
63 
64  auto found_pos = std::remove_if(file_list.begin(), file_list.end(), [](const Item& p) {
65  return not boost::filesystem::exists(p);
66  });
67 
68  file_list.erase(found_pos, file_list.end());
69 
70  return removeDuplicates(file_list);
71 }
72 
73 template <typename T>
74 Item getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
75 
76  using std::vector;
77 
78  vector<Item> location_list = getLocationsFromEnv(path_variable);
79 
80  return getPathFromLocations(file_name, location_list);
81 }
82 
83 template <typename T>
84 std::string joinPath(const std::vector<T>& path_list) {
85 
86  using std::string;
87  using std::vector;
88 
89  vector<string> elems(path_list.size());
90 
91  std::transform(path_list.cbegin(), path_list.cend(), elems.begin(), [](const T& s) {
92  return Item{s}.string();
93  });
94 
96 
97  return result;
98 }
99 
100 template <typename... Args>
101 auto join(Args&&... args) -> decltype(joinPath(std::forward<Args>(args)...)) {
102  return joinPath(std::forward<Args>(args)...);
103 }
104 
105 template <typename... Args>
106 auto split(Args&&... args) -> decltype(splitPath(std::forward<Args>(args)...)) {
107  return splitPath(std::forward<Args>(args)...);
108 }
109 
110 template <typename T, typename U>
111 std::vector<Item> multiPathAppend(const std::vector<T>& initial_locations, const std::vector<U>& suffixes) {
112 
113  using std::vector;
114 
115  vector<Item> result(initial_locations.size() * suffixes.size());
116 
117  auto pos = result.begin();
118 
119  std::for_each(initial_locations.cbegin(), initial_locations.cend(), [&pos, &suffixes](const T& l) {
120  std::transform(suffixes.cbegin(), suffixes.cend(), pos, [l](const U& s) {
121  return Item{l} / s;
122  });
123  pos += static_cast<std::ptrdiff_t>(suffixes.size());
124  });
125 
126  return result;
127 }
128 
129 template <typename T>
131 
133 
134  std::vector<Item> output(path_list.size());
135 
136  auto end = copy_if(path_list.cbegin(), path_list.cend(), output.begin(), [&s](const T& i) {
137  return s.insert(Item{i}.string()).second;
138  });
139 
140  output.erase(end, output.end());
141 
142  return output;
143 }
144 
145 } // namespace Path
146 } // namespace Kernel
147 } // namespace Elements
148 
149 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_
T copy_if(T...args)
ELEMENTS_API auto split(Args &&...args) -> decltype(splitPath(std::forward< Args >(args)...))
alias for the splitPath function
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
T cend(T...args)
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
T remove_if(T...args)
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
ELEMENTS_API const std::string PATH_SEP
Separator of path entries. Usually &quot;:&quot; on Unix.
Definition: Path.cpp:44
STL class.
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:92
constexpr double s
boost::filesystem::path Item
Definition: Path.h:56
ELEMENTS_API std::string joinPath(const std::vector< T > &path_list)
collate a vector of path into a string using PATH_SEP
T insert(T...args)
T find_if(T...args)
T size(T...args)
STL class.
T cbegin(T...args)
ELEMENTS_API auto join(Args &&...args) -> decltype(joinPath(std::forward< Args >(args)...))
alias for the joinPath function
constexpr double second
ELEMENTS_API std::vector< Item > removeDuplicates(const std::vector< T > &path_list)
remove duplicated paths keeping the order
T transform(T...args)
T for_each(T...args)
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:70