Click on the banner to return to the Class Reference home page.

©Copyright 1996 Rogue Wave Software

RWAuditStreamBuffer


RWAuditStreamBufferstreambuf

Synopsis

#include <rw/auditbuf.h>
#include <iostream.h>
RWAuditStreamBuffer buf(arguments)
ostream os(&buf); // may be used for ostreams
istream is(&buf); // or istreams of any kind

Description

Class RWAuditStreamBuffer is used to construct a stream, after which the RWAuditStreamBuffer instance will count all the bytes that pass through the stream. If constructed with a function pointer, RWAuditStreamBuffer will call that function with each byte that passes through the stream. The counting capacity provides for streams the equivalent of the RWCollectable method recursiveStoreSize() which is only available for RWFile.

Persistence

None

Short Example

#include <rw/auditbuf.h>
#include <rw/bstream.h>
#include <rw/pstream.h>
#include <iostream.h>
int main() {
  RWCollectable ct;
  fillCollectable();  // make a collection, somehow
  RWAuditStreamBuffer bcounter, pcounter;
  RWbostream bcount(&bcounter); //ctor takes streambuf pointer
  RWpostream pcount(&pcounter);
//_
  bcount << ct;
  pcount << ct;
cout  << "We just counted " << bcounter 
      << " bytes from an RWbostream." << endl;
cout  << "We just counted " << pcounter 
      << " bytes from an RWpostream." << endl;
return 0;
}

Related Classes

RWAuditStreamBuffer may be used as the streambuf for any stream, including those derived from RWvostream or RWvistream, strstream, ifstream, ofstream, etc.

Global Typedef

typedef void (*RWauditFunction)(unsigned char, void*);

Public Constructors

RWAuditStreamBuffer(RWauditFunction=0, void*=0);
RWAuditStreamBuffer(istream&, RWauditFunction=0, void*=0);
RWAuditStreamBuffer(iostream&, RWauditFunction=0, void*=0);
RWAuditStreamBuffer(ostream&, RWauditFunction=0, void*=0);
RWAuditStreamBuffer(streambuf*, RWauditFunction=0, void*=0);

Public Destructor

virtual ~RWAuditStreamBuffer();

Public Member Operator

operator unsigned long();

Public Member Function

unsigned long
reset(unsigned long value = 0);

Extended Example

#include <iostream.h>
#include <fstream.h>
#include <rw/auditbuf.h>
#include <rw/pstream.h>
#include <rw/cstring.h>
void doCrc (unsigned char c, void* x) {
  *(unsigned char*)x ^= c;
}

int main() {
if(1) { // just a block to control variable lifetime
    unsigned char check = '\0';

    // create an output stream
    ofstream                            op("crc.pst");

    // create an RWAuditStreamBuffer that will do CRC
    RWAuditStreamBuffer               crcb(op,doCrc,&check);

    // create an RWpostream to put the data through.
RWpostream                           p(&crcb);

    // now send some random stuff to the stream
    p << RWCString("The value of Tools.h++ is at least ");
    p << (int)4;
    p << RWCString(" times that of the next best library!\n") ;
    p << RWCString("Pi is about ") << (double)3.14159 << '.';

    // finally, save the sum on the stream itself.
p << (unsigned int)check; // alters check, _after_ saving it...

    // just for fun, print out some statistics:
    cout << "We just saved " << crcb 
         << " bytes of data to the file." << endl;
    cout << "The checksum for those bytes was " <<check << endl;
} // end of block

  // now read the data back in, checking to see if it survived.
  unsigned char check = '\0';

  // create an instream
  ifstream                            ip("crc.pst");

  // create an RWAuditStreamBuffer that will do CRC
  RWAuditStreamBuffer               crcb(ip,doCrc,&check);

  // create an RWpistream to interpret the bytes
  RWpistream                           p(&crcb);

  RWCString first, mid1, mid2;
  int value;
  double pi;
  char pnc;
  unsigned int savedCRC;
  unsigned char matchCRC;
  // read in the data. Don\'t read the checksum yet!
  p >> first >> value >> mid1 >> mid2 >> pi >> pnc;
  // save the checksum
  matchCRC = check;
  // Now it is safe to alter the running checksum by reading in
  // the one saved in the file.
p >> savedCRC;

  if(savedCRC != matchCRC) {
    cout << "Checksum error. Saved CRC: " << savedCRC
         << " built CRC: " << matchCRC << dec << endl;
  }
  else {
    cout << "The message was: " << endl;
    cout << first << value << mid1 << mid2 << pi << pnc << endl;
  }
  // just for fun, print out some statistics:
  cout  << "We just read " << crcb 
        << " bytes of data from the file." << endl;
  cout  << "The checksum was " << matchCRC << flush;
  cout  << " and the saved checksum was " << savedCRC << endl;
return 0;
}