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