Browse Source

Move URL functions from main to StringUtils

pull/2/head
Nubben 5 years ago
parent
commit
34b9038c9b
  1. 28
      src/StringUtils.cpp
  2. 2
      src/StringUtils.h
  3. 31
      src/main.cpp

28
src/StringUtils.cpp

@ -7,3 +7,31 @@ std::string getFilenameExtension(std::string const& fileName) { @@ -7,3 +7,31 @@ std::string getFilenameExtension(std::string const& fileName) {
}
return "";
}
const std::string getDocumentFromURL(const std::string &url) {
int slashes = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
slashes++;
if (slashes == 3) {
return url.substr(i, url.length() - i);
}
}
}
return "";
}
const std::string getHostFromURL(const std::string &url) {
int slashes = 0;
unsigned int start = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
if (slashes == 2) {
return url.substr(start, i - start);
}
slashes++;
start = i + 1;
}
}
return "";
}

2
src/StringUtils.h

@ -4,5 +4,7 @@ @@ -4,5 +4,7 @@
#include <string>
std::string getFilenameExtension(std::string const& fileName);
const std::string getDocumentFromURL(const std::string &url);
const std::string getHostFromURL(const std::string &url);
#endif

31
src/main.cpp

@ -7,9 +7,8 @@ @@ -7,9 +7,8 @@
#include <ctime>
#include <iostream>
#include <memory>
#include "StringUtils.h"
const std::string getDocumentFromURL(const std::string &url);
const std::string getHostFromURL(const std::string &url);
void handleRequest(const HTTPResponse &response);
const std::unique_ptr<Window> window = std::make_unique<Window>();
@ -88,34 +87,6 @@ void navTo(std::string url) { @@ -88,34 +87,6 @@ void navTo(std::string url) {
request->sendRequest(handleRequest);
}
const std::string getDocumentFromURL(const std::string &url) {
int slashes = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
slashes++;
if (slashes == 3) {
return url.substr(i, url.length() - i);
}
}
}
return "";
}
const std::string getHostFromURL(const std::string &url) {
int slashes = 0;
unsigned int start = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
if (slashes == 2) {
return url.substr(start, i - start);
}
slashes++;
start = i + 1;
}
}
return "";
}
void handleRequest(const HTTPResponse &response) {
std::cout << "main:::handleRequest - statusCode: " << response.statusCode << std::endl;
if (response.statusCode == 200) {

Loading…
Cancel
Save