-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElement.cpp
65 lines (55 loc) · 1.64 KB
/
Element.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
#include <stdlib.h>
#include <boost/pointer_cast.hpp>
#include "Element.h"
#include "Dictionary.h"
#include "Integer.h"
#include "String.h"
#include "List.h"
#include "Exception.h"
boost::shared_ptr<Bencoding::Element> Bencoding::Element::factory(const char* const buffer, unsigned& offset)
{
if (buffer[offset] == 'd')
{
return Bencoding::Dictionary::factory(buffer, offset);
}
if (buffer[offset] == 'i')
{
return Bencoding::Integer::factory(buffer, offset);
}
if (buffer[offset] == 'l')
{
return Bencoding::List::factory(buffer, offset);
}
return Bencoding::String::factory(buffer, offset);
}
int Bencoding::Element::intValue(const char* const buffer, unsigned& offset)
{
std::string numberString;
while (isdigit(buffer[offset])) {
numberString += buffer[offset++];
}
if (numberString.length() == 0)
{
throw Exception("Integer value does not start with number", offset);
}
return atoi(numberString.c_str());
}
std::ostream& operator<< (std::ostream& aStream, boost::shared_ptr<Bencoding::Element> element)
{
switch (element->getType())
{
case Bencoding::DICTIONARY:
aStream << boost::dynamic_pointer_cast<Bencoding::Dictionary>(element);
break;
case Bencoding::LIST:
aStream << boost::dynamic_pointer_cast<Bencoding::List>(element);
break;
case Bencoding::INTEGER:
aStream << boost::dynamic_pointer_cast<Bencoding::Integer>(element);
break;
case Bencoding::STRING:
aStream << boost::dynamic_pointer_cast<Bencoding::String>(element);
break;
}
return aStream;
}