Browse Source

Support file://localhost/foo URIs in serd_uri_to_path().

Support Windows file://c:/foo URIs in serd_uri_to_path() on all platforms.
100% test coverage (by line) for uri.c.


git-svn-id: http://svn.drobilla.net/serd/trunk@269 490d8e77-9747-427b-9fa3-0b8f29cee8a0
zrythm_meson
David Robillard 11 years ago
parent
commit
5444866800
  1. 2
      ChangeLog
  2. 26
      src/serd_test.c
  3. 26
      src/uri.c

2
ChangeLog

@ -19,6 +19,8 @@ serd (UNRELEASED) unstable; urgency=low @@ -19,6 +19,8 @@ serd (UNRELEASED) unstable; urgency=low
* Escape ASCII control characters in output (e.g. fix problems with string
literals that start with a backspace)
* Improve URI resolution to cover most of the abnormal cases from RFC3986
* Support file://localhost/foo URIs in serd_uri_to_path()
* Support Windows file://c:/foo URIs in serd_uri_to_path() on all platforms
-- David Robillard <d@drobilla.net> (UNRELEASED)

26
src/serd_test.c

@ -126,6 +126,7 @@ main() @@ -126,6 +126,7 @@ main()
}
// Test serd_strlen
const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 };
size_t n_bytes;
@ -139,6 +140,7 @@ main() @@ -139,6 +140,7 @@ main()
}
// Test serd_strerror
const uint8_t* msg = NULL;
if (strcmp((const char*)(msg = serd_strerror(SERD_SUCCESS)), "Success")) {
fprintf(stderr, "Bad message `%s' for SERD_SUCCESS\n", msg);
@ -151,7 +153,29 @@ main() @@ -151,7 +153,29 @@ main()
return 1;
}
}
// Test serd_uri_to_path
const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl";
if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri);
return 1;
}
uri = (const uint8_t*)"file://localhost/home/user/foo.ttl";
if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri);
return 1;
}
uri = (const uint8_t*)"file:illegal/file/uri";
if (serd_uri_to_path(uri)) {
fprintf(stderr, "Converted invalid URI `%s' to path `%s'\n",
uri, serd_uri_to_path(uri));
}
uri = (const uint8_t*)"file:///c:/awful/system";
if (strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")) {
fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri);
return 1;
}
printf("Success\n");
return 0;
}

26
src/uri.c

@ -26,29 +26,27 @@ SERD_API @@ -26,29 +26,27 @@ SERD_API
const uint8_t*
serd_uri_to_path(const uint8_t* uri)
{
const uint8_t* filename = NULL;
const uint8_t* path = NULL;
if (serd_uri_string_has_scheme(uri)) {
// Absolute URI, ensure it a file and chop scheme
if (strncmp((const char*)uri, "file:", 5)) {
fprintf(stderr, "Non-file URI `%s'\n", uri);
return NULL;
} else if (strncmp((const char*)uri + 5, "//", 2)) {
fprintf(stderr, "Illegal file URI `%s'\n", uri);
return NULL;
#ifdef __WIN32__
} else if (!strncmp((const char*)uri, "file:///", 8)) {
filename = uri + 8;
#else
} else if (!strncmp((const char*)uri, "file://localhost/", 17)) {
path = uri + 16;
} else if (!strncmp((const char*)uri, "file://", 7)) {
filename = uri + 7;
#endif
path = uri + 7;
} else {
filename = uri + 5;
fprintf(stderr, "Invalid file URI `%s'\n", uri);
return NULL;
}
// Special case for awful Windows file URIs
if (is_alpha(path[1]) && path[2] == ':' && path[3] == '/') {
++path; // Special case for terrible Windows file URIs
}
} else {
filename = uri;
path = uri;
}
return filename;
return path;
}
SERD_API

Loading…
Cancel
Save