changeset 21272:987c1a79d33f

new set_internal_variable variant * variables.h, variables.cc (set_internal_variable): Set a string variable limited to a list of choices.
author John W. Eaton <jwe@octave.org>
date Tue, 16 Feb 2016 14:39:52 -0500
parents 7e67c7f82fc1
children cbced1c09916
files libinterp/corefcn/variables.cc libinterp/corefcn/variables.h
diffstat 2 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/variables.cc	Tue Feb 16 12:58:32 2016 -0500
+++ b/libinterp/corefcn/variables.cc	Tue Feb 16 14:39:52 2016 -0500
@@ -969,6 +969,49 @@
   return retval;
 }
 
+octave_value
+set_internal_variable (std::string& var, const octave_value_list& args,
+                       int nargout, const char *nm, const char **choices)
+{
+  octave_value retval;
+  int nchoices = 0;
+  while (choices[nchoices] != 0)
+    nchoices++;
+
+  int nargin = args.length ();
+
+  if (nargout > 0 || nargin == 0)
+    retval = var;
+
+  if (wants_local_change (args, nargin))
+    {
+      if (! try_local_protect (var))
+        warning ("\"local\" has no effect outside a function");
+    }
+
+  if (nargin > 1)
+    print_usage ();
+
+  if (nargin == 1)
+    {
+      std::string sval = args(0).xstring_value ("%s: first argument must be a string", nm);
+
+      int i = 0;
+      for (; i < nchoices; i++)
+        {
+          if (sval == choices[i])
+            {
+              var = sval;
+              break;
+            }
+        }
+      if (i == nchoices)
+        error ("%s: value not allowed (\"%s\")", nm, sval.c_str ());
+    }
+
+  return retval;
+}
+
 struct
 whos_parameter
 {
--- a/libinterp/corefcn/variables.h	Tue Feb 16 12:58:32 2016 -0500
+++ b/libinterp/corefcn/variables.h	Tue Feb 16 14:39:52 2016 -0500
@@ -116,6 +116,10 @@
                        int nargout, const char *nm, bool empty_ok = true);
 
 extern OCTINTERP_API octave_value
+set_internal_variable (std::string& var, const octave_value_list& args,
+                       int nargout, const char *nm, const char **choices);
+
+extern OCTINTERP_API octave_value
 set_internal_variable (int& var, const octave_value_list& args,
                        int nargout, const char *nm, const char **choices);