diff options
author | David Robillard <d@drobilla.net> | 2012-03-10 22:24:09 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-03-10 22:24:09 +0000 |
commit | d14a7d113628578b6ed065b08db6c92536f550bd (patch) | |
tree | ff7ab0c295103f640e031c5013fa5c774f6fb821 /src/serd_internal.h | |
parent | 60d7a9ff146ff3c137a99b82dd75fe8f57525938 (diff) | |
download | serd-d14a7d113628578b6ed065b08db6c92536f550bd.tar.gz serd-d14a7d113628578b6ed065b08db6c92536f550bd.tar.bz2 serd-d14a7d113628578b6ed065b08db6c92536f550bd.zip |
Add serd_writer_set_root_uri(), root parameter to
serd_uri_serialise_relative(), and -r option to serdi, to enable writing URIs
with up references (../) within a parent namespace.
Resolve dot segments in serd_uri_resolve() instead of at write time.
git-svn-id: http://svn.drobilla.net/serd/trunk@336 490d8e77-9747-427b-9fa3-0b8f29cee8a0
Diffstat (limited to 'src/serd_internal.h')
-rw-r--r-- | src/serd_internal.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/serd_internal.h b/src/serd_internal.h index f0137f28..08d68fd5 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -240,4 +240,55 @@ is_windows_path(const uint8_t* path) && (path[2] == '/' || path[2] == '\\'); } +/* URI utilities */ + +static inline bool +chunk_equals(const SerdChunk* a, const SerdChunk* b) +{ + return a->len == b->len + && !strncmp((const char*)a->buf, (const char*)b->buf, a->len); +} + +static inline size_t +uri_path_len(const SerdURI* uri) +{ + return uri->path_base.len + uri->path.len; +} + +static inline uint8_t +uri_path_at(const SerdURI* uri, size_t i) +{ + if (i < uri->path_base.len) { + return uri->path_base.buf[i]; + } else { + return uri->path.buf[i - uri->path_base.len]; + } +} + +/** Return true iff @p uri is within the base of @p root */ +static inline bool +uri_is_under(const SerdURI* uri, const SerdURI* root) +{ + if (!root || !uri || !root->scheme.len || + !chunk_equals(&root->scheme, &uri->scheme) || + !chunk_equals(&root->authority, &uri->authority)) { + return false; + } + + bool differ = false; + const size_t path_len = uri_path_len(uri); + const size_t root_len = uri_path_len(root); + for (size_t i = 0; i < path_len && i < root_len; ++i) { + if (uri_path_at(uri, i) != uri_path_at(root, i)) { + differ = true; + } + if (differ && uri_path_at(root, i) == '/') { + return false; + } + } + + return true; +} + + #endif // SERD_INTERNAL_H |