Elements  5.12.0
A C++ base framework for the Euclid Software.
Path.cpp
Go to the documentation of this file.
1 
23 #include "ElementsKernel/Path.h"
24 
25 #include <string> // for string
26 #include <vector> // for vector
27 #include <algorithm> // for transform, remove_if
28 #include <map> // for map
29 
30 #include <boost/filesystem.hpp> // for boost::filesystem
31 #include <boost/algorithm/string.hpp> // for boost::split
32 
33 #include "ElementsKernel/System.h" // for getEnv, SHLIB_VAR_NAME
34 
35 using std::string;
36 using std::vector;
37 using std::map;
38 
39 namespace Elements {
40 namespace Path {
41 
42 const string PATH_SEP {":"};
43 
45  {Type::executable, "PATH"},
47  {Type::python, "PYTHONPATH"},
48  {Type::configuration, "ELEMENTS_CONF_PATH"},
49  {Type::auxiliary, "ELEMENTS_AUX_PATH"}
50 };
51 
53  {Type::executable, {"scripts", "bin"}},
54  {Type::library, {"lib"}},
55  {Type::python, {"python"}},
56  {Type::configuration, {"conf", "share/conf"}},
57  {Type::auxiliary, {"auxdir", "aux", "share/auxdir", "share/aux"}}
58 };
59 
61  {Type::executable, {}},
62  {Type::library, {"/usr/lib64", "/usr/lib"}},
63  {Type::python, {}},
64  {Type::configuration, {"/usr/share/conf"}},
65  {Type::auxiliary, {"/usr/share/auxdir", "/usr/share/aux"}}
66 };
67 
69  {Type::executable, false},
70  {Type::library, false},
71  {Type::python, true},
72  {Type::configuration, true},
73  {Type::auxiliary, true}
74 };
75 
76 vector<Item> getLocationsFromEnv(const string& path_variable, bool exist_only) {
77 
78  using System::getEnv;
79 
80  string env_content = getEnv(path_variable);
81 
82  vector<Item> found_list = split(env_content);
83 
84  if (exist_only) {
85  auto new_end = std::remove_if(found_list.begin(),
86  found_list.end(),
87  [](const Item& p){
88  return (not boost::filesystem::exists(p));
89  });
90  found_list.erase(new_end, found_list.end());
91  }
92 
93  return found_list;
94 }
95 
96 vector<Item> getLocations(const Type& path_type, bool exist_only) {
97  return getLocationsFromEnv(VARIABLE.at(path_type), exist_only);
98 }
99 
100 
101 vector<Item> splitPath(const string& path_string) {
102 
103  vector<string> str_list;
104  boost::split(str_list, path_string, boost::is_any_of(PATH_SEP));
105 
106  vector<Item> found_list(str_list.size());
107  std::transform(str_list.cbegin(), str_list.cend(),
108  found_list.begin(),
109  [](const string& s){
110  return Item{s};
111  });
112 
113  return found_list;
114 }
115 
116 // Template instantiation for the most common types
117 template Item getPathFromLocations(const Item& file_name, const vector<Item>& locations);
118 template Item getPathFromLocations(const Item& file_name, const vector<string>& locations);
119 template Item getPathFromLocations(const string& file_name, const vector<Item>& locations);
120 template Item getPathFromLocations(const string& file_name, const vector<string>& locations);
121 
122 template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<Item>& locations);
123 template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<string>& locations);
124 template vector<Item> getAllPathFromLocations(const string& file_name, const vector<Item>& locations);
125 template vector<Item> getAllPathFromLocations(const string& file_name, const vector<string>& locations);
126 
127 template Item getPathFromEnvVariable<Item>(const Item& file_name, const string& path_variable);
128 template Item getPathFromEnvVariable<string>(const string& file_name, const string& path_variable);
129 
130 template string joinPath(const vector<Item>& path_list);
131 template string joinPath(const vector<string>& path_list);
132 
133 template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<Item>& suffixes);
134 template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<string>& suffixes);
135 template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<Item>& suffixes);
136 template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<string>& suffixes);
137 
138 template vector<Item> removeDuplicates(const vector<Item>& path_list);
139 template vector<Item> removeDuplicates(const vector<string>& path_list);
140 
141 } // namespace Path
142 } // namespace Elements
provide functions to retrieve resources pointed by environment variables
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
T begin(T... args)
T end(T... args)
T erase(T... args)
ELEMENTS_API const std::map< Type, const std::string > VARIABLE
map containing the name of the path variable for each type
Definition: Path.cpp:44
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:101
ELEMENTS_API std::vector< Item > getLocations(const Type &path_type, bool exist_only=false)
function to get the locations for the specific type
Definition: Path.cpp:96
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 auto split(Args &&... args) -> decltype(splitPath(std::forward< Args >(args)...))
alias for the splitPath function
ELEMENTS_API const std::map< Type, const std::vector< std::string > > DEFAULT_LOCATIONS
map containing the default external locations for each variable
Definition: Path.cpp:60
ELEMENTS_API std::string joinPath(const std::vector< T > &path_list)
collate a vector of path into a string using PATH_SEP
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::map< Type, const bool > HAS_SUBLEVELS
map containing the sub-level property of the path components
Definition: Path.cpp:68
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 const std::string PATH_SEP
Separator of path entries. Usually ":" on Unix.
Definition: Path.cpp:42
ELEMENTS_API const std::map< Type, const std::vector< std::string > > SUFFIXES
map containing the default project installation suffixes for each variable
Definition: Path.cpp:52
ELEMENTS_API std::vector< Item > removeDuplicates(const std::vector< T > &path_list)
remove duplicated paths keeping the order
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:76
template ELEMENTS_API Item getPathFromEnvVariable< Item >(const Item &file_name, const std::string &path_variable)
boost::filesystem::path Item
Definition: Path.h:61
template Item getPathFromEnvVariable< string >(const string &file_name, const string &path_variable)
const std::string SHLIB_VAR_NAME
name of the shared dynamic library path
Definition: System.h:58
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
Definition: System.cpp:331
constexpr double s
T remove_if(T... args)
T size(T... args)
T transform(T... args)