Browse Source
git-svn-id: http://svn.drobilla.net/serd/trunk@36 490d8e77-9747-427b-9fa3-0b8f29cee8a0zrythm_meson

6 changed files with 156 additions and 159 deletions
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
/* Serd, an RDF serialisation library.
|
||||
* Copyright 2011 David Robillard <d@drobilla.net> |
||||
* |
||||
* Serd is free software: you can redistribute it and/or modify it under |
||||
* the terms of the GNU Lesser General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Serd is distributed in the hope that it will be useful, but WITHOUT |
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
||||
* License for details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#include <assert.h> |
||||
#include <stdbool.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include "serd/serd.h" |
||||
|
||||
typedef struct { |
||||
SerdString* name; |
||||
SerdString* uri; |
||||
} SerdPrefix; |
||||
|
||||
struct SerdEnvImpl { |
||||
SerdPrefix* prefixes; |
||||
size_t n_prefixes; |
||||
}; |
||||
|
||||
SERD_API |
||||
SerdEnv |
||||
serd_env_new() |
||||
{ |
||||
SerdEnv env = malloc(sizeof(struct SerdEnvImpl)); |
||||
env->prefixes = NULL; |
||||
env->n_prefixes = 0; |
||||
return env; |
||||
} |
||||
|
||||
SERD_API |
||||
void |
||||
serd_env_free(SerdEnv env) |
||||
{ |
||||
for (size_t i = 0; i < env->n_prefixes; ++i) { |
||||
serd_string_free(env->prefixes[i].name); |
||||
serd_string_free(env->prefixes[i].uri); |
||||
} |
||||
free(env->prefixes); |
||||
free(env); |
||||
} |
||||
|
||||
static inline SerdPrefix* |
||||
serd_env_find(SerdEnv env, |
||||
const uint8_t* name, |
||||
size_t name_len) |
||||
{ |
||||
for (size_t i = 0; i < env->n_prefixes; ++i) { |
||||
const SerdString* prefix_name = env->prefixes[i].name; |
||||
if (prefix_name->n_bytes == name_len + 1) { |
||||
if (!memcmp(prefix_name->buf, name, name_len)) { |
||||
return &env->prefixes[i]; |
||||
} |
||||
} |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
SERD_API |
||||
void |
||||
serd_env_add(SerdEnv env, |
||||
const SerdString* name, |
||||
const SerdString* uri) |
||||
{ |
||||
assert(name && uri); |
||||
SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_chars); |
||||
if (prefix) { |
||||
serd_string_free(prefix->uri); |
||||
prefix->uri = serd_string_copy(uri); |
||||
} else { |
||||
env->prefixes = realloc(env->prefixes, |
||||
(++env->n_prefixes) * sizeof(SerdPrefix)); |
||||
env->prefixes[env->n_prefixes - 1].name = serd_string_copy(name); |
||||
env->prefixes[env->n_prefixes - 1].uri = serd_string_copy(uri); |
||||
} |
||||
} |
||||
|
||||
SERD_API |
||||
bool |
||||
serd_env_expand(const SerdEnv env, |
||||
const SerdString* qname, |
||||
SerdChunk* uri_prefix, |
||||
SerdChunk* uri_suffix) |
||||
{ |
||||
const uint8_t* const colon = memchr(qname->buf, ':', qname->n_bytes); |
||||
if (!colon) { |
||||
return false; // Illegal qname
|
||||
} |
||||
|
||||
const size_t name_len = colon - qname->buf; |
||||
const SerdPrefix* const prefix = serd_env_find(env, qname->buf, name_len); |
||||
if (prefix) { |
||||
uri_prefix->buf = prefix->uri->buf; |
||||
uri_prefix->len = prefix->uri->n_bytes - 1; |
||||
uri_suffix->buf = colon + 1; |
||||
uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 2; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
@ -1,115 +0,0 @@
@@ -1,115 +0,0 @@
|
||||
/* Serd, an RDF serialisation library.
|
||||
* Copyright 2011 David Robillard <d@drobilla.net> |
||||
* |
||||
* Serd is free software: you can redistribute it and/or modify it under |
||||
* the terms of the GNU Lesser General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Serd is distributed in the hope that it will be useful, but WITHOUT |
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
||||
* License for details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#include <assert.h> |
||||
#include <stdbool.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include "serd/serd.h" |
||||
|
||||
typedef struct { |
||||
SerdString* name; |
||||
SerdString* uri; |
||||
} SerdNamespace; |
||||
|
||||
struct SerdNamespacesImpl { |
||||
SerdNamespace* namespaces; |
||||
size_t n_namespaces; |
||||
}; |
||||
|
||||
SERD_API |
||||
SerdNamespaces |
||||
serd_namespaces_new() |
||||
{ |
||||
SerdNamespaces ns = malloc(sizeof(struct SerdNamespacesImpl)); |
||||
ns->namespaces = NULL; |
||||
ns->n_namespaces = 0; |
||||
return ns; |
||||
} |
||||
|
||||
SERD_API |
||||
void |
||||
serd_namespaces_free(SerdNamespaces ns) |
||||
{ |
||||
for (size_t i = 0; i < ns->n_namespaces; ++i) { |
||||
serd_string_free(ns->namespaces[i].name); |
||||
serd_string_free(ns->namespaces[i].uri); |
||||
} |
||||
free(ns->namespaces); |
||||
free(ns); |
||||
} |
||||
|
||||
static inline SerdNamespace* |
||||
serd_namespaces_find(SerdNamespaces ns, |
||||
const uint8_t* name, |
||||
size_t name_len) |
||||
{ |
||||
for (size_t i = 0; i < ns->n_namespaces; ++i) { |
||||
const SerdString* ns_name = ns->namespaces[i].name; |
||||
if (ns_name->n_bytes == name_len + 1) { |
||||
if (!memcmp(ns_name->buf, name, name_len)) { |
||||
return &ns->namespaces[i]; |
||||
} |
||||
} |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
SERD_API |
||||
void |
||||
serd_namespaces_add(SerdNamespaces ns, |
||||
const SerdString* name, |
||||
const SerdString* uri) |
||||
{ |
||||
assert(name); |
||||
assert(uri); |
||||
SerdNamespace* const record = serd_namespaces_find(ns, name->buf, name->n_chars); |
||||
if (record) { |
||||
serd_string_free(record->uri); |
||||
record->uri = serd_string_copy(uri); |
||||
} else { |
||||
++ns->n_namespaces; |
||||
ns->namespaces = realloc(ns->namespaces, |
||||
ns->n_namespaces * sizeof(SerdNamespace)); |
||||
ns->namespaces[ns->n_namespaces - 1].name = serd_string_copy(name); |
||||
ns->namespaces[ns->n_namespaces - 1].uri = serd_string_copy(uri); |
||||
} |
||||
} |
||||
|
||||
SERD_API |
||||
bool |
||||
serd_namespaces_expand(const SerdNamespaces ns, |
||||
const SerdString* qname, |
||||
SerdChunk* uri_prefix, |
||||
SerdChunk* uri_suffix) |
||||
{ |
||||
const uint8_t* colon = memchr((const char*)qname->buf, ':', qname->n_bytes); |
||||
if (!colon) { |
||||
return false; // Illegal qname
|
||||
} |
||||
|
||||
SerdNamespace* const record = serd_namespaces_find(ns, qname->buf, colon - qname->buf); |
||||
if (record) { |
||||
uri_prefix->buf = record->uri->buf; |
||||
uri_prefix->len = record->uri->n_bytes - 1; |
||||
uri_suffix->buf = colon + 1; |
||||
uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 2; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
Loading…
Reference in new issue