Mercurial > hg > octave-avbm
changeset 4732:b484cdca27be
[project @ 2004-02-04 04:32:48 by jwe]
author | jwe |
---|---|
date | Wed, 04 Feb 2004 04:32:48 +0000 |
parents | 176fcf62c464 |
children | 80156474b068 |
files | liboctave/ChangeLog liboctave/idx-vector.cc liboctave/lo-sstream.h src/ChangeLog src/data.cc src/defun.cc src/error.cc src/error.h src/ov-base.cc src/utils.cc |
diffstat | 10 files changed, 135 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,10 @@ +2004-02-03 John W. Eaton <jwe@bevo.che.wisc.edu> + + * idx-vector.cc (tree_to_mat_idx): New arg, conversion_error. + Call error handler and return conversion_error == true if arg is + not integer. + (IDX_VEC_REP::idx_vector_rep): Exit early if conversion_error. + 2004-02-02 John W. Eaton <jwe@bevo.che.wisc.edu> * boolNDArray.h (boolNDArray::boolNDArray): Declare dim_vector
--- a/liboctave/idx-vector.cc +++ b/liboctave/idx-vector.cc @@ -60,11 +60,23 @@ } static inline int -tree_to_mat_idx (double x) +tree_to_mat_idx (double x, bool& conversion_error) { - return (x > 0) - ? (static_cast<int> (x + 0.5) - 1) - : (static_cast<int> (x - 0.5) - 1); + int retval = -1; + + conversion_error = false; + + if (D_NINT (x) != x) + { + (*current_liboctave_error_handler) + ("expecting integer index, found %f", x); + + conversion_error = true; + } + else + retval = static_cast<int> (x - 1); + + return retval; } static inline int @@ -107,6 +119,8 @@ { data = new int [len]; + bool conversion_error = false; + for (int i = 0; i < len; i++) { double d = v.elem (i); @@ -114,7 +128,10 @@ if (idx_is_inf_or_nan (d)) return; else - data[i] = tree_to_mat_idx (d); + data[i] = tree_to_mat_idx (d, conversion_error); + + if (conversion_error) + return; } } @@ -138,6 +155,8 @@ int k = 0; data = new int [len]; + bool conversion_error = false; + for (int i = 0; i < len; i++) { double d = nda.elem (i); @@ -145,7 +164,10 @@ if (idx_is_inf_or_nan (d)) return; else - data[k++] = tree_to_mat_idx (d); + data[k++] = tree_to_mat_idx (d, conversion_error); + + if (conversion_error) + return; } } @@ -175,6 +197,8 @@ data = new int [len]; + bool conversion_error = false; + for (int i = 0; i < len; i++) { double val = b + i * step; @@ -182,7 +206,10 @@ if (idx_is_inf_or_nan (val)) return; else - data[i] = tree_to_mat_idx (val); + data[i] = tree_to_mat_idx (val, conversion_error); + + if (conversion_error) + return; } init_state (); @@ -201,7 +228,12 @@ { data = new int [len]; - data[0] = tree_to_mat_idx (d); + bool conversion_error = false; + + data[0] = tree_to_mat_idx (d, conversion_error); + + if (conversion_error) + return; } init_state ();
--- a/liboctave/lo-sstream.h +++ b/liboctave/lo-sstream.h @@ -37,6 +37,8 @@ #define OSSTREAM std::ostringstream #define OSSTREAM_STR(os) (os).str () +// XXX FIXME XXX -- how long is the temporary created by the str() +// method guaranteed to exist? #define OSSTREAM_C_STR(os) (os).str () . c_str () #define OSSTREAM_ENDS "" #define OSSTREAM_FREEZE(os) do { } while (0)
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,30 @@ +2004-02-03 John W. Eaton <jwe@bevo.che.wisc.edu> + + * error.cc (defun_usage_message): Only accept one arg, a const + std::string& object. Change all uses. Pass nul-terminated string + to defun_message_1. + (defun_usage_message_1): New function, equivalent to old + defun_usage_message. + + * ov-base.cc (nint_value): Use fix, not NINT. + (INT_CONV_METHOD): Likewise, use fix instead of just casting. + + * data.cc (make_diag): Use int_value instead of nint_value to + extract k for Matlab compatibility. + (Flinspace): Likewise, for npoints. + (fill_matrix): Likewise, for extracting dims. + (Fsize): Likewise, for extracting dim argument. Require int value. + * utils.cc (get_dimensions): Likewise. Use fix, not NINT. + 2004-02-02 John W. Eaton <jwe@bevo.che.wisc.edu> + * error.cc (verror, error_1): New arg, os. Use this instead + of always printing to std:cerr. Change all callers. + + * error.cc (defun_usage_message): New function. + * error.h: Provide decl. + * defun.cc (print_usage): Use it to display error message. + * error.cc (verror): New arg, save_last_error. Change all callers. (usage): Set error_state after calling verror.
--- a/src/data.cc +++ b/src/data.cc @@ -567,7 +567,7 @@ { octave_value retval; - int k = b.nint_value (); + int k = b.int_value (); if (error_state) { @@ -846,7 +846,7 @@ } else if (nargin == 2 && nargout < 2) { - int nd = args(1).nint_value (); + int nd = args(1).int_value (true); if (error_state) error ("size: expecting scalar as second argument"); @@ -1060,11 +1060,11 @@ for (int i = 0; i < nargin; i++) { - dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); + dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); if (error_state) { - error ("%s: expecting scalar arguments", fcn); + error ("%s: expecting scalar integer arguments", fcn); break; } } @@ -1247,7 +1247,7 @@ } if (nargin == 3) - npoints = args(2).nint_value (); + npoints = args(2).int_value (); if (! error_state) { @@ -1281,6 +1281,8 @@ } } } + else + error ("linspace: expecting third argument to be an integer"); return retval; }
--- a/src/defun.cc +++ b/src/defun.cc @@ -51,14 +51,22 @@ if (h.length () > 0) { - octave_stdout << "\n*** " << nm << ":\n\n"; + OSSTREAM buf; + + buf << "\n*** " << nm << ":\n\n"; - display_help_text (octave_stdout, h); + display_help_text (buf, h); - octave_stdout << "\n"; + buf << "\n"; if (! just_usage) - additional_help_message (octave_stdout); + additional_help_message (buf); + + buf << OSSTREAM_ENDS; + + defun_usage_message (OSSTREAM_STR (buf)); + + OSSTREAM_FREEZE (buf); } } else
--- a/src/error.cc +++ b/src/error.cc @@ -154,7 +154,8 @@ } static void -verror (bool save_last_error, const char *name, const char *fmt, va_list args) +verror (bool save_last_error, std::ostream& os, + const char *name, const char *fmt, va_list args) { if (discard_error_messages) return; @@ -216,7 +217,7 @@ else { octave_diary << msg_string; - std::cerr << msg_string; + os << msg_string; } } @@ -225,7 +226,7 @@ // just set the error state. static void -error_1 (const char *name, const char *fmt, va_list args) +error_1 (std::ostream& os, const char *name, const char *fmt, va_list args) { if (error_state != -2) { @@ -240,14 +241,14 @@ { char *tmp_fmt = strsave (fmt); tmp_fmt[len - 1] = '\0'; - verror (true, name, tmp_fmt, args); + verror (true, os, name, tmp_fmt, args); delete [] tmp_fmt; } error_state = -2; } else - verror (true, name, fmt, args); + verror (true, os, name, fmt, args); } } else @@ -263,7 +264,7 @@ { va_list args; va_start (args, fmt); - verror (false, name, fmt, args); + verror (false, std::cerr, name, fmt, args); va_end (args); } @@ -272,7 +273,7 @@ { va_list args; va_start (args, fmt); - verror (true, "usage", fmt, args); + verror (true, std::cerr, "usage", fmt, args); error_state = -1; va_end (args); } @@ -291,12 +292,12 @@ { char *tmp_fmt = strsave (fmt); tmp_fmt[len - 1] = '\0'; - verror (false, 0, tmp_fmt, args); + verror (false, std::cerr, 0, tmp_fmt, args); delete [] tmp_fmt; } } else - verror (false, 0, fmt, args); + verror (false, std::cerr, 0, fmt, args); } } else @@ -398,7 +399,7 @@ va_list args; va_start (args, fmt); - error_1 ("error", fmt, args); + error_1 (std::cerr, "error", fmt, args); va_end (args); if ((interactive || forced_interactive) @@ -422,7 +423,7 @@ { va_list args; va_start (args, fmt); - error_1 (0, fmt, args); + error_1 (std::cerr, 0, fmt, args); va_end (args); } @@ -433,11 +434,26 @@ va_start (args, fmt); buffer_error_messages = 0; discard_error_messages = false; - verror (false, "panic", fmt, args); + verror (false, std::cerr, "panic", fmt, args); va_end (args); abort (); } +static void +defun_usage_message_1 (const char *fmt, ...) +{ + va_list args; + va_start (args, fmt); + error_1 (octave_stdout, 0, fmt, args); + va_end (args); +} + +void +defun_usage_message (const std::string& msg) +{ + defun_usage_message_1 ("%s", msg.c_str ()); +} + typedef void (*error_fun)(const char *, ...); extern octave_value_list Fsprintf (const octave_value_list&, int);
--- a/src/error.h +++ b/src/error.h @@ -38,6 +38,9 @@ extern void parse_error (const char *fmt, ...); extern void panic (const char *fmt, ...) GCC_ATTR_NORETURN; +// Helper function for print_usage defined in defun.cc. +extern void defun_usage_message (const std::string& msg); + // Current error state. extern int error_state;
--- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -33,6 +33,7 @@ #include <iostream> #include "lo-ieee.h" +#include "lo-mappers.h" #include "so-array.h" #include "gripes.h" @@ -260,7 +261,7 @@ error ("conversion of %g to short int out of range (%d, %d)", \ d, MIN_LIMIT, MAX_LIMIT); \ else \ - retval = static_cast<T> (d); \ + retval = static_cast<T> (fix (d)); \ } \ else \ gripe_wrong_type_arg ("octave_base_value::" #F "_value ()", \ @@ -293,7 +294,7 @@ return retval; } - retval = NINT (d); + retval = static_cast<int> (fix (d)); } else gripe_wrong_type_arg ("octave_base_value::nint_value ()", type_name ());
--- a/src/utils.cc +++ b/src/utils.cc @@ -49,6 +49,7 @@ #include "dir-ops.h" #include "file-ops.h" #include "file-stat.h" +#include "lo-mappers.h" #include "lo-sstream.h" #include "oct-cmplx.h" #include "oct-env.h" @@ -791,7 +792,7 @@ if (a.is_scalar_type ()) { dim.resize (2); - dim(0) = a.nint_value (); + dim(0) = a.int_value (); dim(1) = dim(0); } else @@ -809,7 +810,7 @@ int n = v.length (); dim.resize (n); for (int i = 0; i < n; i++) - dim(i) = NINT (v(i)); + dim(i) = fix (v(i)); } else warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); @@ -825,7 +826,7 @@ { if (a.is_scalar_type ()) { - nr = nc = a.nint_value (); + nr = nc = a.int_value (); } else { @@ -839,8 +840,8 @@ if (error_state) return; - nr = NINT (v (0)); - nc = NINT (v (1)); + nr = fix (v (0)); + nc = fix (v (1)); } else warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); @@ -853,8 +854,8 @@ get_dimensions (const octave_value& a, const octave_value& b, const char *warn_for, int& nr, int& nc) { - nr = a.is_empty () ? 0 : a.nint_value (); - nc = b.is_empty () ? 0 : b.nint_value (); + nr = a.is_empty () ? 0 : a.int_value (); + nc = b.is_empty () ? 0 : b.int_value (); if (error_state) error ("%s: expecting two scalar arguments", warn_for);