]> git.sesse.net Git - cubemap/blob - input.cpp
1e617807d0058c88b92d6b5f8b13da97c0c837c2
[cubemap] / input.cpp
1 #include <string.h>
2 #include <string>
3
4 #include "input.h"
5
6 using namespace std;
7
8 // Extremely rudimentary URL parsing.
9 bool parse_url(const string &url, string *protocol, string *host, string *port, string *path)
10 {
11         if (url.find("http://") != 0) {
12                 return false;
13         }
14
15         *protocol = "http";
16         
17         string rest = url.substr(strlen("http://"));
18         size_t split = rest.find_first_of(":/");
19         if (split == string::npos) {
20                 // http://foo
21                 *host = rest;
22                 *port = "http";
23                 *path = "/";
24                 return true;
25         }
26
27         *host = string(rest.begin(), rest.begin() + split);
28         char ch = rest[split];  // Colon or slash.
29         rest = string(rest.begin() + split + 1, rest.end());
30
31         if (ch == ':') {
32                 // Parse the port.
33                 split = rest.find_first_of('/');
34                 if (split == string::npos) {
35                         // http://foo:1234
36                         *port = rest;
37                         *path = "/";
38                         return true;
39                 } else {
40                         // http://foo:1234/bar
41                         *port = string(rest.begin(), rest.begin() + split);
42                         *path = string(rest.begin() + split, rest.end());
43                         return true;
44                 }
45         }
46
47         // http://foo/bar
48         *port = "http";
49         *path = rest;
50         return true;
51 }
52
53 Input::~Input() {}
54