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 {
51class URL :
public URI {
55 constexpr URL() noexcept = default;
56 constexpr
URL(
URL const&) noexcept = default;
57 constexpr
URL(
URL&&) noexcept = default;
58 constexpr
URL& operator=(
URL const&) noexcept = default;
59 constexpr
URL& operator=(
URL&&) noexcept = default;
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)) {}
109 if (validate_scheme and not(not
scheme() or
scheme() ==
"file")) {
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();
119 auto has_root_name =
false;
123 if (not server.empty() and server !=
"localhost") {
124 validate_file_server(server);
125 has_root_name =
true;
134 hi_assert(has_root_name ==
false or p.absolute());
136 if (p.double_absolute()) {
140 throw url_error(
"file URL has two server names.");
143 has_root_name =
true;
147 validate_file_server(*it);
153 auto empty_segment =
false;
155 validate_file_segment(*it);
156 empty_segment = it->empty();
158 if (it == first and empty_segment) {
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.");
168 if (has_root_name or p.absolute()) {
171 r += has_root_name ?
'$' :
':';
184 has_root_name =
true;
196 for (; it != last; ++it) {
197 validate_file_segment(*it);
198 empty_segment = it->empty();
203 if (not empty_segment) {
218 if (
auto scheme_ =
scheme()) {
219 if (scheme_ ==
"resource") {
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)
259 for (
auto c : 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)
268 for (
auto c : 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{};
279 hilet root_name =
path.root_name().generic_u8string();
280 if (root_name.empty()) {
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.
Definition path_location_intf.hpp:27
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:22
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
constexpr Out up_cast(In *rhs) noexcept
Cast a pointer to a class to its base class or itself.
Definition cast.hpp:65
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