2 * @file ElementsKernel/_impl/Path.icpp
3 * @brief implementation of the templates declared in ElementsKernel/Path.h
5 * @author Hubert Degaudenzi
8 * @copyright 2012-2020 Euclid Science Ground Segment
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)
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
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
22 #ifdef ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_
27 #include <boost/filesystem/operations.hpp>
29 #include "ElementsKernel/Path.h"
33 //-----------------------------------------------------------------------------
35 template<typename T, typename ITER>
36 std::vector<T> pathSearch(const std::string& searched_name, T directory) {
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 Path::Item l_directory { directory };
42 // the default constructor of ITER return a pointer to one-past last element
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.emplace_back(l_result);
59 std::vector<T> searchOption(std::string searched_name, T directory,
60 SearchType search_type) {
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,
69 case SearchType::Recursive:
70 searchResults = pathSearch<T, boost::filesystem::recursive_directory_iterator>(
71 searched_name, directory);
79 std::vector<T> pathSearch(const std::string& searched_name,
81 SearchType search_type) {
82 return searchOption<T>(searched_name, directory, search_type);
86 } // namespace Elements
88 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATHSEARCH_IMPL_