# HG changeset patch # User jwe # Date 954492650 0 # Node ID 25e84fcef38a3cbfe347cf1c86f4ac2c9171ddb5 # Parent c2305b99fbd78006f8da06ffab9e3a9e69c016fc [project @ 2000-03-31 08:50:48 by jwe] diff --git a/scripts/general/postpad.m b/scripts/general/postpad.m --- a/scripts/general/postpad.m +++ b/scripts/general/postpad.m @@ -33,8 +33,8 @@ usage ("postpad (x, l) or postpad (x, l, c)"); endif - if (! is_vector (x)) - error ("first argument must be a vector"); + if (! is_matrix (x)) + error ("first argument must be a vector or matrix"); elseif (! is_scalar (l)) error ("second argument must be a scaler"); endif @@ -43,17 +43,18 @@ error ("second argument must be non-negative"); endif - lx = length (x); - - if (lx >= l) - y = x(1:l); + [nr, nc] = size (x); + if (nr == 1) + if (nc >= l) + y = x(1:l); + else + y = [x, c*ones(1,l-nc)]; + endif else - if (rows (x) > 1) - tmp = c * ones (l-lx, 1); - y = [x; tmp]; + if (nr >= l) + y = x(1:l,:); else - tmp = c * ones (1, l-lx); - y = [x, tmp]; + y = [x ; c*ones(l-nr,nc)]; endif endif diff --git a/scripts/general/prepad.m b/scripts/general/prepad.m --- a/scripts/general/prepad.m +++ b/scripts/general/prepad.m @@ -27,6 +27,8 @@ ## ## If @code{length (@var{x}) > @var{l}}, elements from the beginning (end) of ## @var{x} are removed until a vector of length @var{l} is obtained. +## +## If @var{x} is a matrix, elements are prepended or removed from each row. ## @end deftypefn ## Author: Tony Richardson @@ -40,8 +42,8 @@ usage ("prepad (x, l) or prepad (x, l, c)"); endif - if (! is_vector (x)) - error ("first argument must be a vector"); + if (! is_matrix (x)) + error ("first argument must be a vector or matrix"); elseif (! is_scalar (l)) error ("second argument must be a scaler"); endif @@ -50,17 +52,18 @@ error ("second argument must be non-negative"); endif - lx = length (x); - - if (lx >= l) - y = x(lx-l+1:lx); + [nr, nc] = size (x); + if (nr == 1) + if (nc >= l) + y = x(nc-l+1:nc); + else + y = [c*ones(1,l-nc), x]; + endif else - if (rows (x) > 1) - tmp = c * ones (l-lx, 1); - y = [tmp; x]; + if (nr >= l) + y = x(nr-l+1:nr,:); else - tmp = c * ones (1, l-lx); - y = [tmp, x]; + y = [c*ones(l-nr,nc); x]; endif endif diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,24 @@ +2000-03-31 John W. Eaton + + * oct-fstrm.cc (octave_fstream::do_close): New function. + * oct-stdstrm.cc (octave_istdiostream::do_close): Ditto. + (octave_ostdiostream::do_close): Ditto. + * c-file-ptr-stream.cc (c_file_ptr_buf::close): Ditto. + (i_c_file_ptr_stream::close): Ditto. + (o_c_file_ptr_stream::close): Ditto. + * oct-prcstrm.cc (octave_iprocstream::do_close): Ditto. + (octave_oprocstream::do_close): Ditto. + (octave_iprocstram::~octave_iprocstram): Call do_close here. + (octave_iprocstram::~octave_oprocstram): Likewise. + + * oct-stream.h (octave_base_stream::do_close): New virtual function. + (octave_base_stream::close): If stream is open, call do_close. + + * c-file-ptr-stream.cc (c_file_ptr_buf::flush): New function. + (c_file_ptr_buf::~c_file_ptr_buf): Use it. + (c_file_ptr_buf::overflow): Ditto. + (c_file_ptr_buf::sync): Ditto. + 2000-03-30 John W. Eaton * oct-procbuf.cc (octave_procbuf::open): Make output streams line 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,7 @@ c_file_ptr_buf::~c_file_ptr_buf (void) { - if (f) - fflush (f); + flush (); } // XXX FIXME XXX -- I'm sure there is room for improvement here... @@ -54,7 +53,7 @@ c_file_ptr_buf::overflow (int c) { if (f) - return (c != EOF) ? fputc (c, f) : fflush (f); + return (c != EOF) ? fputc (c, f) : flush (); else return EOF; } @@ -141,9 +140,38 @@ int c_file_ptr_buf::sync (void) { + flush (); + return 0; } +int +c_file_ptr_buf::flush (void) +{ + return f ? fflush (f) : EOF; +} + +int +c_file_ptr_buf::close (void) +{ + if (f) + return fclose (f); +} + +void +i_c_file_ptr_stream::close (void) +{ + if (buf) + buf->close (); +} + +void +o_c_file_ptr_stream::close (void) +{ + if (buf) + buf->close (); +} + /* ;;; Local Variables: *** ;;; mode: C++ *** 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 @@ -61,6 +61,10 @@ int sync (void); + int flush (void); + + int close (void); + protected: FILE *f; @@ -78,6 +82,8 @@ c_file_ptr_buf *rdbuf (void) { return buf; } + void close (void); + private: c_file_ptr_buf *buf; @@ -95,6 +101,8 @@ c_file_ptr_buf *rdbuf (void) { return buf; } + void close (void); + private: c_file_ptr_buf *buf; diff --git a/src/oct-fstrm.cc b/src/oct-fstrm.cc --- a/src/oct-fstrm.cc +++ b/src/oct-fstrm.cc @@ -102,6 +102,12 @@ return fs.eof (); } +void +octave_fstream::do_close (void) +{ + fs.close (); +} + std::istream * octave_fstream::input_stream (void) { diff --git a/src/oct-fstrm.h b/src/oct-fstrm.h --- a/src/oct-fstrm.h +++ b/src/oct-fstrm.h @@ -54,6 +54,8 @@ bool eof (void) const; + void do_close (void); + // The name of the file. std::string name (void) const { return nm; } diff --git a/src/oct-prcstrm.cc b/src/oct-prcstrm.cc --- a/src/oct-prcstrm.cc +++ b/src/oct-prcstrm.cc @@ -51,6 +51,12 @@ octave_iprocstream::~octave_iprocstream (void) { + do_close (); +} + +void +octave_iprocstream::do_close (void) +{ if (fp) { pclose (fp); @@ -81,6 +87,12 @@ octave_oprocstream::~octave_oprocstream (void) { + do_close (); +} + +void +octave_oprocstream::do_close (void) +{ if (fp) { pclose (fp); diff --git a/src/oct-prcstrm.h b/src/oct-prcstrm.h --- a/src/oct-prcstrm.h +++ b/src/oct-prcstrm.h @@ -25,6 +25,9 @@ #include "oct-stdstrm.h" +// XXX FIXME XXX -- why don't these classes use iprocstream and +// oprocstream, which in turn use the octave_procbuf class? + class octave_iprocstream : public octave_istdiostream { @@ -39,6 +42,8 @@ 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); @@ -66,6 +71,8 @@ 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 @@ -105,6 +105,13 @@ delete is; } +void +octave_istdiostream::do_close (void) +{ + if (is) + is->close (); +} + octave_stream octave_ostdiostream::create (const std::string& n, FILE *f, std::ios::openmode arg_md, @@ -127,6 +134,13 @@ delete os; } +void +octave_ostdiostream::do_close (void) +{ + if (os) + os->close (); +} + /* ;;; Local Variables: *** ;;; mode: C++ *** diff --git a/src/oct-stdstrm.h b/src/oct-stdstrm.h --- a/src/oct-stdstrm.h +++ b/src/oct-stdstrm.h @@ -94,7 +94,8 @@ std::ostream *output_stream (void) { return 0; } // XXX FIXME XXX -- should not have to cast away const here. - c_file_ptr_buf *rdbuf (void) const { return is ? is->rdbuf () : 0; } + c_file_ptr_buf *rdbuf (void) const + { return is ? (const_cast (is))->rdbuf () : 0; } bool bad (void) const { return is ? is->bad () : true; } @@ -104,6 +105,8 @@ is->clear (); } + void do_close (void); + protected: i_c_file_ptr_stream *is; @@ -154,6 +157,8 @@ os->clear (); } + void do_close (void); + protected: o_c_file_ptr_stream *os; diff --git a/src/oct-stream.h b/src/oct-stream.h --- a/src/oct-stream.h +++ b/src/oct-stream.h @@ -364,7 +364,16 @@ bool is_open (void) const { return open_state; } - void close (void) { open_state = false; } + virtual void do_close (void) { } + + void close (void) + { + if (is_open ()) + { + open_state = false; + do_close (); + } + } int file_number (void); diff --git a/src/procstream.h b/src/procstream.h --- a/src/procstream.h +++ b/src/procstream.h @@ -73,7 +73,7 @@ iprocstream (void) : procstreambase () { } iprocstream (const char *name, int mode = std::ios::in) - : procstreambase(name, mode) { } + : procstreambase (name, mode) { } ~iprocstream (void) { } @@ -100,7 +100,7 @@ ~oprocstream (void) { } void open (const char *name, int mode = std::ios::out) - { procstreambase::open(name, mode); } + { procstreambase::open (name, mode); } private: @@ -117,12 +117,12 @@ procstream (void) : procstreambase () { } procstream (const char *name, int mode) - : procstreambase(name, mode) { } + : procstreambase (name, mode) { } ~procstream (void) { } void open (const char *name, int mode) - { procstreambase::open(name, mode); } + { procstreambase::open (name, mode); } private: