WvStreams
wvencoder.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A top-level data encoder class and a few useful encoders.
6  */
7 #ifndef __WVENCODER_H
8 #define __WVENCODER_H
9 
10 #include "wvbuf.h"
11 #include "wvlinklist.h"
12 #include "wvstring.h"
13 
67 class WvEncoder
68 {
69 protected:
70  bool okay;
71  bool finished;
74 public:
76  WvEncoder();
77 
79  virtual ~WvEncoder();
80 
90  bool isok() const
91  { return okay && _isok(); }
92 
101  bool isfinished() const
102  { return finished || _isfinished(); }
103 
109  WvString geterror() const;
110 
152  bool encode(WvBuf &inbuf, WvBuf &outbuf, bool flush = false,
153  bool finish = false);
154 
163  bool flush(WvBuf &inbuf, WvBuf &outbuf,
164  bool finish = false)
165  { return encode(inbuf, outbuf, true, finish); }
166 
184  bool finish(WvBuf &outbuf);
185 
199  bool reset();
200 
209  bool flushstrbuf(WvStringParm instr, WvBuf &outbuf,
210  bool finish = false);
211 
222  bool flushstrstr(WvStringParm instr, WvString &outstr,
223  bool finish = false);
224 
236  bool encodebufstr(WvBuf &inbuf, WvString &outstr,
237  bool flush = false, bool finish = false);
238 
249  bool flushbufstr(WvBuf &inbuf, WvString &outstr,
250  bool finish = false)
251  { return encodebufstr(inbuf, outstr, true, finish); }
252 
260  WvString strflushstr(WvStringParm instr, bool finish = false);
261 
269  WvString strflushbuf(WvBuf &inbuf, bool finish = false);
270 
280  bool flushmembuf(const void *inmem, size_t inlen, WvBuf &outbuf,
281  bool finish = false);
282 
299  bool flushmemmem(const void *inmem, size_t inlen, void *outmem,
300  size_t *outlen, bool finish = false);
301 
319  bool encodebufmem(WvBuf &inbuf, void *outmem, size_t *outlen,
320  bool flush = false, bool finish = false);
321 
337  bool flushbufmem(WvBuf &inbuf, void *outmem, size_t *outlen,
338  bool finish = false)
339  { return encodebufmem(inbuf, outmem, outlen, true, finish); }
340 
356  bool flushstrmem(WvStringParm instr, void *outmem, size_t *outlen,
357  bool finish = false);
358 
367  WvString strflushmem(const void *inmem, size_t inlen, bool finish = false);
368 
369 protected:
371  void setnotok()
372  { okay = false; }
373 
375  void seterror(WvStringParm message)
376  { errstr = message; setnotok(); }
377 
379  void seterror(WVSTRING_FORMAT_DECL)
380  { seterror(WvString(WVSTRING_FORMAT_CALL)); }
381 
383  void setfinished()
384  { finished = true; }
385 
386 protected:
400  virtual bool _isok() const
401  { return true; }
402 
416  virtual bool _isfinished() const
417  { return false; }
418 
433  virtual WvString _geterror() const
434  { return WvString::null; }
435 
459  virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush) = 0;
460 
483  virtual bool _finish(WvBuf &outbuf)
484  { return true; }
485 
498  virtual bool _reset()
499  { return false; }
500 };
501 
502 
504 class WvNullEncoder : public WvEncoder
505 {
506 protected:
507  virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
508  virtual bool _reset(); // supported: does nothing
509 };
510 
511 
522 {
523  size_t total;
524 
525 public:
527  virtual ~WvPassthroughEncoder() { }
528 
533  size_t bytes_processed() { return total; }
534 
535 protected:
536  virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
537  virtual bool _reset(); // supported: resets the count to zero
538 };
539 
540 
549 class WvEncoderChain : public WvEncoder
550 {
551  class ChainElem
552  {
553  public:
554  WvEncoder *enc;
555  WvDynBuf out;
556  bool autofree;
557 
558  ChainElem(WvEncoder *enc, bool autofree)
559  : enc(enc), autofree(autofree) { }
560  ~ChainElem() { if (autofree) delete enc; }
561  };
562  DeclareWvList(ChainElem);
563 
564  ChainElemList encoders;
565  ChainElem *last_run;
566 
567 public:
569  WvEncoderChain();
570 
572  virtual ~WvEncoderChain();
573 
578  void append(WvEncoder *enc, bool autofree);
579 
584  void prepend(WvEncoder *enc, bool autofree);
585 
592  bool get_autofree(WvEncoder *enc) const;
593 
601  void set_autofree(WvEncoder *enc, bool autofree);
602 
607  void unlink(WvEncoder *enc);
608 
613  void zap();
614 
628  bool continue_encode(WvBuf &inbuf, WvBuf &outbuf);
629 
633  size_t buffered();
634 
635 protected:
646  virtual bool _isok() const;
647 
658  virtual bool _isfinished() const;
659 
669  virtual WvString _geterror() const;
670 
675  virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
676 
688  virtual bool _finish(WvBuf & out);
689 
698  virtual bool _reset();
699 
700 private:
702  bool do_encode(WvBuf &in, WvBuf &out, ChainElem *start_after,
703  bool flush, bool finish);
704 };
705 
706 #endif // __WVENCODER_H
WvEncoder::okay
bool okay
Definition: wvencoder.h:70
WvPassthroughEncoder::_encode
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvencoder.cc:189
WvEncoder
The base encoder class.
Definition: wvencoder.h:67
WvEncoderChain::_isok
virtual bool _isok() const
Returns true if the encoder has not encountered an error.
Definition: wvencoder.cc:218
WvEncoder::isfinished
bool isfinished() const
Returns true if the encoder can no longer encode data.
Definition: wvencoder.h:101
WvEncoderChain::_finish
virtual bool _finish(WvBuf &out)
Finishes the chain of encoders.
Definition: wvencoder.cc:283
WvEncoder::flushmemmem
bool flushmemmem(const void *inmem, size_t inlen, void *outmem, size_t *outlen, bool finish=false)
Flushes data through the encoder from memory to memory.
Definition: wvencoder.cc:132
WvEncoderChain::zap
void zap()
Clears the encoder chain.
Definition: wvencoder.cc:374
WvEncoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvencoder.h:498
WvEncoderChain::append
void append(WvEncoder *enc, bool autofree)
Appends an encoder to the tail of the chain.
Definition: wvencoder.cc:312
WvEncoderChain::WvEncoderChain
WvEncoderChain()
Creates an initially empty chain of encoders.
Definition: wvencoder.cc:207
WvEncoder::_isfinished
virtual bool _isfinished() const
Template method implementation of isfinished().
Definition: wvencoder.h:416
WvEncoder::setnotok
void setnotok()
Sets 'okay' to false explicitly.
Definition: wvencoder.h:371
WvEncoder::strflushstr
WvString strflushstr(WvStringParm instr, bool finish=false)
Flushes data through the encoder from a string to a string.
Definition: wvencoder.cc:107
WvEncoderChain::_isfinished
virtual bool _isfinished() const
Returns true if the encoder can no longer encode data.
Definition: wvencoder.cc:228
WvEncoder::strflushmem
WvString strflushmem(const void *inmem, size_t inlen, bool finish=false)
Flushes data through the encoder from memory to a string.
Definition: wvencoder.cc:158
WvNullEncoder
An encoder that discards all of its input.
Definition: wvencoder.h:504
WvEncoder::seterror
void seterror(WVSTRING_FORMAT_DECL)
Sets an error condition, then setnotok().
Definition: wvencoder.h:379
WvEncoder::encode
bool encode(WvBuf &inbuf, WvBuf &outbuf, bool flush=false, bool finish=false)
Reads data from the input buffer, encodes it, and writes the result to the output buffer.
Definition: wvencoder.cc:36
WvPassthroughEncoder::bytes_processed
size_t bytes_processed()
Returns the number of bytes processed so far.
Definition: wvencoder.h:533
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvEncoder::flushstrbuf
bool flushstrbuf(WvStringParm instr, WvBuf &outbuf, bool finish=false)
Flushes data through the encoder from a string to a buffer.
Definition: wvencoder.cc:77
WvEncoder::encodebufstr
bool encodebufstr(WvBuf &inbuf, WvString &outstr, bool flush=false, bool finish=false)
Encodes data from a buffer to a string.
Definition: wvencoder.cc:97
WvEncoderChain::_reset
virtual bool _reset()
Resets the chain of encoders.
Definition: wvencoder.cc:298
WvEncoder::~WvEncoder
virtual ~WvEncoder()
Destroys the encoder.
Definition: wvencoder.cc:18
WvEncoder::WvEncoder
WvEncoder()
Creates a new WvEncoder.
Definition: wvencoder.cc:11
WvEncoderChain::buffered
size_t buffered()
Returns true if there is data in an internal buffer.
Definition: wvencoder.cc:380
WvEncoderChain::set_autofree
void set_autofree(WvEncoder *enc, bool autofree)
Sets the autofree state of a particular encoder in the chain.
Definition: wvencoder.cc:334
WvEncoder::_encode
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)=0
Template method implementation of encode().
WvEncoderChain::continue_encode
bool continue_encode(WvBuf &inbuf, WvBuf &outbuf)
"Continues" encoding a buffer.
Definition: wvencoder.cc:290
WvEncoder::flushbufmem
bool flushbufmem(WvBuf &inbuf, void *outmem, size_t *outlen, bool finish=false)
Flushes data through the encoder from a buffer to memory.
Definition: wvencoder.h:337
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
WvEncoder::strflushbuf
WvString strflushbuf(WvBuf &inbuf, bool finish=false)
Flushes data through the encoder from a buffer to a string.
Definition: wvencoder.cc:115
WvEncoder::geterror
WvString geterror() const
Returns an error message if any is available.
Definition: wvencoder.cc:23
WvEncoder::isok
bool isok() const
Returns true if the encoder has not encountered an error.
Definition: wvencoder.h:90
WvEncoder::finish
bool finish(WvBuf &outbuf)
Tells the encoder that NO MORE DATA will ever be encoded.
Definition: wvencoder.cc:49
WvEncoder::flushstrstr
bool flushstrstr(WvStringParm instr, WvString &outstr, bool finish=false)
Flushes data through the encoder from a string to a string.
Definition: wvencoder.cc:86
WvEncoderChain::unlink
void unlink(WvEncoder *enc)
Unlinks the encoder from the chain.
Definition: wvencoder.cc:365
WvEncoder::flushbufstr
bool flushbufstr(WvBuf &inbuf, WvString &outstr, bool finish=false)
Flushes data through the encoder from a buffer to a string.
Definition: wvencoder.h:249
WvEncoder::errstr
WvString errstr
Definition: wvencoder.h:72
WvEncoder::flush
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Definition: wvencoder.h:163
WvPassthroughEncoder
A very efficient passthrough encoder that just merges the input buffer into the output buffer.
Definition: wvencoder.h:521
WvDynBufBase< unsigned char >
WvEncoderChain::prepend
void prepend(WvEncoder *enc, bool autofree)
Prepends an encoder to the head of the chain.
Definition: wvencoder.cc:318
WvEncoderChain
An encoder chain owns a list of encoders that are used in sequence to transform data from a source bu...
Definition: wvencoder.h:549
WvEncoder::setfinished
void setfinished()
Sets 'finished' to true explicitly.
Definition: wvencoder.h:383
WvEncoder::finished
bool finished
Definition: wvencoder.h:71
WvEncoder::_isok
virtual bool _isok() const
Template method implementation of isok().
Definition: wvencoder.h:400
WvNullEncoder::_encode
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvencoder.cc:167
WvEncoder::_geterror
virtual WvString _geterror() const
Template method implementation of geterror().
Definition: wvencoder.h:433
WvNullEncoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvencoder.cc:174
WvEncoder::flushstrmem
bool flushstrmem(WvStringParm instr, void *outmem, size_t *outlen, bool finish=false)
Flushes data through the encoder from a string to memory.
Definition: wvencoder.cc:150
WvEncoder::flushmembuf
bool flushmembuf(const void *inmem, size_t inlen, WvBuf &outbuf, bool finish=false)
Flushes data through the encoder from memory to a buffer.
Definition: wvencoder.cc:123
WvEncoderChain::_encode
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Passes the data through the entire chain of encoders.
Definition: wvencoder.cc:277
WvEncoder::encodebufmem
bool encodebufmem(WvBuf &inbuf, void *outmem, size_t *outlen, bool flush=false, bool finish=false)
Encodes data from a buffer to memory.
Definition: wvencoder.cc:140
WvEncoder::seterror
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
Definition: wvencoder.h:375
WvEncoder::_finish
virtual bool _finish(WvBuf &outbuf)
Template method implementation of finish().
Definition: wvencoder.h:483
WvEncoderChain::~WvEncoderChain
virtual ~WvEncoderChain()
Destroys the encoder chain.
Definition: wvencoder.cc:213
WvEncoderChain::get_autofree
bool get_autofree(WvEncoder *enc) const
Gets the autofree state of a particular encoder in the chain.
Definition: wvencoder.cc:324
WvPassthroughEncoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvencoder.cc:197
WvEncoder::reset
bool reset()
Asks an encoder to reset itself to its initial state at creation time, if supported.
Definition: wvencoder.cc:60
WvEncoderChain::_geterror
virtual WvString _geterror() const
Returns the error message, if any.
Definition: wvencoder.cc:238