HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
URL.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "required.hpp"
8#include <string>
9#include <string_view>
10#include <optional>
11#include <vector>
12#include <unordered_map>
13#include <memory>
14#include <ostream>
15#include <mutex>
16
17namespace tt {
18struct url_parts;
19class resource_view;
20
46class URL {
47private:
48 std::string value;
49
50public:
51 URL() = default;
52 explicit URL(std::string_view url);
53 explicit URL(char const *url);
54 explicit URL(std::string const &url);
55 explicit URL(url_parts const &parts);
56
57 URL(URL const &other) noexcept : value(other.value) {}
58 URL(URL &&other) noexcept = default;
59 URL &operator=(URL const &other) noexcept { value = other.value; return *this; }
60 URL &operator=(URL &&other) noexcept = default;
61
62 [[nodiscard]] size_t hash() const noexcept;
63
64 [[nodiscard]] std::string_view scheme() const noexcept;
65
66 [[nodiscard]] std::string query() const noexcept;
67
68 [[nodiscard]] std::string fragment() const noexcept;
69
70 [[nodiscard]] std::string filename() const noexcept;
71
72 [[nodiscard]] std::string directory() const noexcept;
73
74 [[nodiscard]] std::string nativeDirectory() const noexcept;
75
76 [[nodiscard]] std::string extension() const noexcept;
77
78 [[nodiscard]] std::vector<std::string> pathSegments() const noexcept;
79
80 [[nodiscard]] std::string path() const noexcept;
81
82 [[nodiscard]] std::string nativePath() const noexcept;
83
84 [[nodiscard]] std::wstring nativeWPath() const noexcept;
85
86 [[nodiscard]] bool isFileScheme() const noexcept {
87 return scheme() == "file";
88 }
89
90 [[nodiscard]] bool isAbsolute() const noexcept;
91 [[nodiscard]] bool isRelative() const noexcept;
92 [[nodiscard]] bool isRootDirectory() const noexcept;
93
94 [[nodiscard]] URL urlByAppendingPath(URL const &other) const noexcept;
95
96 [[nodiscard]] URL urlByAppendingPath(std::string_view const other) const noexcept;
97 [[nodiscard]] URL urlByAppendingPath(std::string const &other) const noexcept;
98 [[nodiscard]] URL urlByAppendingPath(char const *other) const noexcept;
99
100 [[nodiscard]] URL urlByAppendingPath(std::wstring_view const other) const noexcept;
101 [[nodiscard]] URL urlByAppendingPath(std::wstring const &other) const noexcept;
102 [[nodiscard]] URL urlByAppendingPath(wchar_t const *other) const noexcept;
103
104 [[nodiscard]] URL urlByAppendingExtension(std::string_view other) const noexcept;
105 [[nodiscard]] URL urlByAppendingExtension(std::string const &other) const noexcept;
106 [[nodiscard]] URL urlByAppendingExtension(char const *other) const noexcept;
107
108 [[nodiscard]] URL urlByRemovingFilename() const noexcept;
109
114
125 [[nodiscard]] std::vector<URL> urlsByScanningWithGlobPattern() const noexcept;
126
127 [[nodiscard]] static URL urlFromPath(std::string_view const path) noexcept;
128 [[nodiscard]] static URL urlFromWPath(std::wstring_view const path) noexcept;
129
130 [[nodiscard]] static URL urlFromCurrentWorkingDirectory() noexcept;
131 [[nodiscard]] static URL urlFromResourceDirectory() noexcept;
132 [[nodiscard]] static URL urlFromExecutableDirectory() noexcept;
133 [[nodiscard]] static URL urlFromExecutableFile() noexcept;
134 [[nodiscard]] static URL urlFromApplicationDataDirectory() noexcept;
135 [[nodiscard]] static URL urlFromApplicationLogDirectory() noexcept;
136 [[nodiscard]] static URL urlFromSystemfontDirectory() noexcept;
137
142 [[nodiscard]] static std::vector<std::string> filenamesByScanningDirectory(std::string_view path) noexcept;
143
144 [[nodiscard]] static std::string nativePathFromPath(std::string_view path) noexcept;
145 [[nodiscard]] static std::wstring nativeWPathFromPath(std::string_view path) noexcept;
146
147 [[nodiscard]] friend bool operator==(URL const &lhs, URL const &rhs) noexcept
148 {
149 return lhs.value == rhs.value;
150 }
151
152 [[nodiscard]] friend bool operator<(URL const &lhs, URL const &rhs) noexcept
153 {
154 return lhs.value < rhs.value;
155 }
156
157 [[nodiscard]] friend bool operator>(URL const &lhs, URL const &rhs) noexcept
158 {
159 return rhs < lhs;
160 }
161
162 [[nodiscard]] friend bool operator!=(URL const &lhs, URL const &rhs) noexcept
163 {
164 return !(lhs == rhs);
165 }
166
167 [[nodiscard]] friend bool operator>=(URL const &lhs, URL const &rhs) noexcept
168 {
169 return !(lhs < rhs);
170 }
171
172 [[nodiscard]] friend bool operator<=(URL const &lhs, URL const &rhs) noexcept
173 {
174 return !(lhs > rhs);
175 }
176
177 [[nodiscard]] friend URL operator/(URL const &lhs, URL const &rhs) noexcept
178 {
179 return lhs.urlByAppendingPath(rhs);
180 }
181
182 [[nodiscard]] friend URL operator/(URL const &lhs, std::string_view const &rhs) noexcept
183 {
184 return lhs.urlByAppendingPath(URL{rhs});
185 }
186
187 [[nodiscard]] friend std::string to_string(URL const &url) noexcept
188 {
189 return url.value;
190 }
191
192 friend std::ostream &operator<<(std::ostream &lhs, const URL &rhs)
193 {
194 return lhs << to_string(rhs);
195 }
196};
197
198
199
200}
201
202namespace std {
203
204template<>
205class hash<tt::URL> {
206public:
207 size_t operator()(tt::URL const& url) const noexcept {
208 return url.hash();
209 }
210};
211
212}
STL namespace.
Definition URL.hpp:46
std::vector< URL > urlsByScanningWithGlobPattern() const noexcept
static std::vector< std::string > filenamesByScanningDirectory(std::string_view path) noexcept
std::unique_ptr< resource_view > loadView() const
Load a resource.
Definition url_parser.hpp:82
T operator()(T... args)