-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.h
206 lines (206 loc) · 4.16 KB
/
buffer.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
////////////////////////////////////////////////////////////////////////
// buffer.h [by dwing] (2009.2.15)
////////////////////////////////////////////////////////////////////////
#pragma once
#include <stdlib.h>
#include <string.h>
////////////////////////////////////////////////////////////////////////
#ifndef TYPE_U8U16U32
#define TYPE_U8U16U32
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;
#endif
////////////////////////////////////////////////////////////////////////
class Buffer //缓冲区类(字节队列,单线程版)
{
public:
enum {BLOCKSIZE = 65500}; //缓冲区块大小
private:
struct Block
{
U8 data[BLOCKSIZE];
Block *next;
};
Block *head, *tail;
U32 headpos, tailpos, bufsize;
public:
Buffer()
{
tail = head = (Block*)malloc(sizeof(Block));
bufsize = tailpos = headpos = 0;
head->next = 0;
}
~Buffer()
{
reset();
free(head);
}
void reset()
{
while(head->next)
{
Block *temp = head->next;
free(head);
head = temp;
}
bufsize = tailpos = headpos = 0;
}
U32 size() const //取当前缓冲区的大小
{
return bufsize;
}
bool write(U8 *data, U32 size) //写缓冲区
{
while(size)
{
if(tailpos == BLOCKSIZE)
{
Block *temp = (Block*)malloc(sizeof(Block));
if(!temp) return false;
tail = tail->next = temp;
tail->next = 0;
tailpos = 0;
}
U32 len = (size > BLOCKSIZE-tailpos) ? (BLOCKSIZE-tailpos) : size;
memcpy(tail->data + tailpos, data, len);
data += len; tailpos += len;
size -= len; bufsize += len;
}
return true;
}
bool read(U8 *data, U32 size) //读缓冲区
{
if(size > bufsize) return false;
while(size)
{
if(headpos == BLOCKSIZE)
{
Block *temp = head->next;
free(head);
head = temp;
headpos = 0;
}
U32 len = (size > BLOCKSIZE-headpos) ? (BLOCKSIZE-headpos) : size;
memcpy(data, head->data + headpos, len);
data += len; headpos += len;
size -= len; bufsize -= len;
}
return true;
}
void skip(U32 size)
{
if(size > bufsize)
reset();
else
{
while(size)
{
if(headpos == BLOCKSIZE)
{
Block *temp = head->next;
free(head);
head = temp;
headpos = 0;
}
U32 len = (size > BLOCKSIZE-headpos) ? (BLOCKSIZE-headpos) : size;
headpos += len;
size -= len; bufsize -= len;
}
}
}
Buffer& operator<<(Buffer& buf)
{
const U32 BUFFERSIZE = 256;
U8 b[BUFFERSIZE];
while(buf.size())
{
U32 len = (buf.size() > BUFFERSIZE) ? BUFFERSIZE : buf.size();
buf.read(b,len);
write(b,len);
}
return *this;
}
Buffer& operator<<(const U8& c) //写缓冲区(1字节)
{
if(tailpos == BLOCKSIZE)
{
Block *temp = (Block*)malloc(sizeof(Block));
tail = tail->next = temp;
tail->next = 0;
tailpos = 0;
}
tail->data[tailpos++] = c;
++bufsize;
return *this;
}
Buffer& operator<<(const char *s) //写缓冲区(1字节)
{
write((U8*)s, (U32)strlen(s));
return *this;
}
Buffer& operator>>(U8& c) //读缓冲区(1字节)
{
if(bufsize)
{
if(headpos == BLOCKSIZE)
{
Block *temp = head->next;
free(head);
head = temp;
headpos = 0;
}
c = head->data[headpos++];
--bufsize;
}
else c = 0;
return *this;
}
U8 pop() //从队头读缓冲区(1字节)
{
U8 c=0;
if(bufsize)
{
c = tail->data[--tailpos];
if(!tailpos && head!=tail)
{
Block *temp = head;
while(temp->next!=tail)
temp = temp->next;
free(tail);
tail = temp;
tail->next = 0;
tailpos = BLOCKSIZE;
}
--bufsize;
}
return c;
}
U8 operator[](U32 pos)
{
if(pos > bufsize) return 0;
U32 i = BLOCKSIZE - headpos;
Block* temp = head;
while(pos >= i)
{
pos -= i;
i = BLOCKSIZE;
temp = temp->next;
}
return temp->data[BLOCKSIZE - (i - pos)];
}
void set(U32 pos, U8 v)
{
if(pos > bufsize) return;
U32 i = BLOCKSIZE - headpos;
Block* temp = head;
while(pos >= i)
{
pos -= i;
i = BLOCKSIZE;
temp = temp->next;
}
temp->data[BLOCKSIZE - (i - pos)] = v;
}
};
////////////////////////////////////////////////////////////////////////