TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: David Nugent
from: Greg Newton
date: 1995-04-09 21:47:18
subject: C++ Streams as Filters?

G'day David,

Replying to a message of David Nugent to Greg Newton:

 DN> Ok. This isn't what I'd call a "filter" though. A filter, in the
 DN> classic sense, is one that takes input from stdin, does something to
 DN> it, and sends output to stdout.

Yes. My expression sometimes (always?) leaves something to be desired.

 DN> The iostreams class hierachy is there TO BE DERIVED FROM. If you look
 DN> at the ios side - where all the 'input' and 'output' come from and go
 DN> to, you'll see that these are just convenient front-ends which do
 DN> formatting and provide a type-safe front end for the actual streams.
 DN> The *real* work is done in streambuf, which is responsible for the
 DN> buffering and output itself.


 >> This example only refers to ostreams, but I want to do the
 >> same for istreams, too.

 >> Any suggestions (serious or otherwise :)  welcome.
 DN> ~
 >> PS.   Detabbing in itself isn't the problem, just an illustration of
 >> it.

 DN> Sure.

 DN> I had a reasonably good example of this before a disk crash recently.
 DN> Oh, now I come to think of it, you'll find it in the C_ECHO SNIPPETS.
 DN> It shows how to derive classes from streambufs for custom I/O source
 DN> and destinations - but the same principle applies.

 DN> With tab expansion, I probably wouldn't go that far though. If all you
 DN> want to do is to expand tabs, then you can do so by defining a custom
 DN> filebuf (ie. derive from filebuf), override the virtual function
 DN> there which handles the stream on a byte by byte basis (do_sputn()),
 DN> and expand tabs by keeping track of the 'current column'.

That's really the 'clue' that I think I've been looking for (do_sputn()). 
If I overload this function for a class derived from say, ofstream, am I
guaranteed that any output, whether using the member functions (write, etc)
or overloaded operators (<<) will use my function?

If so, what's the equivalent for istreams? do_sgetn()????

I've been stumbling around trying to find just such a 'magic bullet',
without much luck.

 DN> Of course, for this task it's just as easy to use istream::getline(),
 DN> expand the tabs and output to your ostream, but since this is a
 DN> learning excercise, we can ignore that. :-)

Tabs were actually a fair way from my mind when I came up with the
'problem' but were a convenient illustration.

 DN> BTW, you can use an ostream_withassign for your streambuf - this is a
 DN> predefined class (and is in fact the class used for cout). To
 DN> initialise it, just do:

 DN>     main()
 DN>     {
 DN>         ostream_withassign myout;

 DN>         myout=new my_class_derived_from_filebuf(1);

 DN> Strangely enough, that's why it's called "ostream_withassign". :)

 DN> The significance of the '1' here is to attach the stdout file handle
 DN> to the stream. You could just as easily pass a filename and mode to
 DN> an alternate constructor.

I've got to admit this bit went by me (this time... hopefully the light
bulb will come on soon).

 DN> Hope this helps,

Thanks.  Experimentation is in order now, I reckon.

Cheers,
Greg |;^)

--- FleetStreet 1.02 #429
* Origin: (3:639/666)
SEEN-BY: 50/99 620/243 623/630 632/312 348 998 633/371 634/384 635/301 502
SEEN-BY: 635/503 541 544 636/100 639/100 666 711/401 409 410 430 510 807 808
SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 639/666 100 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.