aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--serd/serd.h97
-rw-r--r--src/env.c91
-rw-r--r--src/reader.c106
-rw-r--r--src/serdi.c20
4 files changed, 126 insertions, 188 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 3fbe99e4..de090cf4 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -71,16 +71,6 @@ typedef struct SerdEnvImpl SerdEnv;
typedef struct SerdReaderImpl SerdReader;
/**
- Read state.
-
- This maintains the state necessary to fully resolve URIs during a read
- (e.g. the base URI and prefixes). It is implemented separately from
- SerdReader to avoid overhead in situations where this is unnecessary
- (e.g. streaming re-serialisation).
-*/
-typedef struct SerdReadStateImpl SerdReadState;
-
-/**
RDF writer.
Provides a number of functions to allow writing RDF syntax out to some
@@ -426,16 +416,32 @@ void
serd_env_free(SerdEnv* env);
/**
- Add namespace @a uri to @a ns using prefix @a name.
+ Get the current base URI.
*/
SERD_API
-void
-serd_env_add(SerdEnv* env,
- const SerdNode* name,
- const SerdNode* uri);
+const SerdNode*
+serd_env_get_base_uri(SerdEnv* state,
+ SerdURI* out);
+
+/**
+ Set the current base URI.
+*/
+SERD_API
+SerdStatus
+serd_env_set_base_uri(SerdEnv* state,
+ const SerdNode* uri_node);
+
+/**
+ Set a namespace prefix.
+*/
+SERD_API
+SerdStatus
+serd_env_set_prefix(SerdEnv* env,
+ const SerdNode* name,
+ const SerdNode* uri);
/**
- Qualify @a into a CURIE if possible.
+ Qualify @a uri into a CURIE if possible.
*/
SERD_API
bool
@@ -455,6 +461,14 @@ serd_env_expand(const SerdEnv* env,
SerdChunk* uri_suffix);
/**
+ Expand @a node, which must be a CURIE or URI, to a full URI.
+*/
+SERD_API
+SerdNode
+serd_env_expand_node(SerdEnv* env,
+ const SerdNode* node);
+
+/**
Call @a func for each prefix defined in @a env.
*/
SERD_API
@@ -519,57 +533,6 @@ void
serd_reader_free(SerdReader* reader);
/**
- Create a new read state with the given initial base URI and environment.
-
- A reference to @a env will be kept, and @a env will be modified as the
- state is modified.
-*/
-SERD_API
-SerdReadState*
-serd_read_state_new(SerdEnv* env,
- const uint8_t* base_uri_str);
-
-/**
- Free @a state.
-*/
-SERD_API
-void
-serd_read_state_free(SerdReadState* state);
-
-/**
- Expand @a node, which must be a CURIE or URI, to a full URI.
-*/
-SERD_API
-SerdNode
-serd_read_state_expand(SerdReadState* state,
- const SerdNode* node);
-
-/**
- Get the current base URI.
-*/
-SERD_API
-const SerdNode*
-serd_read_state_get_base_uri(SerdReadState* state,
- SerdURI* out);
-
-/**
- Set the current base URI.
-*/
-SERD_API
-SerdStatus
-serd_read_state_set_base_uri(SerdReadState* state,
- const SerdNode* uri_node);
-
-/**
- Set a namespace prefix.
-*/
-SERD_API
-SerdStatus
-serd_read_state_set_prefix(SerdReadState* state,
- const SerdNode* name,
- const SerdNode* uri_node);
-
-/**
@}
@name Writer
@{
diff --git a/src/env.c b/src/env.c
index d8a6508d..499aa706 100644
--- a/src/env.c
+++ b/src/env.c
@@ -29,6 +29,8 @@ typedef struct {
struct SerdEnvImpl {
SerdPrefix* prefixes;
size_t n_prefixes;
+ SerdNode base_uri_node;
+ SerdURI base_uri;
};
SERD_API
@@ -36,8 +38,10 @@ SerdEnv*
serd_env_new()
{
SerdEnv* env = malloc(sizeof(struct SerdEnvImpl));
- env->prefixes = NULL;
- env->n_prefixes = 0;
+ env->prefixes = NULL;
+ env->n_prefixes = 0;
+ env->base_uri_node = SERD_NODE_NULL;
+ env->base_uri = SERD_URI_NULL;
return env;
}
@@ -50,9 +54,39 @@ serd_env_free(SerdEnv* env)
serd_node_free(&env->prefixes[i].uri);
}
free(env->prefixes);
+ serd_node_free(&env->base_uri_node);
free(env);
}
+SERD_API
+const SerdNode*
+serd_env_get_base_uri(SerdEnv* env,
+ SerdURI* out)
+{
+ *out = env->base_uri;
+ return &env->base_uri_node;
+}
+
+SERD_API
+SerdStatus
+serd_env_set_base_uri(SerdEnv* env,
+ const SerdNode* uri_node)
+{
+ // Resolve base URI and create a new node and URI for it
+ SerdURI base_uri;
+ SerdNode base_uri_node = serd_node_new_uri_from_node(
+ uri_node, &env->base_uri, &base_uri);
+
+ if (base_uri_node.buf) {
+ // Replace the current base URI
+ serd_node_free(&env->base_uri_node);
+ env->base_uri_node = base_uri_node;
+ env->base_uri = base_uri;
+ return SERD_SUCCESS;
+ }
+ return SERD_ERR_BAD_ARG;
+}
+
static inline SerdPrefix*
serd_env_find(const SerdEnv* env,
const uint8_t* name,
@@ -69,8 +103,7 @@ serd_env_find(const SerdEnv* env,
return NULL;
}
-SERD_API
-void
+static void
serd_env_add(SerdEnv* env,
const SerdNode* name,
const SerdNode* uri)
@@ -89,6 +122,32 @@ serd_env_add(SerdEnv* env,
}
SERD_API
+SerdStatus
+serd_env_set_prefix(SerdEnv* env,
+ const SerdNode* name,
+ const SerdNode* uri_node)
+{
+ if (serd_uri_string_has_scheme(uri_node->buf)) {
+ // Set prefix to absolute URI
+ serd_env_add(env, name, uri_node);
+ } else {
+ // Resolve relative URI and create a new node and URI for it
+ SerdURI abs_uri;
+ SerdNode abs_uri_node = serd_node_new_uri_from_node(
+ uri_node, &env->base_uri, &abs_uri);
+
+ if (!abs_uri_node.buf) {
+ return SERD_ERR_BAD_ARG;
+ }
+
+ // Set prefix to resolved (absolute) URI
+ serd_env_add(env, name, &abs_uri_node);
+ serd_node_free(&abs_uri_node);
+ }
+ return SERD_SUCCESS;
+}
+
+SERD_API
bool
serd_env_qualify(const SerdEnv* env,
const SerdNode* uri,
@@ -136,6 +195,30 @@ serd_env_expand(const SerdEnv* env,
}
SERD_API
+SerdNode
+serd_env_expand_node(SerdEnv* env,
+ const SerdNode* node)
+{
+ if (node->type == SERD_CURIE) {
+ SerdChunk prefix;
+ SerdChunk suffix;
+ serd_env_expand(env, node, &prefix, &suffix);
+ SerdNode ret = { NULL,
+ prefix.len + suffix.len + 1,
+ prefix.len + suffix.len, // FIXME: UTF-8
+ SERD_URI };
+ ret.buf = malloc(ret.n_bytes);
+ snprintf((char*)ret.buf, ret.n_bytes, "%s%s", prefix.buf, suffix.buf);
+ return ret;
+ } else if (node->type == SERD_URI) {
+ SerdURI ignored;
+ return serd_node_new_uri_from_node(node, &env->base_uri, &ignored);
+ } else {
+ return SERD_NODE_NULL;
+ }
+}
+
+SERD_API
void
serd_env_foreach(const SerdEnv* env,
SerdPrefixSink func,
diff --git a/src/reader.c b/src/reader.c
index 3cac2641..e4e91f4c 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -90,12 +90,6 @@ struct SerdReaderImpl {
#endif
};
-struct SerdReadStateImpl {
- SerdEnv* env;
- SerdNode base_uri_node;
- SerdURI base_uri;
-};
-
static int
error(SerdReader* reader, const char* fmt, ...)
{
@@ -1465,103 +1459,3 @@ serd_reader_read_string(SerdReader* me, const uint8_t* utf8)
me->read_buf = NULL;
return ret ? SERD_SUCCESS : SERD_ERR_UNKNOWN;
}
-
-SERD_API
-SerdReadState*
-serd_read_state_new(SerdEnv* env,
- const uint8_t* base_uri_str)
-{
- SerdReadState* state = malloc(sizeof(struct SerdReadStateImpl));
- SerdURI base_base_uri = SERD_URI_NULL;
- state->env = env;
- state->base_uri_node = serd_node_new_uri_from_string(
- base_uri_str, &base_base_uri, &state->base_uri);
- return state;
-}
-
-SERD_API
-void
-serd_read_state_free(SerdReadState* state)
-{
- serd_node_free(&state->base_uri_node);
- free(state);
-}
-
-SERD_API
-SerdNode
-serd_read_state_expand(SerdReadState* state,
- const SerdNode* node)
-{
- if (node->type == SERD_CURIE) {
- SerdChunk prefix;
- SerdChunk suffix;
- serd_env_expand(state->env, node, &prefix, &suffix);
- SerdNode ret = { NULL,
- prefix.len + suffix.len + 1,
- prefix.len + suffix.len, // FIXME: UTF-8
- SERD_URI };
- ret.buf = malloc(ret.n_bytes);
- snprintf((char*)ret.buf, ret.n_bytes, "%s%s", prefix.buf, suffix.buf);
- return ret;
- } else if (node->type == SERD_URI) {
- SerdURI ignored;
- return serd_node_new_uri_from_node(node, &state->base_uri, &ignored);
- } else {
- return SERD_NODE_NULL;
- }
-}
-
-SERD_API
-const SerdNode*
-serd_read_state_get_base_uri(SerdReadState* state,
- SerdURI* out)
-{
- *out = state->base_uri;
- return &state->base_uri_node;
-}
-
-SERD_API
-SerdStatus
-serd_read_state_set_base_uri(SerdReadState* state,
- const SerdNode* uri_node)
-{
- // Resolve base URI and create a new node and URI for it
- SerdURI base_uri;
- SerdNode base_uri_node = serd_node_new_uri_from_node(
- uri_node, &state->base_uri, &base_uri);
-
- if (base_uri_node.buf) {
- // Replace the current base URI
- serd_node_free(&state->base_uri_node);
- state->base_uri_node = base_uri_node;
- state->base_uri = base_uri;
- return SERD_SUCCESS;
- }
- return SERD_ERR_BAD_ARG;
-}
-
-SERD_API
-SerdStatus
-serd_read_state_set_prefix(SerdReadState* state,
- const SerdNode* name,
- const SerdNode* uri_node)
-{
- if (serd_uri_string_has_scheme(uri_node->buf)) {
- // Set prefix to absolute URI
- serd_env_add(state->env, name, uri_node);
- } else {
- // Resolve relative URI and create a new node and URI for it
- SerdURI abs_uri;
- SerdNode abs_uri_node = serd_node_new_uri_from_node(
- uri_node, &state->base_uri, &abs_uri);
-
- if (!abs_uri_node.buf) {
- return SERD_ERR_BAD_ARG;
- }
-
- // Set prefix to resolved (absolute) URI
- serd_env_add(state->env, name, &abs_uri_node);
- serd_node_free(&abs_uri_node);
- }
- return SERD_SUCCESS;
-}
diff --git a/src/serdi.c b/src/serdi.c
index e3d774ef..bc46bd6b 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -22,9 +22,8 @@
#include "serd-config.h"
typedef struct {
- SerdEnv* env;
- SerdReadState* read_state;
- SerdWriter* writer;
+ SerdEnv* env;
+ SerdWriter* writer;
} State;
static SerdStatus
@@ -32,9 +31,9 @@ event_base(void* handle,
const SerdNode* uri_node)
{
State* const state = (State*)handle;
- if (!serd_read_state_set_base_uri(state->read_state, uri_node)) {
+ if (!serd_env_set_base_uri(state->env, uri_node)) {
SerdURI base_uri;
- serd_read_state_get_base_uri(state->read_state, &base_uri);
+ serd_env_get_base_uri(state->env, &base_uri);
serd_writer_set_base_uri(state->writer, &base_uri);
return SERD_SUCCESS;
}
@@ -47,7 +46,7 @@ event_prefix(void* handle,
const SerdNode* uri_node)
{
State* const state = (State*)handle;
- serd_read_state_set_prefix(state->read_state, name, uri_node);
+ serd_env_set_prefix(state->env, name, uri_node);
serd_writer_set_prefix(state->writer, name, uri_node);
return SERD_SUCCESS;
}
@@ -207,14 +206,14 @@ main(int argc, char** argv)
output_style |= SERD_STYLE_ABBREVIATED;
}
- SerdReadState* read_state = serd_read_state_new(env, base_uri_str);
-
- serd_read_state_get_base_uri(read_state, &base_uri);
+ SerdNode base_uri_node = serd_node_from_string(SERD_URI, base_uri_str);
+ serd_env_set_base_uri(env, &base_uri_node);
+ serd_env_get_base_uri(env, &base_uri);
SerdWriter* writer = serd_writer_new(
output_syntax, output_style, env, &base_uri, file_sink, out_fd);
- State state = { env, read_state, writer };
+ State state = { env, writer };
SerdReader* reader = serd_reader_new(
SERD_TURTLE, &state,
@@ -232,7 +231,6 @@ main(int argc, char** argv)
serd_writer_finish(state.writer);
serd_writer_free(state.writer);
- serd_read_state_free(state.read_state);
serd_env_free(state.env);
return (status == SERD_SUCCESS) ? 0 : 1;