changeset 20681:b0b37f0d7e6d

new cellstr_value function and elimination of error_state * ov.h (octave_value::cellstr_value): New overloaded function with extra error message. * ov-base.h, ov-base.cc (octave_base_value::cellstr_value): Likewise. * ov-cell.h, ov-cell.cc (octave_cell::cellstr_value): Likewise. * ov-str-mat.h, ov-str-mat.cc (octave_str_mat::cellstr_value): Likewise. * graphics.cc, urlwrite.cc, ov-cell.cc, ov-class.cc, ov-struct.cc: Use new cellstr_value function and eliminate use of error_state.
author John W. Eaton <jwe@octave.org>
date Thu, 05 Nov 2015 17:22:16 -0500
parents 8787e80a44b2
children 18f38ed43962
files libinterp/corefcn/graphics.cc libinterp/corefcn/urlwrite.cc libinterp/octave-value/ov-base.cc libinterp/octave-value/ov-base.h libinterp/octave-value/ov-cell.cc libinterp/octave-value/ov-cell.h libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-str-mat.cc libinterp/octave-value/ov-str-mat.h libinterp/octave-value/ov-struct.cc libinterp/octave-value/ov.h
diffstat 11 files changed, 176 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/graphics.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/corefcn/graphics.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -10024,50 +10024,40 @@
 
           if (nargin > 1 && args(1).is_cellstr ())
             {
-              Array<std::string> plist = args(1).cellstr_value ();
-
-              if (! error_state)
+              Array<std::string> plist = args(1).cellstr_value ("get: expecting property name or cell array of property names as second argument");
+
+              octave_idx_type plen = plist.numel ();
+
+              use_cell_format = true;
+
+              vals.resize (dim_vector (len, plen));
+
+              for (octave_idx_type n = 0; n < len; n++)
                 {
-                  octave_idx_type plen = plist.numel ();
-
-                  use_cell_format = true;
-
-                  vals.resize (dim_vector (len, plen));
-
-                  for (octave_idx_type n = 0; n < len; n++)
+                  graphics_object go = gh_manager::get_object (hcv(n));
+
+                  if (go)
                     {
-                      graphics_object go = gh_manager::get_object (hcv(n));
-
-                      if (go)
+                      for (octave_idx_type m = 0; m < plen; m++)
                         {
-                          for (octave_idx_type m = 0; m < plen; m++)
-                            {
-                              caseless_str property = plist(m);
-
-                              vals(n, m) = go.get (property);
-                            }
-                        }
-                      else
-                        {
-                          error ("get: invalid handle (= %g)", hcv(n));
-                          break;
+                          caseless_str property = plist(m);
+
+                          vals(n, m) = go.get (property);
                         }
                     }
+                  else
+                    {
+                      error ("get: invalid handle (= %g)", hcv(n));
+                      break;
+                    }
                 }
-              else
-                error ("get: expecting property name or cell array of property names as second argument");
             }
           else
             {
               caseless_str property;
 
               if (nargin > 1)
-                {
-                  property = args(1).string_value ();
-
-                  if (error_state)
-                    error ("get: expecting property name or cell array of property names as second argument");
-                }
+                property = args(1).string_value ("get: expecting property name or cell array of property names as second argument");
 
               vals.resize (dim_vector (len, 1));
 
--- a/libinterp/corefcn/urlwrite.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/corefcn/urlwrite.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -363,13 +363,7 @@
           return retval;
         }
 
-      param = args(3).cellstr_value ();
-
-      if (error_state)
-        {
-          error ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
-          return retval;
-        }
+      param = args(3).cellstr_value ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
 
       if (param.numel () % 2 == 1)
         {
@@ -503,13 +497,7 @@
           return retval;
         }
 
-      param = args(2).cellstr_value ();
-
-      if (error_state)
-        {
-          error ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
-          return retval;
-        }
+      param = args(2).cellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
 
       if (param.numel () % 2 == 1)
         {
--- a/libinterp/octave-value/ov-base.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-base.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -56,6 +56,30 @@
 #include "toplev.h"
 #include "variables.h"
 
+static void
+gripe_wrong_type_arg_with_msg (const char *name, const std::string& type,
+                               const char *fmt, va_list args)
+{
+  // 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
+    {
+      gripe_wrong_type_arg (name, type);
+    }
+  catch (const octave_execution_exception&)
+    {
+      if (fmt)
+        verror (fmt, args);
+
+      throw;
+    }
+}
+
 builtin_type_t btyp_mixed_numeric (builtin_type_t x, builtin_type_t y)
 {
   builtin_type_t retval = btyp_unknown;
@@ -540,24 +564,7 @@
 Cell
 octave_base_value::cell_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
-    {
-      gripe_wrong_type_arg ("cell value", type_name ());
-    }
-  catch (const octave_execution_exception&)
-    {
-      if (fmt)
-        verror (fmt, args);
-
-      throw;
-    }
+  gripe_wrong_type_arg_with_msg ("cell value", type_name (), fmt, args);
 
   return Cell ();
 }
@@ -938,24 +945,7 @@
 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
-    {
-      gripe_wrong_type_arg ("string value", type_name ());
-    }
-  catch (const octave_execution_exception&)
-    {
-      if (fmt)
-        verror (fmt, args);
-
-      throw;
-    }
+  gripe_wrong_type_arg_with_msg ("string value", type_name (), fmt, args);
 
   return std::string ();
 }
@@ -964,11 +954,19 @@
 octave_base_value::cellstr_value (void) const
 {
   Array<std::string> retval;
-  gripe_wrong_type_arg ("octave_base_value::cellstry_value()",
+  gripe_wrong_type_arg ("octave_base_value::cellstr_value()",
                         type_name ());
   return retval;
 }
 
+Array<std::string>
+octave_base_value::cellstr_value (const char *fmt, va_list args) const
+{
+  gripe_wrong_type_arg_with_msg ("cellstr value", type_name (), fmt, args);
+
+  return Array<std::string> ();
+}
+
 Range
 octave_base_value::range_value (void) const
 {
--- a/libinterp/octave-value/ov-base.h	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-base.h	Thu Nov 05 17:22:16 2015 -0500
@@ -570,6 +570,9 @@
 
   virtual Array<std::string> cellstr_value (void) const;
 
+  virtual Array<std::string>
+  cellstr_value (const char *fmt, va_list args) const;
+
   virtual Range range_value (void) const;
 
   virtual octave_map map_value (void) const;
--- a/libinterp/octave-value/ov-cell.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-cell.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -480,17 +480,12 @@
 {
   octave_value retval;
 
-  if (is_cellstr ())
-    {
-      Array<std::string> tmp = cellstr_value ();
-
-      tmp = tmp.sort (dim, mode);
+  Array<std::string> tmp = cellstr_value ("sort: only cell arrays of character strings may be sorted");
 
-      // We already have the cache.
-      retval = new octave_cell (tmp);
-    }
-  else
-    error ("sort: only cell arrays of character strings may be sorted");
+  tmp = tmp.sort (dim, mode);
+
+  // We already have the cache.
+  retval = new octave_cell (tmp);
 
   return retval;
 }
@@ -501,17 +496,12 @@
 {
   octave_value retval;
 
-  if (is_cellstr ())
-    {
-      Array<std::string> tmp = cellstr_value ();
-
-      tmp = tmp.sort (sidx, dim, mode);
+  Array<std::string> tmp = cellstr_value ("sort: only cell arrays of character strings may be sorted");
 
-      // We already have the cache.
-      retval = new octave_cell (tmp);
-    }
-  else
-    error ("sort: only cell arrays of character strings may be sorted");
+  tmp = tmp.sort (sidx, dim, mode);
+
+  // We already have the cache.
+  retval = new octave_cell (tmp);
 
   return retval;
 }
@@ -521,14 +511,9 @@
 {
   sortmode retval = UNSORTED;
 
-  if (is_cellstr ())
-    {
-      Array<std::string> tmp = cellstr_value ();
+  Array<std::string> tmp = cellstr_value ("issorted: A is not a cell array of strings");
 
-      retval = tmp.is_sorted (mode);
-    }
-  else
-    error ("issorted: A is not a cell array of strings");
+  retval = tmp.is_sorted (mode);
 
   return retval;
 }
@@ -539,14 +524,9 @@
 {
   Array<octave_idx_type> retval;
 
-  if (is_cellstr ())
-    {
-      Array<std::string> tmp = cellstr_value ();
+  Array<std::string> tmp = cellstr_value ("sortrows: only cell arrays of character strings may be sorted");
 
-      retval = tmp.sort_rows_idx (mode);
-    }
-  else
-    error ("sortrows: only cell arrays of character strings may be sorted");
+  retval = tmp.sort_rows_idx (mode);
 
   return retval;
 }
@@ -556,14 +536,9 @@
 {
   sortmode retval = UNSORTED;
 
-  if (is_cellstr ())
-    {
-      Array<std::string> tmp = cellstr_value ();
+  Array<std::string> tmp = cellstr_value ("issorted: A is not a cell array of strings");
 
-      retval = tmp.is_sorted_rows (mode);
-    }
-  else
-    error ("issorted: A is not a cell array of strings");
+  retval = tmp.is_sorted_rows (mode);
 
   return retval;
 }
@@ -661,6 +636,49 @@
   return retval;
 }
 
+Array<std::string>
+octave_cell::cellstr_value (const char *fmt, ...) const
+{
+  Array<std::string> retval;
+  va_list args;
+  retval = cellstr_value (fmt, args);
+  va_end (args);
+  return retval;
+}
+
+Array<std::string>
+octave_cell::cellstr_value (const char *fmt, va_list args) const
+{
+  Array<std::string> retval;
+
+  if (! fmt)
+    return cellstr_value ();
+
+  bool conversion_error = false;
+
+  if (is_cellstr ())
+    {
+      try
+        {
+          if (cellstr_cache->is_empty ())
+            *cellstr_cache = matrix.cellstr_value ();
+
+          retval = *cellstr_cache;
+        }
+      catch (const octave_execution_exception&)
+        {
+          conversion_error = true;
+        }
+    }
+  else
+    conversion_error = true;
+
+  if (conversion_error)
+    verror (fmt, args);
+
+  return retval;
+}
+
 bool
 octave_cell::print_as_scalar (void) const
 {
--- a/libinterp/octave-value/ov-cell.h	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-cell.h	Thu Nov 05 17:22:16 2015 -0500
@@ -146,6 +146,10 @@
 
   Array<std::string> cellstr_value (void) const;
 
+  Array<std::string> cellstr_value (const char *fmt, ...) const;
+
+  Array<std::string> cellstr_value (const char *fmt, va_list args) const;
+
   bool print_as_scalar (void) const;
 
   void print (std::ostream& os, bool pr_as_read_syntax = false);
--- a/libinterp/octave-value/ov-class.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-class.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -1917,12 +1917,7 @@
 
   octave_value obj = args(0);  // not const because of find_parent_class ()
   std::string obj_cls = obj.class_name ();
-  Array<std::string> clsnames = args(1).cellstr_value ();
-  if (error_state)
-    {
-      error ("isa: CLASSNAME must be a string or cell array of strings");
-      return retval;
-    }
+  Array<std::string> clsnames = args(1).cellstr_value ("isa: CLASSNAME must be a string or cell array of strings");
 
   boolNDArray matches (clsnames.dims (), false);
   for (octave_idx_type idx = 0; idx < clsnames.numel (); idx++)
--- a/libinterp/octave-value/ov-str-mat.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-str-mat.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -303,6 +303,41 @@
   return retval;
 }
 
+Array<std::string>
+octave_char_matrix_str::cellstr_value (const char *fmt, va_list args) const
+{
+  Array<std::string> retval;
+
+  if (! fmt)
+    return cellstr_value ();
+
+  bool conversion_error = false;
+
+  if (matrix.ndims () == 2)
+    {
+      const charMatrix chm (matrix);
+      octave_idx_type nr = chm.rows ();
+      retval.clear (nr, 1);
+
+      try
+        {
+          for (octave_idx_type i = 0; i < nr; i++)
+            retval.xelem (i) = chm.row_as_string (i);
+        }
+      catch (const octave_execution_exception&)
+        {
+          conversion_error = true;
+        }
+    }
+  else
+    conversion_error = true;
+
+  if (conversion_error)
+    verror (fmt, args);
+
+  return retval;
+}
+
 void
 octave_char_matrix_str::print_raw (std::ostream& os,
                                    bool pr_as_read_syntax) const
--- a/libinterp/octave-value/ov-str-mat.h	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-str-mat.h	Thu Nov 05 17:22:16 2015 -0500
@@ -133,6 +133,8 @@
 
   Array<std::string> cellstr_value (void) const;
 
+  Array<std::string> cellstr_value (const char *fmt, va_list args) const;
+
   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
   { return octave_value (matrix.sort (dim, mode)); }
 
--- a/libinterp/octave-value/ov-struct.cc	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov-struct.cc	Thu Nov 05 17:22:16 2015 -0500
@@ -1814,10 +1814,9 @@
 
       if (nargin == 2)
         {
-          if (args(1).is_cellstr ())
-            retval = octave_map (args(0).dims (), args(1).cellstr_value ());
-          else
-            error ("struct: expecting cell array of field names as second argument");
+          Array<std::string> cstr = args(1).cellstr_value ("struct: expecting cell array of field names as second argument");
+
+          retval = octave_map (args(0).dims (), cstr);
         }
       else
         retval = octave_map (args(0).dims ());
--- a/libinterp/octave-value/ov.h	Thu Nov 05 16:09:38 2015 -0500
+++ b/libinterp/octave-value/ov.h	Thu Nov 05 17:22:16 2015 -0500
@@ -912,6 +912,16 @@
   Array<std::string> cellstr_value (void) const
   { return rep->cellstr_value (); }
 
+  Array<std::string> cellstr_value (const char *fmt, ...) const
+  {
+    Array<std::string> retval;
+    va_list args;
+    va_start (args,fmt);
+    retval = rep->cellstr_value (fmt, args);
+    va_end (args);
+    return retval;
+  }
+
   Range range_value (void) const
   { return rep->range_value (); }