Arbiter  0.0.0
util.hpp
1 #pragma once
2 
3 #include <string>
4 
5 #ifdef ARBITER_CUSTOM_NAMESPACE
6 namespace ARBITER_CUSTOM_NAMESPACE
7 {
8 #endif
9 
10 namespace arbiter
11 {
12 
14 namespace util
15 {
19  std::string stripPostfixing(std::string path);
20 
31  std::string getBasename(std::string fullPath);
32 
38  std::string getNonBasename(std::string fullPath);
39 
41  inline bool isSlash(char c) { return c == '/' || c == '\\'; }
42 
44  inline bool isGlob(std::string path)
45  {
46  return path.size() && path.back() == '*';
47  }
48 
50  inline bool isDirectory(std::string path)
51  {
52  return (path.size() && isSlash(path.back())) || isGlob(path);
53  }
54 
55  inline std::string joinImpl(bool first = false) { return std::string(); }
56 
57  template <typename ...Paths>
58  inline std::string joinImpl(
59  bool first,
60  std::string current,
61  Paths&&... paths)
62  {
63  std::string next(joinImpl(false, std::forward<Paths>(paths)...));
64  while (next.size() && isSlash(next.front())) next = next.substr(1);
65 
66  if (first)
67  {
68  if (
69  current.size() > 1 &&
70  isSlash(current.back()) &&
71  !isSlash(current.at(current.size() - 2)))
72  {
73  current.pop_back();
74  }
75  }
76  else
77  {
78  while (current.size() && isSlash(current.back()))
79  {
80  current.pop_back();
81  }
82  if (current.empty()) return next;
83  }
84 
85  const std::string sep(
86  next.size() && (current.empty() || !isSlash(current.back())) ?
87  "/" : "");
88 
89  return current + sep + next;
90  }
116  template <typename ...Paths>
117  inline std::string join(std::string path, Paths&&... paths)
118  {
119  return joinImpl(true, path, std::forward<Paths>(paths)...);
120  }
121 
125  std::unique_ptr<std::string> env(const std::string& var);
126 
127 } // namespace util
128 
129 } // namespace arbiter
130 
131 #ifdef ARBITER_CUSTOM_NAMESPACE
132 }
133 #endif
134 
std::string join(std::string path, Paths &&...paths)
Join one or more path components "intelligently".
Definition: util.hpp:117
std::string getBasename(const std::string fullPath)
Definition: util.cpp:34
std::unique_ptr< std::string > env(const std::string &var)
Extract an environment variable, if it exists, independent of platform.
Definition: util.cpp:70
std::string stripPostfixing(const std::string path)
Definition: util.cpp:17
Definition: arbiter.cpp:16
std::string getNonBasename(const std::string fullPath)
Definition: util.cpp:52