Elements 6.3.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Path.cpp
Go to the documentation of this file.
1
22
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/operations.hpp> // for exists
32
33#include "ElementsKernel/Environment.h" // for the Environment class
34#include "ElementsKernel/System.h" // for getEnv, SHLIB_VAR_NAME
35
36using std::map;
37using std::string;
38using std::vector;
39
40namespace Elements {
41inline namespace Kernel {
42namespace Path {
43
44const string PATH_SEP{":"};
45
46// clang-format off
47
48const map<Type, const string> VARIABLE{
49 {Type::executable, "PATH"},
51 {Type::python, "PYTHONPATH"},
52 {Type::configuration, "ELEMENTS_CONF_PATH"},
53 {Type::auxiliary, "ELEMENTS_AUX_PATH"}
54};
55
56const map<Type, const vector<string>> SUFFIXES{
57 {Type::executable, {"scripts", "bin"}},
58 {Type::library, {"lib"}},
59 {Type::python, {"python"}},
60 {Type::configuration, {"conf", "share/conf"}},
61 {Type::auxiliary, {"auxdir", "aux", "share/auxdir", "share/aux"}}
62};
63
64const map<Type, const vector<string>> DEFAULT_LOCATIONS{
65 {Type::executable, {}},
66 {Type::library, {"/usr/lib64", "/usr/lib"}},
67 {Type::python, {}},
68 {Type::configuration, {"/usr/share/conf"}},
69 {Type::auxiliary, {"/usr/share/auxdir", "/usr/share/aux"}}
70};
71
72const std::map<Type, const bool> HAS_SUBLEVELS{
73 {Type::executable, false},
74 {Type::library, false},
75 {Type::python, true},
76 {Type::configuration, true},
77 {Type::auxiliary, true}
78};
79
80// clang-format on
81
82vector<Item> getLocationsFromEnv(const string& path_variable, bool exist_only) {
83
84 Environment current_env;
85
86 string env_content = current_env[path_variable];
87
88 vector<Item> found_list = split(env_content);
89
90 if (exist_only) {
91 auto new_end = std::remove_if(found_list.begin(), found_list.end(), [](const Item& p) {
92 return (not boost::filesystem::exists(p));
93 });
94 found_list.erase(new_end, found_list.end());
95 }
96
97 return found_list;
98}
99
100vector<Item> getLocations(const Type& path_type, bool exist_only) {
101 return getLocationsFromEnv(VARIABLE.at(path_type), exist_only);
102}
103
104vector<Item> splitPath(const string& path_string) {
105
106 vector<string> str_list;
107 boost::split(str_list, path_string, boost::is_any_of(PATH_SEP));
108
109 vector<Item> found_list(str_list.size());
110 std::transform(str_list.cbegin(), str_list.cend(), found_list.begin(), [](const string& s) {
111 return Item{s};
112 });
113
114 return found_list;
115}
116
117// Template instantiation for the most common types
118template Item getPathFromLocations(const Item& file_name, const vector<Item>& locations);
119template Item getPathFromLocations(const Item& file_name, const vector<string>& locations);
120template Item getPathFromLocations(const string& file_name, const vector<Item>& locations);
121template Item getPathFromLocations(const string& file_name, const vector<string>& locations);
122
123template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<Item>& locations);
124template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<string>& locations);
125template vector<Item> getAllPathFromLocations(const string& file_name, const vector<Item>& locations);
126template vector<Item> getAllPathFromLocations(const string& file_name, const vector<string>& locations);
127
128template Item getPathFromEnvVariable<Item>(const Item& file_name, const string& path_variable);
129template Item getPathFromEnvVariable<string>(const string& file_name, const string& path_variable);
130
131template string joinPath(const vector<Item>& path_list);
132template string joinPath(const vector<string>& path_list);
133
134template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<Item>& suffixes);
135template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<string>& suffixes);
136template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<Item>& suffixes);
137template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<string>& suffixes);
138
139template vector<Item> removeDuplicates(const vector<Item>& path_list);
140template vector<Item> removeDuplicates(const vector<string>& path_list);
141
142} // namespace Path
143} // namespace Kernel
144} // namespace Elements
Defines a class to handle the Environment.
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)
Python dictionary-like Environment interface.
Definition Environment.h:44
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:56
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:64
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:82
ELEMENTS_API std::vector< Item > removeDuplicates(const std::vector< T > &path_list)
remove duplicated paths keeping the order
ELEMENTS_API std::string joinPath(const std::vector< T > &path_list)
collate a vector of path into a string using PATH_SEP
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::string > VARIABLE
map containing the name of the path variable for each type
Definition Path.cpp:48
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:104
ELEMENTS_API const std::map< Type, const bool > HAS_SUBLEVELS
map containing the sub-level property of the path components
Definition Path.cpp:72
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:100
ELEMENTS_API const std::string PATH_SEP
Separator of path entries. Usually ":" on Unix.
Definition Path.cpp:44
ELEMENTS_API Item getPathFromEnvVariable(const T &file_name, const std::string &path_variable)
retrieve path from a file name and an environment variable to look into
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
boost::filesystem::path Item
Definition Path.h:57
const std::string SHLIB_VAR_NAME
name of the shared dynamic library path
Definition System.h:58