From e45e85dae29af1a8fef5442aa7c72fa991ac83f1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 10 Jan 2007 17:41:25 +0000 Subject: Moved RDFWriter to RAUL. More work on LADSPA->LV2 converter (use RAUL's RDFWriter now). git-svn-id: http://svn.drobilla.net/lad/raul@246 a436a847-0d15-0410-975c-d299462d15a1 --- configure.ac | 23 ++++++ raul/Path.h | 155 +++----------------------------------- raul/RDFWriter.h | 73 ++++++++++++++++++ src/Makefile.am | 9 ++- src/RDFWriter.cpp | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 2 + 6 files changed, 332 insertions(+), 148 deletions(-) create mode 100644 raul/RDFWriter.h create mode 100644 src/RDFWriter.cpp diff --git a/configure.ac b/configure.ac index 3b40be9..c0eaf8c 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,29 @@ AC_CHECK_FUNCS([strdup strerror]) AC_CHECK_HEADER([pthread.h], [], AC_MSG_ERROR([Raul requires POSIX threads.])) +# Check for debugging flag +debug="no" +AC_ARG_ENABLE(debug, + [AS_HELP_STRING(--enable-debug, [Enable debugging (false)])], + [debug="$enableval"]) +if test "$debug" = "yes"; then + CFLAGS="-O0 -g -DDEBUG" + CXXFLAGS="-O0 -g -DDEBUG" +else + CFLAGS="$CFLAGS -DNDEBUG" + CXXFLAGS="$CXXFLAGS -DNDEBUG" +fi + +# Check for strict flag +strict="no" +AC_ARG_ENABLE(strict, + [AS_HELP_STRING(--enable-strict, [Enable strict compiler warnings and errors (false)])], + [strict="$enableval"]) +if test "$strict" = "yes"; then + CFLAGS="$CFLAGS -std=c99 -pedantic -Wall -Wextra -Wconversion -Winit-self" + CXXFLAGS="$CXXFLAGS -ansi -pedantic -Wall -Wextra -Wconversion -Winit-self" +fi + # Build unit tests? build_unit_tests="no" AC_ARG_ENABLE(unit-tests, diff --git a/raul/Path.h b/raul/Path.h index c19767d..200d05e 100644 --- a/raul/Path.h +++ b/raul/Path.h @@ -1,4 +1,4 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. +/* This file is part of Ingen. Copyright (C) 2006-2007 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 @@ -63,142 +63,20 @@ public: assert(is_valid(cpath)); } - - static bool is_valid(const std::basic_string& path) - { - if (path.length() == 0) - return false; - - // Must start with a / - if (path.at(0) != '/') - return false; - - // Must not end with a slash unless "/" - if (path.length() > 1 && path.at(path.length()-1) == '/') - return false; + static bool is_valid(const std::basic_string& path); - assert(path.find_last_of("/") != string::npos); - - // Double slash not allowed - if (path.find("//") != string::npos) - return false; - - // All characters must be printable ASCII - for (size_t i=0; i < path.length(); ++i) - if (path.at(i) < 32 || path.at(i) > 126) - return false; - - // Disallowed characters - if ( path.find(" ") != string::npos - || path.find("#") != string::npos - || path.find("*") != string::npos - || path.find(",") != string::npos - || path.find("?") != string::npos - || path.find("[") != string::npos - || path.find("]") != string::npos - || path.find("{") != string::npos - || path.find("}") != string::npos) - return false; - - return true; - } - - static bool is_valid_name(const std::basic_string& path) + static bool is_valid_name(const std::basic_string& name) { - return is_valid(string("/").append(path)); + return is_valid(string("/").append(name)); } + static string pathify(const std::basic_string& str); + static string nameify(const std::basic_string& str); - /** Convert a string to a valid full path. - * - * This will make a best effort at turning @a str into a complete, valid - * Path, and will always return one. - */ - static string pathify(const std::basic_string& str) - { - string path = str; - - if (path.length() == 0) - return "/"; // this might not be wise - - // Must start with a / - if (path.at(0) != '/') - path = string("/").append(path); - - // Must not end with a slash unless "/" - if (path.length() > 1 && path.at(path.length()-1) == '/') - path = path.substr(0, path.length()-1); // chop trailing slash - - assert(path.find_last_of("/") != string::npos); - - replace_invalid_chars(path, false); - - assert(is_valid(path)); - - return path; - } + static void replace_invalid_chars(string& str, bool replace_slash = false); - /** Convert a string to a valid name (or "method" - tokens between slashes) - * - * This will strip all slashes, etc, and always return a valid name/method. - */ - static string nameify(const std::basic_string& str) - { - string name = str; - - if (name.length() == 0) - return "."; // this might not be wise - - replace_invalid_chars(name, true); - - assert(is_valid(string("/") + name)); - - return name; - } - - - /** 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] == '_') { - str[i+1] = std::toupper(str[i+1]); // capitalize next char - str = str.substr(0, i) + str.substr(i+1); // chop space/underscore - --i; - } 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] == '?' - || (replace_slash && str[i] == '/')) { - 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; - } - - } - } + 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 '/'). @@ -224,6 +102,7 @@ public: return (parent == "") ? "/" : parent; } + /** Parent path with a "/" appended. * * This exists to avoid needing to be careful about the special case of "/". @@ -238,20 +117,6 @@ public: else return (*this) + "/"; } - - inline bool is_child_of(const Path& parent) const - { - /*return (length() > parent.length() - && substr(0, parent.length()) == parent - && (*this)[parent.length()] == '/');*/ - const string parent_base = parent.base(); - return (substr(0, parent_base.length()) == parent_base); - } - - inline bool is_parent_of(const Path& child) const - { - return child.is_child_of(*this); - } }; diff --git a/raul/RDFWriter.h b/raul/RDFWriter.h new file mode 100644 index 0000000..c67645e --- /dev/null +++ b/raul/RDFWriter.h @@ -0,0 +1,73 @@ +/* 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 RDFWRITER_H +#define RDFWRITER_H + +#include +#include +#include +#include +#include "raul/Atom.h" +using std::string; using std::map; + + +class RdfId { +public: + enum Type { ANONYMOUS, RESOURCE }; + + RdfId(Type t, const string& s) : _type(t), _string(s) {} + + Type type() const { return _type; } + const string& to_string() const { return _string; } + +private: + Type _type; + string _string; ///< URI or blank node ID, depending on _type +}; + + +class RDFWriter { +public: + RDFWriter(); + + void add_prefix(const string& prefix, const string& uri); + string expand_uri(const string& uri); + + void start_to_filename(const string& filename) throw (std::logic_error); + void start_to_string() throw (std::logic_error); + string finish() throw (std::logic_error); + + bool serialization_in_progress() { return (_serializer != NULL); } + + void write(const RdfId& subject, + const RdfId& predicate, + const RdfId& object); + + void write(const RdfId& subject, + const RdfId& predicate, + const Atom& object); + +private: + void setup_prefixes(); + + raptor_serializer* _serializer; + unsigned char* _string_output; + map _prefixes; +}; + + +#endif // RDFWRITER_H diff --git a/src/Makefile.am b/src/Makefile.am index aa1143e..bbbad24 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,13 @@ AM_CXXFLAGS = -I$(top_srcdir) -lib_LTLIBRARIES = libraul.la +libraul_la_CFLAGS = @RAPTOR_CFLAGS@ +libraul_la_LIBADD = @RAPTOR_LIBS@ -#libraul_la_LIBADD = @FOO_LIBS@ +lib_LTLIBRARIES = libraul.la libraul_la_SOURCES = \ - Thread.cpp + Thread.cpp \ + Path.cpp \ + RDFWriter.cpp diff --git a/src/RDFWriter.cpp b/src/RDFWriter.cpp new file mode 100644 index 0000000..9540380 --- /dev/null +++ b/src/RDFWriter.cpp @@ -0,0 +1,218 @@ +/* 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 + */ + +#include "raul/RDFWriter.h" +#include "raul/AtomRaptor.h" + +#define U(x) ((const unsigned char*)(x)) + +//static const char* const RDF_LANG = "rdfxml-abbrev"; +static const char* const RDF_LANG = "turtle"; + + +RDFWriter::RDFWriter() + : _serializer(NULL) + , _string_output(NULL) +{ + add_prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + //add_prefix("rdfs", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); +} + + +void +RDFWriter::add_prefix(const string& prefix, const string& uri) +{ + _prefixes[prefix] = uri; +} + + +void +RDFWriter::setup_prefixes() +{ + assert(_serializer); + + for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) { + raptor_serialize_set_namespace(_serializer, + raptor_new_uri(U(i->second.c_str())), U(i->first.c_str())); + } +} + + +/** Expands the prefix of URI, if the prefix is registered. + */ +string +RDFWriter::expand_uri(const string& uri) +{ + for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) + if (uri.substr(0, i->first.length()+1) == i->first + ":") + return i->second + uri.substr(i->first.length()+1); + + return uri; +} + + + +/** Begin a serialization to a file. + * + * This must be called before any write methods. + */ +void +RDFWriter::start_to_filename(const string& filename) throw (std::logic_error) +{ + if (_serializer) + throw std::logic_error("start_to_string called with serialization in progress"); + + raptor_init(); + _serializer = raptor_new_serializer(RDF_LANG); + setup_prefixes(); + raptor_serialize_start_to_filename(_serializer, filename.c_str()); +} + + +/** Begin a serialization to a string. + * + * This must be called before any write methods. + * + * The results of the serialization will be returned by the finish() method after + * the desired objects have been serialized. + */ +void +RDFWriter::start_to_string() throw (std::logic_error) +{ + if (_serializer) + throw std::logic_error("start_to_string called with serialization in progress"); + + raptor_init(); + _serializer = raptor_new_serializer(RDF_LANG); + setup_prefixes(); + raptor_serialize_start_to_string(_serializer, + NULL /*base_uri*/, + (void**)&_string_output, + NULL /*size*/); +} + + +/** Finish a serialization. + * + * If this was a serialization to a string, the serialization output + * will be returned, otherwise the empty string is returned. + */ +string +RDFWriter::finish() throw(std::logic_error) +{ + string ret = ""; + + if (!_serializer) + throw std::logic_error("finish() called with no serialization in progress"); + + raptor_serialize_end(_serializer); + + if (_string_output) { + ret = string((char*)_string_output); + free(_string_output); + _string_output = NULL; + } + + raptor_free_serializer(_serializer); + _serializer = NULL; + + raptor_finish(); + + return ret; +} + + +void +RDFWriter::write(const RdfId& subject, + const RdfId& predicate, + const RdfId& object) +{ + assert(_serializer); + + raptor_statement triple; + + // FIXME: leaks? + + if (subject.type() == RdfId::RESOURCE) { + triple.subject = (void*)raptor_new_uri((const unsigned char*) + expand_uri(subject.to_string()).c_str()); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(subject.type() == RdfId::ANONYMOUS); + triple.subject = (unsigned char*)(strdup(subject.to_string().c_str())); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + assert(predicate.type() == RdfId::RESOURCE); + triple.predicate = (void*)raptor_new_uri((const unsigned char*) + expand_uri(predicate.to_string()).c_str()); + triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + + if (object.type() == RdfId::RESOURCE) { + triple.object = (void*)raptor_new_uri((const unsigned char*) + expand_uri(object.to_string()).c_str()); + triple.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(object.type() == RdfId::ANONYMOUS); + triple.object = (unsigned char*)(strdup(object.to_string().c_str())); + triple.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + raptor_serialize_statement(_serializer, &triple); + + if (subject.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.subject); + + raptor_free_uri((raptor_uri*)triple.predicate); + + if (object.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.object); +} + + +void +RDFWriter::write(const RdfId& subject, + const RdfId& predicate, + const Atom& object) +{ + assert(_serializer); + + raptor_statement triple; + + if (subject.type() == RdfId::RESOURCE) { + triple.subject = (void*)raptor_new_uri((const unsigned char*) + expand_uri(subject.to_string()).c_str()); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(subject.type() == RdfId::ANONYMOUS); + triple.subject = (unsigned char*)(strdup(subject.to_string().c_str())); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + assert(predicate.type() == RdfId::RESOURCE); + triple.predicate = (void*)raptor_new_uri((const unsigned char*) + expand_uri(predicate.to_string()).c_str()); + triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + + AtomRaptor::atom_to_triple_object(&triple, object); + + raptor_serialize_statement(_serializer, &triple); + + if (subject.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.subject); + + raptor_free_uri((raptor_uri*)triple.predicate); +} diff --git a/tests/Makefile.am b/tests/Makefile.am index 2389628..6cb5a59 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,6 +4,8 @@ AM_CXXFLAGS = -I.. -lpthread bin_PROGRAMS = path_test thread_test queue_test thread_test_LDADD = ../src/libraul.la +path_test_LDADD = ../src/libraul.la +queue_test_LDADD = ../src/libraul.la path_test_SOURCES = path_test.cpp thread_test_SOURCES = thread_test.cpp -- cgit v1.2.1