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 #ifdef ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_
23 #include <string> // for string
24 #include <vector> // for vector
25 #include <unordered_set> // for unordered_set
26 #include <algorithm> // for find_if, transform, for_each
28 #include <boost/filesystem/operations.hpp> // for exists
29 #include <boost/algorithm/string/join.hpp> // for join
35 template <typename T, typename U>
36 Item getPathFromLocations(const T& file_name, const std::vector<U>& locations) {
39 Item file_path {file_name};
41 auto found_pos = std::find_if(locations.cbegin(), locations.cend(),
42 [file_path](const U& l) {
43 return boost::filesystem::exists(Item {l} / file_path);
46 if (found_pos != locations.cend()) {
47 found_path = Item {*found_pos} / file_path;
54 template <typename T, typename U>
55 std::vector<Item> getAllPathFromLocations(const T& file_name, const std::vector<U>& locations) {
57 std::vector<Item> file_list(locations.size());
58 Item file_path {file_name};
60 std::transform(locations.cbegin(), locations.cend(),
62 [file_path](const U& l){
63 return Item {l} / file_path;
66 auto found_pos = std::remove_if(file_list.begin(), file_list.end(),
68 return not boost::filesystem::exists(p);
71 file_list.erase(found_pos, file_list.end());
73 return removeDuplicates(file_list);
78 Item getPathFromEnvVariable(const T& file_name, const std::string& path_variable) {
82 vector<Item> location_list = getLocationsFromEnv(path_variable);
84 return getPathFromLocations(file_name, location_list);
89 std::string joinPath(const std::vector<T>& path_list) {
94 vector<string> elems(path_list.size());
96 std::transform(path_list.cbegin(), path_list.cend(),
99 return Item{s}.string();
102 std::string result = boost::algorithm::join(elems, PATH_SEP);
107 template <typename... Args>
108 auto join(Args&&... args) -> decltype(joinPath(std::forward<Args>(args)...)) {
109 return joinPath(std::forward<Args>(args)...);
112 template <typename... Args>
113 auto split(Args&&... args) -> decltype(splitPath(std::forward<Args>(args)...)) {
114 return splitPath(std::forward<Args>(args)...);
118 template <typename T, typename U>
119 std::vector<Item> multiPathAppend(const std::vector<T>& initial_locations, const std::vector<U>& suffixes) {
123 vector<Item> result(initial_locations.size()*suffixes.size());
125 auto pos = result.begin();
127 std::for_each(initial_locations.cbegin(), initial_locations.cend(),
128 [&pos, &suffixes](const T& l) {
129 std::transform(suffixes.cbegin(), suffixes.cend(),
134 pos += static_cast<std::ptrdiff_t>(suffixes.size());
142 template <typename T>
143 std::vector<Item> removeDuplicates(const std::vector<T>& path_list) {
145 std::unordered_set<std::string> s;
147 std::vector<Item> output(path_list.size());
149 auto end = copy_if(path_list.cbegin(), path_list.cend(), output.begin(),
151 return s.insert(Item{i}.string()).second;
154 output.erase(end, output.end());
162 } // namespace Elements
164 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_PATH_IMPL_