diff libinterp/octave-value/ov-struct.cc @ 20797:492738d32c60

eliminate return statements after calls to print_usage * ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-java.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-re-mat.cc, ov-struct.cc, ov-type-conv.h, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc: Eliminate return statements after calls to print_usage.
author John W. Eaton <jwe@octave.org>
date Thu, 03 Dec 2015 14:59:30 -0500
parents ee2743bd07a8
children fee9c6315762
line wrap: on
line diff
--- a/libinterp/octave-value/ov-struct.cc	Fri Dec 04 10:03:38 2015 -0800
+++ b/libinterp/octave-value/ov-struct.cc	Thu Dec 03 14:59:30 2015 -0500
@@ -27,6 +27,7 @@
 #include <iostream>
 
 #include "Cell.h"
+#include "builtin-defun-decls.h"
 #include "defun.h"
 #include "error.h"
 #include "gripes.h"
@@ -1851,14 +1852,10 @@
 @seealso{ismatrix, iscell, isa}\n\
 @end deftypefn")
 {
-  octave_value retval;
-
-  if (args.length () == 1)
-    retval = args(0).is_map ();
-  else
+  if (args.length () != 1)
     print_usage ();
 
-  return retval;
+  return octave_value (args(0).is_map ());
 }
 
 DEFUN (__fieldnames__, args, ,
@@ -1904,47 +1901,45 @@
 
   int nargin = args.length ();
 
-  if (nargin == 2)
+  if (nargin != 2)
+    print_usage ();
+
+  retval = false;
+
+  if (args(0).is_map ())
     {
-      retval = false;
-
-      if (args(0).is_map ())
+      octave_map m = args(0).map_value ();
+
+      // FIXME: should this work for all types that can do
+      // structure reference operations?
+
+      if (args(1).is_string ())
         {
-          octave_map m = args(0).map_value ();
-
-          // FIXME: should this work for all types that can do
-          // structure reference operations?
-
-          if (args(1).is_string ())
-            {
-              std::string key = args(1).string_value ();
-
-              retval = m.isfield (key);
-            }
-          else if (args(1).is_cell ())
+          std::string key = args(1).string_value ();
+
+          retval = m.isfield (key);
+        }
+      else if (args(1).is_cell ())
+        {
+          Cell c = args(1).cell_value ();
+          boolNDArray bm (c.dims ());
+          octave_idx_type n = bm.numel ();
+
+          for (octave_idx_type i = 0; i < n; i++)
             {
-              Cell c = args(1).cell_value ();
-              boolNDArray bm (c.dims ());
-              octave_idx_type n = bm.numel ();
-
-              for (octave_idx_type i = 0; i < n; i++)
+              if (c(i).is_string ())
                 {
-                  if (c(i).is_string ())
-                    {
-                      std::string key = c(i).string_value ();
-
-                      bm(i) = m.isfield (key);
-                    }
-                  else
-                    bm(i) = false;
+                  std::string key = c(i).string_value ();
+
+                  bm(i) = m.isfield (key);
                 }
-
-              retval = bm;
+              else
+                bm(i) = false;
             }
+
+          retval = bm;
         }
     }
-  else
-    print_usage ();
 
   return retval;
 }
@@ -1960,12 +1955,13 @@
 
   int nargin = args.length ();
 
-  if (nargin == 1 && args(0).is_map ())
-    {
-      retval = static_cast<double> (args(0).nfields ());
-    }
+  if (nargin != 1)
+    print_usage ();
+
+  if (args(0).is_map ())
+    retval = static_cast<double> (args(0).nfields ());
   else
-    print_usage ();
+    error ("numfields: argument must be a struct");
 
   return retval;
 }
@@ -2014,83 +2010,81 @@
 
   int nargin = args.length ();
 
-  if (nargin == 2 || nargin == 3)
+  if (nargin < 2 || nargin > 3)
+    print_usage ();
+
+  if (! args(0).is_cell ())
+    {
+      error ("cell2struct: argument CELL must be of type cell");
+      return retval;
+    }
+
+  if (! (args(1).is_cellstr () || args(1).is_char_matrix ()))
     {
-      if (! args(0).is_cell ())
-        {
-          error ("cell2struct: argument CELL must be of type cell");
-          return retval;
-        }
-
-      if (! (args(1).is_cellstr () || args(1).is_char_matrix ()))
+      error ("cell2struct: FIELDS must be a cell array of strings or a character matrix");
+      return retval;
+    }
+
+  const Cell vals = args(0).cell_value ();
+  const Array<std::string> fields = args(1).cellstr_value ();
+
+  octave_idx_type ext = 0;
+
+  int dim = 0;
+
+  if (nargin == 3)
+    {
+      if (args(2).is_real_scalar ())
+        dim = nargin == 2 ? 0 : args(2).int_value () - 1;
+      else
         {
-          error ("cell2struct: FIELDS must be a cell array of strings or a character matrix");
-          return retval;
-        }
-
-      const Cell vals = args(0).cell_value ();
-      const Array<std::string> fields = args(1).cellstr_value ();
-
-      octave_idx_type ext = 0;
-
-      int dim = 0;
-
-      if (nargin == 3)
-        {
-          if (args(2).is_real_scalar ())
-            dim = nargin == 2 ? 0 : args(2).int_value () - 1;
-          else
-            {
-              error ("cell2struct: DIM must be a real scalar");
-              return retval;
-            }
-        }
-
-      if (dim < 0)
-        {
-          error ("cell2struct: DIM must be a valid dimension");
+          error ("cell2struct: DIM must be a real scalar");
           return retval;
         }
-
-      ext = vals.ndims () > dim ? vals.dims ()(dim) : 1;
-
-      if (ext != fields.numel ())
-        {
-          error ("cell2struct: number of FIELDS does not match dimension");
-          return retval;
-        }
-
-      int nd = std::max (dim+1, vals.ndims ());
-      // result dimensions.
-      dim_vector rdv = vals.dims ().redim (nd);
-
-      assert (ext == rdv(dim));
-      if (nd == 2)
-        {
-          rdv(0) = rdv(1-dim);
-          rdv(1) = 1;
-        }
-      else
-        {
-          for (int i =  dim + 1; i < nd; i++)
-            rdv(i-1) = rdv(i);
-
-          rdv.resize (nd-1);
-        }
-
-      octave_map map (rdv);
-      Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon);
-
-      for (octave_idx_type i = 0; i < ext; i++)
-        {
-          ia(dim) = i;
-          map.setfield (fields(i), vals.index (ia).reshape (rdv));
-        }
-
-      retval = map;
+    }
+
+  if (dim < 0)
+    {
+      error ("cell2struct: DIM must be a valid dimension");
+      return retval;
+    }
+
+  ext = vals.ndims () > dim ? vals.dims ()(dim) : 1;
+
+  if (ext != fields.numel ())
+    {
+      error ("cell2struct: number of FIELDS does not match dimension");
+      return retval;
+    }
+
+  int nd = std::max (dim+1, vals.ndims ());
+  // result dimensions.
+  dim_vector rdv = vals.dims ().redim (nd);
+
+  assert (ext == rdv(dim));
+  if (nd == 2)
+    {
+      rdv(0) = rdv(1-dim);
+      rdv(1) = 1;
     }
   else
-    print_usage ();
+    {
+      for (int i =  dim + 1; i < nd; i++)
+        rdv(i-1) = rdv(i);
+
+      rdv.resize (nd-1);
+    }
+
+  octave_map map (rdv);
+  Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon);
+
+  for (octave_idx_type i = 0; i < ext; i++)
+    {
+      ia(dim) = i;
+      map.setfield (fields(i), vals.index (ia).reshape (rdv));
+    }
+
+  retval = map;
 
   return retval;
 }
@@ -2112,9 +2106,6 @@
 */
 
 
-// So we can call Fcellstr directly.
-extern octave_value_list Fcellstr (const octave_value_list& args, int);
-
 DEFUN (rmfield, args, ,
        "-*- texinfo -*-\n\
 @deftypefn  {Built-in Function} {@var{sout} =} rmfield (@var{s}, \"@var{f}\")\n\
@@ -2131,33 +2122,26 @@
 
   int nargin = args.length ();
 
-  if (nargin == 2)
+  if (nargin != 2)
+    print_usage ();
+
+  octave_map m = args(0).xmap_value ("rmfield: first argument must be a struct");
+
+  octave_value_list fval = Fcellstr (args(1), 1);
+
+  Cell fcell = fval(0).cell_value ();
+
+  for (int i = 0; i < fcell.numel (); i++)
     {
-      octave_map m = args(0).map_value ();
-
-      octave_value_list fval = Fcellstr (args(1), 1);
-
-      Cell fcell = fval(0).cell_value ();
-
-      for (int i = 0; i < fcell.numel (); i++)
-        {
-          std::string key = fcell(i).string_value ();
-
-          if (m.isfield (key))
-            m.rmfield (key);
-          else
-            {
-              error ("rmfield: structure does not contain field %s",
-                     key.c_str ());
-
-              break;
-            }
-        }
-
-      retval = m;
+      std::string key = fcell(i).string_value ();
+
+      if (m.isfield (key))
+        m.rmfield (key);
+      else
+        error ("rmfield: structure does not contain field %s", key.c_str ());
     }
-  else
-    print_usage ();
+
+  retval = m;
 
   return retval;
 }