std_fstream.h (basic_filebuf::sync): Hoist unconditional flush on lower-layer handle to here...

* include/std/std_fstream.h (basic_filebuf::sync): Hoist
	unconditional flush on lower-layer handle to here...
	* include/bits/fstream.tcc (basic_filebuf::_M_really_overflow):
	...from here.  Optimize remaining _M_file.sync() call pattern.
	* testsuite/27_io/narrow_stream_objects.cc (test04): New test.
	(test05): Likewise.

From-SVN: r52699
This commit is contained in:
Loren J. Rittle 2002-04-24 00:33:28 +00:00 committed by Loren J. Rittle
parent b602511f62
commit 9385d9cb0d
4 changed files with 56 additions and 7 deletions

View File

@ -1,3 +1,12 @@
2002-04-23 Loren J. Rittle <ljrittle@acm.org>
* include/std/std_fstream.h (basic_filebuf::sync): Hoist
unconditional flush on lower-layer handle to here...
* include/bits/fstream.tcc (basic_filebuf::_M_really_overflow):
...from here. Optimize remaining _M_file.sync() call pattern.
* testsuite/27_io/narrow_stream_objects.cc (test04): New test.
(test05): Likewise.
2002-04-23 Jason Merrill <jason@redhat.com>
* include/bits/fstream.tcc (basic_filebuf::seekoff): Fix for

View File

@ -476,16 +476,20 @@ namespace std
__elen, __plen);
// Convert pending sequence to external representation, output.
// If eof, then just attempt sync.
if (!traits_type::eq_int_type(__c, traits_type::eof()))
{
char_type __pending = traits_type::to_char_type(__c);
_M_convert_to_external(&__pending, 1, __elen, __plen);
}
// Last, sync internal and external buffers.
// NB: Need this so that external byte sequence reflects
// internal buffer plus pending sequence.
if (__elen == __plen && !_M_file.sync())
// User code must flush when switching modes (thus don't sync).
if (__elen == __plen)
{
_M_set_indeterminate();
__ret = traits_type::not_eof(__c);
}
}
else if (!_M_file.sync())
{
_M_set_indeterminate();
__ret = traits_type::not_eof(__c);

View File

@ -204,14 +204,16 @@ namespace std
// Make sure that the internal buffer resyncs its idea of
// the file position with the external file.
if (__testput && !_M_file.sync())
if (__testput)
{
// Need to restore current position after the write.
off_type __off = _M_out_cur - _M_out_end;
_M_really_overflow();
_M_really_overflow(); // _M_file.sync() will be called within
if (__off)
_M_file.seekoff(__off, ios_base::cur);
}
else
_M_file.sync();
_M_last_overflowed = false;
return 0;
}

View File

@ -113,6 +113,38 @@ void test03()
cout << "i == " << i << endl;
}
// Interactive test, to be exercised as follows:
// assign stderr to stdout in shell command line,
// pipe stdout to cat process and/or redirect stdout to file.
// "hello fine world\n" should be written to stdout in proper order.
// This is a version of the scott snyder test taken from:
// http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00108.html
void test04()
{
using namespace std;
cout << "hello ";
cout.flush ();
cerr << "fine ";
cerr.flush ();
cout << "world" << endl;
cout.flush ();
}
// Interactive test, to be exercised as follows:
// run test under truss(1) or strace(1). Look at
// size and pattern of write system calls.
// Should be 2 or 3 write(1,[...]) calls when run interactively
// depending upon buffering mode enforced.
void test05()
{
std::cout << "hello" << ' ' << "world" <<std::endl;
std::cout << "Enter your name: ";
std::string s;
std::cin >> s;
std::cout << "hello " << s << std::endl;
}
int
main()
{
@ -120,5 +152,7 @@ main()
// test02();
// test03();
// test04();
// test05();
return 0;
}