TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Greg Newton
from: David Nugent
date: 1995-04-15 19:38:28
subject: C++ Streams as Filters?

> 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()????

do_sgetn() is a streambuf (virtual) function, so you can override it, no
problem. i|o|iostreams are JUST FORMATTERS and a convenient front end to
shove data into a streambuf or extract data from one. They take input and
send out output, and sometimes forward special functions to the streambuf
class, but essentially, if you want to override anything, you have to do it
in streambuf.  The ios classes have no virtual functions to override, nor
do they need any.


 > 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).

What I'm saying is that an ostream_withassign is a neat, multipurpose
generic ostream which can use any sort of streambuf that you throw at it.
It has all of the characteristics of an ostream, except that - unlike an
ostream - it isn't an abstract base class, so you can actually create an
instance of one. Just assign any sort of streambuf you might derive and you
don't have to touch that side of the class hierachy, only the streambuf or
whatever type of streambuf you happen to be working on.

streambuf provides you byte-level access to the stream, direct access to
the buffers themselves - everything you want. Don't worry about fiddling
with ostreams, since that's the wrong place to do what you want to do.

  cheers,
  david

---
* Origin: Unique Computing, Melbourne, Australia (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 312 348 998 633/371 379 634/384 388
SEEN-BY: 635/301 502 503 541 544 727 636/100 639/100 711/401 409 410 430 510
SEEN-BY: 711/807 808 809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 632/103 348 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™.