Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
paths.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jan Olszak <j.olszak@samsung.com>
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 
25 #ifndef COMMON_UTILS_PATHS_HPP
26 #define COMMON_UTILS_PATHS_HPP
27 
28 #include <string>
29 #include <vector>
30 #include <algorithm>
31 
32 
33 namespace utils {
34 
35 
36 template <class ...Paths> std::string createFilePath(const Paths& ... paths)
37 {
38  std::vector<std::string> pathVec = {paths...};
39  std::string retPath = "";
40 
41  if (pathVec.empty()) {
42  return retPath;
43  }
44 
45  for (std::string& p : pathVec) {
46  // Repeat until retPath is not empty
47  if (retPath.empty() || p.empty()) {
48  retPath += p;
49  continue;
50  }
51 
52  // We need a slash
53  if (retPath.back() != '/' && p.front() != '/' && p.front() != '.') {
54  retPath += "/" + p;
55  continue;
56  }
57 
58  // Too many slashes
59  if (retPath.back() == '/' && p.front() == '/') {
60  retPath += p.substr(1);
61  continue;
62  }
63 
64  retPath += p;
65  }
66 
67  return retPath;
68 }
69 
70 namespace {
71 
72 inline void removeDuplicateSlashes(std::string& path)
73 {
74  auto it = std::unique(path.begin(), path.end(),
75  [](char a, char b) {
76  return (a == '/' && a == b);
77  });
78  path.erase(it, path.end());
79 }
80 
81 inline void removeTrailingSlash(std::string& path)
82 {
83  size_t size = path.size();
84 
85  if (size > 1 && path[size - 1] == '/') {
86  path.resize(size - 1);
87  }
88 }
89 
90 } // namespace
91 
92 /*
93  * Gets the dir name of a file path, analogous to dirname(1)
94  */
95 inline std::string dirName(std::string path)
96 {
97  removeDuplicateSlashes(path);
98  removeTrailingSlash(path);
99  path.erase(std::find(path.rbegin(), path.rend(), '/').base(), path.end());
100  removeTrailingSlash(path);
101 
102  if (path.empty()) {
103  return ".";
104  }
105 
106  return path;
107 }
108 
109 /*
110  * Gets absolute path to specified file (if needed)
111  */
112 inline std::string getAbsolutePath(const std::string& path, const std::string& base)
113 {
114  if (path[0] == '/') {
115  return path;
116  } else {
117  return utils::createFilePath(base, "/", path);
118  }
119 }
120 
121 } // namespace utils
122 
123 
124 #endif // COMMON_UTILS_PATHS_HPP
std::string createFilePath(const Paths &...paths)
Definition: paths.hpp:36
std::string dirName(std::string path)
Definition: paths.hpp:95
std::string getAbsolutePath(const std::string &path, const std::string &base)
Definition: paths.hpp:112