diff options
author | Loren J. Rittle <ljrittle@acm.org> | 2002-04-24 00:33:28 +0000 |
---|---|---|
committer | Loren J. Rittle <ljrittle@gcc.gnu.org> | 2002-04-24 00:33:28 +0000 |
commit | 9385d9cb0d46686a89108fa6b82b75e4d36b6e0f (patch) | |
tree | c2e2fb49adde7393d30c2b4c38bf72c2cf4e18ef | |
parent | b602511f62e6f05166194ce08c1c02b21960930b (diff) | |
download | gcc-9385d9cb0d46686a89108fa6b82b75e4d36b6e0f.zip gcc-9385d9cb0d46686a89108fa6b82b75e4d36b6e0f.tar.gz gcc-9385d9cb0d46686a89108fa6b82b75e4d36b6e0f.tar.bz2 |
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
-rw-r--r-- | libstdc++-v3/ChangeLog | 9 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/fstream.tcc | 14 | ||||
-rw-r--r-- | libstdc++-v3/include/std/std_fstream.h | 6 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc | 34 |
4 files changed, 56 insertions, 7 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index bc89207..3e4eb3e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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 diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc index 6019cd1..740d5e2 100644 --- a/libstdc++-v3/include/bits/fstream.tcc +++ b/libstdc++-v3/include/bits/fstream.tcc @@ -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); diff --git a/libstdc++-v3/include/std/std_fstream.h b/libstdc++-v3/include/std/std_fstream.h index 0b20df1..ce3a48e 100644 --- a/libstdc++-v3/include/std/std_fstream.h +++ b/libstdc++-v3/include/std/std_fstream.h @@ -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; } diff --git a/libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc b/libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc index c420449..3abcb07 100644 --- a/libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc +++ b/libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc @@ -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; } |