13#include "../char_maps/module.hpp"
14#include "../utility/utility.hpp"
15#include "../macros.hpp"
20#include <unordered_map>
26hi_export_module(hikogui.path.URL);
31hi_warning_ignore_msvc(26434);
33namespace hi {
inline namespace v1 {
75 constexpr explicit URL(std::string_view str) :
URI(str) {}
91 constexpr explicit URL(
const char *str) :
URL(
std::string_view{str}) {}
100 explicit URL(std::filesystem::path
const&
path) :
URI(make_file_url_string(
path)) {}
110 throw url_error(
"URL::generic_path() is only valid on a file: scheme URL");
115 hilet first = p.begin();
116 hilet last = p.end();
124 validate_file_server(
server);
136 if (p.double_absolute()) {
140 throw url_error(
"file URL has two server names.");
147 validate_file_server(*
it);
155 validate_file_segment(*
it);
162 }
else if (
auto i =
it->find(
':'); i != std::string::npos) {
165 throw url_error(
"file URL contains a device name which is a security issue.");
196 for (;
it != last; ++
it) {
197 validate_file_segment(*
it);
225 throw url_error(std::format(
"Resource {} not found.", *
this));
228 }
else if (
scheme_ ==
"file") {
231 throw url_error(
"URL can not be converted to a filesystem path.");
241 operator std::filesystem::path()
const
246 [[
nodiscard]]
constexpr friend URL operator/(
URL const& base,
URI const& ref)
noexcept
251 [[
nodiscard]]
constexpr friend URL operator/(
URL const& base, std::string_view ref)
noexcept
257 constexpr void static validate_file_segment(std::string_view
segment)
260 if (c ==
'/' or c ==
'\\') {
261 throw url_error(
"Filename server name may not contain slash or back-slash.");
266 constexpr void static validate_file_server(std::string_view
server)
269 if (c ==
'/' or c ==
'\\') {
270 throw url_error(
"Filename segments may not contain slash or back-slash.");
275 static std::string make_file_url_string(std::filesystem::path
const&
path)
277 auto r = std::u8string{};
282 if (
not path.root_directory().empty()) {
284 r +=
u8"file:" +
path.root_directory().generic_u8string();
290 }
else if (hilet i =
root_name.find(
':'); i != std::string::npos) {
293 r +=
u8"file:///" +
root_name +
path.root_directory().generic_u8string();
295 throw url_error(
"Paths containing a device are not allowed to be converted to a URL.");
299 r +=
u8"file://" +
root_name +
path.root_directory().generic_u8string();
300 if (
not path.root_directory().empty()) {
301 throw url_error(
"Invalid path contains server name without a root directory.");
305 return to_string(r +
path.relative_path().generic_u8string());
319template<
typename CharT>
320struct std::formatter<
hi::URL, CharT> : std::formatter<hi::URI, CharT> {
321 auto format(
hi::URL const& t,
auto& fc)
const
323 return std::formatter<hi::URI, CharT>::format(t, fc);
functions to locate files and directories.
constexpr std::u8string to_u8string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:112
std::optional< std::filesystem::path > find_path(path_location location, std::filesystem::path const &ref) noexcept
Find a path.
Definition path_location_intf.hpp:86
@ resource_dirs
The location of application resources.
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A Uniform Resource Identifier.
Definition URI.hpp:39
constexpr std::optional< authority_type > const & authority() const noexcept
Get the authority-component of the URI.
Definition URI.hpp:552
constexpr std::optional< std::string > const & scheme() const noexcept
Get the scheme-component of the URI.
Definition URI.hpp:529
Universal Resource Locator.
Definition URL.hpp:51
constexpr URL() noexcept=default
Create an empty URL.
URL(std::filesystem::path const &path)
Convert a filesystem-path to a file-scheme URL.
Definition URL.hpp:100
constexpr URL(URI &&other) noexcept
Convert a URI to an URL.
Definition URL.hpp:67
operator std::filesystem::path() const
Definition URL.hpp:241
constexpr URL(const char *str)
Construct a URI from a string.
Definition URL.hpp:91
constexpr URL(std::string const &str)
Construct a URI from a string.
Definition URL.hpp:83
std::filesystem::path filesystem_path() const
Create a filesystem path from a file URL.
Definition URL.hpp:216
constexpr std::u8string filesystem_path_generic_u8string(bool validate_scheme=true) const
Return a generic path.
Definition URL.hpp:107
constexpr URL(std::string_view str)
Construct a URI from a string.
Definition URL.hpp:75
Definition exception_intf.hpp:203