Skip to content

Commit

Permalink
Add url_from_file_path function with std::filesystem::path parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Dec 13, 2024
1 parent 7360681 commit c4a84c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <cassert>
#include <cstddef>
#include <cstdint> // uint8_t
#include <filesystem>
#include <functional> // std::hash
#include <iterator>
#include <string>
Expand Down Expand Up @@ -3190,6 +3191,21 @@ template <class StrT, enable_if_str_arg_t<StrT> = 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.
Expand Down
9 changes: 9 additions & 0 deletions test/test-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit c4a84c2

Please sign in to comment.