Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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"}},
59  {Type::auxiliary, {"auxdir", "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/auxiliary"}}
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<string> str_list;
86  boost::split(str_list, env_content, boost::is_any_of(PATH_SEP));
87 
88  vector<path> found_list(str_list.size());
89  std::transform(str_list.cbegin(), str_list.cend(),
90  found_list.begin(),
91  [](string s){
92  return path{s};
93  });
94 
95  if (exist_only) {
96  auto new_end = std::remove_if(found_list.begin(),
97  found_list.end(),
98  [](path p){
99  return boost::filesystem::exists(p);
100  });
101  found_list.erase(new_end, found_list.end());
102  }
103 
104  return found_list;
105 }
106 
107 
108 // Template instantiation for the most common types
109 template path getPathFromLocations(const path& file_name, const vector<path>& locations);
110 template path getPathFromLocations(const path& file_name, const vector<string>& locations);
111 template path getPathFromLocations(const string& file_name, const vector<path>& locations);
112 template path getPathFromLocations(const string& file_name, const vector<string>& locations);
113 
114 template vector<path> getAllPathFromLocations(const path& file_name, const vector<path>& locations);
115 template vector<path> getAllPathFromLocations(const path& file_name, const vector<string>& locations);
116 template vector<path> getAllPathFromLocations(const string& file_name, const vector<path>& locations);
117 template vector<path> getAllPathFromLocations(const string& file_name, const vector<string>& locations);
118 
119 template path getPathFromEnvVariable<path>(const path& file_name, const string& path_variable);
120 template path getPathFromEnvVariable<string>(const string& file_name, const string& path_variable);
121 
122 template vector<path> multiPathAppend(const vector<path>& initial_locations, const vector<path>& suffixes);
123 template vector<path> multiPathAppend(const vector<path>& initial_locations, const vector<string>& suffixes);
124 template vector<path> multiPathAppend(const vector<string>& initial_locations, const vector<path>& suffixes);
125 template vector<path> multiPathAppend(const vector<string>& initial_locations, const vector<string>& suffixes);
126 
127 
128 } // namespace Path
129 } // namespace Elements
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
constexpr double s
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)
template path getPathFromEnvVariable< string >(const string &file_name, const string &path_variable)
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
T remove_if(T...args)
ELEMENTS_API const std::map< Type, const bool > HAS_SUBLEVELS
map containing the sub-level property of the path components
Definition: Path.cpp:70
STL class.
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
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
Definition: System.cpp:331
This file is intended to iron out all the differences between systems (currently Linux and MacOSX) ...
boost::filesystem::path path
Definition: DataSyncUtils.h:33
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
std::vector< boost::filesystem::path > multiPathAppend(const std::vector< T > &initial_locations, const std::vector< U > &suffixes)
Definition: Path.icpp:116
T size(T...args)
const std::string SHLIB_VAR_NAME
name of the shared dynamic library path
Definition: System.h:58
template path getPathFromEnvVariable< path >(const path &file_name, const string &path_variable)
STL class.
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
provide functions to retrieve resources pointed by environment variables
T transform(T...args)
ELEMENTS_API const std::string PATH_SEP
Separator of path entries. Usually &quot;:&quot; on Unix.
Definition: Path.cpp:44