2 * @file ElementsKernel/_impl/Path.icpp
3 * @brief implementation of the templates declared in ElementsKernel/Path.h
5 * @author Hubert Degaudenzi
7 * @copyright 2012-2020 Euclid Science Ground Segment
9 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
10 * Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
13 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef ELEMENTSKERNEL__IMPL_PATH_ICPP_
22 #define ELEMENTSKERNEL__IMPL_PATH_ICPP_
24 #include <string> // for string
25 #include <vector> // for vector
26 #include <set> // for set
27 #include <algorithm> // for find_if, transform, for_each
29 #include <boost/filesystem.hpp> // for boost::filesystem
30 #include <boost/algorithm/string/join.hpp> // for join
36 template <typename T, typename U>
37 boost::filesystem::path getPathFromLocations(const T& file_name, const std::vector<U>& locations) {
39 using boost::filesystem::path;
42 path file_path {file_name};
44 auto found_pos = std::find_if(locations.cbegin(), locations.cend(),
46 return boost::filesystem::exists(path {l} / file_path);
49 if (found_pos != locations.cend()) {
50 found_path = path {*found_pos} / file_path;
57 template <typename T, typename U>
58 std::vector<boost::filesystem::path> getAllPathFromLocations(const T& file_name, const std::vector<U>& locations) {
60 using boost::filesystem::path;
62 std::vector<path> file_list(locations.size());
63 path file_path {file_name};
65 std::transform(locations.cbegin(), locations.cend(),
68 return path {l} / file_path;
71 auto found_pos = std::remove_if(file_list.begin(), file_list.end(),
73 return not boost::filesystem::exists(p);
76 file_list.erase(found_pos, file_list.end());
78 std::set<path> file_set(file_list.begin(), file_list.end());
80 return std::vector<path>(file_set.begin(), file_set.end());
85 boost::filesystem::path getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
88 using boost::filesystem::path;
90 vector<path> location_list = getLocationsFromEnv(path_variable);
92 return getPathFromLocations(file_name, location_list);
97 std::string joinPath(const std::vector<T> path_list) {
102 vector<string> elems(path_list.size());
104 std::transform(path_list.cbegin(), path_list.cend(),
107 return boost::filesystem::path(s).string();
110 std::string result = boost::algorithm::join(elems, PATH_SEP);
115 template <typename T, typename U>
116 std::vector<boost::filesystem::path> multiPathAppend(const std::vector<T>& initial_locations, const std::vector<U>& suffixes) {
119 using boost::filesystem::path;
121 vector<path> result(initial_locations.size()*suffixes.size());
123 auto pos = result.begin();
125 std::for_each(initial_locations.cbegin(), initial_locations.cend(),
126 [&pos, &suffixes](T l) {
127 std::transform(suffixes.cbegin(), suffixes.cend(),
132 pos += suffixes.size();
141 } // namespace Elements
143 #endif // ELEMENTSKERNEL__IMPL_PATH_ICPP_