Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
PathSearch.cpp
Go to the documentation of this file.
1
23
24#include <iterator> // for operator!=
25#include <string> // for allocator, string, basic_string
26#include <vector> // for vector
27
28#include <boost/algorithm/string.hpp> // for is_any_ofF, is_any_of, split
29#include <boost/filesystem/operations.hpp> // for exists, is_directory
30#include <boost/filesystem/path.hpp> // for operator==, path
31
32#include "ElementsKernel/Environment.h" // for Environment
33#include "ElementsKernel/Logging.h" // for Logging
34#include "ElementsKernel/Path.h" // for Item
35
37class directory_iterator;
38}
39namespace boost::filesystem {
40class recursive_directory_iterator;
41}
42
43using std::string;
44using std::vector;
45
46using boost::filesystem::directory_iterator;
47using boost::filesystem::recursive_directory_iterator;
48
49namespace Elements {
50inline namespace Kernel {
51
52namespace {
53auto log = Logging::getLogger("PathSearch");
54}
55
56// template instantiations
57
58template vector<string> pathSearch<string, directory_iterator>(const string& searched_name, string directory);
60 Path::Item directory);
61template vector<string> pathSearch<string, recursive_directory_iterator>(const string& searched_name, string directory);
63 Path::Item directory);
64
65template vector<Path::Item> pathSearch(const string& searched_name, Path::Item directory, SearchType search_type);
66template vector<string> pathSearch(const string& searched_name, string directory, SearchType search_type);
67
75vector<Path::Item> pathSearchInEnvVariable(const string& file_name, const string& path_like_env_variable,
76 SearchType search_type) {
77 // Placeholder for the to-be-returned search result
78 vector<Path::Item> search_results{};
79
80 // get the multiple path from the environment variable
81 string multiple_path{};
82
83 Environment current_env;
84
85 if (current_env.hasKey(path_like_env_variable)) {
86 multiple_path = current_env[path_like_env_variable];
87 } else {
88 log.warn() << "Environment variable \"" << path_like_env_variable << "\" is not defined !";
89 }
90
91 // Tokenize the path elements
92 vector<string> path_elements;
93 boost::split(path_elements, multiple_path, boost::is_any_of(";:"));
94
95 // Loop over all path elements
96 for (string path_element : path_elements) {
97 // Check if directory exists
98 if (boost::filesystem::exists(path_element) && boost::filesystem::is_directory(path_element)) {
99 // loop recursively inside directory
100 auto single_path_results = pathSearch(file_name, Path::Item{path_element}, search_type);
101 search_results.insert(search_results.end(), single_path_results.cbegin(), single_path_results.cend());
102 }
103 }
104 return search_results;
105}
106
107} // namespace Kernel
108} // namespace Elements
Defines a class to handle the Environment.
Logging facility.
provide functions to retrieve resources pointed by environment variables
Python dictionary-like Environment interface.
Definition Environment.h:44
static bool hasKey(const std::string &)
static Logging getLogger(const std::string &name="")
Definition Logging.cpp:75
ELEMENTS_API std::vector< T > pathSearch(const std::string &searched_name, T directory, SearchType search_type)
Searches for a file or a directory in a directory. The search can be recursive (SearchType....
boost::filesystem::path Item
Definition Path.h:57
template vector< Path::Item > pathSearch< Path::Item, directory_iterator >(const string &searched_name, Path::Item directory)
template vector< string > pathSearch< string, directory_iterator >(const string &searched_name, string directory)
template vector< string > pathSearch< string, recursive_directory_iterator >(const string &searched_name, string directory)
ELEMENTS_API std::vector< Path::Item > pathSearchInEnvVariable(const std::string &file_name, const std::string &path_like_env_variable, SearchType search_type=SearchType::Recursive)
Searches for a file or a directory in a path pointed by an environment variable. It can contains coll...
template vector< Path::Item > pathSearch< Path::Item, recursive_directory_iterator >(const string &searched_name, Path::Item directory)