Elements  6.2
A C++ base framework for the Euclid Software.
PathSearch.cpp
Go to the documentation of this file.
1 
22 #include "ElementsKernel/PathSearch.h" // for SearchType, etc
23 
24 #include <ostream> // for operator<<, basic_ostream, etc
25 #include <string> // for string, char_traits
26 #include <vector> // for vector
27 
28 #include <boost/algorithm/string.hpp>
29 #include <boost/filesystem/operations.hpp>
30 
31 #include "ElementsKernel/Environment.h" // for Environment
32 #include "ElementsKernel/Exception.h" // for Exception
33 #include "ElementsKernel/Logging.h" // for the logger
34 #include "ElementsKernel/Path.h" // for Path::Item
35 
36 using std::string;
37 using std::vector;
38 
39 using boost::filesystem::directory_iterator;
40 using boost::filesystem::recursive_directory_iterator;
41 
42 namespace Elements {
43 inline namespace Kernel {
44 
45 namespace {
46 auto log = Logging::getLogger("PathSearch");
47 }
48 
49 // template instantiations
50 
51 template vector<string> pathSearch<string, directory_iterator>(const string& searched_name, string directory);
52 template vector<Path::Item> pathSearch<Path::Item, directory_iterator>(const string& searched_name,
53  Path::Item directory);
54 template vector<string> pathSearch<string, recursive_directory_iterator>(const string& searched_name, string directory);
55 template vector<Path::Item> pathSearch<Path::Item, recursive_directory_iterator>(const string& searched_name,
56  Path::Item directory);
57 
58 template vector<Path::Item> pathSearch(const string& searched_name, Path::Item directory, SearchType search_type);
59 template vector<string> pathSearch(const string& searched_name, string directory, SearchType search_type);
60 
68 vector<Path::Item> pathSearchInEnvVariable(const string& file_name, const string& path_like_env_variable,
69  SearchType search_type) {
70  // Placeholder for the to-be-returned search result
71  vector<Path::Item> search_results{};
72 
73  // get the multiple path from the environment variable
74  string multiple_path{};
75 
76  Environment current_env;
77 
78  if (current_env.hasKey(path_like_env_variable)) {
79  multiple_path = current_env[path_like_env_variable];
80  } else {
81  log.warn() << "Environment variable \"" << path_like_env_variable << "\" is not defined !";
82  }
83 
84  // Tokenize the path elements
85  vector<string> path_elements;
86  boost::split(path_elements, multiple_path, boost::is_any_of(";:"));
87 
88  // Loop over all path elements
89  for (string path_element : path_elements) {
90  // Check if directory exists
91  if (boost::filesystem::exists(path_element) && boost::filesystem::is_directory(path_element)) {
92  // loop recursively inside directory
93  auto single_path_results = pathSearch(file_name, Path::Item{path_element}, search_type);
94  search_results.insert(search_results.end(), single_path_results.cbegin(), single_path_results.cend());
95  }
96  }
97  return search_results;
98 }
99 
100 } // namespace Kernel
101 } // namespace Elements
Elements::Kernel::Path::Item
boost::filesystem::path Item
Definition: Path.h:56
Elements::Kernel::pathSearch< string, recursive_directory_iterator >
template vector< string > pathSearch< string, recursive_directory_iterator >(const string &searched_name, string directory)
std::string
STL class.
Elements::Kernel::SearchType
SearchType
Definition: PathSearch.h:38
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.
Elements::Environment::hasKey
static bool hasKey(const std::string &)
Definition: Environment.cpp:250
Elements::Kernel::pathSearch< string, directory_iterator >
template vector< string > pathSearch< string, directory_iterator >(const string &searched_name, string directory)
Elements::Kernel::pathSearchInEnvVariable
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...
Definition: PathSearch.cpp:68
Exception.h
defines the base Elements exception class
Elements::Examples::log
auto log
Definition: BackTrace.cpp:36
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Elements::Kernel::pathSearch
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....
PathSearch.h
Environment.h
Defines a class to handle the Environment.
Elements::Environment
Python dictionary-like Environment interface.
Definition: Environment.h:44
Logging.h
Logging facility.
Elements
Definition: callBackExample.h:35