473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

streambuf, yet again

Is there any way to prevent the streambuf from calling overflow or
xsputn unless I specifically flush the stream?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
4 1698

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
Is there any way to prevent the streambuf from calling overflow or
xsputn unless I specifically flush the stream?


Its ostream that calls overflow or xsputn.

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?

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!

john
Jul 22 '05 #2
John Harrison <jo*************@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.
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
John Harrison <jo*************@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
Jul 22 '05 #4
Hi,
John Harrison wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
Is there any way to prevent the streambuf from calling overflow or
xsputn unless I specifically flush the stream?

No. 'overflow()' is called when the stream buffer's buffer is full and
character is supposed to be dumped. Where should this one go? There are
several strategies how to deal with this though. The strategy used eg.
by the file streams is to dump the buffer to the file. Another strategy
is to increase the buffer's size and put the character there. ... or to
use a buffer which is not made accessible to 'std::streambuf' and which
is flushed in some form if a certain condition occures, eg. when a
newline is inserted.

'xsputn()' is quite similar but it is actually not really necessary to
override this function: the default behavior of 'xsputn()' is to process
the characters individually such that it is sufficient to override
'overflow()'. In this case the function is called with each character.
The function 'xsputn()' is mainly an optimization for cases where
processing sequences of characters can be done more efficiently.
Its ostream that calls overflow or xsputn.


No. Both 'overflow()' and 'xsputn()' are protected. They are called by
stream buffer functions. The former when the internal buffer is full
and the latter when 'sputn()' is called. Well, the latter is pretty
direct.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Chaplain | last post by:
Hi there. I'm writing a program that uses the Telnet protocol over TCP/IP sockets. Of course, that has no bearing here, so I'll rephrase that in Standard C++ :) In essense, I'm trying to...
3
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
5
by: Christopher Benson-Manica | last post by:
Yes, it's me again, with my dear, dear friends std::streambuf and std::ostream... On the bright side, the code compiles. On the gloomy side, I can't seem to get overflow() or xsputn() called... ...
9
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Raymond Martineau | last post by:
I have the following code segment for a class intended to split output between cout and a file: class SplitStream : public std::streambuf { std::streambuf *x; public: SplitStream() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.