# HG changeset patch # User John W. Eaton # Date 1444337002 14400 # Node ID 40ed9b46a800a5670168211f53df2f1f8d374cd5 # Parent a61f0d6beb714108b5773efd461a25e51c85ac6f new octave_value::string_value method with optional error message * ov.h (octave_value::string_vector): New method. ov-base.cc, ov-base.h (octave_base_value::string_vector): New default method. ov-str-mat.cc, ov-str-mat.h (octave_char_matrix_str::string_value): New method. diff -r a61f0d6beb71 -r 40ed9b46a800 libinterp/octave-value/ov-base.cc --- a/libinterp/octave-value/ov-base.cc Thu Oct 08 20:15:19 2015 +0100 +++ b/libinterp/octave-value/ov-base.cc Thu Oct 08 16:43:22 2015 -0400 @@ -910,6 +910,33 @@ return retval; } +std::string +octave_base_value::string_value (const char *fmt, va_list args) const +{ + // Note that this method does not need to be particularly efficient + // since it is already an error to end up here. + + // FIXME: do we want both the wrong-type-argument error and any custom + // error message, or just the custom error message, or should that + // behavior be optional in some way? + + try + { + std::string tn = type_name (); + + error ("wrong type argument '%s'\n", tn.c_str ()); + } + catch (const octave_execution_exception&) + { + if (fmt) + verror (fmt, args); + + throw; + } + + return std::string (); +} + Array octave_base_value::cellstr_value (void) const { diff -r a61f0d6beb71 -r 40ed9b46a800 libinterp/octave-value/ov-base.h --- a/libinterp/octave-value/ov-base.h Thu Oct 08 20:15:19 2015 +0100 +++ b/libinterp/octave-value/ov-base.h Thu Oct 08 16:43:22 2015 -0400 @@ -564,6 +564,8 @@ virtual std::string string_value (bool force = false) const; + virtual std::string string_value (const char *fmt, va_list args) const; + virtual Array cellstr_value (void) const; virtual Range range_value (void) const; diff -r a61f0d6beb71 -r 40ed9b46a800 libinterp/octave-value/ov-str-mat.cc --- a/libinterp/octave-value/ov-str-mat.cc Thu Oct 08 20:15:19 2015 +0100 +++ b/libinterp/octave-value/ov-str-mat.cc Thu Oct 08 16:43:22 2015 -0400 @@ -252,6 +252,38 @@ return retval; } +std::string +octave_char_matrix_str::string_value (const char *fmt, va_list args) const +{ + std::string retval; + + if (! fmt) + return string_value (); + + bool conversion_error = false; + + if (matrix.ndims () == 2) + { + charMatrix chm (matrix); + + try + { + retval = chm.row_as_string (0); // FIXME? + } + catch (const octave_execution_exception&) + { + conversion_error = true; + } + } + else + conversion_error = true; + + if (conversion_error) + verror (fmt, args); + + return retval; +} + Array octave_char_matrix_str::cellstr_value (void) const { diff -r a61f0d6beb71 -r 40ed9b46a800 libinterp/octave-value/ov-str-mat.h --- a/libinterp/octave-value/ov-str-mat.h Thu Oct 08 20:15:19 2015 +0100 +++ b/libinterp/octave-value/ov-str-mat.h Thu Oct 08 16:43:22 2015 -0400 @@ -129,6 +129,8 @@ std::string string_value (bool force = false) const; + std::string string_value (const char *fmt, va_list args) const; + Array cellstr_value (void) const; octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const diff -r a61f0d6beb71 -r 40ed9b46a800 libinterp/octave-value/ov.h --- a/libinterp/octave-value/ov.h Thu Oct 08 20:15:19 2015 +0100 +++ b/libinterp/octave-value/ov.h Thu Oct 08 16:43:22 2015 -0400 @@ -897,6 +897,14 @@ std::string string_value (bool force = false) const { return rep->string_value (force); } + std::string string_value (const char *fmt, ...) const + { + va_list args; + va_start (args,fmt); + return rep->string_value (fmt, args); + va_end (args); + } + Array cellstr_value (void) const { return rep->cellstr_value (); }