Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PathSearch.icpp
Go to the documentation of this file.
1 
22 #ifndef ELEMENTSKERNEL__IMPL_PATHSEARCH_ICPP_
23 #define ELEMENTSKERNEL__IMPL_PATHSEARCH_ICPP_
24 
25 #include <string>
26 #include <vector>
27 
28 #include <boost/filesystem.hpp>
29 
30 
31 namespace Elements {
32 
33 //-----------------------------------------------------------------------------
34 // Function search
35 template<typename T, typename ITER>
36 std::vector<T> pathSearch(const std::string& searched_name, T directory) {
37 
38  // create the resulting vector
39  std::vector<T> searchResults { };
40  // make sure directory is ps::path, changing from string to path if T is string.
41  boost::filesystem::path l_directory { directory };
42  // the default constructor of ITER return a pointer to one-past last element
43  ITER end_iter;
44  if (boost::filesystem::is_directory(l_directory)) {
45  // ITER constructor return a pointer to the first element of l_directory
46  for (ITER dir_iter(l_directory); dir_iter != end_iter; ++dir_iter) {
47  if (dir_iter->path().filename() == searched_name) {
48  // File found: make sure the result is T: string to string or string to
49  // boost::filesystem::path
50  T l_result { dir_iter->path().string() };
51  searchResults.push_back(l_result);
52  }
53  }
54  }
55  return searchResults;
56 }
57 
58 template<typename T>
59 std::vector<T> searchOption(std::string searched_name, T directory,
60  SearchType search_type) {
61 
62  // create a local tmp vector result to avoid multiple return statements
63  std::vector<T> searchResults { };
64  switch (search_type) {
65  case SearchType::Local:
66  searchResults = pathSearch<T, boost::filesystem::directory_iterator>(searched_name,
67  directory);
68  break;
70  searchResults = pathSearch<T, boost::filesystem::recursive_directory_iterator>(
71  searched_name, directory);
72  break;
73  }
74  return searchResults;
75 }
76 
77 
78 template <typename T>
79 std::vector<T> pathSearch(const std::string& searched_name,
80  T directory,
81  SearchType search_type) {
82  return searchOption<T>(searched_name, directory, search_type);
83 }
84 
85 
86 } // namespace Elements
87 
88 #endif // ELEMENTSKERNEL__IMPL_PATH_ICPP_
STL class.
boost::filesystem::path path
Definition: DataSyncUtils.h:33
std::vector< T > searchOption(std::string searched_name, T directory, SearchType search_type)
Definition: PathSearch.icpp:59
STL class.
std::vector< T > pathSearch(const std::string &searched_name, T directory)
Definition: PathSearch.icpp:36