-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurl.hpp
60 lines (49 loc) · 1.26 KB
/
url.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Nonolith Connect
// https://github.com/nonolith/connect
// URL parsing
// Released under the terms of the GNU GPLv3+
// (C) 2012 Nonolith Labs, LLC
// Authors:
// Kevin Mehall <km@kevinmehall.net>
#pragma once
#include <string>
#include <vector>
#include <map>
#include <boost/lexical_cast.hpp>
using std::string;
void parse_query(const string& query, std::map<string, string>& map);
string map_get(std::map<string, string>& map, const string key, const string def="");
template <class T>
T map_get_num(std::map<string, string>& map, const string key, const T def){
std::map<std::string, std::string>::iterator it = map.find(key);
if (it != map.end()){
return boost::lexical_cast<T>(it->second);
}else{
return def;
}
}
struct Url{
Url(const string url);
std::vector<std::string> pathparts;
std::map<std::string, std::string> params;
};
struct UrlPath{
UrlPath(Url &url_, unsigned level_):url(url_), level(level_){}
Url& url;
const unsigned level;
UrlPath sub(){
return UrlPath(url, level+1);
}
const string& get(){
return url.pathparts.at(level);
}
bool matches(string m){
return get() == m;
}
bool leaf(){
return url.pathparts.size() <= level;
}
string param(string key, string def=""){
return map_get(url.params, key, def);
}
};