RAUL  0.5.1
AtomRDF.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_ATOM_RDF_HPP
19 #define RAUL_ATOM_RDF_HPP
20 
21 #include <cstring>
22 #include <string>
23 #include <sstream>
24 
25 #include CONFIG_H_PATH
26 
27 #include <redlandmm/Node.hpp>
28 #include <redlandmm/World.hpp>
29 
30 #define CUC(x) ((const unsigned char*)(x))
31 
32 namespace Raul {
33 
38 namespace AtomRDF {
39 
41 inline Atom
42 node_to_atom(const Redland::Node& node)
43 {
44  if (node.type() == Redland::Node::RESOURCE)
45  return Atom(node.to_c_string());
46  else if (node.is_float())
47  return Atom(node.to_float());
48  else if (node.is_int())
49  return Atom(node.to_int());
50  else if (node.is_bool())
51  return Atom(node.to_bool());
52  else
53  return Atom(node.to_c_string());
54 }
55 
56 
60 inline Redland::Node
61 atom_to_node(Redland::World& world, const Atom& atom)
62 {
63  std::ostringstream os;
64  std::string str;
65  librdf_uri* type = NULL;
66  librdf_node* node = NULL;
67 
68  switch (atom.type()) {
69  case Atom::INT:
70  os << atom.get_int32();
71  str = os.str();
72  // xsd:integer -> pretty integer literals in Turtle
73  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#integer"));
74  break;
75  case Atom::FLOAT:
76  os.precision(8);
77  os << atom.get_float();
78  str = os.str();
79  if (str.find(".") == std::string::npos)
80  str += ".0";
81  // xsd:decimal -> pretty decimal (float) literals in Turtle
82  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
83  break;
84  case Atom::BOOL:
85  // xsd:boolean -> pretty boolean literals in Turtle
86  if (atom.get_bool())
87  str = "true";
88  else
89  str = "false";
90  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean"));
91  break;
92  case Atom::STRING:
93  str = atom.get_string();
94  break;
95  case Atom::BLOB:
96  case Atom::NIL:
97  default:
98  //std::cerr << "WARNING: Unserializable Atom!" << std::endl;
99  break;
100  }
101 
102  if (str != "")
103  node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
104 
105  return Redland::Node(world, node);
106 }
107 
108 
109 } // namespace AtomRDF
110 } // namespace Raul
111 
112 #endif // RAUL_ATOM_RDF_HPP
113 
An OSC atom (fundamental data types OSC messages are composed of).
Definition: Atom.hpp:39
Redland::Node atom_to_node(Redland::World &world, const Atom &atom)
Convert a Raul::Atom to a Redland::Node Note that not all Atoms are serialisable, the returned node s...
Definition: AtomRDF.hpp:61
Definition: Array.hpp:26
Type type() const
Type of this atom.
Definition: Atom.hpp:161
Atom node_to_atom(const Redland::Node &node)
Convert a Redland::Node to a Raul::Atom.
Definition: AtomRDF.hpp:42