WvStreams
|
00001 /* -*- Mode: C++ -*- 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * A top-level data encoder class and a few useful encoders. 00006 */ 00007 #ifndef __WVENCODER_H 00008 #define __WVENCODER_H 00009 00010 #include "wvbuf.h" 00011 #include "wvlinklist.h" 00012 #include "wvstring.h" 00013 00067 class WvEncoder 00068 { 00069 protected: 00070 bool okay; 00071 bool finished; 00072 WvString errstr; 00074 public: 00076 WvEncoder(); 00077 00079 virtual ~WvEncoder(); 00080 00090 bool isok() const 00091 { return okay && _isok(); } 00092 00101 bool isfinished() const 00102 { return finished || _isfinished(); } 00103 00109 WvString geterror() const; 00110 00152 bool encode(WvBuf &inbuf, WvBuf &outbuf, bool flush = false, 00153 bool finish = false); 00154 00163 bool flush(WvBuf &inbuf, WvBuf &outbuf, 00164 bool finish = false) 00165 { return encode(inbuf, outbuf, true, finish); } 00166 00184 bool finish(WvBuf &outbuf); 00185 00199 bool reset(); 00200 00209 bool flushstrbuf(WvStringParm instr, WvBuf &outbuf, 00210 bool finish = false); 00211 00222 bool flushstrstr(WvStringParm instr, WvString &outstr, 00223 bool finish = false); 00224 00236 bool encodebufstr(WvBuf &inbuf, WvString &outstr, 00237 bool flush = false, bool finish = false); 00238 00249 bool flushbufstr(WvBuf &inbuf, WvString &outstr, 00250 bool finish = false) 00251 { return encodebufstr(inbuf, outstr, true, finish); } 00252 00260 WvString strflushstr(WvStringParm instr, bool finish = false); 00261 00269 WvString strflushbuf(WvBuf &inbuf, bool finish = false); 00270 00280 bool flushmembuf(const void *inmem, size_t inlen, WvBuf &outbuf, 00281 bool finish = false); 00282 00299 bool flushmemmem(const void *inmem, size_t inlen, void *outmem, 00300 size_t *outlen, bool finish = false); 00301 00319 bool encodebufmem(WvBuf &inbuf, void *outmem, size_t *outlen, 00320 bool flush = false, bool finish = false); 00321 00337 bool flushbufmem(WvBuf &inbuf, void *outmem, size_t *outlen, 00338 bool finish = false) 00339 { return encodebufmem(inbuf, outmem, outlen, true, finish); } 00340 00356 bool flushstrmem(WvStringParm instr, void *outmem, size_t *outlen, 00357 bool finish = false); 00358 00367 WvString strflushmem(const void *inmem, size_t inlen, bool finish = false); 00368 00369 protected: 00371 void setnotok() 00372 { okay = false; } 00373 00375 void seterror(WvStringParm message) 00376 { errstr = message; setnotok(); } 00377 00379 void seterror(WVSTRING_FORMAT_DECL) 00380 { seterror(WvString(WVSTRING_FORMAT_CALL)); } 00381 00383 void setfinished() 00384 { finished = true; } 00385 00386 protected: 00400 virtual bool _isok() const 00401 { return true; } 00402 00416 virtual bool _isfinished() const 00417 { return false; } 00418 00433 virtual WvString _geterror() const 00434 { return WvString::null; } 00435 00459 virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush) = 0; 00460 00483 virtual bool _finish(WvBuf &outbuf) 00484 { return true; } 00485 00498 virtual bool _reset() 00499 { return false; } 00500 }; 00501 00502 00504 class WvNullEncoder : public WvEncoder 00505 { 00506 protected: 00507 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush); 00508 virtual bool _reset(); // supported: does nothing 00509 }; 00510 00511 00521 class WvPassthroughEncoder : public WvEncoder 00522 { 00523 size_t total; 00524 00525 public: 00526 WvPassthroughEncoder(); 00527 virtual ~WvPassthroughEncoder() { } 00528 00533 size_t bytes_processed() { return total; } 00534 00535 protected: 00536 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush); 00537 virtual bool _reset(); // supported: resets the count to zero 00538 }; 00539 00540 00549 class WvEncoderChain : public WvEncoder 00550 { 00551 class ChainElem 00552 { 00553 public: 00554 WvEncoder *enc; 00555 WvDynBuf out; 00556 bool autofree; 00557 00558 ChainElem(WvEncoder *enc, bool autofree) 00559 : enc(enc), autofree(autofree) { } 00560 ~ChainElem() { if (autofree) delete enc; } 00561 }; 00562 DeclareWvList(ChainElem); 00563 00564 ChainElemList encoders; 00565 ChainElem *last_run; 00566 00567 public: 00569 WvEncoderChain(); 00570 00572 virtual ~WvEncoderChain(); 00573 00578 void append(WvEncoder *enc, bool autofree); 00579 00584 void prepend(WvEncoder *enc, bool autofree); 00585 00592 bool get_autofree(WvEncoder *enc) const; 00593 00601 void set_autofree(WvEncoder *enc, bool autofree); 00602 00607 void unlink(WvEncoder *enc); 00608 00613 void zap(); 00614 00628 bool continue_encode(WvBuf &inbuf, WvBuf &outbuf); 00629 00633 size_t buffered(); 00634 00635 protected: 00646 virtual bool _isok() const; 00647 00658 virtual bool _isfinished() const; 00659 00669 virtual WvString _geterror() const; 00670 00675 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush); 00676 00688 virtual bool _finish(WvBuf & out); 00689 00698 virtual bool _reset(); 00699 00700 private: 00702 bool do_encode(WvBuf &in, WvBuf &out, ChainElem *start_after, 00703 bool flush, bool finish); 00704 }; 00705 00706 #endif // __WVENCODER_H