]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/nargchk.m
update packages
[CreaPhase.git] / octave_packages / m / general / nargchk.m
1 ## Copyright (C) 2008-2012 Bill Denney
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} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs})
21 ## @deftypefnx {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string")
22 ## @deftypefnx {Function File} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct")
23 ## Return an appropriate error message string (or structure) if the
24 ## number of inputs requested is invalid.
25 ##
26 ## This is useful for checking to see that the number of input arguments
27 ## supplied to a function is within an acceptable range.
28 ## @seealso{nargoutchk, narginchk, error, nargin, nargout}
29 ## @end deftypefn
30
31 ## Author: Bill Denney <bill@denney.ws>
32
33 function msg = nargchk (minargs, maxargs, nargs, outtype = "string")
34
35   if (nargin < 3 || nargin > 4)
36     print_usage ();
37   elseif (minargs > maxargs)
38     error ("nargchk: MINARGS must be <= MAXARGS");
39   elseif (! any (strcmpi (outtype, {"string", "struct"})))
40     error ('nargchk: output type must be either "string" or "struct"');
41   elseif (! (isscalar (minargs) && isscalar (maxargs) && isscalar (nargs)))
42     error ("nargchk: MINARGS, MAXARGS, and NARGS must be scalars");
43   endif
44
45   msg = struct ("message", "", "identifier", "");
46   if (nargs < minargs)
47     msg.message = "not enough input arguments";
48     msg.identifier = "Octave:nargchk:not-enough-inputs";
49   elseif (nargs > maxargs)
50     msg.message = "too many input arguments";
51     msg.identifier = "Octave:nargchk:too-many-inputs";
52   endif
53
54   if (strcmpi (outtype, "string"))
55     msg = msg.message;
56   elseif (isempty (msg.message))
57     ## Compatability: Matlab returns a 0x1 empty struct when nargchk passes
58     msg = resize (msg, 0, 1);
59   endif
60
61 endfunction
62
63
64 ## Tests
65 %!shared stnul, stmin, stmax
66 %!  stnul = resize (struct ("message", "", "identifier", ""), 0, 1);
67 %!  stmin = struct ("message", "not enough input arguments",
68 %!                  "identifier", "Octave:nargchk:not-enough-inputs");
69 %!  stmax = struct ("message", "too many input arguments",
70 %!                  "identifier", "Octave:nargchk:too-many-inputs");
71 %!assert (nargchk (0, 1, 0), "")
72 %!assert (nargchk (0, 1, 1), "")
73 %!assert (nargchk (1, 1, 0), "not enough input arguments")
74 %!assert (nargchk (0, 1, 2), "too many input arguments")
75 %!assert (nargchk (0, 1, 2, "string"), "too many input arguments")
76 ## Struct outputs
77 %!assert (isequal (nargchk (0, 1, 0, "struct"), stnul))
78 %!assert (isequal (nargchk (0, 1, 1, "struct"), stnul))
79 %!assert (nargchk (1, 1, 0, "struct"), stmin)
80 %!assert (nargchk (0, 1, 2, "struct"), stmax)