/
BinaryString.h
61 lines (47 loc) · 960 Bytes
/
BinaryString.h
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
#ifndef __TORRENTSTREAM_BINARYSTRING_H
#define __TORRENTSTREAM_BINARYSTRING_H
namespace TorrentStream
{
enum Endianess
{
BigEndian,
LittleEndian,
};
template <typename T>
T ByteSwap(T v)
{
return _byteswap_ulong(v);
}
class BinaryString
{
public:
void PushInt32(int value, Endianess endianess = LittleEndian)
{
int val = value;
auto dataSize = m_Data.size();
m_Data.resize(dataSize + sizeof(int));
if (endianess == BigEndian)
{
val = _byteswap_ulong(val);
}
memcpy(m_Data.data() + dataSize, &val , sizeof(int));
}
void PushUint8(char value)
{
m_Data.push_back(value);
}
void PushString(const std::string& value)
{
auto dataSize = m_Data.size();
m_Data.resize(dataSize + value.length());
std::copy(value.begin(), value.end(), m_Data.begin() + dataSize);
}
const std::vector<char>& Get() const
{
return m_Data;
}
private:
std::vector<char> m_Data;
};
}
#endif