RAUL  0.5.1
Path.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave 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 
29 namespace Raul {
30 
31 
46 class Path : public std::basic_string<char> {
47 public:
48 
50  Path() : std::basic_string<char>("/") {}
51 
57  Path(const std::basic_string<char>& path)
58  : std::basic_string<char>(path)
59  {
60  assert(is_valid(path));
61  }
62 
63 
69  Path(const char* cpath)
70  : std::basic_string<char>(cpath)
71  {
72  assert(is_valid(cpath));
73  }
74 
75  static bool is_valid(const std::basic_string<char>& path);
76 
77  static bool is_valid_name(const std::basic_string<char>& name) {
78  return name.length() > 0 && is_valid(std::string("/").append(name));
79  }
80 
81  static std::string pathify(const std::basic_string<char>& str);
82  static std::string nameify(const std::basic_string<char>& str);
83 
84  static void replace_invalid_chars(std::string& str, bool replace_slash = false);
85 
86  bool is_child_of(const Path& parent) const;
87  bool is_parent_of(const Path& child) const;
88 
89 
93  inline Symbol name() const {
94  if ((*this) == "/")
95  return "";
96  else
97  return substr(find_last_of("/")+1);
98  }
99 
100 
106  inline Path parent() const {
107  std::basic_string<char> parent = substr(0, find_last_of("/"));
108  return (parent == "") ? "/" : parent;
109  }
110 
111 
114  inline Path relative_to_base(const Path& base) const {
115  if ((*this) == base) {
116  return "/";
117  } else {
118  assert(length() > base.length());
119  return substr(base.length());
120  }
121  }
122 
123 
129  inline const std::string base() const {
130  if ((*this) == "/")
131  return *this;
132  else
133  return (*this) + "/";
134  }
135 
137  static bool descendant_comparator(const Path& parent, const Path& child) {
138  return ( child == parent || (child.length() > parent.length() &&
139  (!std::strncmp(parent.c_str(), child.c_str(), parent.length())
140  && (parent == "/" || child[parent.length()] == '/'))) );
141  }
142 };
143 
144 
145 } // namespace Raul
146 
147 #endif // RAUL_PATH_HPP
Simple wrapper around standard string with useful path-specific methods.
Definition: Path.hpp:46
Path(const std::basic_string< char > &path)
Construct a Path from an std::string.
Definition: Path.hpp:57
const std::string base() const
Return path with a trailing "/".
Definition: Path.hpp:129
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:137
Path parent() const
Return the parent's path.
Definition: Path.hpp:106
Definition: Array.hpp:26
Symbol name() const
Return the name of this object (everything after the last '/').
Definition: Path.hpp:93
Path()
Construct an uninitialzed path, because the STL is annoying.
Definition: Path.hpp:50
A restricted string (C identifier, which is a component of a path).
Definition: Symbol.hpp:36
Path(const char *cpath)
Construct a Path from a C string.
Definition: Path.hpp:69
Path relative_to_base(const Path &base) const
Return path relative to soe base path (chop prefix)
Definition: Path.hpp:114