- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.cpp
More file actions
Latest commit
104 lines (82 loc) · 1.74 KB
/
Copy pathbuffer.cpp
File metadata and controls
104 lines (82 loc) · 1.74 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include"buffer.h"
#include<string.h>
#include"exceptions.h"
#include"unicode.h"
Buffer::Buffer(int sz, int alloc)
{
allocated = alloc;
size = sz;
if (size > allocated)
allocated = size;
if (allocated < 1024)
allocated = 1024;
data = malloc(allocated);
if (! data)
throwException(L"Error allocating memory for Buffer");
}
Buffer::~Buffer()
{
free(data);
}
voidBuffer::setSize(size_t sz)
{
if (sz > allocated) {
int newAl = allocated + sz + 1024;
void *d = realloc(data, newAl);
if (! d)
throwException(L"Error expanding buffer memory");
data = d;
allocated = newAl;
}
size = sz;
}
size_tBuffer::getSize()
{
return size;
}
size_tBuffer::getAllocated()
{
return allocated;
}
void* Buffer::getData()
{
return data;
}
voidBuffer::gotoPos(int offset)
{
currentPos = offset;
}
size_tBuffer::putData(constunsignedchar *d, size_t length)
{
if (size < currentPos + length)
setSize(currentPos + length);
memcpy((unsignedchar*)data + currentPos, d, length);
currentPos += length;
return length;
}
size_tBuffer::putInteger(int v)
{
unsignedchar b[4];
int i, ib;
for (i = 0; i < 4; i++) {
ib = v & 0xFF;
v = v >> 8;
b[i] = ib;
}
returnputData(b, 4);
}
size_tBuffer::putUtf8(const std::wstring &string)
{
std::string s(toUtf8(string));
putInteger(s.length());
putData((constunsignedchar*)s.c_str(), s.length());
return4 + s.length();
}
size_tBuffer::putByte(unsignedchar value)
{
if (size < (size_t)currentPos + 1)
setSize(currentPos + 1);
((unsignedchar*)data)[currentPos] = value;
currentPos++;
return1;
}