# HG changeset patch # User jwe # Date 968302744 0 # Node ID 08fe5f74c7d42435d0207fb57c3772d8df782abe # Parent e5ff21d2bac6cfe8c2b61b36db56e6f7589e4fac [project @ 2000-09-07 04:59:03 by jwe] diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,23 @@ +2000-09-06 John W. Eaton + + * 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 * syscalls.cc (Ffcntl): Don't assume that the file id passed in is diff --git a/src/c-file-ptr-stream.cc b/src/c-file-ptr-stream.cc --- a/src/c-file-ptr-stream.cc +++ b/src/c-file-ptr-stream.cc @@ -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; } diff --git a/src/c-file-ptr-stream.h b/src/c-file-ptr-stream.h --- a/src/c-file-ptr-stream.h +++ b/src/c-file-ptr-stream.h @@ -31,13 +31,16 @@ #include 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; } diff --git a/src/oct-prcstrm.cc b/src/oct-prcstrm.cc --- a/src/oct-prcstrm.cc +++ b/src/oct-prcstrm.cc @@ -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++ *** diff --git a/src/oct-prcstrm.h b/src/oct-prcstrm.h --- a/src/oct-prcstrm.h +++ b/src/oct-prcstrm.h @@ -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); diff --git a/src/oct-stdstrm.cc b/src/oct-stdstrm.cc --- a/src/oct-stdstrm.cc +++ b/src/oct-stdstrm.cc @@ -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) diff --git a/src/oct-stdstrm.h b/src/oct-stdstrm.h --- a/src/oct-stdstrm.h +++ b/src/oct-stdstrm.h @@ -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); diff --git a/src/oct-stream.cc b/src/oct-stream.cc --- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -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; diff --git a/src/utils.cc b/src/utils.cc --- a/src/utils.cc +++ b/src/utils.cc @@ -24,6 +24,7 @@ #include #endif +#include #include #include @@ -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 (errno); + else + print_usage ("errno"); + + return retval; +} + +#endif + static void warn_old_style_preference (bool val, const std::string& sval) {