Elements  5.8
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 <set> // for 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](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](U l){
68  return path {l} / file_path;
69  });
70 
71  auto found_pos = std::remove_if(file_list.begin(), file_list.end(),
72  [](path p){
73  return not boost::filesystem::exists(p);
74  });
75 
76  file_list.erase(found_pos, file_list.end());
77 
78  std::set<path> file_set(file_list.begin(), file_list.end());
79 
80  return std::vector<path>(file_set.begin(), file_set.end());
81 
82 }
83 
84 template <typename T>
85 boost::filesystem::path getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
86 
87  using std::vector;
89 
90  vector<path> location_list = getLocationsFromEnv(path_variable);
91 
92  return getPathFromLocations(file_name, location_list);
93 
94 }
95 
96 template <typename T>
98 
99  using std::vector;
100  using std::string;
101 
102  vector<string> elems(path_list.size());
103 
104  std::transform(path_list.cbegin(), path_list.cend(),
105  elems.begin(),
106  [](T s){
107  return boost::filesystem::path(s).string();
108  });
109 
110  std::string result = boost::algorithm::join(elems, PATH_SEP);
111 
112  return result;
113 }
114 
115 template <typename T, typename U>
117 
118  using std::vector;
120 
121  vector<path> result(initial_locations.size()*suffixes.size());
122 
123  auto pos = result.begin();
124 
125  std::for_each(initial_locations.cbegin(), initial_locations.cend(),
126  [&pos, &suffixes](T l) {
127  std::transform(suffixes.cbegin(), suffixes.cend(),
128  pos,
129  [l](U s){
130  return path(l) / s;
131  });
132  pos += suffixes.size();
133 
134  });
135 
136 
137  return result;
138 }
139 
140 } // namespace Path
141 } // namespace Elements
142 
143 #endif // ELEMENTSKERNEL__IMPL_PATH_ICPP_
constexpr double s
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:85
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
T cend(T...args)
T remove_if(T...args)
STL class.
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
boost::filesystem::path path
Definition: DataSyncUtils.h:33
std::vector< boost::filesystem::path > multiPathAppend(const std::vector< T > &initial_locations, const std::vector< U > &suffixes)
Definition: Path.icpp:116
T find_if(T...args)
T size(T...args)
STL class.
STL class.
std::string joinPath(const std::vector< T > path_list)
collate a vector of path into a string using PATH_SEP
Definition: Path.icpp:97
T cbegin(T...args)
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
T transform(T...args)
T for_each(T...args)
ELEMENTS_API const std::string PATH_SEP
Separator of path entries. Usually &quot;:&quot; on Unix.
Definition: Path.cpp:44