summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/Path.hpp6
-rw-r--r--raul/Symbol.hpp70
-rw-r--r--src/Makefile.am1
-rw-r--r--src/Symbol.cpp77
4 files changed, 152 insertions, 2 deletions
diff --git a/raul/Path.hpp b/raul/Path.hpp
index 8cda1fa..092b337 100644
--- a/raul/Path.hpp
+++ b/raul/Path.hpp
@@ -24,6 +24,8 @@
#include <cstring>
#include <cassert>
+#include <raul/Symbol.hpp>
+
namespace Raul {
@@ -44,7 +46,7 @@ namespace Raul {
class Path : public std::basic_string<char> {
public:
- /** Contrust an uninitialzed path, because the STL is annoying. */
+ /** Construct an uninitialzed path, because the STL is annoying. */
Path() : std::basic_string<char>("/") {}
/** Construct a Path from an std::string.
@@ -88,7 +90,7 @@ public:
/** 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 {
+ inline Symbol name() const {
if ((*this) == "/")
return "";
else
diff --git a/raul/Symbol.hpp b/raul/Symbol.hpp
new file mode 100644
index 0000000..0b8cba7
--- /dev/null
+++ b/raul/Symbol.hpp
@@ -0,0 +1,70 @@
+/* This file is part of Raul.
+ * Copyright (C) 2008 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_SYMBOL_HPP
+#define RAUL_SYMBOL_HPP
+
+#include <iostream>
+#include <cctype>
+#include <string>
+#include <cstring>
+#include <cassert>
+
+namespace Raul {
+
+
+/** A restricted string (C identifier, which is a component of a path).
+ *
+ * A Symbol is an lv2-compliant symbol, which is also valid as a component of
+ * a URI, filesystem or OSC path, programming language identifier, etc.
+ * \ingroup raul
+ */
+class Symbol : public std::basic_string<char> {
+public:
+
+ /** Construct a Symbol from an std::string.
+ *
+ * It is a fatal error to construct a Symbol from an invalid string,
+ * use is_valid first to check.
+ */
+ Symbol(const std::basic_string<char>& symbol)
+ : std::basic_string<char>(symbol)
+ {
+ assert(is_valid(symbol));
+ }
+
+
+ /** Construct a Symbol from a C string.
+ *
+ * It is a fatal error to construct a Symbol from an invalid string,
+ * use is_valid first to check.
+ */
+ Symbol(const char* csymbol)
+ : std::basic_string<char>(csymbol)
+ {
+ assert(is_valid(csymbol));
+ }
+
+ static bool is_valid(const std::basic_string<char>& path);
+
+ static std::string symbolify(const std::basic_string<char>& str);
+};
+
+
+} // namespace Raul
+
+#endif // RAUL_SYMBOL_HPP
diff --git a/src/Makefile.am b/src/Makefile.am
index 57e5152..7932412 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,6 +7,7 @@ lib_LTLIBRARIES = libraul.la
libraul_la_SOURCES = \
Maid.cpp \
Path.cpp \
+ Symbol.cpp \
SMFReader.cpp \
SMFWriter.cpp \
Thread.cpp
diff --git a/src/Symbol.cpp b/src/Symbol.cpp
new file mode 100644
index 0000000..f551b0a
--- /dev/null
+++ b/src/Symbol.cpp
@@ -0,0 +1,77 @@
+/* This file is part of Raul.
+ * Copyright (C) 2008 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
+ */
+
+#include <raul/Symbol.hpp>
+#include <raul/Path.hpp>
+
+using namespace std;
+
+namespace Raul {
+
+
+bool
+Symbol::is_valid(const std::basic_string<char>& symbol)
+{
+ if (symbol.length() == 0)
+ return false;
+
+ // Slash not allowed
+ if (symbol.find("/") != string::npos)
+ return false;
+
+ // All characters must be _, a-z, A-Z, 0-9
+ for (size_t i=0; i < symbol.length(); ++i)
+ if (symbol[i] != '_'
+ && (symbol[i] < 'a' || symbol[i] > 'z')
+ && (symbol[i] < 'A' || symbol[i] > 'Z')
+ && (symbol[i] < '0' || symbol[i] > '9') )
+ return false;
+
+ // First character must not be a number
+ if (std::isdigit(symbol[0]))
+ return false;
+
+ return true;
+}
+
+
+/** Convert a string to a valid symbol.
+ *
+ * This will make a best effort at turning @a str into a complete, valid
+ * Symbol, and will always return one.
+ */
+string
+Symbol::symbolify(const std::basic_string<char>& str)
+{
+ string symbol = str;
+
+ Path::replace_invalid_chars(symbol, true);
+
+ if (symbol.length() == 0)
+ return "_";
+
+ if (isdigit(symbol[0]))
+ symbol = string("_").append(symbol);
+
+ assert(is_valid(symbol));
+
+ return symbol;
+}
+
+
+} // namespace Raul
+