-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathString.cpp
56 lines (45 loc) · 1.17 KB
/
String.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
#include <string>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string.h>
#include "String.h"
#include "Exception.h"
#include "Indent.h"
Bencoding::Type Bencoding::String::getType()
{
return STRING;
}
boost::shared_ptr<Bencoding::Element> Bencoding::String::factory(const char* const buffer, unsigned& offset)
{
unsigned startingOffset = offset;
return boost::shared_ptr<Bencoding::Element>(new Bencoding::String(buffer, offset));
}
Bencoding::String::~String()
{
if (value != 0)
{
delete[] value;
}
}
Bencoding::String::String(const char* const buffer, unsigned& offset) : value(0)
{
length = intValue(buffer, offset);
if (length <= 0)
{
throw Exception("Expected positive integer for length in string", offset);
}
if (buffer[offset] != ':')
{
throw Exception("Expected ':' in string", offset);
}
offset++;
value = new char[length];
memcpy(value, buffer+offset, length);
offset += length;
}
std::ostream& operator<< (std::ostream& aStream, boost::shared_ptr<Bencoding::String> string)
{
aStream << Bencoding::Indent::indent() << string->getValue();
return aStream;
}