Article View: borland.public.cppbuilder.nativeapi
Article #6900Re: cout <<"HELP" << endl; How to redirect all cout to a Memo ?
From: Thomas Maeder [TeamB]
Date: Thu, 31 Mar 2005 21:04
Date: Thu, 31 Mar 2005 21:04
72 lines
3047 bytes
3047 bytes
someone <someone@somewhere.com> writes:
> Does anyone know how to send "cout" to TMemo instead of sending it
> to console?
The Standard C++ Library is designed to allow this sort of tweaking.
C++ streams (such as std::cout) are of type std::ostream and have a
data member of type (pointer to) std::streambuf. std::ostream is
responsible for formatting (i.e. converting any kind of data into a
stream of characters); std::streambuf is the base class for classes
that forward that stream of characters to the console, a file, a
socket etc.
Obviously, what you want to do is to derive your own class from
std::streambuf, which, instead of forwarding the character stream to
the console or a file, adds it to a TMemo object.
This is an outline of how to do that:
Create your streambuf class:
class memo_streambuf : public std::streambuf
{
public:
memo_streambuf(TMemo &);
protected:
virtual int overflow(int c);
private:
TMemo &myBackend;
};
memo_streambuf::memo_streambuf(TMemo &m)
: myBackend(m)
{
}
//virtual
int memo_streambuf::overflow(int c)
{
if (c!=EOF)
; // append c to myBackend - I'm sure you know how to do that
return c;
}
memo_streambuf is sort of a misnomer, because it doesn't have a
buffer. Therefore, overflow() will be called for each character.
Somewhere in your program, configure std::cout to use a memo_streambuf
object as its streambuf instead what it would use by default:
std::streambuf *savebuf;
void somefunc()
{
savebuf = std::cout.rdbuf(new memo_streambuf(theMemo));
}
Later, before the program terminates, reset std::cout to its initial
streambuf object, and delete the memo_streambuf object:
void someotherfunc()
{
delete std::cout.rdbuf(savebuf);
}
Technical Details
Message-ID:
<m2fyyb34t0.fsf@madbox2.local>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<424ad7c1$1@newsgroups.borland.com> <424afb34$1@newsgroups.borland.com> <opsoid7o10ajtsdq@news.noeatingpuzzlepieces> <424c24fa@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,70264b2d092f1bae
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: cout <<"HELP" << endl; How to redirect all cout to a Memo ?
References: <424ad7c1$1@newsgroups.borland.com> <424afb34$1@newsgroups.borland.com>
<opsoid7o10ajtsdq@news.noeatingpuzzlepieces>
<424c24fa@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Thu, 31 Mar 2005 23:04:43 +0200
Message-ID: <m2fyyb34t0.fsf@madbox2.local>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:uewQaQeEUcDkZyQ2VLj1X4vd6Ys=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 217.162.1.33
X-Trace: newsgroups.borland.com 1112302945 217.162.1.33 (31 Mar 2005 13:02:25 -0800)
Lines: 72
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail