diff options
author | David Robillard <d@drobilla.net> | 2011-02-19 07:13:59 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-02-19 07:13:59 +0000 |
commit | d4b996a4149f2e303e4ef16c67804c8ad49524bb (patch) | |
tree | 0e13e2a9a55ea312cf74f03a24dc2acc29a762c1 | |
parent | 36d5e18e931ffdb0b203178a7a5acc3e909a9536 (diff) | |
download | sord-d4b996a4149f2e303e4ef16c67804c8ad49524bb.tar.gz sord-d4b996a4149f2e303e4ef16c67804c8ad49524bb.tar.bz2 sord-d4b996a4149f2e303e4ef16c67804c8ad49524bb.zip |
String reading/writing.
git-svn-id: http://svn.drobilla.net/sord/trunk@39 3d64ff67-21c5-427c-a301-fe4f08042e5a
-rw-r--r-- | sord/sord.h | 13 | ||||
-rw-r--r-- | sord/sordmm.hpp | 23 | ||||
-rw-r--r-- | src/syntax.c | 143 |
3 files changed, 140 insertions, 39 deletions
diff --git a/sord/sord.h b/sord/sord.h index f1169f9..c632815 100644 --- a/sord/sord.h +++ b/sord/sord.h @@ -338,6 +338,12 @@ sord_read_file_handle(SordModel model, SORD_API bool +sord_read_string(SordModel model, + const uint8_t* str, + const uint8_t* base_uri); + +SORD_API +bool sord_write_file(SordModel model, SerdEnv env, const uint8_t* uri, @@ -353,6 +359,13 @@ sord_write_file_handle(SordModel model, const SordNode graph, const uint8_t* blank_prefix); +SORD_API +uint8_t* +sord_write_string(SordModel model, + SerdEnv env, + const uint8_t* base_uri); + + /** @} */ /** @} */ diff --git a/sord/sordmm.hpp b/sord/sordmm.hpp index 78fc14c..259e2c2 100644 --- a/sord/sordmm.hpp +++ b/sord/sordmm.hpp @@ -202,7 +202,7 @@ private: inline std::ostream& operator<<(std::ostream& os, const Node& node) { - return os << node.to_string() << std::endl; + return os << node.to_string(); } class URI : public Node { @@ -284,14 +284,12 @@ Node::~Node() } } - inline std::string Node::to_string() const { - return std::string(to_c_string()); + return _c_obj ? (const char*)sord_node_get_string(_c_obj) : ""; } - inline const char* Node::to_c_string() const { @@ -391,7 +389,8 @@ public: inline void write_to_file_handle(FILE* fd, const char* lang); inline void write_to_file(const Glib::ustring& uri, const char* lang); - inline char* write_to_string(const char* lang); + + inline std::string write_to_string(const char* lang); inline void add_statement(const Node& subject, const Node& predicate, @@ -428,7 +427,9 @@ Model::load_string(const char* str, const Glib::ustring& base_uri, const std::string lang) { - // TODO + sord_read_string(_c_obj, + (const uint8_t*)str, + (const uint8_t*)base_uri.c_str()); } inline Model::~Model() @@ -465,11 +466,15 @@ Model::write_to_file(const Glib::ustring& uri, const char* lang) NULL); } -inline char* +inline std::string Model::write_to_string(const char* lang) { - std::cerr << "TODO: serialise" << std::endl; - return NULL; + uint8_t* const c_str = sord_write_string(_c_obj, + _world.prefixes().c_obj(), + base_uri().to_u_string()); + std::string ret((const char*)c_str); + free(c_str); + return ret; } inline void diff --git a/src/syntax.c b/src/syntax.c index 6542071..686893d 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -238,7 +238,7 @@ sord_read_file(SordModel model, SORD_API bool -sord_read_file_handle(SordModel sord, +sord_read_file_handle(SordModel model, FILE* fd, const uint8_t* base_uri_str_in, const SordNode graph, @@ -246,6 +246,7 @@ sord_read_file_handle(SordModel sord, { size_t base_uri_n_bytes = 0; uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes); + SerdURI base_uri; if (!serd_uri_parse(base_uri_str, &base_uri)) { fprintf(stderr, "invalid base URI `%s'\n", base_uri_str); @@ -260,7 +261,7 @@ sord_read_file_handle(SordModel sord, ReadState state = { NULL, env, graph, base_uri_node, base_uri, - sord_get_world(sord), sord }; + sord_get_world(model), model }; state.reader = serd_reader_new( SERD_TURTLE, &state, @@ -280,6 +281,43 @@ sord_read_file_handle(SordModel sord, SORD_API bool +sord_read_string(SordModel model, + const uint8_t* str, + const uint8_t* base_uri_str_in) +{ + size_t base_uri_n_bytes = 0; + uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes); + + SerdURI base_uri; + if (!serd_uri_parse(base_uri_str, &base_uri)) { + fprintf(stderr, "invalid base URI `%s'\n", base_uri_str); + } + + SerdEnv env = serd_env_new(); + + const SerdNode base_uri_node = { SERD_URI, + base_uri_n_bytes, + base_uri_n_bytes - 1, // FIXME: UTF-8 + base_uri_str }; + + ReadState state = { NULL, env, NULL, + base_uri_node, base_uri, + sord_get_world(model), model }; + + state.reader = serd_reader_new( + SERD_TURTLE, &state, + event_base, event_prefix, event_statement, NULL); + + const bool success = serd_reader_read_string(state.reader, str); + + serd_reader_free(state.reader); + serd_node_free(&state.base_uri_node); + + return success; +} + +SORD_API +bool sord_write_file(SordModel model, SerdEnv env, const uint8_t* uri, @@ -309,33 +347,11 @@ file_sink(const void* buf, size_t len, void* stream) return fwrite(buf, 1, len, file); } -SORD_API -bool -sord_write_file_handle(SordModel model, - SerdEnv env, - FILE* fd, - const uint8_t* base_uri_str_in, - const SordNode graph, - const uint8_t* blank_prefix) +static void +sord_write(const SordModel model, + const SordNode graph, + SerdWriter writer) { - size_t base_uri_n_bytes = 0; - uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes); - SerdURI base_uri; - if (!serd_uri_parse(base_uri_str, &base_uri)) { - fprintf(stderr, "invalid base URI `%s'\n", base_uri_str); - } - - SerdWriter writer = serd_writer_new(SERD_TURTLE, - SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED, - env, - &base_uri, - file_sink, - fd); - - serd_env_foreach(env, - (SerdPrefixSink)serd_writer_set_prefix, - writer); - SerdNode s_graph; sord_node_to_serd_node(graph, &s_graph); for (SordIter i = sord_begin(model); !sord_iter_end(i); sord_iter_next(i)) { @@ -366,9 +382,76 @@ sord_write_file_handle(SordModel model, &subject, &predicate, &object, &datatype, &language); } +} - serd_writer_free(writer); - free(base_uri_str); +static SerdWriter +make_writer(SerdEnv env, + const uint8_t* base_uri_str_in, + SerdSink sink, + void* stream) +{ + size_t base_uri_n_bytes = 0; + uint8_t* base_uri_str = copy_string(base_uri_str_in, &base_uri_n_bytes); + SerdURI base_uri; + if (!serd_uri_parse(base_uri_str, &base_uri)) { + fprintf(stderr, "invalid base URI `%s'\n", base_uri_str); + } + + SerdWriter writer = serd_writer_new(SERD_TURTLE, + SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED, + env, + &base_uri, + sink, + stream); + + serd_env_foreach(env, + (SerdPrefixSink)serd_writer_set_prefix, + writer); + + return writer; +} + +SORD_API +bool +sord_write_file_handle(SordModel model, + SerdEnv env, + FILE* fd, + const uint8_t* base_uri_str_in, + const SordNode graph, + const uint8_t* blank_prefix) +{ + SerdWriter writer = make_writer(env, base_uri_str_in, file_sink, fd); + sord_write(model, graph, writer); + serd_writer_free(writer); return true; } + +struct SerdBuffer { + uint8_t* buf; + size_t len; +}; + +static size_t +string_sink(const void* buf, size_t len, void* stream) +{ + struct SerdBuffer* out = (struct SerdBuffer*)stream; + out->buf = realloc(out->buf, out->len + len); + memcpy(out->buf + out->len, buf, len); + out->len += len; + return len; +} + +SORD_API +uint8_t* +sord_write_string(SordModel model, + SerdEnv env, + const uint8_t* base_uri) +{ + struct SerdBuffer buf = { NULL, 0 }; + SerdWriter writer = make_writer(env, base_uri, string_sink, &buf); + sord_write(model, NULL, writer); + serd_writer_free(writer); + string_sink("", 1, &buf); + return buf.buf; +} |