OSDN Git Service

* include/std/std_fstream.h (basic_filebuf::sync): Hoist
authorljrittle <ljrittle@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 24 Apr 2002 00:33:28 +0000 (00:33 +0000)
committerljrittle <ljrittle@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 24 Apr 2002 00:33:28 +0000 (00:33 +0000)
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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/fstream.tcc
libstdc++-v3/include/std/std_fstream.h
libstdc++-v3/testsuite/27_io/narrow_stream_objects.cc

index bc89207..3e4eb3e 100644 (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
index 6019cd1..740d5e2 100644 (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);
index 0b20df1..ce3a48e 100644 (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;
       }
index c420449..3abcb07 100644 (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;
 }