RAUL  0.8.0
Path.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_PATH_HPP
19 #define RAUL_PATH_HPP
20 
21 #include <iostream>
22 #include <cctype>
23 #include <string>
24 #include <cstring>
25 #include <cassert>
26 
27 #include "raul/Symbol.hpp"
28 #include "raul/URI.hpp"
29 
30 namespace Raul {
31 
32 
46 class Path : public URI {
47 public:
48  class BadPath : public std::exception {
49  public:
50  BadPath(const std::string& path) : _path(path) {}
51  ~BadPath() throw() {}
52  const char* what() const throw() { return _path.c_str(); }
53  private:
54  std::string _path;
55  };
56 
63  static const Path root();
64 
78  static void set_root(const Raul::URI& uri);
79 
80  static bool is_path(const Raul::URI& uri);
81 
83  Path() : URI(root()) {}
84 
90  Path(const std::basic_string<char>& path);
91 
97  Path(const char* cpath);
98 
104  Path(const Path& copy) : URI(copy) {}
105 
106  static bool is_valid(const std::basic_string<char>& path);
107 
108  static bool is_valid_name(const std::basic_string<char>& name) {
109  return name.length() > 0 && name.find("/") == std::string::npos
110  && is_valid(std::string("/").append(name));
111  }
112 
113  static std::string pathify(const std::basic_string<char>& str);
114  static std::string nameify(const std::basic_string<char>& str);
115 
116  static void replace_invalid_chars(std::string& str, size_t start, bool replace_slash = false);
117 
118  bool is_root() const { return (*this) == root(); }
119 
120  bool is_child_of(const Path& parent) const;
121  bool is_parent_of(const Path& child) const;
122 
123  Path child(const std::string& s) const {
124  if (is_valid(s))
125  return base() + Path(s).chop_scheme().substr(1);
126  else
127  return base() + s;
128  }
129 
130  Path child(const Path& p) const {
131  return base() + p.chop_scheme().substr(1);
132  }
133 
134  Path operator+(const Path& p) const { return child(p); }
135 
141  inline const char* symbol() const {
142  if ((*this) != root()) {
143  const char* last_slash = strrchr(c_str(), '/');
144  if (last_slash) {
145  return last_slash + 1;
146  }
147  }
148  return "";
149  }
150 
156  inline Path parent() const {
157  if ((*this) == root()) {
158  return *this;
159  } else {
160  const std::string str(this->str());
161  const size_t first_slash = str.find('/');
162  const size_t last_slash = str.find_last_of('/');
163  return (first_slash == last_slash) ? root() : str.substr(0, last_slash);
164  }
165  }
166 
167 
170  inline Path child(const Raul::Symbol& symbol) const {
171  return base() + symbol.c_str();
172  }
173 
174 
177  inline Path relative_to_base(const Path& base) const {
178  if ((*this) == base) {
179  return "/";
180  } else {
181  assert(length() > base.length());
182  return substr(base.length() - 1);
183  }
184  }
185 
186 
192  inline const std::string base() const {
193  std::string ret = str();
194  if ((*this) == root() && ret[ret.length() - 1] == '/')
195  return ret;
196  else
197  return ret + '/';
198  }
199 
205  inline const std::string base_no_scheme() const {
206  return base().substr(find(":") + 1);
207  }
208 
209 
211  static bool descendant_comparator(const Path& parent, const Path& child) {
212  return ( child == parent || (child.length() > parent.length() &&
213  (!std::strncmp(parent.c_str(), child.c_str(), parent.length())
214  && (parent == root() || child.str()[parent.length()] == '/'))) );
215  }
216 
217 private:
218  inline Path(bool unchecked, const URI& uri) : URI(uri) {}
219 };
220 
221 
222 } // namespace Raul
223 
224 #endif // RAUL_PATH_HPP
A URI which is a path (for example a filesystem or OSC path).
Definition: Path.hpp:46
const std::string base_no_scheme() const
Return path with a trailing "/".
Definition: Path.hpp:205
Path child(const Raul::Symbol &symbol) const
Return the path's child with the given name (symbol)
Definition: Path.hpp:170
Path parent() const
Return the parent's path.
Definition: Path.hpp:156
Path(const Path &copy)
Construct a Path from another path.
Definition: Path.hpp:104
static bool descendant_comparator(const Path &parent, const Path &child)
Return true if child is equal to, or a descendant of parent.
Definition: Path.hpp:211
const char * symbol() const
Return the symbol of this path (everything after the last '/').
Definition: Path.hpp:141
static std::string nameify(const std::basic_string< char > &str)
Convert a string to a valid name (or "method" - tokens between slashes)
Definition: Path.cpp:143
static std::string pathify(const std::basic_string< char > &str)
Convert a string to a valid full path.
Definition: Path.cpp:110
Path relative_to_base(const Path &base) const
Return path relative to some base path (chop prefix)
Definition: Path.hpp:177
const std::string base() const
Return path with a trailing "/".
Definition: Path.hpp:192
static void set_root(const Raul::URI &uri)
Set the root path.
Definition: Path.cpp:29
static const Path root()
Return the root path.
Definition: Path.cpp:28
static void replace_invalid_chars(std::string &str, size_t start, bool replace_slash=false)
Replace any invalid characters in str with a suitable replacement.
Definition: Path.cpp:161
Path()
Construct an uninitialzed path, because the STL is annoying.
Definition: Path.hpp:83
A restricted string (C identifier, which is a component of a Path).
Definition: Symbol.hpp:43
Simple wrapper around standard string with useful URI-specific methods.
Definition: URI.hpp:37