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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
* Copyright (C) 2002,2005 Daniel Heck
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ecl_buffer.hh"
#include <iostream>
using namespace ecl;
using namespace std;
Buffer::Buffer(size_t startlen)
: buf(new char[startlen])
, rpos(buf), wpos(buf)
, sz(0)
, capacity(startlen)
, iostate(GOODBIT)
{}
void Buffer::assign (char *data, size_t size)
{
clear();
write (data, size);
}
char* Buffer::get_wspace(size_t len)
{
size_t newcap = capacity;
while (wpos + len > buf + newcap)
newcap *= 2;
if (newcap != capacity)
{
char* newbuf = new char[newcap];
memcpy(newbuf, buf, sz);
capacity = newcap;
wpos = (wpos - buf) + newbuf;
rpos = (rpos - buf) + newbuf;
delete[] buf;
buf = newbuf;
}
char* ret = wpos;
wpos += len;
sz += len;
return ret;
}
char* Buffer::get_rspace(size_t len)
{
if (!good() || rpos + len > buf + sz)
{
iostate = State(iostate | FAILBIT | EOFBIT);
return 0;
}
char* ret = rpos;
rpos += len;
return ret;
}
int Buffer::read()
{
if (good() && rpos < buf+sz)
return *rpos++;
else
return -1;
}
Buffer& Buffer::write(char c)
{
char* ptr = get_wspace(1);
*ptr = c;
return *this;
}
Buffer& Buffer::read(void* dest, size_t len)
{
if (!good() || (rpos + len > buf + sz))
{
iostate = State(iostate | EOFBIT | FAILBIT);
}
else
{
memcpy(dest, rpos, len);
rpos += len;
}
return *this;
}
Buffer& Buffer::write(const void* src, size_t len)
{
if (char* dest = get_wspace(len)) {
memcpy(dest, src, len);
}
return *this;
}
int Buffer::seekr(long pos, SeekMode whence)
{
size_t offs = 0;
switch (whence) {
case SET: offs = pos; break;
case CUR: offs = (rpos - buf) + pos; break;
case END: offs = sz + pos; break;
}
if ( /* offs < 0 || */ /* always false */
offs > sz)
return -1;
rpos = buf + offs;
return 0;
}
int Buffer::seekw(long pos, SeekMode whence)
{
size_t offs = 0;
switch (whence) {
case SET: offs = pos; break;
case CUR: offs = (wpos - buf) + pos; break;
case END: offs = sz + pos; break;
}
if (/* offs < 0 || */ /* always false */
offs > sz)
return -1;
wpos = buf + offs;
return 0;
}
Buffer::operator void*() const
{
return (iostate & FAILBIT) ? 0 : reinterpret_cast<void*>(1);
}
bool Buffer::operator!() const
{
return (iostate & FAILBIT) ? true : false;
}
//----------------------------------------
// Buffer helper routines.
//----------------------------------------
/*
** === Read and write bytes ===
*/
Buffer& ecl::read(Buffer& buf, Uint8& byte)
{
int a = buf.read();
if (a != -1)
byte = a;
return buf;
}
Buffer& ecl::write(Buffer& buf, Uint8 byte)
{
buf.write(byte);
return buf;
}
/*
** === Read and write words ===
*/
Buffer& ecl::read(Buffer& buf, Uint16& word)
{
Uint8* ptr = (Uint8*) buf.get_rspace(2);
if (ptr != 0)
word = ptr[0] + (ptr[1] << 8);
return buf;
}
Buffer& ecl::write(Buffer& buf, Uint16 word)
{
Uint8* ptr = (Uint8*) buf.get_wspace(2);
ptr[0] = word & 0xff;
ptr[1] = (word >> 8) & 0xff;
return buf;
}
/*
** === Read and write dwords ===
*/
Buffer& ecl::read(Buffer& buf, Uint32& dword)
{
Uint8* ptr = (Uint8*) buf.get_rspace(4);
if (ptr != 0)
{
dword = ptr[0];
dword |= (ptr[1] << 8);
dword |= (ptr[2] << 16);
dword |= (ptr[3] << 24);
}
return buf;
}
Buffer& ecl::write(Buffer& buf, Uint32 dword)
{
Uint8* ptr = (Uint8*) buf.get_wspace(4);
ptr[0] = dword & 0xff;
ptr[1] = (dword >> 8) & 0xff;
ptr[2] = (dword >> 16) & 0xff;
ptr[3] = (dword >> 24) & 0xff;
return buf;
}
/*
** === Read and write long longs ===
*/
Buffer& ecl::read(Buffer& buf, Uint64& lvar)
{
Uint8* ptr = (Uint8*) buf.get_rspace(8);
if (ptr != 0)
{
lvar = ptr[0];
lvar |= (Uint64(ptr[1]) << 8);
lvar |= (Uint64(ptr[2]) << 16);
lvar |= (Uint64(ptr[3]) << 24);
lvar |= (Uint64(ptr[4]) << 32);
lvar |= (Uint64(ptr[5]) << 40);
lvar |= (Uint64(ptr[6]) << 48);
lvar |= (Uint64(ptr[7]) << 56);
}
return buf;
}
Buffer& ecl::write(Buffer& buf, Uint64 lvar)
{
Uint8* ptr = (Uint8*) buf.get_wspace(8);
ptr[0] = Uint8(lvar & 0xff);
ptr[1] = Uint8((lvar >> 8) & 0xff);
ptr[2] = Uint8((lvar >> 16) & 0xff);
ptr[3] = Uint8((lvar >> 24) & 0xff);
ptr[4] = Uint8((lvar >> 32) & 0xff);
ptr[5] = Uint8((lvar >> 40) & 0xff);
ptr[6] = Uint8((lvar >> 48) & 0xff);
ptr[7] = Uint8((lvar >> 56) & 0xff);
return buf;
}
/*
** === Read and write floats ===
*/
/* NOTE: we reinterpret floats and doubles as Uint32/Uint64,
** and byte-swap them like integers.
** This is how quake does it, too (except they only use floats).
** I couldn't believe that this worked, so I checked it out
** (PPC <-> x86), and it does. The upside (over multiplying
** with 2048 and converting to int, as it was before) is that
** we have the same rep on both machines, possibly leading to
** less desynchronisation in network games.
*/
Buffer& ecl::read(Buffer& buf, float& dvar)
{
union
{
float f;
Uint32 a;
} t;
if (buf >> t.a)
dvar = t.f;
return buf;
}
Buffer& ecl::write(Buffer& buf, float dvar)
{
union
{
float f;
Uint32 a;
} t;
t.f = dvar;
write(buf, t.a);
return buf;
}
/*
** === Read and write doubles ===
*/
Buffer& ecl::read(Buffer& buf, double& dvar)
{
union
{
double d;
Uint64 a;
} t;
if (buf >> t.a)
dvar = t.d;
return buf;
}
Buffer& ecl::write(Buffer& buf, double dvar)
{
union
{
double d;
Uint64 a;
} t;
t.d=dvar;
write(buf, t.a);
return buf;
}
/*
** === Read and write another buffer ===
*/
Buffer& ecl::read(Buffer& srcbuf, Buffer& destbuf, int len)
{
if (&srcbuf != &destbuf)
{
char* dest = destbuf.get_wspace(len);
char* src = srcbuf.get_rspace(len);
if (src != 0)
memcpy(dest, src, len);
}
return srcbuf;
}
Buffer& ecl::write(Buffer& buf, const Buffer& databuf)
{
if (&buf != &databuf)
{
buf.write(databuf.data(), databuf.size());
}
return buf;
}
/*
** === Read and write strings ===
*/
Buffer& ecl::read(Buffer& buf, string& str)
{
Uint16 size;
if (buf >> size)
{
char* ptr = buf.get_rspace(size);
if (ptr != 0)
str.assign(ptr, ptr+size);
}
return buf;
}
Buffer& ecl::write(Buffer& buf, const string& str)
{
Uint16 size = str.size();
buf << size;
buf.write(str.c_str(), size);
return buf;
}
/*
** Read and write from and to streams
*/
ostream& ecl::operator<<(ostream& os, const Buffer& buf)
{
os.write(buf.data(), buf.size());
return os;
}
// istream& ecl::operator>>(istream& is, Buffer& buf)
// {
// buf.clear();
// char tmp[2048];
// while (is.read(tmp, sizeof tmp))
// {
// buf.write(tmp, sizeof tmp);
// }
// buf.write(tmp, is.gcount());
// return is;
// }
|