changeset 11004:594adb99a25e

cache file id in octave_tstdiostream class
author John W. Eaton <jwe@octave.org>
date Mon, 20 Sep 2010 20:39:56 -0400
parents b1cfff739af5
children 0de4eff677d6
files src/ChangeLog src/file-io.cc src/oct-stdstrm.h src/oct-stream.cc src/oct-stream.h
diffstat 5 files changed, 106 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,24 @@
+2010-09-20  John W. Eaton  <jwe@octave.org>
+
+	Bug #31085:
+
+	* file-io.cc (do_stream_open): Use fopen, fileno, and gzdopen
+	instead of gzopen.  Pass integer file id to
+	octave_zstdiostream::create.
+
+	* oct-stdstrm.h (class octave_base_tstdiostream): Rename from
+	octave_tstdiostream.
+	(octave_tstdiostream::fnum): New data member.
+	(octave_tstdiostream::file_number): New member function.
+	(octave_tstdiostream::octave_tstdiostream): New arg, FID.
+	(octave_tstdiostream::create): Delete.
+	(class octave_stdiostream, class octave_zstdiostream): New
+	non-template classes derived from octave_tstdiostream to replace
+	typedefs.
+
+	* oct-stream.h (octave_base_stream::file_number): Now virtual
+	and const.  Return -1 in defaault implementation.
+
 2010-09-20  John W. Eaton  <jwe@octave.org>
 
 	* DLD-FUNCTIONS/regexp.cc (octregexp_list) [HAVE_PCRE]:
--- a/src/file-io.cc
+++ b/src/file-io.cc
@@ -492,10 +492,15 @@
                 {
                   tmode.erase (pos, 1);
 
-                  gzFile fptr = ::gzopen (fname.c_str (), tmode.c_str ());
+                  FILE *fptr = ::fopen (fname.c_str (), tmode.c_str ());
+
+                  int fd = ::fileno (fptr);
+
+                  gzFile gzf = ::gzdopen (fd, tmode.c_str ());
 
                   if (fptr)
-                    retval = octave_zstdiostream::create (fname, fptr, md, flt_fmt);
+                    retval = octave_zstdiostream::create (fname, gzf, fd,
+                                                          md, flt_fmt);
                   else
                     retval.error (gnulib::strerror (errno));
                 }
--- a/src/oct-stdstrm.h
+++ b/src/oct-stdstrm.h
@@ -33,25 +33,15 @@
 {
 public:
 
-  octave_tstdiostream (const std::string& n, FILE_T f = 0,
+  octave_tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
                        std::ios::openmode m = std::ios::in|std::ios::out,
                        oct_mach_info::float_format ff
                          = oct_mach_info::native_float_format (),
                        typename BUF_T::close_fcn cf = BUF_T::file_close)
     : octave_base_stream (m, ff), nm (n), md (m),
-      s (f ? new STREAM_T (f, cf) : 0)
+      s (f ? new STREAM_T (f, cf) : 0), fnum (fid)
   { }
 
-  static octave_stream
-  create (const std::string& n, FILE_T f = 0,
-          std::ios::openmode m = std::ios::in|std::ios::out,
-          oct_mach_info::float_format ff
-            = oct_mach_info::native_float_format (),
-          typename BUF_T::close_fcn cf = BUF_T::file_close)
-  {
-    return octave_stream (new octave_tstdiostream (n, f, m, ff, cf));
-  }
-
   // Position a stream at OFFSET relative to ORIGIN.
 
   int seek (long offset, int origin)
@@ -77,6 +67,8 @@
   BUF_T *rdbuf (void) const
     { return s ? (const_cast<STREAM_T *> (s))->rdbuf () : 0; }
 
+  int file_number (void) const { return fnum; }
+
   bool bad (void) const { return s ? s->bad () : true; }
 
   void clear (void) { if (s) s->clear (); }
@@ -91,6 +83,9 @@
 
   STREAM_T *s;
 
+  // The file number associated with this file.
+  int fnum;
+
   ~octave_tstdiostream (void) { delete s; }
 
 private:
@@ -102,11 +97,79 @@
   octave_tstdiostream& operator = (const octave_tstdiostream&);
 };
 
-typedef octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *> octave_stdiostream;
+class
+octave_stdiostream
+  : public octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *>
+{
+public:
+
+  octave_stdiostream (const std::string& n, FILE *f = 0,
+                      std::ios::openmode m = std::ios::in|std::ios::out,
+                      oct_mach_info::float_format ff
+                        = oct_mach_info::native_float_format (),
+                      c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
+    : octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *> (n, f, f ? ::fileno (f) : -1, m, ff, cf) { }
+
+  static octave_stream
+  create (const std::string& n, FILE *f = 0,
+          std::ios::openmode m = std::ios::in|std::ios::out,
+          oct_mach_info::float_format ff
+            = oct_mach_info::native_float_format (),
+          c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
+  {
+    return octave_stream (new octave_stdiostream (n, f, m, ff, cf));
+  }
+
+protected:
+
+  ~octave_stdiostream (void) { }
+
+private:
+
+  // No copying!
+
+  octave_stdiostream (const octave_stdiostream&);
+
+  octave_stdiostream& operator = (const octave_stdiostream&);
+};
 
 #ifdef HAVE_ZLIB
 
-typedef octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile> octave_zstdiostream;
+class
+octave_zstdiostream
+  : public octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile>
+{
+public:
+
+  octave_zstdiostream (const std::string& n, gzFile f = 0, int fid = 0,
+                       std::ios::openmode m = std::ios::in|std::ios::out,
+                       oct_mach_info::float_format ff
+                         = oct_mach_info::native_float_format (),
+                       c_zfile_ptr_buf::close_fcn cf = c_zfile_ptr_buf::file_close)
+    : octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile> (n, f, fid, m, ff, cf) { }
+
+  static octave_stream
+  create (const std::string& n, gzFile f = 0, int fid = 0,
+          std::ios::openmode m = std::ios::in|std::ios::out,
+          oct_mach_info::float_format ff
+            = oct_mach_info::native_float_format (),
+          c_zfile_ptr_buf::close_fcn cf = c_zfile_ptr_buf::file_close)
+  {
+    return octave_stream (new octave_zstdiostream (n, f, fid, m, ff, cf));
+  }
+
+protected:
+
+  ~octave_zstdiostream (void) { }
+
+private:
+
+  // No copying!
+
+  octave_zstdiostream (const octave_zstdiostream&);
+
+  octave_zstdiostream& operator = (const octave_zstdiostream&);
+};
 
 #endif
 
--- a/src/oct-stream.cc
+++ b/src/oct-stream.cc
@@ -876,51 +876,6 @@
     }
 }
 
-int
-octave_base_stream::file_number (void)
-{
-  // Kluge alert!
-
-  if (name () == "stdin")
-    return 0;
-
-  if (name () == "stdout")
-    return 1;
-
-  if (name () == "stderr")
-    return 2;
-
-  int retval = -1;
-
-  std::istream *is = input_stream ();
-  std::ostream *os = output_stream ();
-
-  // There is no standard way to get the underlying file descriptor from 
-  // std::filebuf (nor in the GNU libstdc++-v3 implementation). We cache
-  // the descriptor in c_file_ptr_buf, and then extract it here.
-
-  c_file_ptr_buf *ibuf
-    = is ? dynamic_cast<c_file_ptr_buf *> (is->rdbuf ()) : 0;
-
-  c_file_ptr_buf *obuf
-    = os ? dynamic_cast<c_file_ptr_buf *> (os->rdbuf ()) : 0;
-
-  int i_fid = ibuf ? ibuf->file_number () : -1;
-  int o_fid = obuf ? obuf->file_number () : -1;
-
-  if (i_fid >= 0)
-    {
-      if (o_fid >= 0)
-        retval = (i_fid == o_fid) ? i_fid : -1;
-      else
-        retval = i_fid;
-    }
-  else if (o_fid >= 0)
-    retval = o_fid;
-
-  return retval;
-}
-
 void
 octave_base_stream::error (const std::string& msg)
 {
--- a/src/oct-stream.h
+++ b/src/oct-stream.h
@@ -383,7 +383,7 @@
         }
     }
 
-  int file_number (void);
+  virtual int file_number (void) const { return -1; }
 
   bool ok (void) const { return ! fail; }