diff libinterp/dldfcn/fftw.cc @ 20614:10ec79b47808

use new string_value method to handle value extraction errors * __voronoi__.cc, chol.cc, colamd.cc, fftw.cc: Use new string_value method.
author John W. Eaton <jwe@octave.org>
date Thu, 08 Oct 2015 18:15:56 -0400
parents f90c8372b7ba
children
line wrap: on
line diff
--- a/libinterp/dldfcn/fftw.cc	Thu Oct 08 17:41:21 2015 -0400
+++ b/libinterp/dldfcn/fftw.cc	Thu Oct 08 18:15:56 2015 -0400
@@ -143,196 +143,183 @@
     }
 
 #if defined (HAVE_FFTW)
-  if (args(0).is_string ())
+  std::string arg0 = args(0).string_value ("fftw: first argument must be a string");
+
+  if (arg0 == "planner")
     {
-      std::string arg0 = args(0).string_value ();
+      if (nargin == 2)  //planner setter
+        {
+          // Use STL function to convert to lower case
+          std::transform (arg0.begin (), arg0.end (), arg0.begin (),
+                          tolower);
+
+          std::string arg1 = args(1).string_value ("fftw: planner expects a string value as METHOD");
 
-      if (arg0 == "planner")
-        {
-          if (nargin == 2)  //planner setter
+          std::transform (arg1.begin (), arg1.end (),
+                          arg1.begin (), tolower);
+          octave_fftw_planner::FftwMethod meth
+            = octave_fftw_planner::UNKNOWN;
+          octave_float_fftw_planner::FftwMethod methf
+            = octave_float_fftw_planner::UNKNOWN;
+
+          if (arg1 == "estimate")
+            {
+              meth = octave_fftw_planner::ESTIMATE;
+              methf = octave_float_fftw_planner::ESTIMATE;
+            }
+          else if (arg1 == "measure")
+            {
+              meth = octave_fftw_planner::MEASURE;
+              methf = octave_float_fftw_planner::MEASURE;
+            }
+          else if (arg1 == "patient")
             {
-              if (args(1).is_string ())
-                {
-                  // Use STL function to convert to lower case
-                  std::transform (arg0.begin (), arg0.end (), arg0.begin (),
-                                  tolower);
-                  std::string arg1 = args(1).string_value ();
+              meth = octave_fftw_planner::PATIENT;
+              methf = octave_float_fftw_planner::PATIENT;
+            }
+          else if (arg1 == "exhaustive")
+            {
+              meth = octave_fftw_planner::EXHAUSTIVE;
+              methf = octave_float_fftw_planner::EXHAUSTIVE;
+            }
+          else if (arg1 == "hybrid")
+            {
+              meth = octave_fftw_planner::HYBRID;
+              methf = octave_float_fftw_planner::HYBRID;
+            }
+          else
+            error ("fftw: unrecognized planner METHOD");
 
-                  std::transform (arg1.begin (), arg1.end (),
-                                  arg1.begin (), tolower);
-                  octave_fftw_planner::FftwMethod meth
-                    = octave_fftw_planner::UNKNOWN;
-                  octave_float_fftw_planner::FftwMethod methf
-                    = octave_float_fftw_planner::UNKNOWN;
+          meth = octave_fftw_planner::method (meth);
+          octave_float_fftw_planner::method (methf);
+
+          if (meth == octave_fftw_planner::MEASURE)
+            retval = octave_value ("measure");
+          else if (meth == octave_fftw_planner::PATIENT)
+            retval = octave_value ("patient");
+          else if (meth == octave_fftw_planner::EXHAUSTIVE)
+            retval = octave_value ("exhaustive");
+          else if (meth == octave_fftw_planner::HYBRID)
+            retval = octave_value ("hybrid");
+          else
+            retval = octave_value ("estimate");
+        }
+      else //planner getter
+        {
+          octave_fftw_planner::FftwMethod meth =
+            octave_fftw_planner::method ();
 
-                  if (arg1 == "estimate")
-                    {
-                      meth = octave_fftw_planner::ESTIMATE;
-                      methf = octave_float_fftw_planner::ESTIMATE;
-                    }
-                  else if (arg1 == "measure")
-                    {
-                      meth = octave_fftw_planner::MEASURE;
-                      methf = octave_float_fftw_planner::MEASURE;
-                    }
-                  else if (arg1 == "patient")
-                    {
-                      meth = octave_fftw_planner::PATIENT;
-                      methf = octave_float_fftw_planner::PATIENT;
-                    }
-                  else if (arg1 == "exhaustive")
-                    {
-                      meth = octave_fftw_planner::EXHAUSTIVE;
-                      methf = octave_float_fftw_planner::EXHAUSTIVE;
-                    }
-                  else if (arg1 == "hybrid")
-                    {
-                      meth = octave_fftw_planner::HYBRID;
-                      methf = octave_float_fftw_planner::HYBRID;
-                    }
-                  else
-                    error ("fftw: unrecognized planner METHOD");
+          if (meth == octave_fftw_planner::MEASURE)
+            retval = octave_value ("measure");
+          else if (meth == octave_fftw_planner::PATIENT)
+            retval = octave_value ("patient");
+          else if (meth == octave_fftw_planner::EXHAUSTIVE)
+            retval = octave_value ("exhaustive");
+          else if (meth == octave_fftw_planner::HYBRID)
+            retval = octave_value ("hybrid");
+          else
+            retval = octave_value ("estimate");
+        }
+    }
+  else if (arg0 == "dwisdom")
+    {
+      if (nargin == 2)  //dwisdom setter
+        {
+          // Use STL function to convert to lower case
+          std::transform (arg0.begin (), arg0.end (), arg0.begin (),
+                          tolower);
+
+          std::string arg1 = args(1).string_value ("fftw: WISDOM must be a string");
+
+          char *str = fftw_export_wisdom_to_string ();
+
+          if (arg1.length () < 1)
+            fftw_forget_wisdom ();
+          else if (! fftw_import_wisdom_from_string (arg1.c_str ()))
+            error ("fftw: could not import supplied WISDOM");
+
+          retval = octave_value (std::string (str));
+
+          // FIXME: need to free string even if there is an
+          // exception.
+          free (str);
+        }
+      else //dwisdom getter
+        {
+          char *str = fftw_export_wisdom_to_string ();
+          retval = octave_value (std::string (str));
 
-                  meth = octave_fftw_planner::method (meth);
-                  octave_float_fftw_planner::method (methf);
+          // FIXME: need to free string even if there is an
+          // exception.
+          free (str);
+        }
+    }
+  else if (arg0 == "swisdom")
+    {
+      //swisdom uses fftwf_ functions (float), dwisdom fftw_ (real)
+      if (nargin == 2)  //swisdom setter
+        {
+          // Use STL function to convert to lower case
+          std::transform (arg0.begin (), arg0.end (), arg0.begin (),
+                          tolower);
+
+          std::string arg1 = args(1).string_value ("fftw: WISDOM must be a string");
+
+          char *str = fftwf_export_wisdom_to_string ();
+
+          if (arg1.length () < 1)
+            fftwf_forget_wisdom ();
+          else if (! fftwf_import_wisdom_from_string (arg1.c_str ()))
+            error ("fftw: could not import supplied WISDOM");
+
+          retval = octave_value (std::string (str));
 
-                  if (meth == octave_fftw_planner::MEASURE)
-                    retval = octave_value ("measure");
-                  else if (meth == octave_fftw_planner::PATIENT)
-                    retval = octave_value ("patient");
-                  else if (meth == octave_fftw_planner::EXHAUSTIVE)
-                    retval = octave_value ("exhaustive");
-                  else if (meth == octave_fftw_planner::HYBRID)
-                    retval = octave_value ("hybrid");
-                  else
-                    retval = octave_value ("estimate");
+          // FIXME: need to free string even if there is an
+          // exception.
+          free (str);
+        }
+      else //swisdom getter
+        {
+          char *str = fftwf_export_wisdom_to_string ();
+          retval = octave_value (std::string (str));
+
+          // FIXME: need to free string even if there is an
+          // exception.
+          free (str);
+        }
+    }
+  else if (arg0 == "threads")
+    {
+      if (nargin == 2)  //threads setter
+        {
+          if (args(1).is_real_scalar ())
+            {
+              int nthreads = args(1).int_value();
+              if (nthreads >= 1)
+                {
+#if defined (HAVE_FFTW3_THREADS)
+                  octave_fftw_planner::threads (nthreads);
+#else
+                  gripe_disabled_feature ("fftw", "multithreaded FFTW");
+#endif
+#if defined (HAVE_FFTW3F_THREADS)
+                  octave_float_fftw_planner::threads (nthreads);
+#else
+                  gripe_disabled_feature ("fftw", "multithreaded FFTW");
+#endif
                 }
               else
-                error ("fftw: planner expects a string value as METHOD");
-            }
-          else //planner getter
-            {
-              octave_fftw_planner::FftwMethod meth =
-                octave_fftw_planner::method ();
-
-              if (meth == octave_fftw_planner::MEASURE)
-                retval = octave_value ("measure");
-              else if (meth == octave_fftw_planner::PATIENT)
-                retval = octave_value ("patient");
-              else if (meth == octave_fftw_planner::EXHAUSTIVE)
-                retval = octave_value ("exhaustive");
-              else if (meth == octave_fftw_planner::HYBRID)
-                retval = octave_value ("hybrid");
-              else
-                retval = octave_value ("estimate");
+                error ("fftw: number of threads must be >=1");
             }
-        }
-      else if (arg0 == "dwisdom")
-        {
-          if (nargin == 2)  //dwisdom setter
-            {
-              if (args(1).is_string ())
-                {
-                  // Use STL function to convert to lower case
-                  std::transform (arg0.begin (), arg0.end (), arg0.begin (),
-                                  tolower);
-                  std::string arg1 = args(1).string_value ();
-
-                  char *str = fftw_export_wisdom_to_string ();
-
-                  if (arg1.length () < 1)
-                    fftw_forget_wisdom ();
-                  else if (! fftw_import_wisdom_from_string (arg1.c_str ()))
-                    error ("fftw: could not import supplied WISDOM");
-
-                  retval = octave_value (std::string (str));
-
-                  // FIXME: need to free string even if there is an
-                  // exception.
-                  free (str);
-                }
-            }
-          else //dwisdom getter
-            {
-              char *str = fftw_export_wisdom_to_string ();
-              retval = octave_value (std::string (str));
-
-              // FIXME: need to free string even if there is an
-              // exception.
-              free (str);
-            }
+          else
+            error ("fftw: setting threads needs one integer argument");
         }
-      else if (arg0 == "swisdom")
-        {
-          //swisdom uses fftwf_ functions (float), dwisdom fftw_ (real)
-          if (nargin == 2)  //swisdom setter
-            {
-              if (args(1).is_string ())
-                {
-                  // Use STL function to convert to lower case
-                  std::transform (arg0.begin (), arg0.end (), arg0.begin (),
-                                  tolower);
-                  std::string arg1 = args(1).string_value ();
-
-                  char *str = fftwf_export_wisdom_to_string ();
-
-                  if (arg1.length () < 1)
-                    fftwf_forget_wisdom ();
-                  else if (! fftwf_import_wisdom_from_string (arg1.c_str ()))
-                    error ("fftw: could not import supplied WISDOM");
-
-                  retval = octave_value (std::string (str));
-
-                  // FIXME: need to free string even if there is an
-                  // exception.
-                  free (str);
-                }
-            }
-          else //swisdom getter
-            {
-              char *str = fftwf_export_wisdom_to_string ();
-              retval = octave_value (std::string (str));
-
-              // FIXME: need to free string even if there is an
-              // exception.
-              free (str);
-            }
-        }
-      else if (arg0 == "threads")
-        {
-          if (nargin == 2)  //threads setter
-            {
-              if (args(1).is_real_scalar ())
-                {
-                  int nthreads = args(1).int_value();
-                  if (nthreads >= 1)
-                    {
+      else //threads getter
 #if defined (HAVE_FFTW3_THREADS)
-                      octave_fftw_planner::threads (nthreads);
+        retval = octave_value (octave_fftw_planner::threads());
 #else
-                      gripe_disabled_feature ("fftw", "multithreaded FFTW");
-#endif
-#if defined (HAVE_FFTW3F_THREADS)
-                      octave_float_fftw_planner::threads (nthreads);
-#else
-                      gripe_disabled_feature ("fftw", "multithreaded FFTW");
+      retval = 1;
 #endif
-                    }
-                  else
-                    error ("fftw: number of threads must be >=1");
-                }
-              else
-                error ("fftw: setting threads needs one integer argument");
-            }
-          else //threads getter
-#if defined (HAVE_FFTW3_THREADS)
-            retval = octave_value (octave_fftw_planner::threads());
-#else
-            retval = 1;
-#endif
-        }
-      else
-        error ("fftw: unrecognized argument");
     }
   else
     error ("fftw: unrecognized argument");