-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPrettyPrinter.cpp
72 lines (59 loc) · 1.65 KB
/
PrettyPrinter.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 "ValueTypes.h"
#include "PrettyPrinter.h"
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <iostream>
using namespace bencode;
PrettyPrinter::PrettyPrinter(int indentation)
: indentation_(indentation)
{
}
int PrettyPrinter::getIndentation() const
{
return indentation_;
}
std::string PrettyPrinter::getSpace() const
{
return std::string(getIndentation() * 2, ' ');
}
bool PrettyPrinter::isAscii(int c) const
{
return (c >= 9 && c <= 13) || (c >= 32 && c <= 126);
}
bool PrettyPrinter::isAscii(const std::string& value) const
{
for (unsigned int i = 0; i < value.size(); i++)
if (!isAscii(static_cast<int>(value[i])))
return false;
return true;
}
void PrettyPrinter::operator()(int t) const
{
std::cout << t << std::endl;
}
void PrettyPrinter::operator()(const std::string& t) const
{
if (!isAscii(t))
std::cout << "BINARY DATA (length: " << t.size() << ")" << std::endl;
else {
const int MAX_STRING_LENGTH = 100;
std::cout << t.substr(0, MAX_STRING_LENGTH) << std::endl;
}
}
void PrettyPrinter::operator()(const ValueDictionary& t) const
{
std::cout << std::endl;
for (ValueDictionary::const_iterator i = t.begin(); i != t.end(); ++i) {
std::cout << getSpace() << "{" << i->first << "}: ";
boost::apply_visitor(PrettyPrinter(getIndentation() + 1), i->second);
}
}
void PrettyPrinter::operator()(const ValueVector& t) const
{
std::cout << std::endl;
for (unsigned int i = 0; i != t.size(); ++i) {
std::cout << getSpace() << "[" << i << "]: ";
boost::apply_visitor(PrettyPrinter(getIndentation() + 1), t[i]);
}
}