Module: Essential Tools Module Group: Virtual Stream Classes
RWAuditStreamBufferstd::streambuf
#include <rw/auditbuf.h> #include <iostream> RWAuditStreamBuffer buf(arguments) std::ostream os(&buf); // may be used for ostreams std::istream is(&buf); // or istreams of any kind
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.
None
#include <rw/bstream.h> #include <rw/pstream.h> #include <rw/auditbuf.h> #include <rw/collint.h> // for RWCollectableInt #include <rw/collstr.h> // for RWCollectableString #include <rw/dlistcol.h> // for RWDlistCollectables #include <iostream> int main () { RWCollectableInt i(7); RWCollectableString s("Hello!\n"); RWDlistCollectables ct; // Construct a collection ct.append(&i); ct.append(&s); ct.prepend(&ct); RWAuditStreamBuffer bcounter, pcounter; RWbostream bcount(&bcounter); // ctor takes streambuf pointer RWpostream pcount(&pcounter); bcount << ct; pcount << ct; std::cout << "We just counted " << bcounter << " bytes from an RWbostream.\n"; std::cout << "We just counted " << pcounter << " bytes from an RWpostream." << std::endl; return 0; }
Program output:
We just counted 43 bytes from an RWbostream. We just counted 52 bytes from an RWpostream.
RWAuditStreamBuffer may be used as the streambuf for any stream, including those derived from RWvostream or RWvistream, std::strstream, std::ifstream, std::ofstream, etc.
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 counter to be manipulated by RWAuditFunction.
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(std::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 third argument to the constructor allows you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(std::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 third argument to the constructor allows you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(std::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 count or examine the bytes being output to a file through a stream derived from RWvostream. The third argument to the constructor allows you to supply storage for the byte count. It is optional.
RWAuditStreamBuffer(std::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 third argument to the constructor allows you to supply storage for the byte count. It is optional.
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.
operator unsigned long();
Provides the count of bytes seen so far.
unsigned long reset(unsigned long value = 0);
Resets the count of bytes seen so far. Returns the current count.
#include <iostream> #include <fstream> #include <rw/auditbuf.h> #include <rw/pstream.h> #include <rw/cstring.h> void crc_func (unsigned char c, void* x) { *(unsigned char*)x ^= c; } void write_to_file (const char* s) { unsigned char check = 0; // Create an output stream std::ofstream os (s); // Create an RWAuditStreamBuffer that will do CRC RWAuditStreamBuffer sb (os, crc_func, &check); // Create an RWpostream wrapper to the streambuffer RWpostream p (&sb); // Send data to the stream p << double (3.1415962); p << int (3); p << RWCString("Tools library enables easy streaming."); // Save checksum before it gets modified in the streaming process. unsigned saved_check = check; // Save the CRC on the stream p << unsigned(check); // Print a description std::cout << "We just saved " << sb << " bytes of data to the file.\n"; std::cout << "The checksum for those bytes was " << unsigned (saved_check) << "." << std::endl; } void read_from_file (const char* s) { unsigned char check = 0; // Create an output stream std::ifstream is (s); // Create an RWAuditStreamBuffer that will do CRC RWAuditStreamBuffer sb (is, crc_func, &check); // Create an RWpistream wrapper to the streambuffer RWpistream p (&sb); RWCString str; int i; double d; // Retrieve data to the stream p >> d >> i >> str; // The CRC unsigned saved_check, computed_check; computed_check = check; // The saved CRC p >> saved_check; if(saved_check != computed_check) { std::cout << "Checksum error. Saved CRC: " << saved_check << " built CRC: " << computed_check << "\n"; } else { std::cout << "The message was:\n"; std::cout << d << i << str << std::endl; } // Print out summary: std::cout << "We just read " << sb << " bytes of data from the file.\n"; std::cout << "The checksum was: " << unsigned (computed_check); std::cout << " and the saved checksum was: " << unsigned (saved_check) << "." << std::endl; } int main () { write_to_file ("crc.txt"); read_from_file ("crc.txt"); return 0; }
Program output:
We just saved 81 bytes of data to the file. The checksum for those bytes was 72. The message was: 3.14163Tools library enables easy streaming. We just read 81 bytes of data from the file. The checksum was: 72 and the saved checksum was: 72.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.