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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@52699
138bc75d-0d04-0410-961f-
82ee72b054a4
+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
__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);
// 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;
}
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()
{
// test02();
// test03();
+ // test04();
+ // test05();
return 0;
}