summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util/Atom.h12
-rw-r--r--src/common/util/LibloAtom.h2
-rw-r--r--src/common/util/Makefile.am3
-rw-r--r--src/common/util/Path.h28
-rw-r--r--src/common/util/RedlandAtom.h77
5 files changed, 112 insertions, 10 deletions
diff --git a/src/common/util/Atom.h b/src/common/util/Atom.h
index f28b9339..2a6ae982 100644
--- a/src/common/util/Atom.h
+++ b/src/common/util/Atom.h
@@ -20,6 +20,9 @@
#include <cstdlib>
#include <cassert>
#include <cstring>
+#include <string>
+
+using std::string;
/** An OSC atom (fundamental data types OSC messages are composed of).
@@ -34,10 +37,11 @@ public:
BLOB
};
- Atom() : _type(NIL), _blob_val(0) {}
- Atom(int32_t val) : _type(INT), _int_val(val) {}
- Atom(float val) : _type(FLOAT), _float_val(val) {}
- Atom(const char* val) : _type(STRING), _string_val(strdup(val)) {}
+ Atom() : _type(NIL), _blob_val(0) {}
+ Atom(int32_t val) : _type(INT), _int_val(val) {}
+ Atom(float val) : _type(FLOAT), _float_val(val) {}
+ Atom(const char* val) : _type(STRING), _string_val(strdup(val)) {}
+ Atom(const string& val) : _type(STRING), _string_val(strdup(val.c_str())) {}
Atom(void* val) : _type(BLOB), _blob_size(sizeof(val)), _blob_val(malloc(_blob_size))
{ memcpy(_blob_val, val, sizeof(_blob_size)); }
diff --git a/src/common/util/LibloAtom.h b/src/common/util/LibloAtom.h
index 02a469fe..db76a807 100644
--- a/src/common/util/LibloAtom.h
+++ b/src/common/util/LibloAtom.h
@@ -23,7 +23,7 @@
/** Support for serializing an Atom to/from liblo messages.
*
- * (Here to prevent a unnecessary liblo dependency on Atom).
+ * (Here to prevent a unnecessary liblo dependency for Atom).
*/
class LibloAtom {
public:
diff --git a/src/common/util/Makefile.am b/src/common/util/Makefile.am
index 845a5d9a..6bddb707 100644
--- a/src/common/util/Makefile.am
+++ b/src/common/util/Makefile.am
@@ -8,4 +8,5 @@ EXTRA_DIST = \
Thread.h \
Slave.h \
Atom.h \
- LibloAtom.h
+ LibloAtom.h \
+ RedlandAtom.h
diff --git a/src/common/util/Path.h b/src/common/util/Path.h
index 2fcf5774..bcb047ad 100644
--- a/src/common/util/Path.h
+++ b/src/common/util/Path.h
@@ -17,6 +17,7 @@
#ifndef PATH_H
#define PATH_H
+#include <cctype>
#include <string>
#include <cassert>
using std::string;
@@ -137,7 +138,7 @@ public:
/** Convert a string to a valid name (or "method" - tokens between slashes)
*
- * This will strip all slashes and always return a valid name/method.
+ * This will strip all slashes, etc, and always return a valid name/method.
*/
static string nameify(const std::basic_string<char>& str)
{
@@ -155,18 +156,22 @@ public:
/** Replace any invalid characters in @a str with a suitable replacement.
+ *
+ * Makes a pretty name - underscores are a valid character, but this chops
+ * both spaces and underscores, uppercasing the next letter, to create
+ * uniform CamelCase names that look nice
*/
static void replace_invalid_chars(string& str, bool replace_slash = false)
{
for (size_t i=0; i < str.length(); ++i) {
- if (str[i] == ' ') {
- str[i] = '_';
+ if (str[i] == ' ' || str[i] == '_') {
+ str[i+1] = std::toupper(str[i+1]); // capitalize next char
+ str = str.substr(0, i) + str.substr(i+1); // chop space/underscore
} else if (str[i] == '[' || str[i] == '{') {
str[i] = '(';
} else if (str[i] == ']' || str[i] == '}') {
str[i] = ')';
} else if (str[i] < 32 || str.at(i) > 126
- || str[i] == ' '
|| str[i] == '#'
|| str[i] == '*'
|| str[i] == ','
@@ -175,6 +180,21 @@ public:
str[i] = '.';
}
}
+
+ // Chop brackets
+ while (true) {
+
+ const string::size_type open = str.find("(");
+ const string::size_type close = str.find(")");
+
+ if (open != string::npos) {
+ if (close != string::npos)
+ str.erase(open, (close - open) + 1);
+ } else {
+ break;
+ }
+
+ }
}
diff --git a/src/common/util/RedlandAtom.h b/src/common/util/RedlandAtom.h
new file mode 100644
index 00000000..6b5658cf
--- /dev/null
+++ b/src/common/util/RedlandAtom.h
@@ -0,0 +1,77 @@
+/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard.
+ *
+ * Ingen 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.
+ *
+ * Ingen 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 REDLAND_ATOM_H
+#define REDLAND_ATOM_H
+
+#include <redland.h>
+#include "util/Atom.h"
+
+#define U(x) ((const unsigned char*)(x))
+
+/** Support for serializing an Atom to/from RDF (via redland aka librdf).
+ *
+ * (Here to prevent a unnecessary redland dependency for Atom).
+ */
+class RedlandAtom {
+public:
+ static librdf_node* atom_to_node(librdf_world* world, const Atom& atom) {
+ char tmp_buf[32];
+
+ switch (atom.type()) {
+ //case NIL:
+ // (see below)
+ //break;
+ case Atom::INT:
+ snprintf(tmp_buf, 32, "%d", atom.get_int32());
+ return librdf_new_node_from_typed_literal(world, U(tmp_buf), NULL, librdf_new_uri(world, U("http://www.w3.org/2001/XMLSchema#integer")));
+ break;
+ case Atom::FLOAT:
+ snprintf(tmp_buf, 32, "%f", atom.get_float());
+ return librdf_new_node_from_typed_literal(world, U(tmp_buf), NULL, librdf_new_uri(world, U("http://www.w3.org/2001/XMLSchema#float")));
+ break;
+ case Atom::STRING:
+ return librdf_new_node_from_literal(world, U(atom.get_string()), NULL, 0);
+ case Atom::BLOB:
+ cerr << "WARNING: Unserializable atom!" << endl;
+ return NULL;
+ default: // This catches Atom::Type::NIL too
+ return librdf_new_node(world); // blank node
+ }
+ }
+
+ static Atom node_to_atom(librdf_node* node) {
+ /*switch (type) {
+ case 'i':
+ return Atom(arg->i);
+ case 'f':
+ return Atom(arg->f);
+ case 's':
+ return Atom(&arg->s);
+ //case 'b'
+ // FIXME: How to get a blob from a lo_arg?
+ //return Atom(arg->b);
+ default:
+ return Atom();
+ }*/
+ cerr << "FIXME: node_to_atom\n";
+ return Atom();
+ }
+
+};
+
+
+#endif // REDLAND_ATOM_H