Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sordmm.hpp 16KB

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