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