Moved URL class into fr namespace

It should not have been in the global namespace, as it's a part of the library.
This commit is contained in:
Fred Nicolson 2017-05-24 09:54:19 +01:00
parent c110fb6c81
commit 431f646bae
2 changed files with 215 additions and 208 deletions

View File

@ -8,10 +8,11 @@
#include <unordered_map>
//Note, a URL looks like this: scheme:[//host[:port]][/path][?query][#fragment]
class URL
namespace fr
{
public:
class URL
{
public:
enum Scheme
{
HTTP = 0,
@ -27,6 +28,7 @@ public:
* Constructors
*/
URL() = default;
URL(const std::string &url);
/*!
@ -100,7 +102,7 @@ public:
*/
static const std::string &scheme_to_string(Scheme scheme);
private:
private:
/*!
* Converts a string to lower case
*
@ -117,7 +119,7 @@ private:
std::string query;
std::string fragment;
static std::unordered_map<std::string, URL::Scheme> scheme_string_map;
};
};
}
#endif //FRNETLIB_URLPARSER_H

View File

@ -7,7 +7,10 @@
#include <iostream>
#include "frnetlib/URL.h"
std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
namespace fr
{
std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
{"http", URL::HTTP},
{"https", URL::HTTPS},
{"sftp", URL::FTP},
@ -15,16 +18,16 @@ std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
{"irc", URL::IRC},
{"sftp", URL::SFTP},
{"unknown", URL::Unknown}
};
};
URL::URL(const std::string &url)
{
URL::URL(const std::string &url)
{
parse(url);
}
}
void URL::parse(std::string url)
{
void URL::parse(std::string url)
{
size_t parse_offset = 0;
size_t pos = 0;
@ -98,30 +101,32 @@ void URL::parse(std::string url)
}
return;
}
}
URL::Scheme URL::string_to_scheme(const std::string &scheme)
{
URL::Scheme URL::string_to_scheme(const std::string &scheme)
{
auto iter = scheme_string_map.find(to_lower(scheme));
if(iter == scheme_string_map.end())
return URL::Unknown;
return iter->second;
}
}
std::string URL::to_lower(const std::string &str)
{
std::string URL::to_lower(const std::string &str)
{
std::string out = str;
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
return out;
}
}
const std::string &URL::scheme_to_string(URL::Scheme scheme)
{
auto iter = std::find_if(scheme_string_map.begin(), scheme_string_map.end(), [&](const auto &i){
const std::string &URL::scheme_to_string(URL::Scheme scheme)
{
auto iter = std::find_if(scheme_string_map.begin(), scheme_string_map.end(), [&](const auto &i)
{
return i.second == scheme;
});
if(iter == scheme_string_map.end())
throw std::logic_error("Unknown URL::Scheme value " + std::to_string(scheme));
return iter->first;
}
}