aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/serd/serd.h27
-rw-r--r--src/node.c72
-rw-r--r--src/uri.c74
3 files changed, 101 insertions, 72 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index cec77649..9ffb16fe 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -521,8 +521,8 @@ serd_uri_string_length(SerdURIView uri);
@param sink Sink to write string output to.
@param stream Opaque user argument to pass to `sink`.
- @return The number of bytes written, which is less than
- `serd_uri_string_length(uri)` on error.
+ @return The length of the written URI string (not including a null
+ terminator), which may be less than `serd_uri_string_length(uri)` on error.
*/
SERD_API
size_t
@@ -531,6 +531,29 @@ serd_write_uri(SerdURIView uri,
void* SERD_NONNULL stream);
/**
+ Write a file URI to `sink` from a path and optional hostname.
+
+ Backslashes in Windows paths will be converted, and other characters will be
+ percent encoded as necessary.
+
+ If `path` is relative, `hostname` is ignored.
+
+ @param path File system path.
+ @param hostname Optional hostname.
+ @param sink Sink to write string output to.
+ @param stream Opaque user argument to pass to `sink`.
+
+ @return The length of the written URI string (not including a null
+ terminator).
+*/
+SERD_API
+size_t
+serd_write_file_uri(SerdStringView path,
+ SerdStringView hostname,
+ SerdWriteFunc SERD_NONNULL sink,
+ void* SERD_NONNULL stream);
+
+/**
@}
@defgroup serd_node Node
@{
diff --git a/src/node.c b/src/node.c
index fc9869a8..d9cfbe93 100644
--- a/src/node.c
+++ b/src/node.c
@@ -28,7 +28,6 @@
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -511,79 +510,12 @@ serd_new_resolved_uri(const SerdStringView string, const SerdURIView base)
return result;
}
-static bool
-is_uri_path_char(const char c)
-{
- if (is_alpha(c) || is_digit(c)) {
- return true;
- }
-
- switch (c) {
- // unreserved:
- case '-':
- case '.':
- case '_':
- case '~':
- case ':':
-
- case '@': // pchar
- case '/': // separator
-
- // sub-delimiters:
- case '!':
- case '$':
- case '&':
- case '\'':
- case '(':
- case ')':
- case '*':
- case '+':
- case ',':
- case ';':
- case '=':
- return true;
- default:
- return false;
- }
-}
-
SerdNode*
serd_new_file_uri(const SerdStringView path, const SerdStringView hostname)
{
- const bool is_windows = is_windows_path(path.buf);
- size_t uri_len = 0;
- char* uri = NULL;
+ SerdBuffer buffer = {NULL, 0u};
- if (path.buf[0] == '/' || is_windows) {
- uri_len = strlen("file://") + hostname.len + is_windows;
- uri = (char*)calloc(uri_len + 1, 1);
-
- memcpy(uri, "file://", 7);
-
- if (hostname.len) {
- memcpy(uri + 7, hostname.buf, hostname.len + 1);
- }
-
- if (is_windows) {
- uri[7 + hostname.len] = '/';
- }
- }
-
- SerdBuffer buffer = {uri, uri_len};
- for (size_t i = 0; i < path.len; ++i) {
- if (is_windows && path.buf[i] == '\\') {
- serd_buffer_sink("/", 1, 1, &buffer);
- } else if (path.buf[i] == '%') {
- serd_buffer_sink("%%", 1, 2, &buffer);
- } else if (is_uri_path_char(path.buf[i])) {
- serd_buffer_sink(path.buf + i, 1, 1, &buffer);
- } else {
- char escape_str[10] = {'%', 0, 0, 0, 0, 0, 0, 0, 0, 0};
- snprintf(
- escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path.buf[i]);
- serd_buffer_sink(escape_str, 1, 3, &buffer);
- }
- }
+ serd_write_file_uri(path, hostname, serd_buffer_sink, &buffer);
serd_buffer_sink_finish(&buffer);
SerdNode* node =
diff --git a/src/uri.c b/src/uri.c
index eb4a2533..c66e28d7 100644
--- a/src/uri.c
+++ b/src/uri.c
@@ -495,3 +495,77 @@ serd_write_uri(const SerdURIView uri,
}
return len;
}
+
+static bool
+is_uri_path_char(const char c)
+{
+ if (is_alpha(c) || is_digit(c)) {
+ return true;
+ }
+
+ switch (c) {
+ // unreserved:
+ case '-':
+ case '.':
+ case '_':
+ case '~':
+ case ':':
+
+ case '@': // pchar
+ case '/': // separator
+
+ // sub-delimiters:
+ case '!':
+ case '$':
+ case '&':
+ case '\'':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case ';':
+ case '=':
+ return true;
+ default:
+ return false;
+ }
+}
+
+size_t
+serd_write_file_uri(const SerdStringView path,
+ const SerdStringView hostname,
+ const SerdWriteFunc sink,
+ void* const stream)
+{
+ const bool is_windows = is_windows_path(path.buf);
+ size_t len = 0u;
+
+ if (path.buf[0] == '/' || is_windows) {
+ len += sink("file://", 1, strlen("file://"), stream);
+ if (hostname.len) {
+ len += sink(hostname.buf, 1, hostname.len, stream);
+ }
+
+ if (is_windows) {
+ len += sink("/", 1, 1, stream);
+ }
+ }
+
+ for (size_t i = 0; i < path.len; ++i) {
+ if (is_windows && path.buf[i] == '\\') {
+ len += sink("/", 1, 1, stream);
+ } else if (path.buf[i] == '%') {
+ len += sink("%%", 1, 2, stream);
+ } else if (is_uri_path_char(path.buf[i])) {
+ len += sink(path.buf + i, 1, 1, stream);
+ } else {
+ char escape_str[10] = {'%', 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ snprintf(
+ escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path.buf[i]);
+ len += sink(escape_str, 1, 3, stream);
+ }
+ }
+
+ return len;
+}