Mercurial > hg > octave-nkf
changeset 4468:efb6301dae80
[project @ 2003-07-15 19:18:20 by jwe]
author | jwe |
---|---|
date | Tue, 15 Jul 2003 19:18:20 +0000 |
parents | 78046131cc16 |
children | 53ee020af847 |
files | scripts/ChangeLog scripts/io/printf.m scripts/io/puts.m src/ChangeLog src/file-io.cc src/oct-stream.cc src/oct-stream.h test/octave.test/io/io.exp |
diffstat | 8 files changed, 319 insertions(+), 261 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,5 +1,7 @@ 2003-07-15 John W. Eaton <jwe@bevo.che.wisc.edu> + * io/printf.m, io/puts.m: Delete. + * miscellaneous/dump_prefs.m: Delete define_all_return_values and default_return_value from the list. Add warn_undefined_return_values to the list.
deleted file mode 100644 --- a/scripts/io/printf.m +++ /dev/null @@ -1,40 +0,0 @@ -## Copyright (C) 1996, 1997 John W. Eaton -## -## This file is part of Octave. -## -## Octave is free software; you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2, or (at your option) -## any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, write to the Free -## Software Foundation, 59 Temple Place - Suite 330, Boston, MA -## 02111-1307, USA. - -## -*- texinfo -*- -## @deftypefn {Function File} {} printf (@var{template}, @dots{}) -## The @code{printf} function prints the optional arguments under the -## control of the template string @var{template} to the stream -## @code{stdout}. -## @end deftypefn -## @seealso{fprintf and sprintf} - -## Author: jwe - -function retval = printf (fmt, varargin) - - retval = -1; - - if (nargin > 0) - retval = fprintf (stdout, fmt, varargin{:}); - else - usage ("printf (fmt, ...)"); - endif - -endfunction
deleted file mode 100644 --- a/scripts/io/puts.m +++ /dev/null @@ -1,38 +0,0 @@ -## Copyright (C) 1996, 1997 John W. Eaton -## -## This file is part of Octave. -## -## Octave is free software; you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2, or (at your option) -## any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, write to the Free -## Software Foundation, 59 Temple Place - Suite 330, Boston, MA -## 02111-1307, USA. - -## -*- texinfo -*- -## @deftypefn {Function File} {} puts (@var{string}) -## Write a string to the standard output with no formatting. -## @end deftypefn -## @seealso{fputs, printf and fprintf} - -## Author: jwe - -function retval = puts (s) - - retval = -1; - - if (nargin == 1) - retval = fputs (stdout, s); - else - usage ("puts (string)"); - endif - -endfunction
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,26 @@ 2003-07-15 John W. Eaton <jwe@bevo.che.wisc.edu> + * oct-stream.cc (get_size, octave_base_stream::error, + octave_base_stream::do_gets, octave_base_stream::getl, + octave_base_stream::gets, octave_base_stream::do_scanf, + octave_base_stream::scanf, octave_base_stream::do_oscanf, + octave_base_stream::oscanf, do_printf_conv, + octave_base_stream::do_printf, octave_base_stream::printf, + octave_base_stream::puts, octave_base_stream::invalid_operation, + octave_stream::getl, octave_stream::gets, octave_stream::scanf, + octave_stream::oscanf, octave_stream::printf, octave_stream::puts, + octave_stream::invalid_stream_error): Handle name of calling + function for error messages as a std::string object. Change all + callers. + + * file-io.cc (Fprintf, Fputs): New functions, so we can get + function prefixes on error messages right. + (Ffgetl, Ffgets, Ffprintf, Fsprintf, Fputs, Ffscanf, Fsscanf): + Pass name of calling function to octave_stream method. + + * oct-stream.h (octave_base_stream::do_char_scanf, + octave_base_stream::do_real_scanf): Delete unused decls. + * load-save.cc (read_mat5_binary_element): Correctly read struct arrays.
--- a/src/file-io.cc +++ b/src/file-io.cc @@ -73,18 +73,21 @@ static octave_value stdout_file; static octave_value stderr_file; +static octave_stream stdin_stream; +static octave_stream stdout_stream; +static octave_stream stderr_stream; + void initialize_file_io (void) { - octave_stream stdin_stream = octave_istream::create (&std::cin, "stdin"); + stdin_stream = octave_istream::create (&std::cin, "stdin"); // This uses octave_stdout (see pager.h), not std::cout so that Octave's // standard output stream will pass through the pager. - octave_stream stdout_stream - = octave_ostream::create (&octave_stdout, "stdout"); + stdout_stream = octave_ostream::create (&octave_stdout, "stdout"); - octave_stream stderr_stream = octave_ostream::create (&std::cerr, "stderr"); + stderr_stream = octave_ostream::create (&std::cerr, "stderr"); stdin_file = octave_stream_list::insert (stdin_stream); stdout_file = octave_stream_list::insert (stdout_stream); @@ -227,6 +230,8 @@ If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ @end deftypefn") { + static std::string who = "fgetl"; + octave_value_list retval; retval(1) = 0; @@ -236,7 +241,7 @@ if (nargin == 1 || nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0), "fgetl"); + octave_stream os = octave_stream_list::lookup (args(0), who); if (! error_state) { @@ -245,7 +250,7 @@ bool err = false; - std::string tmp = os.getl (len_arg, err); + std::string tmp = os.getl (len_arg, err, who); if (! err) { @@ -255,7 +260,7 @@ } } else - print_usage ("fgetl"); + print_usage (who); return retval; } @@ -273,6 +278,8 @@ If there are no more characters to read, @code{fgets} returns @minus{}1.\n\ @end deftypefn") { + static std::string who = "fgets"; + octave_value_list retval; retval(1) = 0.0; @@ -282,7 +289,7 @@ if (nargin == 1 || nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0), "fgets"); + octave_stream os = octave_stream_list::lookup (args(0), who); if (! error_state) { @@ -291,7 +298,7 @@ bool err = false; - std::string tmp = os.gets (len_arg, err); + std::string tmp = os.gets (len_arg, err, who); if (! err) { @@ -301,7 +308,7 @@ } } else - print_usage ("fgets"); + print_usage (who); return retval; } @@ -641,7 +648,10 @@ written to the stream @var{fid} instead of @code{stdout}.\n\ @end deftypefn") { + static std::string who = "fprintf"; + octave_value retval = -1; + bool return_char_count = true; int nargin = args.length (); @@ -653,7 +663,7 @@ if (args(0).is_string ()) { - os = octave_stream_list::lookup (1, "fprintf"); + os = octave_stream_list::lookup (1, who); // For compatibility with Matlab, which does not return the // character count when behaving like printf (no file id @@ -664,11 +674,13 @@ else { fmt_n = 1; - os = octave_stream_list::lookup (args(0), "fprintf"); + os = octave_stream_list::lookup (args(0), who); } if (! error_state) { + std::string who; + if (args(fmt_n).is_string ()) { std::string fmt = args(fmt_n).string_value (); @@ -683,14 +695,14 @@ tmp_args(i-fmt_n-1) = args(i); } - retval = os.printf (fmt, tmp_args); + retval = os.printf (fmt, tmp_args, who); } else - ::error ("fprintf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } } else - print_usage ("fprintf"); + print_usage (who); if (return_char_count) return retval; @@ -698,25 +710,86 @@ return octave_value(); } +DEFUN (printf, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} printf (@var{fid}, @var{template}, @dots{})\n\ +Print optional arguments under the control of the template string\n\ +@var{template} to the stream @code{stdout}.\n\ +@end deftypefn\n\ +@seealso{fprintf and sprintf}") +{ + static std::string who = "printf"; + + octave_value retval = -1; + + int nargin = args.length (); + + if (nargin > 0) + { + if (args(0).is_string ()) + { + std::string fmt = args(0).string_value (); + + octave_value_list tmp_args; + + if (nargin > 1) + { + tmp_args.resize (nargin-1, octave_value ()); + + for (int i = 1; i < nargin; i++) + tmp_args(i-1) = args(i); + } + + retval = stdout_stream.printf (fmt, tmp_args, who); + } + else + ::error ("%s: format must be a string", who.c_str ()); + } + else + print_usage (who); + + return retval; +} + DEFUN (fputs, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ Write a string to a file with no formatting.\n\ @end deftypefn") { + static std::string who = "fputs"; + octave_value retval = -1; int nargin = args.length (); if (nargin == 2) { - octave_stream os = octave_stream_list::lookup (args(0), "fputs"); + octave_stream os = octave_stream_list::lookup (args(0), who); if (! error_state) - retval = os.puts (args(1)); + retval = os.puts (args(1), who); } else - print_usage ("fputs"); + print_usage (who); + + return retval; +} + +DEFUN (puts, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} puts (@var{string})\n\ +Write a string to the standard output with no formatting.\n\ +@end deftypefn") +{ + static std::string who = "puts"; + + octave_value retval = -1; + + if (args.length () == 1) + retval = stdout_stream.puts (args(0), who); + else + print_usage (who); return retval; } @@ -731,6 +804,8 @@ converted.\n\ @end deftypefn") { + static std::string who = "sprintf"; + octave_value_list retval; int nargin = args.length (); @@ -761,18 +836,18 @@ tmp_args(i-1) = args(i); } - retval(2) = os.printf (fmt, tmp_args); + retval(2) = os.printf (fmt, tmp_args, who); retval(1) = os.error (); retval(0) = ostr->str (); } else - ::error ("sprintf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } else - ::error ("sprintf: unable to create output buffer"); + ::error ("%s: unable to create output buffer", who.c_str ()); } else - print_usage ("sprintf"); + print_usage (who); return retval; } @@ -820,13 +895,15 @@ conversions is returned in @var{count}\n\ @end deftypefn") { + static std::string who = "fscanf"; + octave_value_list retval; int nargin = args.length (); if (nargin == 3 && args(2).is_string ()) { - octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); + octave_stream os = octave_stream_list::lookup (args(0), who); if (! error_state) { @@ -834,10 +911,10 @@ { std::string fmt = args(1).string_value (); - retval = os.oscanf (fmt); + retval = os.oscanf (fmt, who); } else - ::error ("fscanf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } } else @@ -847,7 +924,7 @@ if (nargin == 2 || nargin == 3) { - octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); + octave_stream os = octave_stream_list::lookup (args(0), who); if (! error_state) { @@ -863,18 +940,18 @@ if (! error_state) { - octave_value tmp = os.scanf (fmt, size, count); + octave_value tmp = os.scanf (fmt, size, count, who); retval(1) = count; retval(0) = tmp; } } else - ::error ("fscanf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } } else - print_usage ("fscanf"); + print_usage (who); } return retval; @@ -889,6 +966,8 @@ string is treated as an end-of-file condition.\n\ @end deftypefn") { + static std::string who = "sscanf"; + octave_value_list retval; int nargin = args.length (); @@ -907,16 +986,17 @@ { std::string fmt = args(1).string_value (); - retval = os.oscanf (fmt); + retval = os.oscanf (fmt, who); } else - ::error ("sscanf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } else - ::error ("sscanf: unable to create temporary input buffer"); + ::error ("%s: unable to create temporary input buffer", + who.c_str ()); } else - ::error ("sscanf: first argument must be a string"); + ::error ("%s: first argument must be a string", who.c_str ()); } else { @@ -945,7 +1025,7 @@ ? args(2).vector_value () : Array<double> (1, lo_ieee_inf_value ()); - octave_value tmp = os.scanf (fmt, size, count); + octave_value tmp = os.scanf (fmt, size, count, who); // XXX FIXME XXX -- is this the right thing to do? // Extract error message first, because getting @@ -958,16 +1038,17 @@ retval(0) = tmp; } else - ::error ("sscanf: format must be a string"); + ::error ("%s: format must be a string", who.c_str ()); } else - ::error ("sscanf: unable to create temporary input buffer"); + ::error ("%s: unable to create temporary input buffer", + who.c_str ()); } else - ::error ("sscanf: first argument must be a string"); + ::error ("%s: first argument must be a string", who.c_str ()); } else - print_usage ("sscanf"); + print_usage (who); } return retval;
--- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -81,7 +81,7 @@ } static int -get_size (double d, const char *warn_for) +get_size (double d, const std::string& who) { int retval = -1; @@ -93,20 +93,20 @@ retval = NINT (d); else ::error ("%s: negative value invalid as size specification", - warn_for); + who.c_str ()); } else retval = -1; } else - ::error ("%s: NaN is invalid as size specification", warn_for); + ::error ("%s: NaN is invalid as size specification", who.c_str ()); return retval; } static void get_size (const Array<double>& size, int& nr, int& nc, bool& one_elt_size_spec, - const char *warn_for) + const std::string& who) { nr = -1; nc = -1; @@ -133,17 +133,17 @@ if (! xisinf (dnr)) dnc = size (1); else - ::error ("%s: invalid size specification", warn_for); + ::error ("%s: invalid size specification", who.c_str ()); } else - ::error ("%s: invalid size specification", warn_for); + ::error ("%s: invalid size specification", who.c_str ()); if (! error_state) { - nr = get_size (dnr, warn_for); + nr = get_size (dnr, who); if (! error_state && dnc >= 0.0) - nc = get_size (dnc, warn_for); + nc = get_size (dnc, who); } } @@ -926,6 +926,13 @@ } void +octave_base_stream::error (const std::string& who, const std::string& msg) +{ + fail = true; + errmsg = who + msg; +} + +void octave_base_stream::clear (void) { fail = false; @@ -937,7 +944,7 @@ std::string octave_base_stream::do_gets (int max_len, bool& err, - bool strip_newline, const char *fcn) + bool strip_newline, const std::string& who) { std::string retval; @@ -984,33 +991,32 @@ else { err = true; - std::string msg = fcn; + if (is.eof () && char_count == 0) - msg.append (": at end of file"); + error (who, "at end of file"); else - msg.append (": read error"); - error (msg); + error (who, "read error"); } } else { err = true; - invalid_operation (fcn, "reading"); + invalid_operation (who, "reading"); } return retval; } std::string -octave_base_stream::getl (int max_len, bool& err) +octave_base_stream::getl (int max_len, bool& err, const std::string& who) { - return do_gets (max_len, err, true, "fgetl"); + return do_gets (max_len, err, true, who); } std::string -octave_base_stream::gets (int max_len, bool& err) +octave_base_stream::gets (int max_len, bool& err, const std::string& who) { - return do_gets (max_len, err, false, "fgets"); + return do_gets (max_len, err, false, who); } octave_value @@ -1373,7 +1379,7 @@ octave_value octave_base_stream::do_scanf (scanf_format_list& fmt_list, int nr, int nc, bool one_elt_size_spec, - int& conversion_count) + int& conversion_count, const std::string& who) { conversion_count = 0; @@ -1636,11 +1642,11 @@ break; case 'p': - error ("fscanf: unsupported format specifier"); + error ("%s: unsupported format specifier", who.c_str ()); break; default: - error ("fscanf: internal format error"); + error ("%s: internal format error", who.c_str ()); break; } @@ -1702,7 +1708,7 @@ // Skip to end of line. bool err; - do_gets (-1, err, false, "fscanf"); + do_gets (-1, err, false, who); } break; @@ -1710,7 +1716,7 @@ } else { - error ("fscanf: internal format error"); + error ("%s: internal format error", who.c_str ()); break; } @@ -1733,7 +1739,7 @@ octave_value octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, - int& conversion_count) + int& conversion_count, const std::string& who) { octave_value retval = Matrix (); @@ -1746,7 +1752,7 @@ scanf_format_list fmt_list (fmt); if (fmt_list.num_conversions () == -1) - ::error ("fscanf: invalid format specified"); + ::error ("%s: invalid format specified", who.c_str ()); else { int nr = -1; @@ -1754,22 +1760,22 @@ bool one_elt_size_spec; - get_size (size, nr, nc, one_elt_size_spec, "fscanf"); + get_size (size, nr, nc, one_elt_size_spec, who); if (! error_state) retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, - conversion_count); + conversion_count, who); } } else - invalid_operation ("fscanf", "reading"); + invalid_operation (who, "reading"); return retval; } bool octave_base_stream::do_oscanf (const scanf_format_elt *elt, - octave_value& retval) + octave_value& retval, const std::string& who) { bool quit = false; @@ -1888,18 +1894,18 @@ break; case 'p': - error ("fscanf: unsupported format specifier"); + error ("%s: unsupported format specifier", who.c_str ()); break; default: - error ("fscanf: internal format error"); + error ("%s: internal format error", who.c_str ()); break; } } if (ok () && is.fail ()) { - error ("fscanf: read error"); + error ("%s: read error", who.c_str ()); // XXX FIXME XXX -- is this the right thing to do? @@ -1908,7 +1914,7 @@ // Skip to end of line. bool err; - do_gets (-1, err, false, "fscanf"); + do_gets (-1, err, false, who); } } } @@ -1917,7 +1923,7 @@ } octave_value_list -octave_base_stream::oscanf (const std::string& fmt) +octave_base_stream::oscanf (const std::string& fmt, const std::string& who) { octave_value_list retval; @@ -1932,7 +1938,7 @@ int nconv = fmt_list.num_conversions (); if (nconv == -1) - ::error ("fscanf: invalid format specified"); + ::error ("%s: invalid format specified", who.c_str ()); else { is.clear (); @@ -1951,7 +1957,7 @@ { octave_value tmp; - quit = do_oscanf (elt, tmp); + quit = do_oscanf (elt, tmp, who); if (quit) break; @@ -1978,13 +1984,13 @@ elt = fmt_list.next (); - do_oscanf (elt, tmp); + do_oscanf (elt, tmp, who); } } } } else - invalid_operation ("fscanf", "reading"); + invalid_operation (who, "reading"); return retval; } @@ -2213,7 +2219,7 @@ template <class T> int do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, - int sa_2, T arg) + int sa_2, T arg, const std::string& who) { int retval = 0; @@ -2232,7 +2238,7 @@ break; default: - ::error ("fprintf: internal error handling format"); + ::error ("%s: internal error handling format", who.c_str ()); break; } @@ -2240,26 +2246,33 @@ } template int -do_printf_conv (std::ostream&, const char*, int, int, int, int); - -template int -do_printf_conv (std::ostream&, const char*, int, int, int, long); - +do_printf_conv (std::ostream&, const char*, int, int, int, int, + const std::string&); + +template int +do_printf_conv (std::ostream&, const char*, int, int, int, long, + const std::string&); + template int -do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int); - -template int -do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long); - -template int -do_printf_conv (std::ostream&, const char*, int, int, int, double); - -template int -do_printf_conv (std::ostream&, const char*, int, int, int, const char*); +do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, + const std::string&); + +template int +do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, + const std::string&); + +template int +do_printf_conv (std::ostream&, const char*, int, int, int, double, + const std::string&); + +template int +do_printf_conv (std::ostream&, const char*, int, int, int, const char*, + const std::string&); int octave_base_stream::do_printf (printf_format_list& fmt_list, - const octave_value_list& args) + const octave_value_list& args, + const std::string& who) { int retval = 0; @@ -2324,7 +2337,7 @@ if (val_cache) retval += do_printf_conv (os, fmt, nsa, sa_1, - sa_2, val.c_str ()); + sa_2, val.c_str (), who); else break; } @@ -2354,14 +2367,16 @@ ? (val < 0 ? "-Inf" : "Inf") : "NaN"; retval += do_printf_conv (os, tfmt.c_str (), - nsa, sa_1, sa_2, tval); + nsa, sa_1, sa_2, + tval, who); } else { tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); retval += do_printf_conv (os, tfmt.c_str (), - nsa, sa_1, sa_2, val); + nsa, sa_1, sa_2, + val, who); } } else @@ -2373,11 +2388,11 @@ if (elt->modifier == 'l') retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, - static_cast<long int> (val)); + static_cast<long int> (val), who); else retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, - static_cast<int> (val)); + static_cast<int> (val), who); } break; @@ -2386,11 +2401,12 @@ if (elt->modifier == 'l') retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, - static_cast<unsigned long int> (val)); + static_cast<unsigned long int> (val), + who); else retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, - static_cast<unsigned int> (val)); + static_cast<unsigned int> (val), who); } break; @@ -2398,11 +2414,12 @@ case 'g': case 'G': retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, - val); + val, who); break; default: - error ("fprintf: invalid format specifier"); + error ("%s: invalid format specifier", + who.c_str ()); return -1; break; } @@ -2414,13 +2431,13 @@ if (! os) { - error ("fprintf: write error"); + error ("%s: write error", who.c_str ()); break; } } else { - ::error ("fprintf: internal error handling format"); + ::error ("%s: internal error handling format", who.c_str ()); retval = -1; break; } @@ -2432,29 +2449,30 @@ } } else - invalid_operation ("fprintf", "writing"); + invalid_operation (who, "writing"); return retval; } int octave_base_stream::printf (const std::string& fmt, - const octave_value_list& args) + const octave_value_list& args, + const std::string& who) { int retval = 0; printf_format_list fmt_list (fmt); if (fmt_list.num_conversions () == -1) - ::error ("fprintf: invalid format specified"); + ::error ("%s: invalid format specified", who.c_str ()); else - retval = do_printf (fmt_list, args); + retval = do_printf (fmt_list, args, who); return retval; } int -octave_base_stream::puts (const std::string& s) +octave_base_stream::puts (const std::string& s, const std::string& who) { int retval = -1; @@ -2480,13 +2498,13 @@ if (os) retval = 0; else - error ("fputs: write error"); + error ("%s: write error", who.c_str ()); } else - error ("fputs: write error"); + error ("%s: write error", who.c_str ()); } else - invalid_operation ("fputs", "writing"); + invalid_operation (who, "writing"); return retval; } @@ -2513,12 +2531,11 @@ } void -octave_base_stream::invalid_operation (const char *op, const char *rw) +octave_base_stream::invalid_operation (const std::string& who, const char *rw) { - std::string msg = op; - msg.append (": stream not open for "); - msg.append (rw); - error (msg); + // Note that this is not ::error () ! + + error (who + ": stream not open for " + rw); } octave_stream::octave_stream (octave_base_stream *bs) @@ -2570,18 +2587,19 @@ } std::string -octave_stream::getl (int max_len, bool& err) +octave_stream::getl (int max_len, bool& err, const std::string& who) { std::string retval; - if (stream_ok ("getl")) - retval = rep->getl (max_len, err); + if (stream_ok (who)) + retval = rep->getl (max_len, err, who); return retval; } std::string -octave_stream::getl (const octave_value& tc_max_len, bool& err) +octave_stream::getl (const octave_value& tc_max_len, bool& err, + const std::string& who) { std::string retval; @@ -2594,27 +2612,28 @@ if (conv_err || max_len < 0) { err = true; - ::error ("fgetl: invalid maximum length specified"); + ::error ("%s: invalid maximum length specified", who.c_str ()); } else - retval = getl (max_len, err); + retval = getl (max_len, err, who); return retval; } std::string -octave_stream::gets (int max_len, bool& err) +octave_stream::gets (int max_len, bool& err, const std::string& who) { std::string retval; - if (stream_ok ("fgets")) - retval = rep->gets (max_len, err); + if (stream_ok (who)) + retval = rep->gets (max_len, err, who); return retval; } std::string -octave_stream::gets (const octave_value& tc_max_len, bool& err) +octave_stream::gets (const octave_value& tc_max_len, bool& err, + const std::string& who) { std::string retval; @@ -2627,10 +2646,10 @@ if (conv_err || max_len < 0) { err = true; - ::error ("fgets: invalid maximum length specified"); + ::error ("%s: invalid maximum length specified", who.c_str ()); } else - retval = gets (max_len, err); + retval = gets (max_len, err, who); return retval; } @@ -2769,45 +2788,46 @@ octave_value octave_stream::scanf (const std::string& fmt, const Array<double>& size, - int& count) + int& count, const std::string& who) { octave_value retval; - if (stream_ok ("fscanf")) - retval = rep->scanf (fmt, size, count); + if (stream_ok (who)) + retval = rep->scanf (fmt, size, count, who); return retval; } octave_value_list -octave_stream::oscanf (const std::string& fmt) +octave_stream::oscanf (const std::string& fmt, const std::string& who) { octave_value_list retval; - if (stream_ok ("fscanf")) - retval = rep->oscanf (fmt); + if (stream_ok (who)) + retval = rep->oscanf (fmt, who); return retval; } int -octave_stream::printf (const std::string& fmt, const octave_value_list& args) +octave_stream::printf (const std::string& fmt, const octave_value_list& args, + const std::string& who) { int retval = -1; - if (stream_ok ("fprintf")) - retval = rep->printf (fmt, args); + if (stream_ok (who)) + retval = rep->printf (fmt, args, who); return retval; } int -octave_stream::puts (const std::string& s) +octave_stream::puts (const std::string& s, const std::string& who) { int retval = -1; - if (stream_ok ("fputs")) - retval = rep->puts (s); + if (stream_ok (who)) + retval = rep->puts (s, who); return retval; } @@ -2815,17 +2835,21 @@ // XXX FIXME XXX -- maybe this should work for string arrays too. int -octave_stream::puts (const octave_value& tc_s) +octave_stream::puts (const octave_value& tc_s, const std::string& who) { int retval = -1; if (tc_s.is_string ()) { std::string s = tc_s.string_value (); - retval = rep->puts (s); + retval = rep->puts (s, who); } else - error ("fputs: argument must be a string"); + { + // Note that this is not ::error () ! + + error (who + ": argument must be a string"); + } return retval; } @@ -2924,9 +2948,9 @@ } void -octave_stream::invalid_stream_error (const char *op) const +octave_stream::invalid_stream_error (const std::string& who) const { - ::error ("%s: attempt to use invalid I/O stream", op); + ::error ("%s: attempt to use invalid I/O stream", who.c_str ()); } octave_stream_list *octave_stream_list::instance = 0;
--- a/src/oct-stream.h +++ b/src/oct-stream.h @@ -392,6 +392,7 @@ // Set current error state and set fail to TRUE. void error (const std::string& msg); + void error (const std::string& who, const std::string& msg); // Clear any error message and set fail to FALSE. @@ -422,10 +423,10 @@ // are those that define is). std::string do_gets (int max_len, bool& err, bool strip_newline, - const char *fcn); + const std::string& who /* = "gets" */); - std::string getl (int max_len, bool& err); - std::string gets (int max_len, bool& err); + std::string getl (int max_len, bool& err, const std::string& who /* = "getl" */); + std::string gets (int max_len, bool& err, const std::string& who /* = "gets" */); octave_value do_read (int nr, int nc, oct_data_conv::data_type dt, int skip, oct_mach_info::float_format flt_fmt, @@ -435,20 +436,18 @@ int skip, oct_mach_info::float_format flt_fmt, int& count); - octave_value do_char_scanf (scanf_format_list& fmt_list, - int nr, int nc, int& count); - - octave_value do_real_scanf (scanf_format_list& fmt_list, - int nr, int nc, int& count); + octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, + bool one_elt_size_spec, int& count, + const std::string& who /* = "scanf" */); - octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, - bool one_elt_size_spec, int& count); + octave_value scanf (const std::string& fmt, const Array<double>& size, + int& count, const std::string& who /* = "scanf" */); - octave_value scanf (const std::string& fmt, const Array<double>& size, int& count); + bool do_oscanf (const scanf_format_elt *elt, octave_value&, + const std::string& who /* = "scanf" */); - bool do_oscanf (const scanf_format_elt *elt, octave_value&); - - octave_value_list oscanf (const std::string& fmt); + octave_value_list oscanf (const std::string& fmt, + const std::string& who /* = "scanf" */); // Functions that are defined for all output streams (output streams // are those that define os). @@ -458,18 +457,20 @@ int write (const octave_value& data, oct_data_conv::data_type dt, int skip, oct_mach_info::float_format flt_fmt); - int do_printf (printf_format_list& fmt_list, const octave_value_list& args); + int do_printf (printf_format_list& fmt_list, const octave_value_list& args, + const std::string& who /* = "printf" */); - int printf (const std::string& fmt, const octave_value_list& args); + int printf (const std::string& fmt, const octave_value_list& args, + const std::string& who /* = "printf" */); - int puts (const std::string& s); + int puts (const std::string& s, const std::string& who /* = "puts" */); // We can always do this in terms of seek(), so the derived class // only has to provide that. int rewind (void); - void invalid_operation (const char *op, const char *rw); + void invalid_operation (const std::string& who, const char *rw); // No copying! @@ -493,11 +494,13 @@ int flush (void); - std::string getl (int max_len, bool& err); - std::string getl (const octave_value& max_len, bool& err); + std::string getl (int max_len, bool& err, const std::string& who /* = "getl" */); + std::string getl (const octave_value& max_len, bool& err, + const std::string& who /* = "getl" */); - std::string gets (int max_len, bool& err); - std::string gets (const octave_value& max_len, bool& err); + std::string gets (int max_len, bool& err, const std::string& who /* = "gets" */); + std::string gets (const octave_value& max_len, bool& err, + const std::string& who /* = "gets" */); int seek (std::streamoff offset, std::ios::seekdir origin); int seek (const octave_value& offset, const octave_value& origin); @@ -517,14 +520,17 @@ int write (const octave_value& data, oct_data_conv::data_type dt, int skip, oct_mach_info::float_format flt_fmt); - octave_value scanf (const std::string& fmt, const Array<double>& size, int& count); + octave_value scanf (const std::string& fmt, const Array<double>& size, + int& count, const std::string& who /* = "scanf" */); - octave_value_list oscanf (const std::string& fmt); + octave_value_list oscanf (const std::string& fmt, + const std::string& who /* = "scanf" */); - int printf (const std::string& fmt, const octave_value_list& args); + int printf (const std::string& fmt, const octave_value_list& args, + const std::string& who /* = "printf" */); - int puts (const std::string& s); - int puts (const octave_value& s); + int puts (const std::string& s, const std::string& who /* = "puts" */); + int puts (const octave_value& s, const std::string& who /* = "puts" */); bool eof (void) const; @@ -561,9 +567,9 @@ // The actual representation of this stream. octave_base_stream *rep; - void invalid_stream_error (const char *op) const; + void invalid_stream_error (const std::string& who) const; - bool stream_ok (const char *op, bool clear = true) const + bool stream_ok (const std::string& who, bool clear = true) const { bool retval = true; @@ -575,7 +581,7 @@ else { retval = false; - invalid_stream_error (op); + invalid_stream_error (who); } return retval; @@ -603,9 +609,11 @@ static octave_value insert (const octave_stream& os); - static octave_stream lookup (int fid, const std::string& who = std::string ()); - static octave_stream lookup (const octave_value& fid, - const std::string& who = std::string ()); + static octave_stream + lookup (int fid, const std::string& who = std::string ()); + + static octave_stream + lookup (const octave_value& fid, const std::string& who = std::string ()); static int remove (int fid, const std::string& who = std::string ()); static int remove (const octave_value& fid,
--- a/test/octave.test/io/io.exp +++ b/test/octave.test/io/io.exp @@ -19,11 +19,11 @@ do_test puts-2.m set test puts-3 -set prog_output "^usage:.*" +set prog_output "\n... puts:.*" do_test puts-3.m set test puts-4 -set prog_output "^usage:.*" +set prog_output "\n... puts:.*" do_test puts-4.m set test sscanf-1 @@ -55,7 +55,7 @@ do_test printf-2.m set test printf-3 -set prog_output "^usage:.*" +set prog_output "\n... printf:.*" do_test printf-3.m set test sprintf-1