annotate win32-mingw/include/sord/sordmm.hpp @ 48:9530b331f8c1

Add Cap'n Proto source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 11:17:01 +0100
parents 7f77decdb26d
children
rev   line source
chris@16 1 /*
chris@16 2 Copyright 2011-2013 David Robillard <http://drobilla.net>
chris@16 3
chris@16 4 Permission to use, copy, modify, and/or distribute this software for any
chris@16 5 purpose with or without fee is hereby granted, provided that the above
chris@16 6 copyright notice and this permission notice appear in all copies.
chris@16 7
chris@16 8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
chris@16 9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
chris@16 10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
chris@16 11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
chris@16 12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
chris@16 13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
chris@16 14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
chris@16 15 */
chris@16 16
chris@16 17 /**
chris@16 18 @file sordmm.hpp
chris@16 19 Public Sord C++ API.
chris@16 20 */
chris@16 21
chris@16 22 #ifndef SORD_SORDMM_HPP
chris@16 23 #define SORD_SORDMM_HPP
chris@16 24
chris@16 25 #include <cassert>
chris@16 26 #include <cstring>
chris@16 27 #include <cstdlib>
chris@16 28 #include <iostream>
chris@16 29 #include <set>
chris@16 30 #include <string>
chris@16 31 #include <sstream>
chris@16 32
chris@16 33 #include "serd/serd.h"
chris@16 34 #include "sord/sord.h"
chris@16 35
chris@16 36 #define SORD_NS_XSD "http://www.w3.org/2001/XMLSchema#"
chris@16 37
chris@16 38 namespace Sord {
chris@16 39
chris@16 40 /** Utility base class to prevent copying. */
chris@16 41 class Noncopyable {
chris@16 42 protected:
chris@16 43 Noncopyable() {}
chris@16 44 ~Noncopyable() {}
chris@16 45 private:
chris@16 46 Noncopyable(const Noncopyable&);
chris@16 47 const Noncopyable& operator=(const Noncopyable&);
chris@16 48 };
chris@16 49
chris@16 50 /** C++ wrapper for a Sord object. */
chris@16 51 template <typename T>
chris@16 52 class Wrapper {
chris@16 53 public:
chris@16 54 inline Wrapper(T c_obj = NULL) : _c_obj(c_obj) {}
chris@16 55
chris@16 56 inline T c_obj() { return _c_obj; }
chris@16 57 inline const T c_obj() const { return _c_obj; }
chris@16 58
chris@16 59 protected:
chris@16 60 T _c_obj;
chris@16 61 };
chris@16 62
chris@16 63 /** Collection of RDF namespaces with prefixes. */
chris@16 64 class Namespaces : public Wrapper<SerdEnv*> {
chris@16 65 public:
chris@16 66 Namespaces() : Wrapper<SerdEnv*>(serd_env_new(NULL)) {}
chris@16 67 ~Namespaces() { serd_env_free(_c_obj); }
chris@16 68
chris@16 69 static inline SerdNode string_to_node(SerdType type, const std::string& s) {
chris@16 70 SerdNode ret = {
chris@16 71 (const uint8_t*)s.c_str(), s.length(), s.length(), 0, type };
chris@16 72 return ret;
chris@16 73 }
chris@16 74
chris@16 75 inline void add(const std::string& name,
chris@16 76 const std::string& uri) {
chris@16 77 const SerdNode name_node = string_to_node(SERD_LITERAL, name);
chris@16 78 const SerdNode uri_node = string_to_node(SERD_URI, uri);
chris@16 79 serd_env_set_prefix(_c_obj, &name_node, &uri_node);
chris@16 80 }
chris@16 81
chris@16 82 inline std::string qualify(std::string uri) const {
chris@16 83 const SerdNode uri_node = string_to_node(SERD_URI, uri);
chris@16 84 SerdNode prefix;
chris@16 85 SerdChunk suffix;
chris@16 86 if (serd_env_qualify(_c_obj, &uri_node, &prefix, &suffix)) {
chris@16 87 std::string ret((const char*)prefix.buf, prefix.n_bytes);
chris@16 88 ret.append(":").append((const char*)suffix.buf, suffix.len);
chris@16 89 return ret;
chris@16 90 }
chris@16 91 return uri;
chris@16 92 }
chris@16 93
chris@16 94 inline std::string expand(const std::string& curie) const {
chris@16 95 assert(curie.find(":") != std::string::npos);
chris@16 96 SerdNode curie_node = string_to_node(SERD_CURIE, curie);
chris@16 97 SerdChunk uri_prefix;
chris@16 98 SerdChunk uri_suffix;
chris@16 99 if (!serd_env_expand(_c_obj, &curie_node, &uri_prefix, &uri_suffix)) {
chris@16 100 std::string ret((const char*)uri_prefix.buf, uri_prefix.len);
chris@16 101 ret.append((const char*)uri_suffix.buf, uri_suffix.len);
chris@16 102 return ret;
chris@16 103 }
chris@16 104 std::cerr << "CURIE `" << curie << "' has unknown prefix." << std::endl;
chris@16 105 return curie;
chris@16 106 }
chris@16 107 };
chris@16 108
chris@16 109 /** Sord library state. */
chris@16 110 class World : public Noncopyable, public Wrapper<SordWorld*> {
chris@16 111 public:
chris@16 112 inline World()
chris@16 113 : _next_blank_id(0)
chris@16 114 {
chris@16 115 _c_obj = sord_world_new();
chris@16 116 }
chris@16 117
chris@16 118 inline ~World() {
chris@16 119 sord_world_free(_c_obj);
chris@16 120 }
chris@16 121
chris@16 122 inline uint64_t blank_id() { return _next_blank_id++; }
chris@16 123
chris@16 124 inline void add_prefix(const std::string& prefix, const std::string& uri) {
chris@16 125 _prefixes.add(prefix, uri);
chris@16 126 }
chris@16 127
chris@16 128 inline const Namespaces& prefixes() const { return _prefixes; }
chris@16 129 inline SordWorld* world() { return _c_obj; }
chris@16 130
chris@16 131 private:
chris@16 132 Namespaces _prefixes;
chris@16 133 std::set<std::string> _blank_ids;
chris@16 134 uint64_t _next_blank_id;
chris@16 135 };
chris@16 136
chris@16 137 /** An RDF Node (resource, literal, etc)
chris@16 138 */
chris@16 139 class Node : public Wrapper<SordNode*> {
chris@16 140 public:
chris@16 141 enum Type {
chris@16 142 UNKNOWN = 0,
chris@16 143 URI = SORD_URI,
chris@16 144 BLANK = SORD_BLANK,
chris@16 145 LITERAL = SORD_LITERAL
chris@16 146 };
chris@16 147
chris@16 148 inline Node() : Wrapper<SordNode*>(NULL), _world(NULL) {}
chris@16 149
chris@16 150 inline Node(World& world, Type t, const std::string& s);
chris@16 151 inline Node(World& world);
chris@16 152 inline Node(World& world, const SordNode* node);
chris@16 153 inline Node(World& world, SordNode* node, bool copy=false);
chris@16 154 inline Node(const Node& other);
chris@16 155 inline ~Node();
chris@16 156
chris@16 157 inline Type type() const {
chris@16 158 return _c_obj ? (Type)sord_node_get_type(_c_obj) : UNKNOWN;
chris@16 159 }
chris@16 160
chris@16 161 inline const SordNode* get_node() const { return _c_obj; }
chris@16 162 inline SordNode* get_node() { return _c_obj; }
chris@16 163
chris@16 164 const SerdNode* to_serd_node() {
chris@16 165 return sord_node_to_serd_node(_c_obj);
chris@16 166 }
chris@16 167
chris@16 168 inline bool is_valid() const { return type() != UNKNOWN; }
chris@16 169
chris@16 170 inline bool operator<(const Node& other) const {
chris@16 171 if (type() != other.type()) {
chris@16 172 return type() < other.type();
chris@16 173 } else {
chris@16 174 return to_string() < other.to_string();
chris@16 175 }
chris@16 176 }
chris@16 177
chris@16 178 Node& operator=(const Node& other) {
chris@16 179 if (&other != this) {
chris@16 180 if (_c_obj) {
chris@16 181 sord_node_free(_world->c_obj(), _c_obj);
chris@16 182 }
chris@16 183 _world = other._world;
chris@16 184 _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : NULL;
chris@16 185 }
chris@16 186 return *this;
chris@16 187 }
chris@16 188
chris@16 189 inline bool operator==(const Node& other) const {
chris@16 190 return sord_node_equals(_c_obj, other._c_obj);
chris@16 191 }
chris@16 192
chris@16 193 inline const uint8_t* to_u_string() const;
chris@16 194 inline const char* to_c_string() const;
chris@16 195 inline std::string to_string() const;
chris@16 196
chris@16 197 inline bool is_literal_type(const char* type_uri) const;
chris@16 198
chris@16 199 inline bool is_uri() const { return _c_obj && type() == URI; }
chris@16 200 inline bool is_blank() const { return _c_obj && type() == BLANK; }
chris@16 201 inline bool is_int() const { return is_literal_type(SORD_NS_XSD "integer"); }
chris@16 202 inline bool is_float() const { return is_literal_type(SORD_NS_XSD "decimal"); }
chris@16 203 inline bool is_bool() const { return is_literal_type(SORD_NS_XSD "boolean"); }
chris@16 204
chris@16 205 inline int to_int() const;
chris@16 206 inline float to_float() const;
chris@16 207 inline bool to_bool() const;
chris@16 208
chris@16 209 inline static Node blank_id(World& world, const std::string base="b") {
chris@16 210 const uint64_t num = world.blank_id();
chris@16 211 std::ostringstream ss;
chris@16 212 ss << base << num;
chris@16 213 return Node(world, Node::BLANK, ss.str());
chris@16 214 }
chris@16 215
chris@16 216 private:
chris@16 217 World* _world;
chris@16 218 };
chris@16 219
chris@16 220 inline std::ostream&
chris@16 221 operator<<(std::ostream& os, const Node& node)
chris@16 222 {
chris@16 223 return os << node.to_string();
chris@16 224 }
chris@16 225
chris@16 226 class URI : public Node {
chris@16 227 public:
chris@16 228 inline URI(World& world, const std::string& s)
chris@16 229 : Node(world, Node::URI, s) {}
chris@16 230 inline URI(World& world, const std::string& s, const std::string& base)
chris@16 231 : Node(world, sord_new_relative_uri(world.world(),
chris@16 232 (const uint8_t*)s.c_str(),
chris@16 233 (const uint8_t*)base.c_str()))
chris@16 234 {}
chris@16 235 };
chris@16 236
chris@16 237 class Curie : public Node {
chris@16 238 public:
chris@16 239 inline Curie(World& world, const std::string& s)
chris@16 240 : Node(world, Node::URI, world.prefixes().expand(s)) {}
chris@16 241 };
chris@16 242
chris@16 243 class Literal : public Node {
chris@16 244 public:
chris@16 245 inline Literal(World& world, const std::string& s)
chris@16 246 : Node(world, Node::LITERAL, s) {}
chris@16 247
chris@16 248 static inline Node decimal(World& world, double d, unsigned frac_digits) {
chris@16 249 const SerdNode val = serd_node_new_decimal(d, 7);
chris@16 250 const SerdNode type = serd_node_from_string(
chris@16 251 SERD_URI, (const uint8_t*)SORD_NS_XSD "decimal");
chris@16 252
chris@16 253 return Node(
chris@16 254 world,
chris@16 255 sord_node_from_serd_node(
chris@16 256 world.c_obj(), world.prefixes().c_obj(), &val, &type, NULL),
chris@16 257 false);
chris@16 258 }
chris@16 259
chris@16 260 static inline Node integer(World& world, int64_t i) {
chris@16 261 const SerdNode val = serd_node_new_integer(i);
chris@16 262 const SerdNode type = serd_node_from_string(
chris@16 263 SERD_URI, (const uint8_t*)SORD_NS_XSD "integer");
chris@16 264
chris@16 265 return Node(
chris@16 266 world,
chris@16 267 sord_node_from_serd_node(
chris@16 268 world.c_obj(), world.prefixes().c_obj(), &val, &type, NULL),
chris@16 269 false);
chris@16 270 }
chris@16 271 };
chris@16 272
chris@16 273 inline
chris@16 274 Node::Node(World& world, Type type, const std::string& s)
chris@16 275 : _world(&world)
chris@16 276 {
chris@16 277 switch (type) {
chris@16 278 case URI:
chris@16 279 _c_obj = sord_new_uri(
chris@16 280 world.world(), (const unsigned char*)s.c_str());
chris@16 281 break;
chris@16 282 case LITERAL:
chris@16 283 _c_obj = sord_new_literal(
chris@16 284 world.world(), NULL, (const unsigned char*)s.c_str(), NULL);
chris@16 285 break;
chris@16 286 case BLANK:
chris@16 287 _c_obj = sord_new_blank(
chris@16 288 world.world(), (const unsigned char*)s.c_str());
chris@16 289 break;
chris@16 290 default:
chris@16 291 _c_obj = NULL;
chris@16 292 }
chris@16 293
chris@16 294 assert(this->type() == type);
chris@16 295 }
chris@16 296
chris@16 297 inline
chris@16 298 Node::Node(World& world)
chris@16 299 : _world(&world)
chris@16 300 {
chris@16 301 Node me = blank_id(world);
chris@16 302 *this = me;
chris@16 303 }
chris@16 304
chris@16 305 inline
chris@16 306 Node::Node(World& world, const SordNode* node)
chris@16 307 : _world(&world)
chris@16 308 {
chris@16 309 _c_obj = sord_node_copy(node);
chris@16 310 }
chris@16 311
chris@16 312 inline
chris@16 313 Node::Node(World& world, SordNode* node, bool copy)
chris@16 314 : _world(&world)
chris@16 315 {
chris@16 316 _c_obj = copy ? sord_node_copy(node) : node;
chris@16 317 }
chris@16 318
chris@16 319 inline
chris@16 320 Node::Node(const Node& other)
chris@16 321 : Wrapper<SordNode*>()
chris@16 322 , _world(other._world)
chris@16 323 {
chris@16 324 if (_world) {
chris@16 325 _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : NULL;
chris@16 326 }
chris@16 327
chris@16 328 assert((!_c_obj && !other._c_obj) || to_string() == other.to_string());
chris@16 329 }
chris@16 330
chris@16 331 inline
chris@16 332 Node::~Node()
chris@16 333 {
chris@16 334 if (_world) {
chris@16 335 sord_node_free(_world->c_obj(), _c_obj);
chris@16 336 }
chris@16 337 }
chris@16 338
chris@16 339 inline std::string
chris@16 340 Node::to_string() const
chris@16 341 {
chris@16 342 return _c_obj ? (const char*)sord_node_get_string(_c_obj) : "";
chris@16 343 }
chris@16 344
chris@16 345 inline const char*
chris@16 346 Node::to_c_string() const
chris@16 347 {
chris@16 348 return (const char*)sord_node_get_string(_c_obj);
chris@16 349 }
chris@16 350
chris@16 351 inline const uint8_t*
chris@16 352 Node::to_u_string() const
chris@16 353 {
chris@16 354 return sord_node_get_string(_c_obj);
chris@16 355 }
chris@16 356
chris@16 357 inline bool
chris@16 358 Node::is_literal_type(const char* type_uri) const
chris@16 359 {
chris@16 360 if (_c_obj && sord_node_get_type(_c_obj) == SORD_LITERAL) {
chris@16 361 const SordNode* datatype = sord_node_get_datatype(_c_obj);
chris@16 362 if (datatype && !strcmp((const char*)sord_node_get_string(datatype),
chris@16 363 type_uri))
chris@16 364 return true;
chris@16 365 }
chris@16 366 return false;
chris@16 367 }
chris@16 368
chris@16 369 inline int
chris@16 370 Node::to_int() const
chris@16 371 {
chris@16 372 assert(is_int());
chris@16 373 char* endptr;
chris@16 374 return strtol((const char*)sord_node_get_string(_c_obj), &endptr, 10);
chris@16 375 }
chris@16 376
chris@16 377 inline float
chris@16 378 Node::to_float() const
chris@16 379 {
chris@16 380 assert(is_float());
chris@16 381 char* endptr;
chris@16 382 return serd_strtod((const char*)sord_node_get_string(_c_obj), &endptr);
chris@16 383 }
chris@16 384
chris@16 385 inline bool
chris@16 386 Node::to_bool() const
chris@16 387 {
chris@16 388 assert(is_bool());
chris@16 389 return !strcmp((const char*)sord_node_get_string(_c_obj), "true");
chris@16 390 }
chris@16 391
chris@16 392 struct Iter : public Wrapper<SordIter*> {
chris@16 393 inline Iter(World& world, SordIter* c_obj)
chris@16 394 : Wrapper<SordIter*>(c_obj), _world(world) {}
chris@16 395 inline ~Iter() { sord_iter_free(_c_obj); }
chris@16 396 inline bool end() const { return sord_iter_end(_c_obj); }
chris@16 397 inline bool next() const { return sord_iter_next(_c_obj); }
chris@16 398 inline Iter& operator++() {
chris@16 399 assert(!end());
chris@16 400 next();
chris@16 401 return *this;
chris@16 402 }
chris@16 403 inline const Node get_subject() const {
chris@16 404 SordQuad quad;
chris@16 405 sord_iter_get(_c_obj, quad);
chris@16 406 return Node(_world, quad[SORD_SUBJECT]);
chris@16 407 }
chris@16 408 inline const Node get_predicate() const {
chris@16 409 SordQuad quad;
chris@16 410 sord_iter_get(_c_obj, quad);
chris@16 411 return Node(_world, quad[SORD_PREDICATE]);
chris@16 412 }
chris@16 413 inline const Node get_object() const {
chris@16 414 SordQuad quad;
chris@16 415 sord_iter_get(_c_obj, quad);
chris@16 416 return Node(_world, quad[SORD_OBJECT]);
chris@16 417 }
chris@16 418 World& _world;
chris@16 419 };
chris@16 420
chris@16 421 /** An RDF Model (collection of triples).
chris@16 422 */
chris@16 423 class Model : public Noncopyable, public Wrapper<SordModel*> {
chris@16 424 public:
chris@16 425 inline Model(World& world,
chris@16 426 const std::string& base_uri,
chris@16 427 unsigned indices = (SORD_SPO | SORD_OPS),
chris@16 428 bool graphs = true);
chris@16 429
chris@16 430 inline ~Model();
chris@16 431
chris@16 432 inline const Node& base_uri() const { return _base; }
chris@16 433
chris@16 434 size_t num_quads() const { return sord_num_quads(_c_obj); }
chris@16 435
chris@16 436 inline void load_file(SerdEnv* env,
chris@16 437 SerdSyntax syntax,
chris@16 438 const std::string& uri,
chris@16 439 const std::string& base_uri="");
chris@16 440
chris@16 441 inline void load_string(SerdEnv* env,
chris@16 442 SerdSyntax syntax,
chris@16 443 const char* str,
chris@16 444 size_t len,
chris@16 445 const std::string& base_uri);
chris@16 446
chris@16 447 inline SerdStatus write_to_file(
chris@16 448 const std::string& uri,
chris@16 449 SerdSyntax syntax = SERD_TURTLE,
chris@16 450 SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIATED
chris@16 451 |SERD_STYLE_CURIED
chris@16 452 |SERD_STYLE_RESOLVED));
chris@16 453
chris@16 454 inline std::string write_to_string(
chris@16 455 const std::string& base_uri,
chris@16 456 SerdSyntax syntax = SERD_TURTLE,
chris@16 457 SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIATED
chris@16 458 |SERD_STYLE_CURIED
chris@16 459 |SERD_STYLE_RESOLVED));
chris@16 460
chris@16 461 inline void add_statement(const Node& subject,
chris@16 462 const Node& predicate,
chris@16 463 const Node& object);
chris@16 464
chris@16 465 inline Iter find(const Node& subject,
chris@16 466 const Node& predicate,
chris@16 467 const Node& object);
chris@16 468
chris@16 469 inline Node get(const Node& subject,
chris@16 470 const Node& predicate,
chris@16 471 const Node& object);
chris@16 472
chris@16 473 inline World& world() const { return _world; }
chris@16 474
chris@16 475 private:
chris@16 476 World& _world;
chris@16 477 Node _base;
chris@16 478 SerdWriter* _writer;
chris@16 479 size_t _next_blank_id;
chris@16 480 };
chris@16 481
chris@16 482 /** Create an empty in-memory RDF model.
chris@16 483 */
chris@16 484 inline
chris@16 485 Model::Model(World& world,
chris@16 486 const std::string& base_uri,
chris@16 487 unsigned indices,
chris@16 488 bool graphs)
chris@16 489 : _world(world)
chris@16 490 , _base(world, Node::URI, base_uri)
chris@16 491 , _writer(NULL)
chris@16 492 {
chris@16 493 _c_obj = sord_new(_world.world(), indices, graphs);
chris@16 494 }
chris@16 495
chris@16 496 inline void
chris@16 497 Model::load_string(SerdEnv* env,
chris@16 498 SerdSyntax syntax,
chris@16 499 const char* str,
chris@16 500 size_t len,
chris@16 501 const std::string& base_uri)
chris@16 502 {
chris@16 503 SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL);
chris@16 504 serd_reader_read_string(reader, (const uint8_t*)str);
chris@16 505 serd_reader_free(reader);
chris@16 506 }
chris@16 507
chris@16 508 inline Model::~Model()
chris@16 509 {
chris@16 510 sord_free(_c_obj);
chris@16 511 }
chris@16 512
chris@16 513 inline void
chris@16 514 Model::load_file(SerdEnv* env,
chris@16 515 SerdSyntax syntax,
chris@16 516 const std::string& data_uri,
chris@16 517 const std::string& base_uri)
chris@16 518 {
chris@16 519 uint8_t* path = serd_file_uri_parse((const uint8_t*)data_uri.c_str(), NULL);
chris@16 520 if (!path) {
chris@16 521 fprintf(stderr, "Failed to parse file URI <%s>\n", data_uri.c_str());
chris@16 522 return;
chris@16 523 }
chris@16 524
chris@16 525 // FIXME: blank prefix parameter?
chris@16 526 SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL);
chris@16 527 serd_reader_read_file(reader, path);
chris@16 528 serd_reader_free(reader);
chris@16 529 free(path);
chris@16 530 }
chris@16 531
chris@16 532 inline SerdStatus
chris@16 533 Model::write_to_file(const std::string& uri, SerdSyntax syntax, SerdStyle style)
chris@16 534 {
chris@16 535 uint8_t* path = serd_file_uri_parse((const uint8_t*)uri.c_str(), NULL);
chris@16 536 if (!path) {
chris@16 537 fprintf(stderr, "Failed to parse file URI <%s>\n", uri.c_str());
chris@16 538 return SERD_ERR_BAD_ARG;
chris@16 539 }
chris@16 540
chris@16 541 FILE* const fd = fopen((const char*)path, "w");
chris@16 542 if (!fd) {
chris@16 543 fprintf(stderr, "Failed to open file %s\n", path);
chris@16 544 free(path);
chris@16 545 return SERD_ERR_UNKNOWN;
chris@16 546 }
chris@16 547 free(path);
chris@16 548
chris@16 549 SerdURI base_uri = SERD_URI_NULL;
chris@16 550 if (serd_uri_parse((const uint8_t*)uri.c_str(), &base_uri)) {
chris@16 551 fprintf(stderr, "Invalid base URI <%s>\n", uri.c_str());
chris@16 552 fclose(fd);
chris@16 553 return SERD_ERR_BAD_ARG;
chris@16 554 }
chris@16 555
chris@16 556 SerdWriter* writer = serd_writer_new(syntax,
chris@16 557 style,
chris@16 558 _world.prefixes().c_obj(),
chris@16 559 &base_uri,
chris@16 560 serd_file_sink,
chris@16 561 fd);
chris@16 562
chris@16 563 serd_env_foreach(_world.prefixes().c_obj(),
chris@16 564 (SerdPrefixSink)serd_writer_set_prefix,
chris@16 565 writer);
chris@16 566
chris@16 567 sord_write(_c_obj, writer, 0);
chris@16 568 serd_writer_free(writer);
chris@16 569 fclose(fd);
chris@16 570
chris@16 571 return SERD_SUCCESS;
chris@16 572 }
chris@16 573
chris@16 574 static size_t
chris@16 575 string_sink(const void* buf, size_t len, void* stream)
chris@16 576 {
chris@16 577 std::string* str = (std::string*)stream;
chris@16 578 str->append((const char*)buf, len);
chris@16 579 return len;
chris@16 580 }
chris@16 581
chris@16 582 inline std::string
chris@16 583 Model::write_to_string(const std::string& base_uri_str,
chris@16 584 SerdSyntax syntax,
chris@16 585 SerdStyle style)
chris@16 586 {
chris@16 587 SerdURI base_uri = SERD_URI_NULL;
chris@16 588 if (serd_uri_parse((const uint8_t*)base_uri_str.c_str(), &base_uri)) {
chris@16 589 fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str.c_str());
chris@16 590 return "";
chris@16 591 }
chris@16 592
chris@16 593 std::string ret;
chris@16 594
chris@16 595 SerdWriter* writer = serd_writer_new(syntax,
chris@16 596 style,
chris@16 597 _world.prefixes().c_obj(),
chris@16 598 &base_uri,
chris@16 599 string_sink,
chris@16 600 &ret);
chris@16 601
chris@16 602 serd_env_foreach(_world.prefixes().c_obj(),
chris@16 603 (SerdPrefixSink)serd_writer_set_prefix,
chris@16 604 writer);
chris@16 605
chris@16 606 sord_write(_c_obj, writer, 0);
chris@16 607
chris@16 608 serd_writer_free(writer);
chris@16 609 return ret;
chris@16 610 }
chris@16 611
chris@16 612 inline void
chris@16 613 Model::add_statement(const Node& subject,
chris@16 614 const Node& predicate,
chris@16 615 const Node& object)
chris@16 616 {
chris@16 617 SordQuad quad = { subject.c_obj(),
chris@16 618 predicate.c_obj(),
chris@16 619 object.c_obj(),
chris@16 620 NULL };
chris@16 621
chris@16 622 sord_add(_c_obj, quad);
chris@16 623 }
chris@16 624
chris@16 625 inline Iter
chris@16 626 Model::find(const Node& subject,
chris@16 627 const Node& predicate,
chris@16 628 const Node& object)
chris@16 629 {
chris@16 630 SordQuad quad = { subject.c_obj(),
chris@16 631 predicate.c_obj(),
chris@16 632 object.c_obj(),
chris@16 633 NULL };
chris@16 634
chris@16 635 return Iter(_world, sord_find(_c_obj, quad));
chris@16 636 }
chris@16 637
chris@16 638 inline Node
chris@16 639 Model::get(const Node& subject,
chris@16 640 const Node& predicate,
chris@16 641 const Node& object)
chris@16 642 {
chris@16 643 SordNode* c_node = sord_get(
chris@16 644 _c_obj, subject.c_obj(), predicate.c_obj(), object.c_obj(), NULL);
chris@16 645 Node node(_world, c_node);
chris@16 646 sord_node_free(_world.c_obj(), c_node);
chris@16 647 return node;
chris@16 648 }
chris@16 649
chris@16 650 } // namespace Sord
chris@16 651
chris@16 652 #endif // SORD_SORDMM_HPP
chris@16 653