diff --git a/include/upa/url.h b/include/upa/url.h index 6573973..d979d81 100644 --- a/include/upa/url.h +++ b/include/upa/url.h @@ -31,6 +31,7 @@ #include #include #include // uint8_t +#include #include // std::hash #include #include @@ -3190,6 +3191,21 @@ template = 0> return url(str_url); } +/// @brief Make URL from OS file path +/// +/// Throws url_error exception on error. +/// +/// @param[in] path absolute file path +/// @return file URL +inline url url_from_file_path(const std::filesystem::path& path) { + // Use the native format, to let the string version of this function detect OS specifics +#ifdef _WIN32 + return url_from_file_path(path.native()); // UTF-16 +#else + return url_from_file_path(path.u8string()); // UTF-8 +#endif +} + /// @brief Get OS path from file URL /// /// Throws url_error exception on error. diff --git a/test/test-url.cpp b/test/test-url.cpp index 6b3d5c9..bfbe7a9 100644 --- a/test/test-url.cpp +++ b/test/test-url.cpp @@ -729,6 +729,15 @@ TEST_CASE("url_from_file_path") { #else CHECK(upa::url_from_file_path("/").href() == "file:///"); CHECK(upa::url_from_file_path("/", upa::file_path_format::native).href() == "file:///"); +#endif + } + SUBCASE("std::filesystem::path") { +#ifdef _WIN32 + std::filesystem::path path{ "c:\\dir\\file.txt" }; + CHECK(upa::url_from_file_path(path).href() == "file:///c:/dir/file.txt"); +#else + std::filesystem::path path{ "/dir/file.txt" }; + CHECK(upa::url_from_file_path(path).href() == "file:///dir/file.txt"); #endif } }