-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencodeVisitor.cpp
72 lines (57 loc) · 1.45 KB
/
encodeVisitor.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
#include "encodeVisitor.h"
#include "Utility.h"
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <iostream>
namespace Bittorrent
{
using namespace utility;
encodeVisitor::encodeVisitor()
{
}
std::string encodeVisitor::operator()(const long long& inputObj) const
{
std::stringstream ss;
ss << "i" << inputObj << "e";
return ss.str();
}
std::string encodeVisitor::operator()(const std::string& inputObj) const
{
std::stringstream ss;
ss << inputObj.size() << ":" << inputObj;
return ss.str();
}
std::string encodeVisitor::operator()(const valueDictionary& inputObj) const
{
std::stringstream ss;
ss << "d";
for (valueDictionary::const_iterator i = inputObj.begin();
i != inputObj.end(); ++i)
{
ss << (*this)(i->first) <<
boost::apply_visitor(encodeVisitor(), i->second);
}
ss << "e";
return ss.str();
}
std::string encodeVisitor::operator()(const valueList& inputObj) const
{
std::stringstream ss;
ss << "l";
for (valueList::const_iterator i = inputObj.begin(); i != inputObj.end();
++i)
{
ss << boost::apply_visitor(encodeVisitor(), *i);
}
ss << "e";
//remove "0:" after "l" in encoding
std::string res = ss.str();
auto emptyListPos = res.find("l0:");
if (emptyListPos != std::string::npos)
{
res.erase(emptyListPos + 1, 2);
}
return ss.str();
}
}