summaryrefslogtreecommitdiffstats
path: root/raul/Path.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-07-24 19:26:47 +0000
committerDavid Robillard <d@drobilla.net>2007-07-24 19:26:47 +0000
commit3b4a308ff647def75e647ac8f97e1e48b57672c3 (patch)
treea33e3a5c78f2ee7f23e151f39fe17ab39b0b81c8 /raul/Path.hpp
parentfcb302e6248527b2f1fb655f4d0a64af18f5f059 (diff)
downloadraul-3b4a308ff647def75e647ac8f97e1e48b57672c3.tar.gz
raul-3b4a308ff647def75e647ac8f97e1e48b57672c3.tar.bz2
raul-3b4a308ff647def75e647ac8f97e1e48b57672c3.zip
Consistently rename all C++ files .cpp/.hpp.
Fix (some) inclusion guard names to not clash with other libs. git-svn-id: http://svn.drobilla.net/lad/raul@613 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul/Path.hpp')
-rw-r--r--raul/Path.hpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/raul/Path.hpp b/raul/Path.hpp
new file mode 100644
index 0000000..5776309
--- /dev/null
+++ b/raul/Path.hpp
@@ -0,0 +1,128 @@
+/* This file is part of Raul.
+ * Copyright (C) 2007 Dave 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 <cctype>
+#include <string>
+#include <cassert>
+using std::string;
+
+namespace Raul {
+
+
+/** Simple wrapper around standard string with useful path-specific methods.
+ *
+ * This enforces that a Path is a valid OSC path (though it is used for
+ * GraphObject paths, which aren't directly OSC paths but a portion of one).
+ *
+ * A path is divided by slashes (/). The first character MUST be a slash, and
+ * the last character MUST NOT be a slash (except in the special case of the
+ * root path "/", which is the only valid single-character path).
+ *
+ * Valid characters are the 95 printable ASCII characters (32-126), excluding:
+ * space # * , ? [ ] { }
+ *
+ * \ingroup raul
+ */
+class Path : public std::basic_string<char> {
+public:
+
+ /** Construct a Path from an std::string.
+ *
+ * It is a fatal error to construct a Path from an invalid string,
+ * use is_valid first to check.
+ */
+ Path(const std::basic_string<char>& path)
+ : std::basic_string<char>(path)
+ {
+ assert(is_valid(path));
+ }
+
+
+ /** Construct a Path from a C string.
+ *
+ * It is a fatal error to construct a Path from an invalid string,
+ * use is_valid first to check.
+ */
+ Path(const char* cpath)
+ : std::basic_string<char>(cpath)
+ {
+ assert(is_valid(cpath));
+ }
+
+ static bool is_valid(const std::basic_string<char>& path);
+
+ static bool is_valid_name(const std::basic_string<char>& name)
+ {
+ return is_valid(string("/").append(name));
+ }
+
+ static string pathify(const std::basic_string<char>& str);
+ static string nameify(const std::basic_string<char>& str);
+
+ static void replace_invalid_chars(string& str, bool replace_slash = false);
+
+ bool is_child_of(const Path& parent) const;
+ bool is_parent_of(const Path& child) const;
+
+
+ /** Return the name of this object (everything after the last '/').
+ * This is the "method name" for OSC paths.
+ */
+ inline std::basic_string<char> name() const
+ {
+ if ((*this) == "/")
+ return "";
+ else
+ return substr(find_last_of("/")+1);
+ }
+
+
+ /** Return the parent's path.
+ *
+ * Calling this on the path "/" will return "/".
+ * This is the (deepest) "container path" for OSC paths.
+ */
+ inline Path parent() const
+ {
+ std::basic_string<char> parent = substr(0, find_last_of("/"));
+ return (parent == "") ? "/" : parent;
+ }
+
+
+ /** Parent path with a "/" appended.
+ *
+ * This exists to avoid needing to be careful about the special case of "/".
+ * To create a child of a path, use parent.base() + child_name.
+ * Returned value is always a valid path, with the single exception that
+ * the last character is "/".
+ */
+ inline string base() const
+ {
+ if ((*this) == "/")
+ return *this;
+ else
+ return (*this) + "/";
+ }
+};
+
+
+} // namespace Raul
+
+#endif // RAUL_PATH_HPP