2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Path utility functions declaration
25 #ifndef COMMON_UTILS_PATHS_HPP
26 #define COMMON_UTILS_PATHS_HPP
36 template <class ...Paths> std::string createFilePath(const Paths& ... paths)
38 std::vector<std::string> pathVec = {paths...};
39 std::string retPath = "";
41 if (pathVec.empty()) {
45 for (std::string& p : pathVec) {
46 // Repeat until retPath is not empty
47 if (retPath.empty() || p.empty()) {
53 if (retPath.back() != '/' && p.front() != '/' && p.front() != '.') {
59 if (retPath.back() == '/' && p.front() == '/') {
60 retPath += p.substr(1);
72 inline void removeDuplicateSlashes(std::string& path)
74 auto it = std::unique(path.begin(), path.end(),
76 return (a == '/' && a == b);
78 path.erase(it, path.end());
81 inline void removeTrailingSlash(std::string& path)
83 size_t size = path.size();
85 if (size > 1 && path[size - 1] == '/') {
86 path.resize(size - 1);
90 } // anonymous namespace
93 * Gets the dir name of a file path, analogous to dirname(1)
95 inline std::string dirName(std::string path)
97 removeDuplicateSlashes(path);
98 removeTrailingSlash(path);
99 path.erase(std::find(path.rbegin(), path.rend(), '/').base(), path.end());
100 removeTrailingSlash(path);
110 * Gets absolute path to specified file (if needed)
112 inline std::string getAbsolutePath(const std::string& path, const std::string& base)
114 if (path[0] == '/') {
117 return utils::createFilePath(base, "/", path);
125 #endif // COMMON_UTILS_PATHS_HPP