13#include "../char_maps/char_maps.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);
38hi_warning_ignore_msvc(4702);
40hi_export
namespace hi {
inline namespace v1 {
62 constexpr URL() noexcept = default;
63 constexpr
URL(
URL const&) noexcept = default;
64 constexpr
URL(
URL&&) noexcept = default;
65 constexpr
URL& operator=(
URL const&) noexcept = default;
66 constexpr
URL& operator=(
URL&&) noexcept = default;
82 constexpr explicit URL(std::string_view str) :
URI(str) {}
98 constexpr explicit URL(
const char *str) :
URL(
std::string_view{str}) {}
107 explicit URL(std::filesystem::path
const&
path) :
URI(make_file_url_string(
path)) {}
116 if (validate_scheme and not(not
scheme() or
scheme() ==
"file")) {
117 throw url_error(
"URL::generic_path() is only valid on a file: scheme URL");
121 auto const& p =
path();
122 auto const first = p.begin();
123 auto const last = p.end();
126 auto has_root_name =
false;
130 if (not server.empty() and server !=
"localhost") {
131 validate_file_server(server);
132 has_root_name =
true;
141 hi_assert(has_root_name ==
false or p.absolute());
143 if (p.double_absolute()) {
147 throw url_error(
"file URL has two server names.");
150 has_root_name =
true;
154 validate_file_server(*it);
160 auto empty_segment =
false;
162 validate_file_segment(*it);
163 empty_segment = it->empty();
165 if (it == first and empty_segment) {
169 }
else if (
auto i = it->find(
':'); i != std::string::npos) {
172 throw url_error(
"file URL contains a device name which is a security issue.");
175 if (has_root_name or p.absolute()) {
178 r += has_root_name ?
'$' :
':';
191 has_root_name =
true;
203 for (; it != last; ++it) {
204 validate_file_segment(*it);
205 empty_segment = it->empty();
210 if (not empty_segment) {
225 if (
auto scheme_ =
scheme()) {
226 if (scheme_ ==
"resource") {
234 throw url_error(std::format(
"Resource '{}' not found in search-path: '{}'", to_string(*
this), to_string(
resource_dirs())));
236 }
else if (scheme_ ==
"file") {
239 throw url_error(
"URL can not be converted to a filesystem path.");
249 operator std::filesystem::path()
const
254 [[nodiscard]]
constexpr friend URL operator/(
URL const& base,
URI const& ref)
noexcept
256 return URL{up_cast<URI const&>(base) / ref};
259 [[nodiscard]]
constexpr friend URL operator/(
URL const& base, std::string_view ref)
noexcept
261 return URL{up_cast<URI const&>(base) / ref};
265 constexpr void static validate_file_segment(std::string_view segment)
267 for (
auto c : segment) {
268 if (c ==
'/' or c ==
'\\') {
269 throw url_error(
"Filename server name may not contain slash or back-slash.");
274 constexpr void static validate_file_server(std::string_view server)
276 for (
auto c : server) {
277 if (c ==
'/' or c ==
'\\') {
278 throw url_error(
"Filename segments may not contain slash or back-slash.");
283 static std::string make_file_url_string(std::filesystem::path
const&
path)
285 auto r = std::u8string{};
287 auto const root_name =
path.root_name().generic_u8string();
288 if (root_name.empty()) {
290 if (not
path.root_directory().empty()) {
292 r += u8
"file:" +
path.root_directory().generic_u8string();
298 }
else if (
auto const i = root_name.find(
':'); i != std::string::npos) {
301 r += u8
"file:///" + root_name +
path.root_directory().generic_u8string();
303 throw url_error(
"Paths containing a device are not allowed to be converted to a URL.");
307 r += u8
"file://" + root_name +
path.root_directory().generic_u8string();
308 if (not
path.root_directory().empty()) {
309 throw url_error(
"Invalid path contains server name without a root directory.");
313 return to_string(r +
path.relative_path().generic_u8string());
329struct std::formatter<
hi::URL, char> : std::formatter<hi::URI, char> {
330 auto format(
hi::URL const& t,
auto& fc)
const
332 return std::formatter<hi::URI, char>::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:116
@ other
The gui_event does not have associated data.
generator< std::filesystem::path > find_path(Locations &&locations, std::filesystem::path const &ref) noexcept
Find a path.
Definition path_location_intf.hpp:50
generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
Definition path_location_win32_impl.hpp:63
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A Uniform Resource Identifier.
Definition URI.hpp:45
constexpr std::optional< authority_type > const & authority() const noexcept
Get the authority-component of the URI.
Definition URI.hpp:558
constexpr std::optional< std::string > const & scheme() const noexcept
Get the scheme-component of the URI.
Definition URI.hpp:535
Universal Resource Locator.
Definition URL.hpp:58
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:107
constexpr URL(URI &&other) noexcept
Convert a URI to an URL.
Definition URL.hpp:74
operator std::filesystem::path() const
Definition URL.hpp:249
constexpr URL(const char *str)
Construct a URI from a string.
Definition URL.hpp:98
constexpr URL(std::string const &str)
Construct a URI from a string.
Definition URL.hpp:90
std::filesystem::path filesystem_path() const
Create a filesystem path from a file URL.
Definition URL.hpp:223
constexpr std::u8string filesystem_path_generic_u8string(bool validate_scheme=true) const
Return a generic path.
Definition URL.hpp:114
constexpr URL(std::string_view str)
Construct a URI from a string.
Definition URL.hpp:82
Definition exception_intf.hpp:204