Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
PathSearch.tpp
Go to the documentation of this file.
1
22// IWYU pragma: private, include "ElementsKernel/PathSearch.h"
23
24#ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_
25#error "This file should not be included directly! Use ElementsKernel/PathSearch.h instead"
26#else
27
28#include <string> // for string
29#include <vector> // for vector
30
31#include <boost/filesystem.hpp> // for is_directory
32
33#include "ElementsKernel/Path.h" // for Item
34
35namespace Elements {
36inline namespace Kernel {
37//-----------------------------------------------------------------------------
38// Function search
39template <typename T, typename ITER>
40std::vector<T> pathSearch(const std::string& searched_name, T directory) {
41
42 // create the resulting vector
43 std::vector<T> searchResults{};
44 // make sure directory is ps::path, changing from string to path if T is string.
45 Path::Item l_directory{directory};
46 // the default constructor of ITER return a pointer to one-past last element
47 ITER end_iter;
48 if (boost::filesystem::is_directory(l_directory)) {
49 // ITER constructor return a pointer to the first element of l_directory
50 for (ITER dir_iter(l_directory); dir_iter != end_iter; ++dir_iter) {
51 if (dir_iter->path().filename() == searched_name) {
52 // File found: make sure the result is T: string to string or string to
53 // boost::filesystem::path
54 T l_result{dir_iter->path().string()};
55 searchResults.emplace_back(l_result);
56 }
57 }
58 }
59 return searchResults;
60}
61
62template <typename T>
63std::vector<T> searchOption(std::string searched_name, T directory, SearchType search_type) {
64
65 // create a local tmp vector result to avoid multiple return statements
66 std::vector<T> searchResults{};
67 switch (search_type) {
69 searchResults = pathSearch<T, boost::filesystem::directory_iterator>(searched_name, directory);
70 break;
72 searchResults = pathSearch<T, boost::filesystem::recursive_directory_iterator>(searched_name, directory);
73 break;
74 }
75 return searchResults;
76}
77
78template <typename T>
79std::vector<T> pathSearch(const std::string& searched_name, T directory, SearchType search_type) {
80 return searchOption<T>(searched_name, directory, search_type);
81}
82
83} // namespace Kernel
84} // namespace Elements
85
86#endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_
provide functions to retrieve resources pointed by environment variables
T emplace_back(T... args)
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