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
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* A top-level data encoder class. See wvencoder.h.
*/
#include "wvencoder.h"
/***** WvEncoder *****/
WvEncoder::WvEncoder()
{
okay = true;
finished = false;
}
WvEncoder::~WvEncoder()
{
}
WvString WvEncoder::geterror() const
{
if (isok())
return WvString::null;
if (!!errstr)
return errstr;
WvString message = _geterror();
if (!!message)
return message;
return "unknown encoder error";
}
bool WvEncoder::encode(WvBuf &inbuf, WvBuf &outbuf,
bool flush, bool _finish)
{
// deliberately not using isok() and isfinished() here
bool success = okay && !finished && (inbuf.used() != 0 || flush);
if (success)
success = _encode(inbuf, outbuf, flush);
if (_finish)
success = finish(outbuf) && success;
return success;
}
bool WvEncoder::finish(WvBuf &outbuf)
{
// deliberately not using isok() and isfinished() here
bool success = okay && !finished;
if (success)
success = _finish(outbuf);
setfinished();
return success;
}
bool WvEncoder::reset()
{
// reset local state
okay = true;
finished = false;
errstr = WvString::null;
// attempt to reset the encoder
bool success = _reset();
if (!success)
{
if (okay)
seterror("reset not supported by encoder");
}
return success;
}
bool WvEncoder::flushstrbuf(WvStringParm instr, WvBuf &outbuf,
bool finish)
{
WvConstStringBuffer inbuf(instr);
bool success = encode(inbuf, outbuf, true, finish);
return success;
}
bool WvEncoder::flushstrstr(WvStringParm instr, WvString &outstr,
bool finish)
{
WvConstStringBuffer inbuf(instr);
WvDynBuf outbuf;
bool success = encode(inbuf, outbuf, true, finish);
outstr.append(outbuf.getstr());
return success;
}
bool WvEncoder::encodebufstr(WvBuf &inbuf, WvString &outstr,
bool flush, bool finish)
{
WvDynBuf outbuf;
bool success = encode(inbuf, outbuf, flush, finish);
outstr.append(outbuf.getstr());
return success;
}
WvString WvEncoder::strflushstr(WvStringParm instr, bool finish)
{
WvString outstr;
flushstrstr(instr, outstr, finish);
return outstr;
}
WvString WvEncoder::strflushbuf(WvBuf &inbuf, bool finish)
{
WvString outstr;
flushbufstr(inbuf, outstr, finish);
return outstr;
}
bool WvEncoder::flushmembuf(const void *inmem, size_t inlen,
WvBuf &outbuf, bool finish)
{
WvConstInPlaceBuf inbuf(inmem, inlen);
bool success = encode(inbuf, outbuf, true, finish);
return success;
}
bool WvEncoder::flushmemmem(const void *inmem, size_t inlen,
void *outmem, size_t *outlen, bool finish)
{
WvConstInPlaceBuf inbuf(inmem, inlen);
return encodebufmem(inbuf, outmem, outlen, true, finish);
}
bool WvEncoder::encodebufmem(WvBuf &inbuf,
void *outmem, size_t *outlen, bool flush, bool finish)
{
WvInPlaceBuf outbuf(outmem, 0, *outlen);
bool success = encode(inbuf, outbuf, true, finish);
*outlen = outbuf.used();
return success;
}
bool WvEncoder::flushstrmem(WvStringParm instr,
void *outmem, size_t *outlen, bool finish)
{
WvConstStringBuffer inbuf(instr);
return flushbufmem(inbuf, outmem, outlen, finish);
}
WvString WvEncoder::strflushmem(const void *inmem, size_t inlen, bool finish)
{
WvConstInPlaceBuf inbuf(inmem, inlen);
return strflushbuf(inbuf, finish);
}
/***** WvNullEncoder *****/
bool WvNullEncoder::_encode(WvBuf &in, WvBuf &out, bool flush)
{
in.zap();
return true;
}
bool WvNullEncoder::_reset()
{
return true;
}
/***** WvPassthroughEncoder *****/
WvPassthroughEncoder::WvPassthroughEncoder()
{
_reset();
}
bool WvPassthroughEncoder::_encode(WvBuf &in, WvBuf &out, bool flush)
{
total += in.used();
out.merge(in);
return true;
}
bool WvPassthroughEncoder::_reset()
{
total = 0;
return true;
}
/***** WvEncoderChain *****/
WvEncoderChain::WvEncoderChain()
{
last_run = NULL;
}
WvEncoderChain::~WvEncoderChain()
{
}
bool WvEncoderChain::_isok() const
{
ChainElemList::Iter it(const_cast<ChainElemList&>(encoders));
for (it.rewind(); it.next(); )
if (!it->enc->isok())
return false;
return true;
}
bool WvEncoderChain::_isfinished() const
{
ChainElemList::Iter it(const_cast<ChainElemList&>(encoders));
for (it.rewind(); it.next(); )
if (it->enc->isfinished())
return true;
return false;
}
WvString WvEncoderChain::_geterror() const
{
ChainElemList::Iter it(const_cast<ChainElemList&>(encoders));
for (it.rewind(); it.next(); )
{
WvString message = it->enc->geterror();
if (!!message) return message;
}
return WvString::null;
}
// NOTE: In this function we deliberately ignore deep isok() and
// isfinished() results to allow addition/removal of
// individual broken encoders while still processing data
// through as much of the chain as possible.
bool WvEncoderChain::do_encode(WvBuf &in, WvBuf &out, ChainElem *start_after,
bool flush, bool finish)
{
bool success = true;
WvBuf *tmpin = ∈
ChainElemList::Iter it(encoders);
it.rewind();
if (start_after) it.find(start_after);
last_run = start_after;
for (; it.cur() && it.next(); )
{
if (!it->enc->encode(*tmpin, it->out, flush))
success = false;
if (finish && !it->enc->finish(it->out))
success = false;
last_run = it.ptr();
tmpin = &it->out;
}
out.merge(*tmpin);
return success;
}
bool WvEncoderChain::_encode(WvBuf &in, WvBuf &out, bool flush)
{
return do_encode(in, out, NULL, flush, false);
}
bool WvEncoderChain::_finish(WvBuf &out)
{
WvNullBuf empty;
return do_encode(empty, out, NULL, true, true);
}
bool WvEncoderChain::continue_encode(WvBuf &in, WvBuf &out)
{
//fprintf(stderr, "continue_encode(%d,%d,%p)\n",
// in.used(), out.used(), last_run);
return do_encode(in, out, last_run, false, false);
}
bool WvEncoderChain::_reset()
{
bool success = true;
ChainElemList::Iter it(encoders);
for (it.rewind(); it.next(); )
{
it->out.zap();
if (!it->enc->reset())
success = false;
}
return success;
}
void WvEncoderChain::append(WvEncoder *enc, bool autofree)
{
encoders.append(new ChainElem(enc, autofree), true);
}
void WvEncoderChain::prepend(WvEncoder *enc, bool autofree)
{
encoders.prepend(new ChainElem(enc, autofree), true);
}
bool WvEncoderChain::get_autofree(WvEncoder *enc) const
{
ChainElemList::Iter i(encoders);
for (i.rewind(); i.next(); )
if (i->enc == enc && i.get_autofree())
return true;
return false;
}
void WvEncoderChain::set_autofree(WvEncoder *enc, bool autofree)
{
ChainElemList::Iter i(encoders);
if (autofree)
{
// Ensure only the first matching encoder has autofree set
bool first = true;
for (i.rewind(); i.next(); )
{
if (i->enc == enc)
{
if (first)
{
i.set_autofree(true);
first = false;
}
else
i.set_autofree(false);
}
}
}
else
{
// Clear autofree for all matching encoders
for (i.rewind(); i.next(); )
if (i->enc == enc)
i.set_autofree(false);
}
}
void WvEncoderChain::unlink(WvEncoder *enc)
{
ChainElemList::Iter it(encoders);
for (it.rewind(); it.next(); )
if (it->enc == enc)
it.xunlink();
}
void WvEncoderChain::zap()
{
encoders.zap();
}
size_t WvEncoderChain::buffered()
{
size_t used = 0;
ChainElemList::Iter it(encoders);
for (it.rewind(); it.next(); )
used += it().out.used();
return used;
}
|