# HG changeset patch # User John W. Eaton # Date 1455651592 18000 # Node ID 987c1a79d33fa8ea99df82e9e797c7852afd6de2 # Parent 7e67c7f82fc1724cd5f9b61b3808f8efc4b557c2 new set_internal_variable variant * variables.h, variables.cc (set_internal_variable): Set a string variable limited to a list of choices. diff -r 7e67c7f82fc1 -r 987c1a79d33f libinterp/corefcn/variables.cc --- 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 { diff -r 7e67c7f82fc1 -r 987c1a79d33f libinterp/corefcn/variables.h --- 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);