Elements  5.10
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Path.icpp
Go to the documentation of this file.
1 
21 #ifndef ELEMENTSKERNEL__IMPL_PATH_ICPP_
22 #define ELEMENTSKERNEL__IMPL_PATH_ICPP_
23 
24 #include <string> // for string
25 #include <vector> // for vector
26 #include <unordered_set> // for unordered_set
27 #include <algorithm> // for find_if, transform, for_each
28 
29 #include <boost/filesystem.hpp> // for boost::filesystem
30 #include <boost/algorithm/string/join.hpp> // for join
31 
32 
33 namespace Elements {
34 namespace Path {
35 
36 template <typename T, typename U>
37 boost::filesystem::path getPathFromLocations(const T& file_name, const std::vector<U>& locations) {
38 
40 
41  path found_path {};
42  path file_path {file_name};
43 
44  auto found_pos = std::find_if(locations.cbegin(), locations.cend(),
45  [file_path](const U& l) {
46  return boost::filesystem::exists(path {l} / file_path);
47  });
48 
49  if (found_pos != locations.cend()) {
50  found_path = path {*found_pos} / file_path;
51  }
52 
53  return found_path;
54 
55 }
56 
57 template <typename T, typename U>
59 
61 
62  std::vector<path> file_list(locations.size());
63  path file_path {file_name};
64 
65  std::transform(locations.cbegin(), locations.cend(),
66  file_list.begin(),
67  [file_path](const U& l){
68  return path {l} / file_path;
69  });
70 
71  auto found_pos = std::remove_if(file_list.begin(), file_list.end(),
72  [](const path& p){
73  return not boost::filesystem::exists(p);
74  });
75 
76  file_list.erase(found_pos, file_list.end());
77 
78  return removeDuplicates(file_list);
79 
80 }
81 
82 template <typename T>
83 boost::filesystem::path getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
84 
85  using std::vector;
87 
88  vector<path> location_list = getLocationsFromEnv(path_variable);
89 
90  return getPathFromLocations(file_name, location_list);
91 
92 }
93 
94 template <typename T>
95 std::string joinPath(const std::vector<T>& path_list) {
96 
97  using std::vector;
98  using std::string;
99 
100  vector<string> elems(path_list.size());
101 
102  std::transform(path_list.cbegin(), path_list.cend(),
103  elems.begin(),
104  [](const T& s){
105  return boost::filesystem::path{s}.string();
106  });
107 
109 
110  return result;
111 }
112 
113 template <typename... Args>
114 auto join(Args&&... args) -> decltype(joinPath(std::forward<Args>(args)...)) {
115  return joinPath(std::forward<Args>(args)...);
116 }
117 
118 template <typename... Args>
119 auto split(Args&&... args) -> decltype(splitPath(std::forward<Args>(args)...)) {
120  return splitPath(std::forward<Args>(args)...);
121 }
122 
123 
124 template <typename T, typename U>
126 
127  using std::vector;
129 
130  vector<path> result(initial_locations.size()*suffixes.size());
131 
132  auto pos = result.begin();
133 
134  std::for_each(initial_locations.cbegin(), initial_locations.cend(),
135  [&pos, &suffixes](const T& l) {
136  std::transform(suffixes.cbegin(), suffixes.cend(),
137  pos,
138  [l](const U& s){
139  return path {l} / s;
140  });
141  pos += static_cast<std::ptrdiff_t>(suffixes.size());
142 
143  });
144 
145 
146  return result;
147 }
148 
149 template <typename T>
151 
153 
155 
156  std::vector<path> output(path_list.size());
157 
158  auto end = copy_if(path_list.cbegin(), path_list.cend(), output.begin(),
159  [&s](const T& i) {
160  return s.insert(path{i}.string()).second;
161  });
162 
163  output.erase(end, output.end());
164 
165  return output;
166 
167 }
168 
169 
170 } // namespace Path
171 } // namespace Elements
172 
173 #endif // ELEMENTSKERNEL__IMPL_PATH_ICPP_
boost::filesystem::path getPathFromEnvVariable(const T &file_name, const std::string &path_variable)
retrieve path from a file name and an environment variable to look into
Definition: Path.icpp:83
auto split(Args &&...args) -> decltype(splitPath(std::forward< Args >(args)...))
alias for the splitPath function
Definition: Path.icpp:119
boost::filesystem::path getPathFromLocations(const T &file_name, const std::vector< U > &locations)
retrieve path from a file name and a set of location to look into
Definition: Path.icpp:37
constexpr double second
constexpr double s
T cend(T...args)
T remove_if(T...args)
STL class.
std::vector< boost::filesystem::path > multiPathAppend(const std::vector< T > &initial_locations, const std::vector< U > &suffixes)
path join each suffix to each initial locations
Definition: Path.icpp:125
ELEMENTS_API const std::string PATH_SEP
Separator of path entries. Usually &quot;:&quot; on Unix.
Definition: Path.cpp:44
ELEMENTS_API std::vector< boost::filesystem::path > splitPath(const std::string &path_string)
split a string into a vector of path using PATH_SEP
Definition: Path.cpp:99
boost::filesystem::path path
Definition: DataSyncUtils.h:38
T insert(T...args)
T find_if(T...args)
T size(T...args)
STL class.
T cbegin(T...args)
std::string joinPath(const std::vector< T > &path_list)
collate a vector of path into a string using PATH_SEP
Definition: Path.icpp:95
std::vector< boost::filesystem::path > 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
Definition: Path.icpp:58
T transform(T...args)
auto join(Args &&...args) -> decltype(joinPath(std::forward< Args >(args)...))
alias for the joinPath function
Definition: Path.icpp:114
T for_each(T...args)
ELEMENTS_API std::vector< boost::filesystem::path > getLocationsFromEnv(const std::string &path_variable, bool exist_only=false)
function to get the locations from an environment variable
Definition: Path.cpp:79
std::vector< boost::filesystem::path > removeDuplicates(const std::vector< T > &path_list)
remove duplicated paths keeping the order
Definition: Path.icpp:150