-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dictionary.cpp
61 lines (52 loc) · 1.9 KB
/
Dictionary.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
#include <iostream>
#include <boost/pointer_cast.hpp>
#include "Dictionary.h"
#include "Exception.h"
#include "Indent.h"
Bencoding::Type Bencoding::Dictionary::getType()
{
return DICTIONARY;
}
boost::shared_ptr<Bencoding::Element> Bencoding::Dictionary::factory(const char* const buffer, unsigned& offset)
{
return boost::shared_ptr<Element>(new Dictionary(buffer, offset));
}
Bencoding::Dictionary::Dictionary(const char* const buffer, unsigned& offset)
{
if (buffer[offset] != 'd')
{
throw Exception("Dictionary does not start with 'd'", offset);
}
offset++;
while (buffer[offset] != 'e')
{
boost::shared_ptr<String> key = boost::dynamic_pointer_cast<String>(Element::factory(buffer, offset));
boost::shared_ptr<Element> value = Element::factory(buffer, offset);
if (key == 0)
{
throw Exception("Dictionary key not a string", offset);
}
dict[key] = value;
}
offset++;
}
std::ostream& operator<< (std::ostream& aStream, boost::shared_ptr<Bencoding::Dictionary> dictionary)
{
aStream << Bencoding::Indent::indent() << "Dictionary:" << std::endl;
Bencoding::Indent::increase();
std::map<boost::shared_ptr<Bencoding::String>, boost::shared_ptr<Bencoding::Element> > dict = dictionary->getMap();
std::map<boost::shared_ptr<Bencoding::String>, boost::shared_ptr<Bencoding::Element> >::iterator iter;
for (iter = dict.begin(); iter != dict.end(); iter++)
{
aStream << Bencoding::Indent::indent() << "Key:" << std::endl;
Bencoding::Indent::increase();
aStream << iter->first << std::endl;
Bencoding::Indent::decrease();
aStream << Bencoding::Indent::indent() << "Value:" << std::endl;
Bencoding::Indent::increase();
aStream << iter->second << std::endl;
Bencoding::Indent::decrease();
}
Bencoding::Indent::decrease();
return aStream;
}