RWAuditStreamBuffer(3C++) RWAuditStreamBuffer(3C++)
NameRWAuditStreamBuffer - Rogue Wave library class
Synopsis
#include <rw/auditbuf.h>
#include <iostream.h>
RWAuditStreamBufferbuf(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;
}
Page 1
RWAuditStreamBuffer(3C++) RWAuditStreamBuffer(3C++)
Related ClassesRWAuditStreamBuffer 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*);
If you wish to do more than count each character handled by the buffer,
you may provide an RWauditFunction to the constructor. The first
parameter to this function is a byte provided by the stream. The second
parameter is the address of the conter to be manipulated by
RWAuditFunction.
Public Constructors
RWAuditStreamBuffer(RWauditFunction=0, void*=0);
Constructs a new RWAuditStreamBuffer that may be used only to examine and
count every byte that passes into an ostream that has the
RWAuditStreamBuffer instance as its streambuf. It will not forward the
bytes to any stream, nor accept bytes from a stream. The second argument
to the constructor allows you to supply storage for the byte count. It is
optional.
RWAuditStreamBuffer(istream&, RWauditFunction=0, void*=0);
Constructs a new RWAuditStreamBuffer that passes bytes from the istream
on which it is constructed to the istream that has the
RWAuditStreamBuffer instance as its streambuf. A typical use would be to
count or examine the bytes being input from a file through a stream
derived from RWvistream. The second argument to the constructor allows
you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(iostream&, RWauditFunction=0, void*=0);
Constructs a new RWAuditStreamBuffer that passes bytes to and from the
iostream on which it is constructed to and from the istream that has the
RWAuditStreamBuffer instance as its streambuf. A typical use would be to
count or examine the bytes being transferred to and from a file used to
store and retrieve changing data. The second argument to the constructor
allows you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(ostream&, RWauditFunction=0, void*=0);
Constructs a new RWAuditStreamBuffer that passes bytes into the ostream
on which it is constructed from the ostream that has the
RWAuditStreamBuffer instance as its streambuf. A typical use would be to
Page 2
RWAuditStreamBuffer(3C++) RWAuditStreamBuffer(3C++)
count or examine the bytes being output to a file through a stream
derived from RWvostream. The second argument to the constructor allows
you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(streambuf*, RWauditFunction=0, void*=0);
Constructs a new RWAuditStreamBuffer that passes bytes into the ostream
on which it is constructed from the ostream that has the
RWAuditStreamBuffer instance as its streambuf. A typical use would be to
count or examine the bytes being output to a file through a stream
derived from RWvostream. The second argument to the constructor allows
you to supply storage for the byte count. It is optional.
Public Destructor
virtual ~RWAuditStreamBuffer();
We have provided an empty destructor since some compilers complain if
there is no virtual destructor for a class that has virtual methods.
Public Member Operator
operator unsigned long();
Provides the count of bytes seen so far.
Public Member Function
unsigned long
reset(unsigned long value = 0);
Resets the count of bytes seen so far. Returns the current count.
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 = ' ';
// create an output stream
ofstream op("crc.pst");
// create an RWAuditStreamBuffer that will do CRC
Page 3
RWAuditStreamBuffer(3C++) RWAuditStreamBuffer(3C++)
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!0) ;
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 = ' ';
// 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;
}
Page 4
RWAuditStreamBuffer(3C++) RWAuditStreamBuffer(3C++)
Page 5