Browse Source

Add URL class

pull/2/head
Nubben 5 years ago
parent
commit
d59c931024
  1. 39
      src/URL.cpp
  2. 23
      src/URL.h

39
src/URL.cpp

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
#include "URL.h"
#include "StringUtils.h"
URL::URL(std::string const& url) {
construct(url);
}
std::string URL::toString() {
if (isRelative()) {
return document;
}
return protocol + "://" + host + document;
}
bool URL::isRelative() {
return protocol.size() == 0;
}
void URL::construct(std::string const& url) {
protocol = getProtocolFromURL(url);
if (protocol.size() != 0) {
host = getHostFromURL(url);
document = getDocumentFromURL(url);
} else {
host = "";
document = url;
}
if (document.size() == 0) {
document = "/";
}
}
std::ostream& operator<<(std::ostream& out, URL& url) {
out << url.toString();
return out;
}

23
src/URL.h

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
#ifndef URL_H
#define URL_H
#include <string>
#include <iostream>
struct URL {
URL(std::string const& url);
std::string toString();
bool isRelative();
std::string protocol;
std::string host;
std::string document;
private:
void construct(std::string const& url);
};
std::ostream& operator<<(std::ostream& out, URL& url);
#endif
Loading…
Cancel
Save