sord.h   sord.h 
/* /*
Copyright 2011 David Robillard <http://drobilla.net> Copyright 2011-2012 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies. copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
skipping to change at line 24 skipping to change at line 24
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/** /**
@file sord.h API for Sord, a lightweight RDF model library. @file sord.h API for Sord, a lightweight RDF model library.
*/ */
#ifndef SORD_SORD_H #ifndef SORD_SORD_H
#define SORD_SORD_H #define SORD_SORD_H
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "serd/serd.h" #include "serd/serd.h"
#ifdef SORD_SHARED #ifdef SORD_SHARED
# ifdef __WIN32__ # ifdef _WIN32
# define SORD_LIB_IMPORT __declspec(dllimport) # define SORD_LIB_IMPORT __declspec(dllimport)
# define SORD_LIB_EXPORT __declspec(dllexport) # define SORD_LIB_EXPORT __declspec(dllexport)
# else # else
# define SORD_LIB_IMPORT __attribute__((visibility("default"))) # define SORD_LIB_IMPORT __attribute__((visibility("default")))
# define SORD_LIB_EXPORT __attribute__((visibility("default"))) # define SORD_LIB_EXPORT __attribute__((visibility("default")))
# endif # endif
# ifdef SORD_INTERNAL # ifdef SORD_INTERNAL
# define SORD_API SORD_LIB_EXPORT # define SORD_API SORD_LIB_EXPORT
# else # else
# define SORD_API SORD_LIB_IMPORT # define SORD_API SORD_LIB_IMPORT
# endif # endif
#else #else
# define SORD_API # define SORD_API
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#else
# include <stdbool.h>
#endif #endif
/** /**
@defgroup sord Sord @defgroup sord Sord
A lightweight RDF model library. A lightweight RDF model library.
Sord stores RDF (subject object predicate context) quads, where the cont ext Sord stores RDF (subject object predicate context) quads, where the cont ext
may be omitted (to represent triples in the default graph). may be omitted (to represent triples in the default graph).
@{ @{
*/ */
skipping to change at line 77 skipping to change at line 78
/** /**
Sord Model. Sord Model.
A model is an indexed set of Quads (i.e. it can contain several RDF A model is an indexed set of Quads (i.e. it can contain several RDF
graphs). It may be searched using various patterns depending on which graphs). It may be searched using various patterns depending on which
indices are enabled. indices are enabled.
*/ */
typedef struct SordModelImpl SordModel; typedef struct SordModelImpl SordModel;
/** /**
Model Inserter.
An inserter is used for writing statements to a model using the Serd sin
k
interface. This makes it simple to write to a model directly using a
SerdReader, or any other code that writes statements to a SerdStatementS
ink.
*/
typedef struct SordInserterImpl SordInserter;
/**
Model Iterator. Model Iterator.
*/ */
typedef struct SordIterImpl SordIter; typedef struct SordIterImpl SordIter;
/** /**
RDF Node. RDF Node.
A Node is a component of a Quad. Nodes may be URIs, blank nodes, or A Node is a component of a Quad. Nodes may be URIs, blank nodes, or
(in the case of quad objects only) string literals. Literal nodes may (in the case of quad objects only) string literals. Literal nodes may
have an associate language or datatype (but not both). have an associate language or datatype (but not both).
*/ */
skipping to change at line 163 skipping to change at line 173
*/ */
/** /**
Get a URI node from a string. Get a URI node from a string.
Note this function measures @c str, which is a common bottleneck. Note this function measures @c str, which is a common bottleneck.
Use sord_node_from_serd_node instead if @c str is already measured. Use sord_node_from_serd_node instead if @c str is already measured.
*/ */
SORD_API SORD_API
SordNode* SordNode*
sord_new_uri(SordWorld* world, const uint8_t* str); sord_new_uri(SordWorld* world, const uint8_t* uri);
/**
Get a URI node from a relative URI string.
*/
SORD_API
SordNode*
sord_new_relative_uri(SordWorld* world,
const uint8_t* str,
const uint8_t* base_uri);
/** /**
Get a blank node from a string. Get a blank node from a string.
Note this function measures @c str, which is a common bottleneck. Note this function measures @c str, which is a common bottleneck.
Use sord_node_from_serd_node instead if @c str is already measured. Use sord_node_from_serd_node instead if @c str is already measured.
*/ */
SORD_API SORD_API
SordNode* SordNode*
sord_new_blank(SordWorld* world, const uint8_t* str); sord_new_blank(SordWorld* world, const uint8_t* str);
skipping to change at line 351 skipping to change at line 370
sord_num_quads(const SordModel* model); sord_num_quads(const SordModel* model);
/** /**
Return an iterator to the start of @c model. Return an iterator to the start of @c model.
*/ */
SORD_API SORD_API
SordIter* SordIter*
sord_begin(const SordModel* model); sord_begin(const SordModel* model);
/** /**
Search for a triple pattern. Search for statements by a quad pattern.
@return an iterator to the first match, or NULL if no matches found. @return an iterator to the first match, or NULL if no matches found.
*/ */
SORD_API SORD_API
SordIter* SordIter*
sord_find(SordModel* model, const SordQuad pat); sord_find(SordModel* model, const SordQuad pat);
/** /**
Search for statements by nodes.
@return an iterator to the first match, or NULL if no matches found.
*/
SORD_API
SordIter*
sord_search(SordModel* model,
const SordNode* s,
const SordNode* p,
const SordNode* o,
const SordNode* g);
/**
Return true iff a statement exists.
*/
SORD_API
bool
sord_ask(SordModel* model,
const SordNode* s,
const SordNode* p,
const SordNode* o,
const SordNode* g);
/**
Return the number of matching statements.
*/
SORD_API
uint64_t
sord_count(SordModel* model,
const SordNode* s,
const SordNode* p,
const SordNode* o,
const SordNode* g);
/**
Check if @a model contains a triple pattern. Check if @a model contains a triple pattern.
*/ */
SORD_API SORD_API
bool bool
sord_contains(SordModel* model, const SordQuad pat); sord_contains(SordModel* model, const SordQuad pat);
/** /**
Add a quad to a model. Add a quad to a model.
*/ */
SORD_API SORD_API
skipping to change at line 383 skipping to change at line 436
Remove a quad from a model. Remove a quad from a model.
Note that is it illegal to remove while iterating over @c model. Note that is it illegal to remove while iterating over @c model.
*/ */
SORD_API SORD_API
void void
sord_remove(SordModel* model, const SordQuad quad); sord_remove(SordModel* model, const SordQuad quad);
/** /**
@} @}
@name Inserter
@{
*/
/**
Create an inserter for writing statements to a model.
*/
SORD_API
SordInserter*
sord_inserter_new(SordModel* model,
SerdEnv* env);
/**
Free an inserter.
*/
SORD_API
void
sord_inserter_free(SordInserter* inserter);
/**
Set the current base URI for writing to the model.
Note this function can be safely casted to SerdBaseSink.
*/
SORD_API
SerdStatus
sord_inserter_set_base_uri(SordInserter* inserter,
const SerdNode* uri);
/**
Set a namespace prefix for writing to the model.
Note this function can be safely casted to SerdPrefixSink.
*/
SORD_API
SerdStatus
sord_inserter_set_prefix(SordInserter* inserter,
const SerdNode* name,
const SerdNode* uri);
/**
Write a statement to the model.
Note this function can be safely casted to SerdStatementSink.
*/
SORD_API
SerdStatus
sord_inserter_write_statement(SordInserter* inserter,
SerdStatementFlags flags,
const SerdNode* graph,
const SerdNode* subject,
const SerdNode* predicate,
const SerdNode* object,
const SerdNode* object_datatype,
const SerdNode* object_lang);
/**
@}
@name Iteration @name Iteration
@{ @{
*/ */
/** /**
Set @c quad to the quad pointed to by @c iter. Set @c quad to the quad pointed to by @c iter.
*/ */
SORD_API SORD_API
void void
sord_iter_get(const SordIter* iter, SordQuad quad); sord_iter_get(const SordIter* iter, SordQuad quad);
/** /**
Return a field of the quad pointed to by @c iter.
*/
SORD_API
const SordNode*
sord_iter_get_node(const SordIter* iter, SordQuadIndex index);
/**
Return the store pointed to by @c iter. Return the store pointed to by @c iter.
*/ */
SORD_API SORD_API
const SordModel* const SordModel*
sord_iter_get_model(SordIter* iter); sord_iter_get_model(SordIter* iter);
/** /**
Increment @c iter to point to the next statement. Increment @c iter to point to the next statement.
*/ */
SORD_API SORD_API
 End of changes. 10 change blocks. 
5 lines changed or deleted 125 lines changed or added


 sordmm.hpp   sordmm.hpp 
/* /*
Copyright 2011 David Robillard <http://drobilla.net> Copyright 2011-2012 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies. copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
skipping to change at line 222 skipping to change at line 222
}; };
inline std::ostream& inline std::ostream&
operator<<(std::ostream& os, const Node& node) operator<<(std::ostream& os, const Node& node)
{ {
return os << node.to_string(); return os << node.to_string();
} }
class URI : public Node { class URI : public Node {
public: public:
inline URI(World& world, const std::string& s) : Node(world, Node::U inline URI(World& world, const std::string& s)
RI, s) {} : Node(world, Node::URI, s) {}
inline URI(World& world, const std::string& s, const std::string& ba
se)
: Node(world, sord_new_relative_uri(world.world(),
(const uint8_t*)s.c_str(
),
(const uint8_t*)base.c_s
tr()))
{}
}; };
class Curie : public Node { class Curie : public Node {
public: public:
inline Curie(World& world, const std::string& s) inline Curie(World& world, const std::string& s)
: Node(world, Node::URI, world.prefixes().expand(s)) {} : Node(world, Node::URI, world.prefixes().expand(s)) {}
}; };
class Literal : public Node { class Literal : public Node {
public: public:
inline Literal(World& world, const std::string& s) : Node(world, Nod inline Literal(World& world, const std::string& s)
e::LITERAL, s) {} : Node(world, Node::LITERAL, s) {}
}; };
inline inline
Node::Node(World& world, Type type, const std::string& s) Node::Node(World& world, Type type, const std::string& s)
: _world(&world) : _world(&world)
{ {
switch (type) { switch (type) {
case URI: case URI:
assert(s.find(":") == std::string::npos assert(s.find(":") == std::string::npos
|| s.substr(0, 5) == "http:" || s.substr(0, 5) == "http:"
skipping to change at line 333 skipping to change at line 340
type_uri)) type_uri))
return true; return true;
} }
return false; return false;
} }
inline int inline int
Node::to_int() const Node::to_int() const
{ {
assert(is_int()); assert(is_int());
std::locale c_locale("C"); char* endptr;
std::stringstream ss((const char*)sord_node_get_string(_c_obj)); return strtol((const char*)sord_node_get_string(_c_obj), &endptr, 10
ss.imbue(c_locale); );
int i = 0;
ss >> i;
return i;
} }
inline float inline float
Node::to_float() const Node::to_float() const
{ {
assert(is_float()); assert(is_float());
std::locale c_locale("C"); char* endptr;
std::stringstream ss((const char*)sord_node_get_string(_c_obj)); return serd_strtod((const char*)sord_node_get_string(_c_obj), &endpt
ss.imbue(c_locale); r);
float f = 0.0f;
ss >> f;
return f;
} }
inline bool inline bool
Node::to_bool() const Node::to_bool() const
{ {
assert(is_bool()); assert(is_bool());
return !strcmp((const char*)sord_node_get_string(_c_obj), "true"); return !strcmp((const char*)sord_node_get_string(_c_obj), "true");
} }
struct Iter : public Wrapper<SordIter*> { struct Iter : public Wrapper<SordIter*> {
inline Iter(World& world, SordIter* c_obj) inline Iter(World& world, SordIter* c_obj)
: Wrapper<SordIter*>(c_obj), _world(world) {} : Wrapper<SordIter*>(c_obj), _world(world) {}
inline ~Iter() { sord_iter_free(_c_obj); } inline ~Iter() { sord_iter_free(_c_obj); }
inline bool end() const { return sord_iter_end(_c_obj); } inline bool end() const { return sord_iter_end(_c_obj); }
inline bool next() const { return sord_iter_next(_c_obj); } inline bool next() const { return sord_iter_next(_c_obj); }
inline Iter& operator++() { assert(!end()); next(); return *this; } inline Iter& operator++() {
assert(!end());
next();
return *this;
}
inline const Node get_subject() const { inline const Node get_subject() const {
SordQuad quad; SordQuad quad;
sord_iter_get(_c_obj, quad); sord_iter_get(_c_obj, quad);
return Node(_world, quad[SORD_SUBJECT]); return Node(_world, quad[SORD_SUBJECT]);
} }
inline const Node get_predicate() const { inline const Node get_predicate() const {
SordQuad quad; SordQuad quad;
sord_iter_get(_c_obj, quad); sord_iter_get(_c_obj, quad);
return Node(_world, quad[SORD_PREDICATE]); return Node(_world, quad[SORD_PREDICATE]);
} }
skipping to change at line 409 skipping to change at line 412
const std::string& base_uri=""); const std::string& base_uri="");
inline void load_string(SerdEnv* env, inline void load_string(SerdEnv* env,
SerdSyntax syntax, SerdSyntax syntax,
const char* str, const char* str,
size_t len, size_t len,
const std::string& base_uri); const std::string& base_uri);
inline SerdStatus write_to_file( inline SerdStatus write_to_file(
const std::string& uri, const std::string& uri,
SerdSyntax syntax=SERD_TURTLE, SerdSyntax syntax = SERD_TURTLE,
SerdStyle style=(SerdStyle)(SERD_STYLE_ABBREVIATED SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIAT
|SERD_STYLE_CURIED)); ED
|SERD_STYLE_CURIED
|SERD_STYLE_RESOLVED
));
inline std::string write_to_string( inline std::string write_to_string(
const std::string& base_uri, const std::string& base_uri,
SerdSyntax syntax=SERD_TURTLE, SerdSyntax syntax = SERD_TURTLE,
SerdStyle style=(SerdStyle)(SERD_STYLE_ABBREVIATED SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIAT
|SERD_STYLE_CURIED)); ED
|SERD_STYLE_CURIED
|SERD_STYLE_RESOLVED
));
inline void add_statement(const Node& subject, inline void add_statement(const Node& subject,
const Node& predicate, const Node& predicate,
const Node& object); const Node& object);
inline Iter find(const Node& subject, inline Iter find(const Node& subject,
const Node& predicate, const Node& predicate,
const Node& object); const Node& object);
inline World& world() const { return _world; } inline World& world() const { return _world; }
skipping to change at line 471 skipping to change at line 476
{ {
sord_free(_c_obj); sord_free(_c_obj);
} }
inline void inline void
Model::load_file(SerdEnv* env, Model::load_file(SerdEnv* env,
SerdSyntax syntax, SerdSyntax syntax,
const std::string& data_uri, const std::string& data_uri,
const std::string& base_uri) const std::string& base_uri)
{ {
if (data_uri.substr(0, 5) != "file:") { uint8_t* path = serd_file_uri_parse((const uint8_t*)data_uri.c_str()
, NULL);
if (!path) {
fprintf(stderr, "Failed to parse file URI <%s>\n", data_uri.
c_str());
return; return;
} }
const uint8_t* path = (const uint8_t*)(data_uri.c_str() + 5);
// FIXME: blank prefix parameter? // FIXME: blank prefix parameter?
SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL); SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL);
serd_reader_read_file(reader, path); serd_reader_read_file(reader, path);
serd_reader_free(reader); serd_reader_free(reader);
} free(path);
static size_t
file_sink(const void* buf, size_t len, void* stream)
{
FILE* file = (FILE*)stream;
return fwrite(buf, 1, len, file);
} }
inline SerdStatus inline SerdStatus
Model::write_to_file(const std::string& uri, SerdSyntax syntax, SerdStyle s tyle) Model::write_to_file(const std::string& uri, SerdSyntax syntax, SerdStyle s tyle)
{ {
if (uri.substr(0, 5) != "file:") { uint8_t* path = serd_file_uri_parse((const uint8_t*)uri.c_str(), NUL
L);
if (!path) {
fprintf(stderr, "Failed to parse file URI <%s>\n", uri.c_str
());
return SERD_ERR_BAD_ARG; return SERD_ERR_BAD_ARG;
} }
const uint8_t* path = (const uint8_t*)(uri.c_str() + 5);
FILE* const fd = fopen((const char*)path, "w"); FILE* const fd = fopen((const char*)path, "w");
if (!fd) { if (!fd) {
fprintf(stderr, "Failed to open file %s\n", path); fprintf(stderr, "Failed to open file %s\n", path);
free(path);
return SERD_ERR_UNKNOWN; return SERD_ERR_UNKNOWN;
} }
free(path);
SerdURI base_uri = SERD_URI_NULL; SerdURI base_uri = SERD_URI_NULL;
if (serd_uri_parse((const uint8_t*)uri.c_str(), &base_uri)) { if (serd_uri_parse((const uint8_t*)uri.c_str(), &base_uri)) {
fprintf(stderr, "Invalid base URI <%s>\n", uri.c_str()); fprintf(stderr, "Invalid base URI <%s>\n", uri.c_str());
fclose(fd); fclose(fd);
return SERD_ERR_BAD_ARG; return SERD_ERR_BAD_ARG;
} }
SerdWriter* writer = serd_writer_new(syntax, SerdWriter* writer = serd_writer_new(syntax,
style, style,
_world.prefixes().c_obj(), _world.prefixes().c_obj(),
&base_uri, &base_uri,
file_sink, serd_file_sink,
fd); fd);
serd_env_foreach(_world.prefixes().c_obj(), serd_env_foreach(_world.prefixes().c_obj(),
(SerdPrefixSink)serd_writer_set_prefix, (SerdPrefixSink)serd_writer_set_prefix,
writer); writer);
sord_write(_c_obj, writer, 0); sord_write(_c_obj, writer, 0);
serd_writer_free(writer); serd_writer_free(writer);
fclose(fd); fclose(fd);
skipping to change at line 594 skipping to change at line 595
const Node& object) const Node& object)
{ {
SordQuad quad = { subject.c_obj(), SordQuad quad = { subject.c_obj(),
predicate.c_obj(), predicate.c_obj(),
object.c_obj(), object.c_obj(),
NULL }; NULL };
return Iter(_world, sord_find(_c_obj, quad)); return Iter(_world, sord_find(_c_obj, quad));
} }
} // namespace Sord } // namespace Sord
#endif // SORD_SORDMM_HPP #endif // SORD_SORDMM_HPP
 End of changes. 18 change blocks. 
39 lines changed or deleted 51 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/