Elements  5.8
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"}},
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::Path::Type::configuration
@ configuration
System.h
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
Elements::System::getEnv
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
Definition: System.cpp:331
Elements::Path::HAS_SUBLEVELS
const ELEMENTS_API std::map< Type, const bool > HAS_SUBLEVELS
map containing the sub-level property of the path components
Definition: Path.cpp:70
std::string
STL class.
Path.h
provide functions to retrieve resources pointed by environment variables
std::vector
STL class.
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:33
Elements::Path::Type::auxiliary
@ auxiliary
Elements::Path::getAllPathFromLocations
template vector< path > getAllPathFromLocations(const string &file_name, const vector< path > &locations)
Elements::Path::PATH_SEP
const ELEMENTS_API std::string PATH_SEP
Separator of path entries. Usually ":" on Unix.
Definition: Path.cpp:44
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::multiPathAppend
template vector< path > multiPathAppend(const vector< string > &initial_locations, const vector< path > &suffixes)
Elements::Path::SUFFIXES
const ELEMENTS_API std::map< Type, const std::vector< std::string > > SUFFIXES
map containing the default project installation suffixes for each variable
Definition: Path.cpp:54
Elements::Path::Type::library
@ library
std::remove_if
T remove_if(T... args)
std::map
STL class.
std::transform
T transform(T... args)
Elements::Path::VARIABLE
const ELEMENTS_API std::map< Type, const std::string > VARIABLE
map containing the name of the path variable for each type
Definition: Path.cpp:46
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::DEFAULT_LOCATIONS
const ELEMENTS_API std::map< Type, const std::vector< std::string > > DEFAULT_LOCATIONS
map containing the default external locations for each variable
Definition: Path.cpp:62
std::vector::cbegin
T cbegin(T... args)
Elements::Units::s
constexpr double s
Definition: SystemOfUnits.h:121
Elements::Path::Type::python
@ python
std::vector::cend
T cend(T... args)
Elements::Path::getPathFromEnvVariable< string >
template path getPathFromEnvVariable< string >(const string &file_name, const string &path_variable)
Elements
Definition: Auxiliary.h:43