aboutsummaryrefslogtreecommitdiffstats
path: root/src/uri.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/uri.c')
-rw-r--r--src/uri.c74
1 files changed, 74 insertions, 0 deletions
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;
+}