]> Creatis software - CreaPhase.git/blob - octave_packages/m/prefs/getpref.m
update packages
[CreaPhase.git] / octave_packages / m / prefs / getpref.m
1 ## Copyright (C) 2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} getpref (@var{group}, @var{pref}, @var{default})
21 ## Return the preference value corresponding to the named preference
22 ## @var{pref} in the preference group @var{group}.
23 ##
24 ## The named preference group must be a character string.
25 ##
26 ## If @var{pref} does not exist in @var{group} and @var{default} is
27 ## specified, return @var{default}.
28 ##
29 ## The preference @var{pref} may be a character string or a cell array
30 ## of character strings.  The corresponding default value @var{default}
31 ## may be any value, or, if @var{pref} is a cell array of strings,
32 ## @var{default} must be a cell array of values with the same size as
33 ## @var{pref}.
34 ##
35 ## If neither @var{pref} nor @var{default} are specified, return a
36 ## structure of preferences for the preference group @var{group}.
37 ##
38 ## If no arguments are specified, return a structure containing all
39 ## groups of preferences and their values.
40 ## @seealso{addpref, setpref, ispref, rmpref}
41 ## @end deftypefn
42
43 ## Author: jwe
44
45 function retval = getpref (group, pref, default)
46
47   if (nargin == 0)
48     retval = loadprefs ();
49   elseif (nargin == 1)
50     if (ischar (group))
51       prefs = loadprefs ();
52       if (isfield (prefs, group))
53         retval = prefs.(group);
54       else
55         retval = [];
56       endif
57     else
58       error ("expecting group to be a character string");
59     endif
60   elseif (nargin == 2 || nargin == 3)
61     grp = getpref (group);
62     if (ischar (pref))
63       if (isfield (grp, pref))
64         retval = grp.(pref);
65       elseif (nargin == 3)
66         retval = default;
67       else
68         error ("preference %s does not exist in group %s", pref, group);
69       endif
70     elseif (iscellstr (pref))
71       if (nargin == 2 || size_equal (pref, default))
72         for i = 1:numel(pref)
73           if (isfield (grp, pref{i}))
74             retval.(pref) = grp.(pref{i});
75           elseif (nargin == 3)
76             retval.(pref) = default{i};
77           else
78             error ("preference %s does not exist in group %s", pref{i}, group);
79           endif
80         endfor
81       else
82         error ("size mismatch for pref and default");
83       endif
84     else
85       error ("expecting pref to be a character string or cellstr");
86     endif
87   else
88     print_usage ();
89   endif
90
91 endfunction
92
93 %% Testing these functions will require some care to avoid wiping out
94 %% existing (or creating unwanted) preferences for the user running the
95 %% tests.