バナーをクリックすれば目次に戻ります
Copyright 1999 Rogue Wave Software
Copyright 1999 Sun Microsystems, Inc.
RWAuditStreamBuffer
RWAuditStreamBuffer
streambuf
#include <rw/auditbuf.h> #include <iostream.h> RWAuditStreamBuffer buf(arguments) ostream os(&buf); // どの種類の ostream または istream istream is(&buf); // にも使用できる
#include <rw/auditbuf.h>
#include <rw/bstream.h>
#include <rw/pstream.h>
#include <iostream.h>
int main() {
RWCollectable ct;
fillCollectable(); // なんらかの方法でコレクションを作る
RWAuditStreamBuffer bcounter, pcounter;
RWbostream bcount(&bcounter); //ctor は streambuf ポインタをとる
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;
}
|
typedef void (*RWauditFunction)(unsigned char, void*);
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);
virtual ~RWAuditStreamBuffer();
operator unsigned long();
unsigned long reset(unsigned long value = 0);
#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) { // 変数の寿命を制御するためのブロック
unsigned char check = '\0';
// 出力ストリームを作成する
ofstream op("crc.pst");
// CRC を実行する RWAuditStreamBuffer を作成する
RWAuditStreamBuffer crcb(op,doCrc,&check);
// データを入れるための RWpostream を作成する
RWpostream p(&crcb);
// ここで、ストリームに適当なデータをいくつか送る
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 << '.';
// 最後に、ストリーム自体に合計を保存する
p << (unsigned int)check; // 保存後に、チェックを変更する
// 試しに、いくつかの統計情報を出力する
cout << "We just saved " << crcb
<< " bytes of data to the file." << endl;
cout << "The checksum for those bytes was " <<check << endl;
} // ブロックの終わり
// ここで、データを読み戻して、正しく保存されているか確かめる
unsigned char check = '\0';
// instream を作成する
ifstream ip("crc.pst");
// CRC を実行する RWAuditStreamBuffer を作成する
RWAuditStreamBuffer crcb(ip,doCrc,&check);
// バイトを解釈するための RWpistream を作成する
RWpistream p(&crcb);
RWCString first, mid1, mid2;
int value;
double pi;
char pnc;
unsigned int savedCRC;
unsigned char matchCRC;
// データを読み込む。検査合計はまだ読み込まないこと
p >> first >> value >> mid1 >> mid2 >> pi >> pnc;
// 検査合計を保存する
matchCRC = check;
// ここで、ファイルに保存されている検査合計を読み込んで、実行検査合計を変
// 更しておくと安全である
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;
}
// 試しに、いくつかの統計情報を出力する
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;
}
|
