-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.cc
177 lines (138 loc) · 2.9 KB
/
buffer.cc
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "buffer.h"
#include "util.h"
Buffer::Buffer()
{
}
Buffer::Buffer(const char *data, size_t size) : datarope(data,size)
{
}
Buffer::Buffer(const Buffer &rhs) : datarope(rhs.datarope)
{
}
Buffer::~Buffer()
{
}
char Buffer::operator[] (const unsigned offset)
{
return datarope[offset];
}
const Buffer & Buffer::operator = (const Buffer & rhs)
{
datarope=rhs.datarope;
return *this;
}
void Buffer::Clear()
{
datarope.erase(0,datarope.size());
}
void Buffer::Insert(const Buffer &rhs, unsigned offset)
{
datarope.insert(offset,rhs.datarope);
}
void Buffer::AddBack(const Buffer &rhs)
{
datarope+=rhs.datarope;
}
void Buffer::AddFront(const Buffer &rhs)
{
datarope.insert(0,rhs.datarope);
}
void Buffer::Erase(unsigned offset, size_t size)
{
datarope.erase(offset,size);
}
Buffer & Buffer::Extract(unsigned offset, size_t size)
{
Buffer *temp = new Buffer();
temp->datarope = datarope.substr(offset,size);
datarope.erase(offset,size);
return *temp;
}
Buffer & Buffer::ExtractFront(size_t size)
{
return Extract(0,size);
}
Buffer & Buffer::ExtractBack(size_t size)
{
return Extract(GetSize()-size,size);
}
size_t Buffer::GetSize() const
{
return datarope.size();
}
size_t Buffer::GetData(char *buf, size_t size, unsigned offset) const
{
// datarope.copy(offset,size,buf);
ROPE_COPY(datarope,offset,size,buf);
return size;
}
size_t Buffer::SetData(const char *buf, size_t len, unsigned offset)
{
size_t size=datarope.size();
if ((offset + len) <= size) {
datarope.replace(offset,len,buf,len);
} else if (offset>=size) {
if (offset-size>0) {
datarope.append(offset-size,0);
}
datarope.append(buf,len);
} else {
size_t r1=size-offset;
size_t r2=offset+len-size;
if (r1>0) {
datarope.replace(offset,r1,buf,r1);
}
if (r2>0) {
datarope.append(&(buf[r1]),r2);
}
}
return len;
}
void Buffer::Serialize(const int fd) const
{
int size = GetSize();
char *buf = new char [size];
if (writeall(fd,(char*)&size,sizeof(size),0,1)!=sizeof(size)) {
throw SerializationException();
}
GetData(buf,size,0);
if (writeall(fd,buf,size,0,1)!=size) {
throw SerializationException();
}
delete [] buf;
}
void Buffer::Unserialize(const int fd)
{
int size;
if (readall(fd,(char*)&size,sizeof(size))!=sizeof(size)) {
throw SerializationException();
}
char *buf = new char[size];
if (readall(fd,buf,size)!=size) {
throw SerializationException();
}
datarope = crope(buf,size);
delete [] buf;
}
ostream & Buffer::Print(ostream &os) const
{
size_t size=GetSize();
char hex[2];
unsigned i;
os<<"Buffer(size="<<size<<", data=";
for (i=0;i<size;i++) {
bytetohexbyte(datarope[i],hex);
os<<hex[0]<<hex[1];
}
os<<", text=\"";
for (i=0;i<size;i++) {
char c=datarope[i];
if (c>=32 && c<=126) {
os<<c;
} else {
os<<'.';
}
}
os << "\")";
return os;
}