| 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 | |
|