Nebula
Loading...
Searching...
No Matches
uri.h
Go to the documentation of this file.
1#pragma once
2#ifndef IO_URI_H
3#define IO_URI_H
4//------------------------------------------------------------------------------
59#include "core/types.h"
60#include "util/string.h"
61#include "util/dictionary.h"
62
63//------------------------------------------------------------------------------
64namespace IO
65{
66class URI
67{
68public:
70 URI();
72 URI(const Util::String& s);
74 URI(const char* s);
76 URI(const URI& rhs);
78 void operator=(const URI& rhs);
80 bool operator==(const URI& rhs) const;
82 bool operator!=(const URI& rhs) const;
83
85 void Set(const Util::String& s);
87 Util::String AsString() const;
88
90 bool IsEmpty() const;
92 bool IsValid() const;
94 void Clear();
96 void SetScheme(const Util::String& s);
98 const Util::String& Scheme() const;
100 void SetUserInfo(const Util::String& s);
102 const Util::String& UserInfo() const;
104 void SetHost(const Util::String& s);
106 const Util::String& Host() const;
108 void SetPort(const Util::String& s);
110 const Util::String& Port() const;
112 void SetLocalPath(const Util::String& s);
114 const Util::String& LocalPath() const;
116 void AppendLocalPath(const Util::String& pathComponent);
118 void SetFragment(const Util::String& s);
120 const Util::String& Fragment() const;
122 void SetQuery(const Util::String& s);
124 const Util::String& Query() const;
128 Util::String GetTail() const;
131
132private:
134 bool Split(const Util::String& s);
136 Util::String Build() const;
137
146};
147
148//------------------------------------------------------------------------------
151inline
153 isEmpty(true)
154{
155 // empty
156}
157
158//------------------------------------------------------------------------------
161inline
163 isEmpty(true)
164{
165 bool validUri = this->Split(s);
166 n_assert2(validUri, s.AsCharPtr());
167}
168
169//------------------------------------------------------------------------------
172inline
173URI::URI(const char* s) :
174 isEmpty(true)
175{
176 bool validUri = this->Split(s);
177 n_assert2(validUri, s);
178}
179
180//------------------------------------------------------------------------------
183inline
184URI::URI(const URI& rhs) :
185 isEmpty(rhs.isEmpty),
186 scheme(rhs.scheme),
187 userInfo(rhs.userInfo),
188 host(rhs.host),
189 port(rhs.port),
190 localPath(rhs.localPath),
191 fragment(rhs.fragment),
192 query(rhs.query)
193{
194 // empty
195}
196
197//------------------------------------------------------------------------------
200inline
201void
203{
204 this->isEmpty = rhs.isEmpty;
205 this->scheme = rhs.scheme;
206 this->userInfo = rhs.userInfo;
207 this->host = rhs.host;
208 this->port = rhs.port;
209 this->localPath = rhs.localPath;
210 this->fragment = rhs.fragment;
211 this->query = rhs.query;
212}
213
214//------------------------------------------------------------------------------
217inline
218bool
219URI::operator==(const URI& rhs) const
220{
221 if (this->isEmpty && rhs.isEmpty)
222 {
223 return true;
224 }
225 else
226 {
227 return ((this->scheme == rhs.scheme) &&
228 (this->userInfo == rhs.userInfo) &&
229 (this->host == rhs.host) &&
230 (this->port == rhs.port) &&
231 (this->localPath == rhs.localPath) &&
232 (this->fragment == rhs.fragment) &&
233 (this->query == rhs.query));
234 }
235}
236
237//------------------------------------------------------------------------------
240inline
241bool
242URI::operator!=(const URI& rhs) const
243{
244 return !(*this == rhs);
245}
246
247//------------------------------------------------------------------------------
250inline
251bool
253{
254 return this->isEmpty;
255}
256
257//------------------------------------------------------------------------------
260inline
261bool
263{
264 return !(this->isEmpty);
265}
266
267//------------------------------------------------------------------------------
270inline
271void
273{
274 this->isEmpty = true;
275 this->scheme.Clear();
276 this->userInfo.Clear();
277 this->host.Clear();
278 this->port.Clear();
279 this->localPath.Clear();
280 this->fragment.Clear();
281 this->query.Clear();
282}
283
284//------------------------------------------------------------------------------
287inline
288void
290{
291 this->Split(s);
292}
293
294//------------------------------------------------------------------------------
297inline
300{
301 return this->Build();
302}
303
304//------------------------------------------------------------------------------
307inline
308void
310{
311 this->isEmpty = false;
312 this->scheme = s;
313}
314
315//------------------------------------------------------------------------------
318inline
319const Util::String&
321{
322 return this->scheme;
323}
324
325//------------------------------------------------------------------------------
328inline
329void
331{
332 this->isEmpty = false;
333 this->userInfo = s;
334}
335
336//------------------------------------------------------------------------------
339inline
340const Util::String&
342{
343 return this->userInfo;
344}
345
346//------------------------------------------------------------------------------
349inline
350void
352{
353 this->isEmpty = false;
354 this->host = s;
355}
356
357//------------------------------------------------------------------------------
360inline
361const Util::String&
363{
364 return this->host;
365}
366
367//------------------------------------------------------------------------------
370inline
371void
373{
374 this->isEmpty = false;
375 this->port = s;
376}
377
378//------------------------------------------------------------------------------
381inline
382const Util::String&
384{
385 return this->port;
386}
387
388//------------------------------------------------------------------------------
391inline
392void
394{
395 this->isEmpty = false;
396 this->localPath = s;
397}
398
399//------------------------------------------------------------------------------
402inline
403const Util::String&
405{
406 return this->localPath;
407}
408
409//------------------------------------------------------------------------------
412inline
413void
415{
416 this->isEmpty = false;
417 this->fragment = s;
418}
419
420//------------------------------------------------------------------------------
423inline
424const Util::String&
426{
427 return this->fragment;
428}
429
430//------------------------------------------------------------------------------
433inline
434void
436{
437 this->isEmpty = false;
438 this->query = s;
439}
440
441//------------------------------------------------------------------------------
444inline
445const Util::String&
447{
448 return this->query;
449}
450
451} // namespace IO
452
453//------------------------------------------------------------------------------
456IO::URI operator ""_uri(const char* c, std::size_t s);
457
458//------------------------------------------------------------------------------
459#endif
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
bool operator==(const URI &rhs) const
equality operator
Definition uri.h:219
bool IsValid() const
return true if the URI is not empty
Definition uri.h:262
bool operator!=(const URI &rhs) const
inequality operator
Definition uri.h:242
void SetHost(const Util::String &s)
set Host component
Definition uri.h:351
Util::String host
Definition uri.h:141
void AppendLocalPath(const Util::String &pathComponent)
append an element to the local path component
Definition uri.cc:251
Util::String Build() const
build string from components
Definition uri.cc:155
const Util::String & Query() const
get Query component (can be empty)
Definition uri.h:446
void SetPort(const Util::String &s)
set Port component
Definition uri.h:372
Util::String fragment
Definition uri.h:144
void SetLocalPath(const Util::String &s)
set LocalPath component
Definition uri.h:393
const Util::String & Host() const
get Host component (can be empty)
Definition uri.h:362
URI()
default constructor
Definition uri.h:152
Util::Dictionary< Util::String, Util::String > ParseQuery() const
parse query parameters into a dictionary
Definition uri.cc:265
Util::String localPath
Definition uri.h:143
Util::String GetTail() const
get the "tail" (path, query and fragment)
Definition uri.cc:204
void SetScheme(const Util::String &s)
set Scheme component (ftp, http, etc...)
Definition uri.h:309
void Clear()
clear the URI
Definition uri.h:272
Util::String query
Definition uri.h:145
bool isEmpty
Definition uri.h:138
void SetUserInfo(const Util::String &s)
set UserInfo component
Definition uri.h:330
Util::String port
Definition uri.h:142
bool Split(const Util::String &s)
split string into components
Definition uri.cc:34
bool IsEmpty() const
return true if the URI is empty
Definition uri.h:252
void operator=(const URI &rhs)
assignmnent operator
Definition uri.h:202
void Set(const Util::String &s)
set complete URI string
Definition uri.h:289
Util::String userInfo
Definition uri.h:140
const Util::String & Port() const
get Port component (can be empty)
Definition uri.h:383
Util::String scheme
Definition uri.h:139
const Util::String & LocalPath() const
get LocalPath component (can be empty)
Definition uri.h:404
const Util::String & Fragment() const
get Fragment component (can be empty)
Definition uri.h:425
Util::String GetHostAndLocalPath() const
get the host and path without scheme
Definition uri.cc:231
Util::String AsString() const
return as concatenated string
Definition uri.h:299
const Util::String & UserInfo() const
get UserInfo component (can be empty)
Definition uri.h:341
void SetQuery(const Util::String &s)
set Query component
Definition uri.h:435
const Util::String & Scheme() const
get Scheme component (default is file)
Definition uri.h:320
void SetFragment(const Util::String &s)
set Fragment component
Definition uri.h:414
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
#define n_assert2(exp, msg)
Definition debug.h:51
Instances of wrapped stream classes.
Definition orientation.cc:10
Nebula's universal string class.
Definition string.h:50
void Clear()
clear the string
Definition string.h:666
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:542