Re: streambuf, yet again

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 20:48:47 -0000


"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c1ljgp$55f$1@chessie.cirr.com...
> John Harrison <john_andronicus@hotmail.com> spoke thus:
>
> > I can't think of any reasonable way to achieve that, but I could easily
be
> > wrong. But I am sure it would be better to code this so that you don't
much
> > care when overflow or xsputn is called. Why exactly does it matter?
>
> Well, this is a deficiency of the class I'm trying to wrap -
> TLSFile::Write() appends a '\n' to its argument before actually
> writing to the file. So I have to code around it, convince my boss to
> change that behavior (he seemed reluctant this morning), or just
> forget wrapping the class with a stream at all.

Hmm awkward. And some people claim that object orientation promotes reusable
code. I'd work on your boss if I were you.

What you can do is create a real buffer (instead of using the unbuffered
mode you are using now). See Josuttis for details. Then when the buffer is
full and overflow is called you scan the characters in the buffer for
newlines. If you find any you can call TLSFile::Write with the characters
before the newline and just drop the newline. You won't be able to empty the
buffer entirely, just the portion that is before the last newline, but that
isn't a problem. If when overflow is called and the buffer is full and there
isn't a single newline in it, then you are either going to have to output a
spurious newline, or you are going to have to expand the buffer.

>
> > Is your stream working now then? What was the problem? Before you were
> > complaining that overflow wasn't being called at all, not you are
> > complaining that its being called to often!
>
> Well, it kind of is. It turns out that the first time through the
> section of code in question, nothing happens. On subsequent trips
> through that section of code, however, things work as expected
> (although not as I would want - see above). Does that sound like a
> specific kind of mistake to you?
>

Sorry, it doesn't.

john