27 #include "raul/Symbol.hpp" 28 #include "raul/URI.hpp" 48 class BadPath :
public std::exception {
50 BadPath(
const std::string& path) : _path(path) {}
52 const char* what()
const throw() {
return _path.c_str(); }
80 static bool is_path(
const Raul::URI& uri);
90 Path(
const std::basic_string<char>& path);
97 Path(
const char* cpath);
106 static bool is_valid(
const std::basic_string<char>& path);
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));
113 static std::string
pathify(
const std::basic_string<char>& str);
114 static std::string
nameify(
const std::basic_string<char>& str);
118 bool is_root()
const {
return (*
this) ==
root(); }
121 bool is_parent_of(
const Path& child)
const;
123 Path child(
const std::string& s)
const {
125 return base() +
Path(s).chop_scheme().substr(1);
134 Path operator+(
const Path& p)
const {
return child(p); }
142 if ((*
this) !=
root()) {
143 const char* last_slash = strrchr(c_str(),
'/');
145 return last_slash + 1;
157 if ((*
this) ==
root()) {
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);
171 return base() + symbol.c_str();
178 if ((*
this) == base) {
181 assert(length() > base.length());
182 return substr(base.length() - 1);
192 inline const std::string
base()
const {
193 std::string ret = str();
194 if ((*
this) ==
root() && ret[ret.length() - 1] ==
'/')
206 return base().substr(find(
":") + 1);
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()] ==
'/'))) );
218 inline Path(
bool unchecked,
const URI& uri) :
URI(uri) {}
224 #endif // RAUL_PATH_HPP A URI which is a path (for example a filesystem or OSC path).
Definition: Path.hpp:46
URI(const std::basic_string< char > &uri="nil:0")
Construct a URI from an std::string.
Definition: URI.hpp:53
Simple wrapper around standard string with useful URI-specific methods.
Definition: URI.hpp:37
const std::string base() const
Return path with a trailing "/".
Definition: Path.hpp:192
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
static const Path root()
Return the root path.
Definition: Path.cpp:28
const std::string base_no_scheme() const
Return path with a trailing "/".
Definition: Path.hpp:205
Path(const Path ©)
Construct a Path from another path.
Definition: Path.hpp:104
Path parent() const
Return the parent's path.
Definition: Path.hpp:156
Path()
Construct an uninitialzed path, because the STL is annoying.
Definition: Path.hpp:83
Path child(const Raul::Symbol &symbol) const
Return the path's child with the given name (symbol)
Definition: Path.hpp:170
static std::string pathify(const std::basic_string< char > &str)
Convert a string to a valid full path.
Definition: Path.cpp:110
A restricted string (C identifier, which is a component of a Path).
Definition: Symbol.hpp:43
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
const char * symbol() const
Return the symbol of this path (everything after the last '/').
Definition: Path.hpp:141
std::string chop_scheme() const
Return the URI with the scheme removed (as a string)
Definition: URI.hpp:78
Path relative_to_base(const Path &base) const
Return path relative to some base path (chop prefix)
Definition: Path.hpp:177
static void set_root(const Raul::URI &uri)
Set the root path.
Definition: Path.cpp:29
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