view scripts/prefs/getpref.m @ 27898:4d6d21839dfd

remove Author: jwe and Adapted-by: jwe lines from source files These lines have little meaning now in the age of modern version control systems. Files affected: url-transfer.cc, url-transfer.h, acot.m, acoth.m, acsc.m, acsch.m, asec.m, asech.m, cot.m, coth.m, csc.m, csch.m, sec.m, sech.m, cart2pol.m, cart2sph.m, deal.m, fliplr.m, flipud.m, int2str.m, logspace.m, num2str.m, pol2cart.m, rot90.m, sortrows.m, sph2cart.m, waitbar.m, doc.m, hsv2rgb.m, rgb2hsv.m, beep.m, isstr.m, setstr.m, strmatch.m, cond.m, rank.m, trace.m, bug_report.m, cast.m, delete.m, dir.m, list_primes.m, ls.m, ls_command.m, menu.m, pack.m, recycle.m, substruct.m, version.m, glpk.m, axis.m, box.m, grid.m, __axis_label__.m, text.m, title.m, view.m, xlabel.m, ylabel.m, zlabel.m, bar.m, barh.m, comet3.m, hist.m, line.m, loglog.m, mesh.m, patch.m, plot.m, polar.m, __bar__.m, __line__.m, __plt__.m, __stem__.m, semilogx.m, semilogy.m, sombrero.m, stairs.m, stem.m, surface.m, __default_plot_options__.m, __gnuplot_drawnow__.m, __next_line_color__.m, __plt_get_axis_arg__.m, __pltopt__.m, clf.m, closereq.m, gca.m, gnuplot_binary.in.m, isfigure.m, meshgrid.m, __gnuplot_draw_axes__.m, __gnuplot_draw_figure__.m, shg.m, addpref.m, getpref.m, ispref.m, loadprefs.m, prefsfile.m, saveprefs.m, rmpref.m, setpref.m, ismember.m, setdiff.m, union.m, hankel.m, hilb.m, vander.m, median.m, std.m, cstrcat.m, isletter.m, str2num.m, strcat.m, oruntests.m, rundemos.m, asctime.m, clock.m, ctime.m, date.m, etime.m, is_leap_year.m.
author John W. Eaton <jwe@octave.org>
date Thu, 02 Jan 2020 15:43:01 -0500
parents 63b5a17ba30f
children b442ec6dda5c
line wrap: on
line source

## Copyright (C) 2012-2019 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {} {@var{val} =} getpref ("@var{group}", "@var{pref}")
## @deftypefnx {} {@var{val} =} getpref ("@var{group}", "@var{pref}", @var{default})
## @deftypefnx {} {@{@var{val1}, @var{val2}, @dots{}@} =} getpref ("@var{group}", @{"@var{pref1}", "@var{pref2"}, @dots{}@})
## @deftypefnx {} {@var{prefstruct} =} getpref ("@var{group}")
## @deftypefnx {} {@var{prefstruct} =} getpref ()
## Return the preference value corresponding to the named preference @var{pref}
## in the preference group @var{group}.
##
## The named preference group must be a string.
##
## If @var{pref} does not exist in @var{group} and @var{default} is specified,
## create the preference with value @var{default} and return @var{default}.
##
## The preference @var{pref} may be a string or cell array of strings.  If it
## is a cell array of strings then a cell array of preferences is returned.
##
## The corresponding default value @var{default} may be any Octave value,
## .e.g., double, struct, cell array, object, etc.  Or, if @var{pref} is a cell
## array of strings then @var{default} must be a cell array of values with the
## same size as @var{pref}.
##
## If neither @var{pref} nor @var{default} are specified, return a structure
## of preferences for the preference group @var{group}.
##
## If no arguments are specified, return a structure containing all groups of
## preferences and their values.
## @seealso{addpref, setpref, ispref, rmpref}
## @end deftypefn

function retval = getpref (group, pref, default)

  if (nargin > 3)
    print_usage ();
  endif

  if (nargin == 0)
    retval = loadprefs ();
  elseif (nargin == 1)
    if (! ischar (group))
      error ("getpref: GROUP must be a string");
    endif
    prefs = loadprefs ();
    if (isfield (prefs, group))
      retval = prefs.(group);
    else
      ## FIXME: Is this the right behavior, or should it produce an error?
      retval = [];
    endif
  else
    if (! (ischar (pref) || iscellstr (pref)))
      error ("getpref: PREF must be a string or cellstr");
    endif

    grp = getpref (group);

    if (ischar (pref))
      if (isfield (grp, pref))
        retval = grp.(pref);
      elseif (nargin == 3)
        addpref (group, pref, default);
        retval = default;
      else
        error ("getpref: preference %s does not exist in GROUP %s",
               pref, group);
      endif
    else
      if (nargin != 2 && ! size_equal (pref, default))
        error ("getpref: size mismatch for PREF and DEFAULT");
      endif

      for i = 1:numel (pref)
        if (isfield (grp, pref{i}))
          retval{i} = grp.(pref{i});
        elseif (nargin == 3)
          addpref (group, pref{i}, default{i});
          retval{i} = default{i};
        else
          error ("getpref: preference %s does not exist in GROUP %s",
                 pref{i}, group);
        endif
      endfor
    endif
  endif

endfunction


%!test
%! HOME = getenv ("HOME");
%! tmp_home = tempname ();
%! save_default_options ("-binary", "local");
%! unwind_protect
%!   mkdir (tmp_home);
%!   setenv ("HOME", tmp_home);
%!
%!   addpref ("group1", "pref1", [1 2 3]);
%!   addpref ("group2", {"prefA", "prefB"}, {"StringA", {"StringB"}});
%!
%!   exp.group1.pref1 = [1 2 3];
%!   exp.group2.prefA = "StringA";
%!   exp.group2.prefB = {"StringB"};
%!   obs = getpref ();
%!   assert (obs, exp);
%!
%!   assert (getpref ("group1"), exp.group1);
%!   assert (getpref ("group2"), exp.group2);
%!   assert (getpref ("group3"), []);
%!
%!   assert (getpref ("group1", "pref1"), [1 2 3]);
%!   assert (getpref ("group2", "prefA"), "StringA");
%!   assert (getpref ("group2", "prefB"), {"StringB"});
%!   assert (getpref ("group1", "pref2", "New_Value"), "New_Value");
%!   assert (getpref ("group1", "pref2"), "New_Value");
%!   fail ('getpref ("group1", "no_such_pref")', ...
%!         "preference no_such_pref does not exist in GROUP group1");
%!
%!   assert (getpref ("group2", {"prefA", "prefB"}), {"StringA", {"StringB"}});
%!   assert (getpref ("group2", {"prefA", "prefC"}, {1, "StringC"}),
%!           {"StringA", "StringC"});
%!   assert (getpref ("group2", "prefC"), "StringC");
%!   fail ('getpref ("group1", {"p1", "p2"}, 1)', ...
%!         "size mismatch for PREF and DEFAULT");
%!   fail ('getpref ("group2", {"prefA", "prefD"})',
%!         "preference prefD does not exist in GROUP group2");
%!
%! unwind_protect_cleanup
%!   unlink (fullfile (tmp_home, ".octave_prefs"));
%!   if (isfolder (tmp_home))
%!     rmdir (tmp_home);
%!   endif
%!   if (isempty (HOME))
%!     unsetenv ("HOME");
%!   else
%!     setenv ("HOME", HOME);
%!   endif
%! end_unwind_protect

%!error getpref (1,2,3,4)
%!error <GROUP must be a string> getpref (1)
%!error <PREF must be a string> getpref ("group1", 1, 2)