]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/parseparams.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / parseparams.m
1 ## Copyright (C) 2006-2012 Alexander Barth
2 ## Copyright (C) 2010 VZLU Prague
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {[@var{reg}, @var{prop}] =} parseparams (@var{params})
22 ## @deftypefnx {Function File} {[@var{reg}, @var{var1}, @dots{}] =} parseparams (@var{params}, @var{name1}, @var{default1}, @dots{})
23 ## Return in @var{reg} the cell elements of @var{param} up to the first
24 ## string element and in @var{prop} all remaining elements beginning
25 ## with the first string element.  For example:
26 ##
27 ## @example
28 ## @group
29 ## [reg, prop] = parseparams (@{1, 2, "linewidth", 10@})
30 ## reg =
31 ## @{
32 ##   [1,1] = 1
33 ##   [1,2] = 2
34 ## @}
35 ## prop =
36 ## @{
37 ##   [1,1] = linewidth
38 ##   [1,2] = 10
39 ## @}
40 ## @end group
41 ## @end example
42 ##
43 ## The parseparams function may be used to separate 'regular'
44 ## arguments and additional arguments given as property/value pairs of
45 ## the @var{varargin} cell array.
46 ##
47 ## In the second form of the call, available options are specified directly
48 ## with their default values given as name-value pairs.
49 ## If @var{params} do not form name-value pairs, or if an option occurs
50 ## that does not match any of the available options, an error occurs.
51 ## When called from an m-file function, the error is prefixed with the
52 ## name of the caller function.
53 ## The matching of options is case-insensitive.
54 ##
55 ## @seealso{varargin}
56 ## @end deftypefn
57
58 ## Author: Alexander Barth <abarth93@users.sourceforge.net>
59 ## Author: Aida Alvera Azcarate <aida@netecho.info>
60
61 function [reg, varargout] = parseparams (params, varargin)
62
63   strs = cellfun ("isclass", params, "char");
64   i = find (strs, 1);
65   if (i)
66     reg = params(1:i-1);
67     prop = params(i:end);
68   else
69     reg = params;
70     prop = {};
71   endif
72
73   if (nargin == 1)
74     varargout = {prop};
75   else
76     names = varargin(1:2:end);
77     defaults = varargin(2:2:end);
78     if (! size_equal (names, defaults))
79       error ("parseparams: needs odd number of arguments");
80     endif
81     [names, sidx] = sort (names);
82
83     varargout = defaults;
84     if (i)
85       ## Let's parse the properties.
86       pnames = prop(1:2:end);
87       values = prop(2:2:end);
88       if (! size_equal (pnames, values) || ! all (strs(i:2:end)))
89         error_as_caller ("options must be given as name-value pairs");
90       endif
91       idx = lookup (toupper(names), toupper(pnames), "m");
92       if (! all (idx))
93         error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)});
94       else
95         varargout(sidx(idx)) = values;
96       endif
97     endif
98   endif
99
100 endfunction
101
102 function error_as_caller (msg, varargin)
103   stack = dbstack (1); # omit me
104   fname = stack(min (2, end)).name;
105   error ([fname, ": ", msg], varargin{:});
106 endfunction
107