5 |
// modify it under the terms of the GNU General Public License |
// modify it under the terms of the GNU General Public License |
6 |
// as published by the Free Software Foundation; either version 2 |
// as published by the Free Software Foundation; either version 2 |
7 |
// of the License, or (at your option) any later version. |
// of the License, or (at your option) any later version. |
8 |
// |
// |
9 |
// This program is distributed in the hope that it will be useful, |
// This program is distributed in the hope that it will be useful, |
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
// GNU General Public License for more details. |
// GNU General Public License for more details. |
13 |
// |
// |
14 |
// You should have received a copy of the GNU General Public License along |
// You should have received a copy of the GNU General Public License along |
15 |
// with this program; if not, write to the Free Software Foundation, Inc., |
// with this program; if not, write to the Free Software Foundation, Inc., |
16 |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
34 |
size_t newcap = capacity; |
size_t newcap = capacity; |
35 |
while (wpos + len > buf + newcap) |
while (wpos + len > buf + newcap) |
36 |
newcap *= 2; |
newcap *= 2; |
37 |
if (newcap != capacity) |
if (newcap != capacity) |
38 |
{ |
{ |
39 |
char* newbuf = new char[newcap]; |
char* newbuf = new char[newcap]; |
40 |
memcpy(newbuf, buf, sz); |
memcpy(newbuf, buf, sz); |
44 |
delete[] buf; |
delete[] buf; |
45 |
buf = newbuf; |
buf = newbuf; |
46 |
} |
} |
47 |
char* data = wpos; |
char* ret = wpos; |
48 |
wpos += len; |
wpos += len; |
49 |
sz += len; |
sz += len; |
50 |
return data; |
return ret; |
51 |
} |
} |
52 |
|
|
53 |
char* Buffer::get_rspace(size_t len) |
char* Buffer::get_rspace(size_t len) |
54 |
{ |
{ |
55 |
if (!good() || rpos + len > buf + sz) |
if (!good() || rpos + len > buf + sz) |
56 |
{ |
{ |
57 |
iostate = State(iostate | FAILBIT | EOFBIT); |
iostate = State(iostate | FAILBIT | EOFBIT); |
58 |
return 0; |
return 0; |
59 |
} |
} |
60 |
char* data = rpos; |
char* ret = rpos; |
61 |
rpos += len; |
rpos += len; |
62 |
return data; |
return ret; |
63 |
} |
} |
64 |
|
|
65 |
int Buffer::read() |
int Buffer::read() |
66 |
{ |
{ |
67 |
if (good() && rpos < buf+sz) |
if (good() && rpos < buf+sz) |
68 |
return *rpos++; |
return *rpos++; |
69 |
else |
else |
70 |
return -1; |
return -1; |
71 |
} |
} |
72 |
|
|
79 |
|
|
80 |
Buffer& Buffer::read(void* dest, size_t len) |
Buffer& Buffer::read(void* dest, size_t len) |
81 |
{ |
{ |
82 |
if (!good() || (rpos + len > buf + sz)) |
if (!good() || (rpos + len > buf + sz)) |
83 |
{ |
{ |
84 |
iostate = State(iostate | EOFBIT | FAILBIT); |
iostate = State(iostate | EOFBIT | FAILBIT); |
85 |
} |
} |
86 |
else |
else |
87 |
{ |
{ |
88 |
memcpy(dest, rpos, len); |
memcpy(dest, rpos, len); |
89 |
rpos += len; |
rpos += len; |
101 |
int Buffer::seekr(long pos, SeekMode whence) |
int Buffer::seekr(long pos, SeekMode whence) |
102 |
{ |
{ |
103 |
size_t offs = 0; |
size_t offs = 0; |
104 |
|
|
105 |
switch (whence) { |
switch (whence) { |
106 |
case SET: offs = pos; break; |
case SET: offs = pos; break; |
107 |
case CUR: offs = (rpos - buf) + pos; break; |
case CUR: offs = (rpos - buf) + pos; break; |
108 |
case END: offs = sz + pos; break; |
case END: offs = sz + pos; break; |
109 |
} |
} |
110 |
if (offs < 0 || offs > sz) |
if ( /* offs < 0 || */ /* always false */ |
111 |
|
offs > sz) |
112 |
return -1; |
return -1; |
113 |
rpos = buf + offs; |
rpos = buf + offs; |
114 |
return 0; |
return 0; |
117 |
int Buffer::seekw(long pos, SeekMode whence) |
int Buffer::seekw(long pos, SeekMode whence) |
118 |
{ |
{ |
119 |
size_t offs = 0; |
size_t offs = 0; |
120 |
|
|
121 |
switch (whence) { |
switch (whence) { |
122 |
case SET: offs = pos; break; |
case SET: offs = pos; break; |
123 |
case CUR: offs = (wpos - buf) + pos; break; |
case CUR: offs = (wpos - buf) + pos; break; |
124 |
case END: offs = sz + pos; break; |
case END: offs = sz + pos; break; |
125 |
} |
} |
126 |
if (offs < 0 || offs > sz) |
if (/* offs < 0 || */ /* always false */ |
127 |
|
offs > sz) |
128 |
return -1; |
return -1; |
129 |
wpos = buf + offs; |
wpos = buf + offs; |
130 |
return 0; |
return 0; |
146 |
//---------------------------------------- |
//---------------------------------------- |
147 |
|
|
148 |
/* |
/* |
149 |
** === Read and write bytes === |
** === Read and write bytes === |
150 |
*/ |
*/ |
151 |
Buffer& px::read(Buffer& buf, Uint8& byte) |
Buffer& px::read(Buffer& buf, Uint8& byte) |
152 |
{ |
{ |
163 |
} |
} |
164 |
|
|
165 |
/* |
/* |
166 |
** === Read and write words === |
** === Read and write words === |
167 |
*/ |
*/ |
168 |
|
|
169 |
Buffer& px::read(Buffer& buf, Uint16& word) |
Buffer& px::read(Buffer& buf, Uint16& word) |
182 |
return buf; |
return buf; |
183 |
} |
} |
184 |
|
|
185 |
/* |
/* |
186 |
** === Read and write dwords === |
** === Read and write dwords === |
187 |
*/ |
*/ |
188 |
|
|
189 |
Buffer& px::read(Buffer& buf, Uint32& dword) |
Buffer& px::read(Buffer& buf, Uint32& dword) |
190 |
{ |
{ |
191 |
Uint8* ptr = (Uint8*) buf.get_rspace(4); |
Uint8* ptr = (Uint8*) buf.get_rspace(4); |
192 |
if (ptr != 0) |
if (ptr != 0) |
193 |
{ |
{ |
194 |
dword = ptr[0]; |
dword = ptr[0]; |
195 |
dword |= (ptr[1] << 8); |
dword |= (ptr[1] << 8); |
210 |
} |
} |
211 |
|
|
212 |
/* |
/* |
213 |
** === Read and write doubles === |
** === Read and write doubles === |
214 |
*/ |
*/ |
215 |
|
|
216 |
Buffer& px::read(Buffer& buf, double& dvar) |
Buffer& px::read(Buffer& buf, double& dvar) |
228 |
return buf; |
return buf; |
229 |
} |
} |
230 |
|
|
231 |
/* |
/* |
232 |
** === Read and write another buffer === |
** === Read and write another buffer === |
233 |
*/ |
*/ |
234 |
|
|
235 |
Buffer& px::read(Buffer& srcbuf, Buffer& destbuf, int len) |
Buffer& px::read(Buffer& srcbuf, Buffer& destbuf, int len) |
236 |
{ |
{ |
237 |
if (&srcbuf != &destbuf) |
if (&srcbuf != &destbuf) |
238 |
{ |
{ |
239 |
char* dest = destbuf.get_wspace(len); |
char* dest = destbuf.get_wspace(len); |
240 |
char* src = srcbuf.get_rspace(len); |
char* src = srcbuf.get_rspace(len); |
246 |
|
|
247 |
Buffer& px::write(Buffer& buf, const Buffer& databuf) |
Buffer& px::write(Buffer& buf, const Buffer& databuf) |
248 |
{ |
{ |
249 |
if (&buf != &databuf) |
if (&buf != &databuf) |
250 |
{ |
{ |
251 |
buf.write(databuf.data(), databuf.size()); |
buf.write(databuf.data(), databuf.size()); |
252 |
} |
} |
254 |
} |
} |
255 |
|
|
256 |
/* |
/* |
257 |
** === Read and write strings === |
** === Read and write strings === |
258 |
*/ |
*/ |
259 |
|
|
260 |
Buffer& px::read(Buffer& buf, string& str) |
Buffer& px::read(Buffer& buf, string& str) |
261 |
{ |
{ |
262 |
Uint16 size; |
Uint16 size; |
263 |
if (buf >> size) |
if (buf >> size) |
264 |
{ |
{ |
265 |
char* ptr = buf.get_rspace(size); |
char* ptr = buf.get_rspace(size); |
266 |
if (ptr != 0) |
if (ptr != 0) |
291 |
{ |
{ |
292 |
buf.clear(); |
buf.clear(); |
293 |
char tmp[2048]; |
char tmp[2048]; |
294 |
while (is.read(tmp, sizeof tmp)) |
while (is.read(tmp, sizeof tmp)) |
295 |
{ |
{ |
296 |
buf.write(tmp, sizeof tmp); |
buf.write(tmp, sizeof tmp); |
297 |
} |
} |