changeset 3716:08fe5f74c7d4

[project @ 2000-09-07 04:59:03 by jwe]
author jwe
date Thu, 07 Sep 2000 04:59:04 +0000
parents e5ff21d2bac6
children e3501697c995
files src/ChangeLog src/c-file-ptr-stream.cc src/c-file-ptr-stream.h src/oct-prcstrm.cc src/oct-prcstrm.h src/oct-stdstrm.cc src/oct-stdstrm.h src/oct-stream.cc src/utils.cc
diffstat 9 files changed, 80 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/ChangeLog	Thu Sep 07 04:59:04 2000 +0000
@@ -1,3 +1,23 @@
+2000-09-06  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* utils.cc (FERRNO): New function (currently commented out).
+
+	* c-file-ptr-stream.cc (c_file_ptr_buf::close): Call flush here.
+	(c_file_ptr_buf::~c_file_ptr_buf): Not here.
+	* c-file-ptr-stream.h (c_fie_ptr_buf::close_fcn): New typedef.
+	(c_file_ptr_buf::cf): New data member.  Add default constructor arg.
+	(class c_file_ptr_buf): Derive from filebuf, not streambuf.
+	(i_c_file_ptr_stream, o_c_file_ptr_stream): Handle close function here.
+	* oct-procstrm.cc (octave_iprocstream, octave_oprocstream): Likewise.
+	(octave_iprocstream::do_close, octave_oprocstream::do_close): Delete.
+	* oct-stdstrm.cc (octave_base_stdiostream::~octave_base_stdiostream):
+	Don't do anything.
+	(octave_istdiostream::create): Handle close function here.
+	(octave_istdiostream::octave_istdiostream): Likewise.
+	(octave_ostdiostream::create): Likewise.
+	(octave_ostdiostream::octave_ostdiostream): Likewise.
+	(class octave_base_stdiostream): Don't cache FILE pointer here.
+
 2000-09-01  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* syscalls.cc (Ffcntl): Don't assume that the file id passed in is
--- a/src/c-file-ptr-stream.cc	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/c-file-ptr-stream.cc	Thu Sep 07 04:59:04 2000 +0000
@@ -44,8 +44,6 @@
 
 c_file_ptr_buf::~c_file_ptr_buf (void)
 {
-  flush ();
-
   close ();
 }
 
@@ -158,9 +156,11 @@
 {
   int retval = -1;
 
+  flush ();
+
   if (f)
     {
-      retval = fclose (f);
+      retval = cf (f);
       f = 0;
     }
 
--- a/src/c-file-ptr-stream.h	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/c-file-ptr-stream.h	Thu Sep 07 04:59:04 2000 +0000
@@ -31,13 +31,16 @@
 #include <stdio.h>
 
 class
-c_file_ptr_buf : public std::streambuf
+c_file_ptr_buf : public std::filebuf
 {
 public:
 
+  typedef int (*close_fcn) (FILE *);
+
   FILE* stdiofile (void) const { return f; }
 
-  c_file_ptr_buf (FILE *f_arg) : std::streambuf (), f (f_arg) { }
+  c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = ::fclose)
+    : std::filebuf (f_arg ? fileno (f_arg) : -1), f (f_arg), cf (cf_arg) { }
 
   ~c_file_ptr_buf (void);
 
@@ -68,6 +71,8 @@
 protected:
 
   FILE *f;
+
+  close_fcn cf;
 };
 
 class
@@ -75,8 +80,8 @@
 {
 public:
 
-  i_c_file_ptr_stream (FILE* f)
-    : std::istream (), buf (new c_file_ptr_buf (f)) { init (buf); }
+  i_c_file_ptr_stream (FILE* f, c_file_ptr_buf::close_fcn cf = ::fclose)
+    : std::istream (), buf (new c_file_ptr_buf (f, cf)) { init (buf); }
 
   ~i_c_file_ptr_stream (void) { delete buf; buf = 0; }
 
@@ -94,8 +99,8 @@
 {
 public:
 
-  o_c_file_ptr_stream (FILE* f)
-    : std::ostream (), buf (new c_file_ptr_buf (f)) { init (buf); }
+  o_c_file_ptr_stream (FILE* f, c_file_ptr_buf::close_fcn cf = ::fclose)
+    : std::ostream (), buf (new c_file_ptr_buf (f, cf)) { init (buf); }
 
   ~o_c_file_ptr_stream (void) { delete buf; buf = 0; }
 
--- a/src/oct-prcstrm.cc	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/oct-prcstrm.cc	Thu Sep 07 04:59:04 2000 +0000
@@ -38,15 +38,9 @@
 octave_iprocstream::octave_iprocstream (const std::string& n,
 					std::ios::openmode arg_md,
 					oct_mach_info::float_format flt_fmt)
-  : octave_istdiostream (n, 0, arg_md, flt_fmt)
+  : octave_istdiostream (n, ::popen (n.c_str (), "r"), ::pclose,
+			 arg_md, flt_fmt)
 {
-  fp = popen (n.c_str (), "r");
-
-  if (fp)
-    {
-      delete is;
-      is = new i_c_file_ptr_stream (fp);
-    }
 }
 
 octave_iprocstream::~octave_iprocstream (void)
@@ -54,16 +48,6 @@
   do_close ();
 }
 
-void
-octave_iprocstream::do_close (void)
-{
-  if (fp)
-    {
-      pclose (fp);
-      fp = 0;
-    }
-}
-
 octave_stream
 octave_oprocstream::create (const std::string& n, std::ios::openmode arg_md,
 			    oct_mach_info::float_format flt_fmt)
@@ -74,15 +58,9 @@
 octave_oprocstream::octave_oprocstream (const std::string& n,
 					std::ios::openmode arg_md,
 					oct_mach_info::float_format flt_fmt)
-  : octave_ostdiostream (n, 0, arg_md, flt_fmt)
+  : octave_ostdiostream (n, ::popen (n.c_str (), "w"), ::pclose,
+			 arg_md, flt_fmt)
 {
-  fp = popen (n.c_str (), "w");
-
-  if (fp)
-    {
-      delete os;
-      os = new o_c_file_ptr_stream (fp);
-    }
 }
 
 octave_oprocstream::~octave_oprocstream (void)
@@ -90,16 +68,6 @@
   do_close ();
 }
 
-void
-octave_oprocstream::do_close (void)
-{
-  if (fp)
-    {
-      pclose (fp);
-      fp = 0;
-    }
-}
-
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
--- a/src/oct-prcstrm.h	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/oct-prcstrm.h	Thu Sep 07 04:59:04 2000 +0000
@@ -42,8 +42,6 @@
   create (const std::string& n, std::ios::openmode arg_md = std::ios::in,
 	  oct_mach_info::float_format flt_fmt = oct_mach_info::native);
 
-  void do_close (void);
-
 protected:
 
   ~octave_iprocstream (void);
@@ -71,8 +69,6 @@
   create (const std::string& n, std::ios::openmode arg_md = std::ios::out,
 	  oct_mach_info::float_format flt_fmt = oct_mach_info::native);
 
-  void do_close (void);
-
 protected:
 
   ~octave_oprocstream (void);
--- a/src/oct-stdstrm.cc	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/oct-stdstrm.cc	Thu Sep 07 04:59:04 2000 +0000
@@ -28,15 +28,6 @@
 
 #include "oct-stdstrm.h"
 
-octave_base_stdiostream::~octave_base_stdiostream (void)
-{
-  if (fp)
-    {
-      fclose (fp);
-      fp = 0;
-    }
-}
-
 // Position a stream at OFFSET relative to ORIGIN.
 
 int
@@ -85,19 +76,21 @@
 
 octave_stream
 octave_istdiostream::create (const std::string& n, FILE *f,
+			     c_file_ptr_buf::close_fcn cf,
 			     std::ios::openmode arg_md,
 			     oct_mach_info::float_format flt_fmt)
 {
-  return octave_stream (new octave_istdiostream (n, f, arg_md, flt_fmt));
+  return octave_stream (new octave_istdiostream (n, f, cf, arg_md, flt_fmt));
 }
 
 octave_istdiostream::octave_istdiostream (const std::string& n, FILE *f,
+					  c_file_ptr_buf::close_fcn cf,
 					  std::ios::openmode arg_md,
 					  oct_mach_info::float_format flt_fmt)
-  : octave_base_stdiostream (n, f, arg_md, flt_fmt), is (0)
+  : octave_base_stdiostream (n, arg_md, flt_fmt), is (0)
 {
   if (f)
-    is = new i_c_file_ptr_stream (f);
+    is = new i_c_file_ptr_stream (f, cf);
 }
 
 octave_istdiostream::~octave_istdiostream (void)
@@ -114,19 +107,21 @@
 
 octave_stream
 octave_ostdiostream::create (const std::string& n, FILE *f,
+			     c_file_ptr_buf::close_fcn cf = ::fclose,
 			     std::ios::openmode arg_md,
 			     oct_mach_info::float_format flt_fmt)
 {
-  return octave_stream (new octave_ostdiostream (n, f, arg_md, flt_fmt));
+  return octave_stream (new octave_ostdiostream (n, f, cf, arg_md, flt_fmt));
 }
 
 octave_ostdiostream::octave_ostdiostream (const std::string& n, FILE *f,
+					  c_file_ptr_buf::close_fcn cf,
 					  std::ios::openmode arg_md,
 					  oct_mach_info::float_format flt_fmt)
-  : octave_base_stdiostream (n, f, arg_md, flt_fmt), os (0)
+  : octave_base_stdiostream (n, arg_md, flt_fmt), os (0)
 {
   if (f)
-    os = new o_c_file_ptr_stream (f);
+    os = new o_c_file_ptr_stream (f, cf);
 }
 
 octave_ostdiostream::~octave_ostdiostream (void)
--- a/src/oct-stdstrm.h	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/oct-stdstrm.h	Thu Sep 07 04:59:04 2000 +0000
@@ -32,10 +32,10 @@
 public:
 
   octave_base_stdiostream
-    (const std::string& n, FILE *f,
+    (const std::string& n,
      std::ios::openmode arg_md = std::ios::in|std::ios::out,
      oct_mach_info::float_format flt_fmt = oct_mach_info::native)
-    : octave_base_stream (arg_md, flt_fmt), nm (n), fp (f) { }
+    : octave_base_stream (arg_md, flt_fmt), nm (n) { }
 
   // Position a stream at OFFSET relative to ORIGIN.
 
@@ -59,9 +59,7 @@
 
   std::string nm;
 
-  FILE *fp;
-
-  ~octave_base_stdiostream (void);
+  ~octave_base_stdiostream (void) { }
 
   // No copying!
 
@@ -76,12 +74,14 @@
 public:
 
   octave_istdiostream (const std::string& n, FILE *f = 0,
+		       c_file_ptr_buf::close_fcn cf = ::fclose,
 		       std::ios::openmode arg_md = std::ios::in,
 		       oct_mach_info::float_format flt_fmt =
 		       oct_mach_info::native);
 
   static octave_stream
   create (const std::string& n, FILE *f = 0,
+	  c_file_ptr_buf::close_fcn cf = ::fclose,
 	  std::ios::openmode arg_md = std::ios::in,
 	  oct_mach_info::float_format flt_fmt = oct_mach_info::native);
 
@@ -128,12 +128,14 @@
 public:
 
   octave_ostdiostream (const std::string& n, FILE *f = 0,
+		       c_file_ptr_buf::close_fcn cf = ::fclose,
 		       std::ios::openmode arg_md = std::ios::out,
 		       oct_mach_info::float_format flt_fmt =
 		       oct_mach_info::native);
 
   static octave_stream
   create (const std::string& n, FILE *f = 0,
+	  c_file_ptr_buf::close_fcn cf = ::fclose,
 	  std::ios::openmode arg_md = std::ios::out,
 	  oct_mach_info::float_format flt_fmt = oct_mach_info::native);
 
--- a/src/oct-stream.cc	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/oct-stream.cc	Thu Sep 07 04:59:04 2000 +0000
@@ -894,6 +894,7 @@
   std::istream *is = input_stream ();
   std::ostream *os = output_stream ();
 
+  // XXX FIXME XXX -- there must be a better way...
   int i_fid = is ? ((std::filebuf *) (is->rdbuf ()))->fd () : -1;
   int o_fid = os ? ((std::filebuf *) (os->rdbuf ()))->fd () : -1;
 
--- a/src/utils.cc	Fri Sep 01 05:52:57 2000 +0000
+++ b/src/utils.cc	Thu Sep 07 04:59:04 2000 +0000
@@ -24,6 +24,7 @@
 #include <config.h>
 #endif
 
+#include <cerrno>
 #include <climits>
 #include <cstring>
 
@@ -566,6 +567,29 @@
   return retval;
 }
 
+#if 0
+
+// Octave could use some way to access the value of ERRNO, but this is
+// probably not the best interface, so don't depend on it...
+
+DEFUN (ERRNO, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {@var{system_error_number}} errno ()\n\
+Return the current value of the system-dependent variable errno.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  if (args.length () == 0)
+    retval = static_cast<double> (errno);
+  else
+    print_usage ("errno");
+
+  return retval;
+}
+
+#endif
+
 static void
 warn_old_style_preference (bool val, const std::string& sval)
 {