Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
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
36using std::map;
37using std::string;
38using std::vector;
39
40namespace Elements {
41inline namespace Kernel {
42namespace Path {
43
44const 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
70vector<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
88vector<Item> getLocations(const Type& path_type, bool exist_only) {
89 return getLocationsFromEnv(VARIABLE.at(path_type), exist_only);
90}
91
92vector<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
106template Item getPathFromLocations(const Item& file_name, const vector<Item>& locations);
107template Item getPathFromLocations(const Item& file_name, const vector<string>& locations);
108template Item getPathFromLocations(const string& file_name, const vector<Item>& locations);
109template Item getPathFromLocations(const string& file_name, const vector<string>& locations);
110
111template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<Item>& locations);
112template vector<Item> getAllPathFromLocations(const Item& file_name, const vector<string>& locations);
113template vector<Item> getAllPathFromLocations(const string& file_name, const vector<Item>& locations);
114template vector<Item> getAllPathFromLocations(const string& file_name, const vector<string>& locations);
115
116template Item getPathFromEnvVariable<Item>(const Item& file_name, const string& path_variable);
117template Item getPathFromEnvVariable<string>(const string& file_name, const string& path_variable);
118
119template string joinPath(const vector<Item>& path_list);
120template string joinPath(const vector<string>& path_list);
121
122template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<Item>& suffixes);
123template vector<Item> multiPathAppend(const vector<Item>& initial_locations, const vector<string>& suffixes);
124template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<Item>& suffixes);
125template vector<Item> multiPathAppend(const vector<string>& initial_locations, const vector<string>& suffixes);
126
127template vector<Item> removeDuplicates(const vector<Item>& path_list);
128template vector<Item> removeDuplicates(const vector<string>& path_list);
129
130} // namespace Path
131} // namespace Kernel
132} // 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)
T begin(T... args)
Python dictionary-like Environment interface.
Definition: Environment.h:44
T end(T... args)
T erase(T... args)
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:52
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:58
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
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:46
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
ELEMENTS_API const std::map< Type, const bool > HAS_SUBLEVELS
map containing the sub-level property of the path components
Definition: Path.cpp:64
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_API const std::string PATH_SEP
Separator of path entries. Usually ":" on Unix.
Definition: Path.cpp:44
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:56
const std::string SHLIB_VAR_NAME
name of the shared dynamic library path
Definition: System.h:58
T remove_if(T... args)
T size(T... args)
T transform(T... args)