# HG changeset patch # User John W. Eaton # Date 1285029596 14400 # Node ID 594adb99a25ed595c8440fb87d2735cd229da552 # Parent b1cfff739af5851a868390ae3f0944de98fef894 cache file id in octave_tstdiostream class diff -r b1cfff739af5 -r 594adb99a25e src/ChangeLog --- a/src/ChangeLog Mon Sep 20 18:47:52 2010 -0400 +++ b/src/ChangeLog Mon Sep 20 20:39:56 2010 -0400 @@ -1,3 +1,24 @@ +2010-09-20 John W. Eaton + + 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 * DLD-FUNCTIONS/regexp.cc (octregexp_list) [HAVE_PCRE]: diff -r b1cfff739af5 -r 594adb99a25e src/file-io.cc --- a/src/file-io.cc Mon Sep 20 18:47:52 2010 -0400 +++ b/src/file-io.cc Mon Sep 20 20:39:56 2010 -0400 @@ -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)); } diff -r b1cfff739af5 -r 594adb99a25e src/oct-stdstrm.h --- a/src/oct-stdstrm.h Mon Sep 20 18:47:52 2010 -0400 +++ b/src/oct-stdstrm.h Mon Sep 20 20:39:56 2010 -0400 @@ -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 (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 octave_stdiostream; +class +octave_stdiostream + : public octave_tstdiostream +{ +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 (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 octave_zstdiostream; +class +octave_zstdiostream + : public octave_tstdiostream +{ +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 (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 diff -r b1cfff739af5 -r 594adb99a25e src/oct-stream.cc --- a/src/oct-stream.cc Mon Sep 20 18:47:52 2010 -0400 +++ b/src/oct-stream.cc Mon Sep 20 20:39:56 2010 -0400 @@ -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 (is->rdbuf ()) : 0; - - c_file_ptr_buf *obuf - = os ? dynamic_cast (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) { diff -r b1cfff739af5 -r 594adb99a25e src/oct-stream.h --- a/src/oct-stream.h Mon Sep 20 18:47:52 2010 -0400 +++ b/src/oct-stream.h Mon Sep 20 20:39:56 2010 -0400 @@ -383,7 +383,7 @@ } } - int file_number (void); + virtual int file_number (void) const { return -1; } bool ok (void) const { return ! fail; }