filebuf::fd()

Phil Edwards pedwards@disaster.jaj.com
Sun Dec 2 21:56:00 GMT 2001


I'd really like for us to decide on a way to conditionalize these extensions
(e.g., my posting on 29 Sept, item #2).

Anyhow, by popular-and-IMHO-slightly-misguided demand, here's a way for
filebuf's to return an underlying file descriptor, for the currently
default --enable-cstdio=stdio case.  I did not investigate the libio case.

Also some cleanups:
    1)  for is_open, make it clear that we're comparing pointers.
    2)  for the testcase file, when our non-standard signature was changed,
        the comment was not.  Also <cassert> is included in the hooks header.

Opinions?


Index: config/io/basic_file_stdio.h
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/config/io/basic_file_stdio.h,v
retrieving revision 1.1
diff -u -3 -p -r1.1 basic_file_stdio.h
--- config/io/basic_file_stdio.h	8 Aug 2001 02:48:58 -0000	1.1
+++ config/io/basic_file_stdio.h	3 Dec 2001 05:48:07 -0000
@@ -131,7 +131,11 @@ namespace std 
   
   template<typename _CharT>
     bool 
-    __basic_file<_CharT>::is_open() { return _M_cfile != 0; }
+    __basic_file<_CharT>::is_open() { return _M_cfile != NULL; }
+  
+  template<typename _CharT>
+    int 
+    __basic_file<_CharT>::fd() { return fileno(_M_cfile) ; }
   
   template<typename _CharT>
     __basic_file<_CharT>* 
Index: include/bits/basic_file.h
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/include/bits/basic_file.h,v
retrieving revision 1.11
diff -u -3 -p -r1.11 basic_file.h
--- include/bits/basic_file.h	23 Nov 2001 16:29:01 -0000	1.11
+++ include/bits/basic_file.h	3 Dec 2001 05:48:07 -0000
@@ -168,6 +168,9 @@ namespace std 
       bool 
       is_open();
 
+      int 
+      fd();
+
       // NB: Must match FILE specific jump table starting here--this
       // means all virtual functions starting with the dtor must match,
       // slot by slot. For glibc-based dystems, this means the _IO_FILE
Index: include/bits/fstream.tcc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/include/bits/fstream.tcc,v
retrieving revision 1.17
diff -u -3 -p -r1.17 fstream.tcc
--- include/bits/fstream.tcc	29 Oct 2001 19:29:29 -0000	1.17
+++ include/bits/fstream.tcc	3 Dec 2001 05:48:07 -0000
@@ -139,6 +139,14 @@ namespace std
     }
 
   template<typename _CharT, typename _Traits>
+    int
+    basic_filebuf<_CharT, _Traits>::
+    fd()
+    {
+      return _M_file->fd();
+    }
+
+  template<typename _CharT, typename _Traits>
     typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 
     basic_filebuf<_CharT, _Traits>::
     open(const char* __s, ios_base::openmode __mode)
Index: include/bits/std_fstream.h
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/include/bits/std_fstream.h,v
retrieving revision 1.12
diff -u -3 -p -r1.12 std_fstream.h
--- include/bits/std_fstream.h	2 Nov 2001 17:38:10 -0000	1.12
+++ include/bits/std_fstream.h	3 Dec 2001 05:48:07 -0000
@@ -97,6 +97,10 @@ namespace std 
       basic_filebuf(__c_file_type* __f, ios_base::openmode __mode, 
 		    int_type __s = static_cast<int_type>(BUFSIZ));
  
+      // Non-standard member:
+      int
+      fd();
+
       virtual 
       ~basic_filebuf() 
       { 
Index: testsuite/27_io/filebuf_members.cc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/testsuite/27_io/filebuf_members.cc,v
retrieving revision 1.10
diff -u -3 -p -r1.10 filebuf_members.cc
--- testsuite/27_io/filebuf_members.cc	7 Aug 2001 03:38:33 -0000	1.10
+++ testsuite/27_io/filebuf_members.cc	3 Dec 2001 05:48:07 -0000
@@ -1,4 +1,4 @@
-// Copyright (C) 2000 Free Software Foundation, Inc.
+// Copyright (C) 2001 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -24,7 +24,6 @@
 // the non-portable functionality in the libstdc++-v3 IO library
 
 #include <fstream>
-#include <cassert>
 #include <unistd.h>
 #include <fcntl.h>
 #include <testsuite_hooks.h>
@@ -32,9 +31,9 @@
 // verify that std::filebuf doesn't close files that it didn't open
 // when using the following std::filebuf ctor:
 //
-//      std::filebuf(int __fd,
-//                   const char* __unused,
-//                   ios_base::openmode __mode);
+//      std::filebuf(__c_file_type*  __f,
+//                   ios_base::openmode __mode,
+//                   int_type  __s);
 //
 // thanks to "George T. Talbot" <george@moberg.com> for uncovering
 // this bug/situation. 
@@ -78,10 +77,30 @@ test_01()
   return test;
 }
 
+int
+test_02()
+{
+  int first_fd = ::open(name_01, O_RDONLY);
+  VERIFY( first_fd != -1 );
+  FILE* first_file = ::fdopen(first_fd, "r");
+  VERIFY( first_file != NULL );
+  std::filebuf fb (first_file, std::ios_base::in);
+
+  int second_fd = fb.fd();
+
+  bool test = first_fd == second_fd;
+
+#ifdef DEBUG_ASSERT
+  assert(test);
+#endif
+
+  return test;
+}
 
 int
 main()
 {
   test_01();
+  test_02();
   return 0;
 }



More information about the Libstdc++ mailing list