00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_PATH_HPP
00019 #define RAUL_PATH_HPP
00020
00021 #include <iostream>
00022 #include <cctype>
00023 #include <string>
00024 #include <cstring>
00025 #include <cassert>
00026
00027 #include <raul/Symbol.hpp>
00028
00029 namespace Raul {
00030
00031
00046 class Path : public std::basic_string<char> {
00047 public:
00048
00050 Path() : std::basic_string<char>("/") {}
00051
00057 Path(const std::basic_string<char>& path)
00058 : std::basic_string<char>(path)
00059 {
00060 assert(is_valid(path));
00061 }
00062
00063
00069 Path(const char* cpath)
00070 : std::basic_string<char>(cpath)
00071 {
00072 assert(is_valid(cpath));
00073 }
00074
00075 static bool is_valid(const std::basic_string<char>& path);
00076
00077 static bool is_valid_name(const std::basic_string<char>& name) {
00078 return name.length() > 0 && is_valid(std::string("/").append(name));
00079 }
00080
00081 static std::string pathify(const std::basic_string<char>& str);
00082 static std::string nameify(const std::basic_string<char>& str);
00083
00084 static void replace_invalid_chars(std::string& str, bool replace_slash = false);
00085
00086 bool is_child_of(const Path& parent) const;
00087 bool is_parent_of(const Path& child) const;
00088
00089
00093 inline Symbol name() const {
00094 if ((*this) == "/")
00095 return "";
00096 else
00097 return substr(find_last_of("/")+1);
00098 }
00099
00100
00106 inline Path parent() const {
00107 std::basic_string<char> parent = substr(0, find_last_of("/"));
00108 return (parent == "") ? "/" : parent;
00109 }
00110
00111
00114 inline Path relative_to_base(const Path& base) const {
00115 if ((*this) == base) {
00116 return "/";
00117 } else {
00118 assert(length() > base.length());
00119 return substr(base.length());
00120 }
00121 }
00122
00123
00129 inline const std::string base() const {
00130 if ((*this) == "/")
00131 return *this;
00132 else
00133 return (*this) + "/";
00134 }
00135
00137 static bool descendant_comparator(const Path& parent, const Path& child) {
00138 return ( child == parent || (child.length() > parent.length() &&
00139 (!std::strncmp(parent.c_str(), child.c_str(), parent.length())
00140 && (parent == "/" || child[parent.length()] == '/'))) );
00141 }
00142 };
00143
00144
00145 }
00146
00147 #endif // RAUL_PATH_HPP