Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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.hpp>
30 
31 #include "ElementsKernel/Exception.h" // for Exception
32 #include "ElementsKernel/System.h"
33 #include "ElementsKernel/Logging.h" // for the logger
34 
35 using std::vector;
36 using std::string;
37 
39 using boost::filesystem::directory_iterator;
40 using boost::filesystem::recursive_directory_iterator;
41 
42 namespace Elements {
43 
44 namespace {
45  auto log = Logging::getLogger("PathSearch");
46 }
47 
48 // template instantiations
49 
50 template vector<string> pathSearch<string, directory_iterator>(const string& searched_name, string directory);
51 template vector<path> pathSearch<path, directory_iterator>(const string& searched_name, path directory);
52 template vector<string> pathSearch<string, recursive_directory_iterator>(const string& searched_name, string directory);
53 template vector<path> pathSearch<path, recursive_directory_iterator>(const string& searched_name, path directory);
54 
55 template vector<path> pathSearch(const string& searched_name, path directory,
56  SearchType search_type);
57 template vector<string> pathSearch(const string& searched_name, string directory,
58  SearchType search_type);
59 
60 
68 vector<path> pathSearchInEnvVariable(const string& file_name,
69  const string& path_like_env_variable,
70  SearchType search_type) {
71  // Placeholder for the to-be-returned search result
72  vector<path> search_results { };
73 
74  // get the multiple path from the environment variable
75  string multiple_path {};
76  if (not System::getEnv(path_like_env_variable.c_str(), multiple_path)) {
77  log.warn() << "Environment variable \"" << path_like_env_variable
78  << "\" is not defined !";
79  }
80 
81  // Tokenize the path elements
82  vector<string> path_elements;
83  boost::split(path_elements, multiple_path, boost::is_any_of(";:"));
84 
85  // Loop over all path elements
86  for (string path_element : path_elements) {
87  // Check if directory exists
88  if (boost::filesystem::exists(path_element) && boost::filesystem::is_directory(path_element)) {
89  // loop recursively inside directory
90  auto single_path_results = pathSearch(file_name,
91  path { path_element },
92  search_type);
93  search_results.insert(search_results.end(),
94  single_path_results.cbegin(), single_path_results.cend());
95  }
96  }
97  return search_results;
98 }
99 
100 } // namespace Elements
template vector< path > pathSearch< path, recursive_directory_iterator >(const string &searched_name, path directory)
Logging facility.
template vector< path > pathSearch< path, directory_iterator >(const string &searched_name, path directory)
T log(T...args)
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)
STL class.
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
Definition: System.cpp:331
This file is intended to iron out all the differences between systems (currently Linux and MacOSX) ...
boost::filesystem::path path
Definition: DataSyncUtils.h:33
STL class.
ELEMENTS_API std::vector< boost::filesystem::path > 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
T c_str(T...args)
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
defines the base Elements exception class
std::vector< T > pathSearch(const std::string &searched_name, T directory)
Definition: PathSearch.icpp:36