Mercurial > octave
diff libinterp/corefcn/file-io.cc @ 20898:8da80da1ac37
maint: Use ovl() more places in the code.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 14 Dec 2015 17:53:27 -0800 |
parents | f1b2a2dbc0e1 |
children | 6f0bd96f93c0 |
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc Mon Dec 14 15:34:39 2015 -0800 +++ b/libinterp/corefcn/file-io.cc Mon Dec 14 17:53:27 2015 -0800 @@ -975,50 +975,43 @@ { static std::string who = "sprintf"; - octave_value_list retval; - int nargin = args.length (); if (nargin == 0) print_usage (); - retval = ovl ("", "unknown error", -1.0); - octave_ostrstream *ostr = new octave_ostrstream (); octave_stream os (ostr); - if (os.is_valid ()) - { - octave_value fmt_arg = args(0); + if (! os.is_valid ()) + error ("%s: unable to create output buffer", who.c_str ()); - if (fmt_arg.is_string ()) - { - octave_value_list tmp_args; + octave_value fmt_arg = args(0); - if (nargin > 1) - { - tmp_args.resize (nargin-1, octave_value ()); + if (! fmt_arg.is_string ()) + error ("%s: format TEMPLATE must be a string", who.c_str ()); - for (int i = 1; i < nargin; i++) - tmp_args(i-1) = args(i); - } + octave_value_list retval (3); - retval(2) = os.printf (fmt_arg, tmp_args, who); - retval(1) = os.error (); + octave_value_list tmp_args; + if (nargin > 1) + { + tmp_args.resize (nargin-1, octave_value ()); - std::string result = ostr->str (); - char type = fmt_arg.is_sq_string () ? '\'' : '"'; + for (int i = 1; i < nargin; i++) + tmp_args(i-1) = args(i); + } - retval(0) = (result.empty () - ? octave_value (charMatrix (1, 0), type) - : octave_value (result, type)); - } - else - error ("%s: format TEMPLATE must be a string", who.c_str ()); - } - else - error ("%s: unable to create output buffer", who.c_str ()); + // NOTE: Call to os.error must precede next call to ostr which might reset it. + retval(2) = os.printf (fmt_arg, tmp_args, who); + retval(1) = os.error (); + + std::string result = ostr->str (); + char type = fmt_arg.is_sq_string () ? '\'' : '"'; + + retval(0) = (result.empty () ? octave_value (charMatrix (1, 0), type) + : octave_value (result, type)); return retval; }