PageRenderTime 40ms CodeModel.GetById 14ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/install/xbt/linux/misc/bt_tracker_url.cpp

http://torrentpier2.googlecode.com/
C++ | 82 lines | 76 code | 6 blank | 0 comment | 15 complexity | a4608d9ae1472c6c69374c5ba5335314 MD5 | raw file
 1#include "stdafx.h"
 2#include "bt_tracker_url.h"
 3
 4#include <boost/algorithm/string.hpp>
 5
 6Cbt_tracker_url::Cbt_tracker_url()
 7{
 8}
 9
10Cbt_tracker_url::Cbt_tracker_url(const std::string& v)
11{
12	write(v);
13}
14
15void Cbt_tracker_url::clear()
16{
17	m_protocol = tp_unknown;
18	m_host.erase();
19	m_port = 0;
20	m_path.erase();
21}
22
23bool Cbt_tracker_url::valid() const
24{
25	switch (m_protocol)
26	{
27	case tp_http:
28		if (m_path.empty() || m_path[0] != '/')
29			return false;
30	case tp_udp:
31		return !m_host.empty()
32			&& m_port >= 0 && m_port < 0x10000;
33	}
34	return false;
35}
36
37void Cbt_tracker_url::write(const std::string& v)
38{
39	clear();
40	size_t a;
41	int protocol;
42	int port;
43	if (boost::istarts_with(v, "http://"))
44	{
45		a = 7;
46		protocol = tp_http;
47		port = 80;
48	}
49	else if (boost::istarts_with(v, "udp://"))
50	{
51		a = 6;
52		protocol = tp_udp;
53		port = 2710;
54	}
55	else
56		return;
57	size_t b = v.find_first_of("/:", a);
58	std::string host;
59	if (b == std::string::npos)
60		host = v.substr(a);
61	else
62	{
63		host = v.substr(a, b - a);
64		if (v[b] == '/')
65			m_path = v.substr(b);
66		else
67		{
68			b++;
69			a = v.find('/', b);
70			if (a == std::string::npos)
71				port = atoi(v.substr(b).c_str());
72			else
73			{
74				port = atoi(v.substr(b, a - b).c_str());
75				m_path = v.substr(a);
76			}
77		}
78	}
79	m_protocol = protocol;
80	m_host = host;
81	m_port = port;
82}