Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
PathSearch.tpp
Go to the documentation of this file.
1/**
2 * @file ElementsKernel/_impl/PathSearch.tpp
3 * @brief implementation of the templates declared in ElementsKernel/Path.h
4 * @date May 17, 2016
5 * @author Hubert Degaudenzi
6 *
7 *
8 * @copyright 2012-2020 Euclid Science Ground Segment
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
11 * Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
12 * any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_
23#error "This file should not be included directly! Use ElementsKernel/Path.h instead"
24#else
25
26#include <string>
27#include <vector>
28
29#include <boost/filesystem/operations.hpp>
30
31#include "ElementsKernel/Path.h"
32
33namespace Elements {
34inline namespace Kernel {
35//-----------------------------------------------------------------------------
36// Function search
37template <typename T, typename ITER>
38std::vector<T> pathSearch(const std::string& searched_name, T directory) {
39
40 // create the resulting vector
41 std::vector<T> searchResults{};
42 // make sure directory is ps::path, changing from string to path if T is string.
43 Path::Item l_directory{directory};
44 // the default constructor of ITER return a pointer to one-past last element
45 ITER end_iter;
46 if (boost::filesystem::is_directory(l_directory)) {
47 // ITER constructor return a pointer to the first element of l_directory
48 for (ITER dir_iter(l_directory); dir_iter != end_iter; ++dir_iter) {
49 if (dir_iter->path().filename() == searched_name) {
50 // File found: make sure the result is T: string to string or string to
51 // boost::filesystem::path
52 T l_result{dir_iter->path().string()};
53 searchResults.emplace_back(l_result);
54 }
55 }
56 }
57 return searchResults;
58}
59
60template <typename T>
61std::vector<T> searchOption(std::string searched_name, T directory, SearchType search_type) {
62
63 // create a local tmp vector result to avoid multiple return statements
64 std::vector<T> searchResults{};
65 switch (search_type) {
66 case SearchType::Local:
67 searchResults = pathSearch<T, boost::filesystem::directory_iterator>(searched_name, directory);
68 break;
69 case SearchType::Recursive:
70 searchResults = pathSearch<T, boost::filesystem::recursive_directory_iterator>(searched_name, directory);
71 break;
72 }
73 return searchResults;
74}
75
76template <typename T>
77std::vector<T> pathSearch(const std::string& searched_name, T directory, SearchType search_type) {
78 return searchOption<T>(searched_name, directory, search_type);
79}
80
81} // namespace Kernel
82} // namespace Elements
83
84#endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_