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 <cassert>
00025
00026 namespace Raul {
00027
00028
00043 class Path : public std::basic_string<char> {
00044 public:
00045
00047 Path() : std::basic_string<char>("/") {}
00048
00054 Path(const std::basic_string<char>& path)
00055 : std::basic_string<char>(path)
00056 {
00057 assert(is_valid(path));
00058 }
00059
00060
00066 Path(const char* cpath)
00067 : std::basic_string<char>(cpath)
00068 {
00069 assert(is_valid(cpath));
00070 }
00071
00072 static bool is_valid(const std::basic_string<char>& path);
00073
00074 static bool is_valid_name(const std::basic_string<char>& name) {
00075 return name.length() > 0 && is_valid(std::string("/").append(name));
00076 }
00077
00078 static std::string pathify(const std::basic_string<char>& str);
00079 static std::string nameify(const std::basic_string<char>& str);
00080
00081 static void replace_invalid_chars(std::string& str, bool replace_slash = false);
00082
00083 bool is_child_of(const Path& parent) const;
00084 bool is_parent_of(const Path& child) const;
00085
00086
00090 inline std::basic_string<char> name() const {
00091 if ((*this) == "/")
00092 return "";
00093 else
00094 return substr(find_last_of("/")+1);
00095 }
00096
00097
00103 inline Path parent() const {
00104 std::basic_string<char> parent = substr(0, find_last_of("/"));
00105 return (parent == "") ? "/" : parent;
00106 }
00107
00108
00114 inline const std::string base() const {
00115 if ((*this) == "/")
00116 return *this;
00117 else
00118 return (*this) + "/";
00119 }
00120
00122 static bool descendant_comparator(const Path& parent, const Path& child) {
00123 return ( child == parent || (child.length() > parent.length() &&
00124 (!strncmp(parent.c_str(), child.c_str(), parent.length())
00125 && (parent == "/" || child[parent.length()] == '/'))) );
00126 }
00127 };
00128
00129
00130 }
00131
00132 #endif // RAUL_PATH_HPP