]> Creatis software - CreaPhase.git/blob - octave_packages/m/prefs/addpref.m
update packages
[CreaPhase.git] / octave_packages / m / prefs / addpref.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} {} addpref (@var{group}, @var{pref}, @var{val})
21 ## Add a preference @var{pref} and associated value @var{val} to the
22 ## named preference group @var{group}.
23 ##
24 ## The named preference group must be a character string.
25 ##
26 ## The preference @var{pref} may be a character string or a cell array
27 ## of character strings.  The corresponding value @var{val} may be any
28 ## value, or, if @var{pref} is a cell array of strings, @var{val}
29 ## must be a cell array of values with the same size as @var{pref}.
30 ## @seealso{setpref, getpref, ispref, rmpref}
31 ## @end deftypefn
32
33 ## Author: jwe
34
35 function addpref (group, pref, val)
36
37   if (nargin == 3)
38     if (ischar (group))
39       prefs = loadprefs ();
40       if (ischar (pref))
41         if (isfield (group, pref))
42           error ("preference %s already exists in group %s", pref, group);
43         else
44           prefs.(group).(pref) = val;
45         endif
46       elseif (iscellstr (pref))
47         if (size_equal (pref, val))
48           for i = 1:numel(pref)
49             if (isfield (group, pref{i}))
50               error ("preference %s already exists in group %s",
51                      pref{i}, group);
52             else
53               prefs.(group).(pref{i}) = val;
54             endif
55           endfor
56         else
57           error ("size mismatch for pref and val");
58         endif
59       else
60         error ("expecting pref to be a character string or cellstr");
61       endif
62       saveprefs (prefs);
63     else
64       error ("expecting group to be a character string");
65     endif
66   else
67     print_usage ();
68   endif
69
70 endfunction
71
72 %% Testing these functions will require some care to avoid wiping out
73 %% existing (or creating unwanted) preferences for the user running the
74 %% tests.