Path.hpp
/* This file is part of Raul.
* Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
*
* Raul is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Raul is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef RAUL_PATH_HPP
#define RAUL_PATH_HPP
#include <iostream>
#include <cctype>
#include <string>
#include <cstring>
#include <cassert>
#include "raul/Symbol.hpp"
#include "raul/URI.hpp"
namespace Raul {
class Path : public URI {
public:
class BadPath : public std::exception {
public:
BadPath(const std::string& path) : _path(path) {}
~BadPath() throw() {}
const char* what() const throw() { return _path.c_str(); }
private:
std::string _path;
};
static const Path root();
static void set_root(const Raul::URI& uri);
static bool is_path(const Raul::URI& uri);
Path() : URI(root()) {}
Path(const std::basic_string<char>& path);
Path(const char* cpath);
Path(const Path& copy) : URI(copy) {}
static bool is_valid(const std::basic_string<char>& path);
static bool is_valid_name(const std::basic_string<char>& name) {
return name.length() > 0 && name.find("/") == std::string::npos
&& is_valid(std::string("/").append(name));
}
static std::string pathify(const std::basic_string<char>& str);
static std::string nameify(const std::basic_string<char>& str);
static void replace_invalid_chars(std::string& str, size_t start, bool replace_slash = false);
bool is_root() const { return (*this) == root(); }
bool is_child_of(const Path& parent) const;
bool is_parent_of(const Path& child) const;
Path child(const std::string& s) const {
if (is_valid(s))
return base() + Path(s).chop_scheme().substr(1);
else
return base() + s;
}
Path child(const Path& p) const {
return base() + p.chop_scheme().substr(1);
}
Path operator+(const Path& p) const { return child(p); }
inline const char* symbol() const {
if ((*this) != root()) {
const char* last_slash = strrchr(c_str(), '/');
if (last_slash) {
return last_slash + 1;
}
}
return "";
}
inline Path parent() const {
if ((*this) == root()) {
return *this;
} else {
const std::string str(this->str());
const size_t first_slash = str.find('/');
const size_t last_slash = str.find_last_of('/');
return (first_slash == last_slash) ? root() : str.substr(0, last_slash);
}
}
inline Path child(const Raul::Symbol& symbol) const {
return base() + symbol.c_str();
}
inline Path relative_to_base(const Path& base) const {
if ((*this) == base) {
return "/";
} else {
assert(length() > base.length());
return substr(base.length() - 1);
}
}
inline const std::string base() const {
std::string ret = str();
if ((*this) == root() && ret[ret.length() - 1] == '/')
return ret;
else
return ret + '/';
}
inline const std::string base_no_scheme() const {
return base().substr(find(":") + 1);
}
static bool descendant_comparator(const Path& parent, const Path& child) {
return ( child == parent || (child.length() > parent.length() &&
(!std::strncmp(parent.c_str(), child.c_str(), parent.length())
&& (parent == root() || child.str()[parent.length()] == '/'))) );
}
private:
inline Path(bool unchecked, const URI& uri) : URI(uri) {}
};
} // namespace Raul
#endif // RAUL_PATH_HPP
Raul::Path
A URI which is a path (for example a filesystem or OSC path).
Definition: Path.hpp:46
Raul::URI
Simple wrapper around standard string with useful URI-specific methods.
Definition: URI.hpp:37
Raul::Path::base
const std::string base() const
Return path with a trailing "/".
Definition: Path.hpp:192
Raul::Path::descendant_comparator
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
Raul::Path::root
static const Path root()
Return the root path.
Definition: Path.cpp:28
Raul::Path::base_no_scheme
const std::string base_no_scheme() const
Return path with a trailing "/".
Definition: Path.hpp:205
Raul::Path::Path
Path(const Path &copy)
Construct a Path from another path.
Definition: Path.hpp:104
Raul::Path::parent
Path parent() const
Return the parent's path.
Definition: Path.hpp:156
Raul
Definition: Array.hpp:26
Raul::Path::Path
Path()
Construct an uninitialzed path, because the STL is annoying.
Definition: Path.hpp:83
Raul::Path::child
Path child(const Raul::Symbol &symbol) const
Return the path's child with the given name (symbol)
Definition: Path.hpp:170
Raul::Path::pathify
static std::string pathify(const std::basic_string< char > &str)
Convert a string to a valid full path.
Definition: Path.cpp:110
Raul::Symbol
A restricted string (C identifier, which is a component of a Path).
Definition: Symbol.hpp:43
Raul::Path::replace_invalid_chars
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
Raul::Path::symbol
const char * symbol() const
Return the symbol of this path (everything after the last '/').
Definition: Path.hpp:141
Raul::Path::relative_to_base
Path relative_to_base(const Path &base) const
Return path relative to some base path (chop prefix)
Definition: Path.hpp:177
Raul::Path::set_root
static void set_root(const Raul::URI &uri)
Set the root path.
Definition: Path.cpp:29
Raul::Path::nameify
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