diff --git a/libinterp/corefcn/gripes.cc b/libinterp/corefcn/gripes.cc --- a/libinterp/corefcn/gripes.cc +++ b/libinterp/corefcn/gripes.cc @@ -132,6 +132,21 @@ gripe_data_conversion (const char *from, } void +gripe_wrong_type_arg (const char *s, bool is_error) +{ + if (is_error) + error ("wrong type argument '%s'", s); + else + warning ("wrong type argument '%s'", s); +} + +void +gripe_wrong_type_arg (const std::string& s, bool is_error) +{ + gripe_wrong_type_arg (s.c_str (), is_error); +} + +void gripe_wrong_type_arg (const char *name, const char *s, bool is_error) { if (is_error) diff --git a/libinterp/corefcn/gripes.h b/libinterp/corefcn/gripes.h --- a/libinterp/corefcn/gripes.h +++ b/libinterp/corefcn/gripes.h @@ -79,6 +79,12 @@ extern OCTINTERP_API void gripe_data_conversion (const char *from, const char *to); extern OCTINTERP_API void +gripe_wrong_type_arg (const char *s, bool is_error = true); + +extern OCTINTERP_API void +gripe_wrong_type_arg (const std::string& s, bool is_error = true); + +extern OCTINTERP_API void gripe_wrong_type_arg (const char *name, const char *s, bool is_error = true); diff --git a/libinterp/octave-value/ov-base.cc b/libinterp/octave-value/ov-base.cc --- a/libinterp/octave-value/ov-base.cc +++ b/libinterp/octave-value/ov-base.cc @@ -913,6 +913,17 @@ octave_base_value::string_value (bool fo return retval; } +std::string +octave_base_value::string_value (const std::string& msg) const +{ + if (msg.empty ()) + gripe_wrong_type_arg (type_name ()); + else + error ("%s", msg.c_str ()); + + return std::string (); +} + Array octave_base_value::cellstr_value (void) const { diff --git a/libinterp/octave-value/ov-base.h b/libinterp/octave-value/ov-base.h --- a/libinterp/octave-value/ov-base.h +++ b/libinterp/octave-value/ov-base.h @@ -564,6 +564,8 @@ public: virtual std::string string_value (bool force = false) const; + virtual std::string string_value (const std::string& msg) const; + virtual Array cellstr_value (void) const; virtual Range range_value (void) const; diff --git a/libinterp/octave-value/ov-str-mat.cc b/libinterp/octave-value/ov-str-mat.cc --- a/libinterp/octave-value/ov-str-mat.cc +++ b/libinterp/octave-value/ov-str-mat.cc @@ -258,6 +258,40 @@ octave_char_matrix_str::string_value (bo return retval; } +std::string +octave_char_matrix_str::string_value (const std::string& msg) const +{ + std::string retval; + + if (msg.empty ()) + return string_value (); + else + { + 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) + error ("%s", msg.c_str ()); + } + + return retval; +} + Array octave_char_matrix_str::cellstr_value (void) const { diff --git a/libinterp/octave-value/ov-str-mat.h b/libinterp/octave-value/ov-str-mat.h --- a/libinterp/octave-value/ov-str-mat.h +++ b/libinterp/octave-value/ov-str-mat.h @@ -129,6 +129,8 @@ public: std::string string_value (bool force = false) const; + std::string string_value (const std::string& msg) const; + Array cellstr_value (void) const; octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const diff --git a/libinterp/octave-value/ov.h b/libinterp/octave-value/ov.h --- a/libinterp/octave-value/ov.h +++ b/libinterp/octave-value/ov.h @@ -897,6 +897,12 @@ public: std::string string_value (bool force = false) const { return rep->string_value (force); } + std::string string_value (const char *msg) const + { return string_value (std::string (msg ? msg : "")); } + + std::string string_value (const std::string& msg) const + { return rep->string_value (msg); } + Array cellstr_value (void) const { return rep->cellstr_value (); }