]> Creatis software - CreaPhase.git/blob - octave_packages/m/testfun/rundemos.m
update packages
[CreaPhase.git] / octave_packages / m / testfun / rundemos.m
1 ## Copyright (C) 2008-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} {} rundemos ()
21 ## @deftypefnx {Function File} {} rundemos (@var{directory})
22 ## Execute built-in demos for all function files in the specified directory.
23 ## If no directory is specified, operate on all directories in Octave's
24 ## search path for functions.
25 ## @seealso{runtests, path}
26 ## @end deftypefn
27
28 ## Author: jwe
29
30 function rundemos (directory)
31
32   if (nargin == 0)
33     dirs = strsplit (path (), pathsep ());
34   elseif (nargin == 1)
35     if (is_absolute_filename (directory))
36       dirs = {directory};
37     else
38       directory = regexprep (directory, ['\',filesep(),'$'], "");
39       fullname = find_dir_in_path (directory);
40       if (! isempty (fullname))
41         dirs = {fullname};
42       else
43         error ("rundemos: DIRECTORY argument must be a valid pathname");
44       endif
45     endif
46   else
47     print_usage ();
48   endif
49
50   for i = 1:numel (dirs)
51     d = dirs{i};
52     run_all_demos (d);
53   endfor
54
55 endfunction
56
57 function run_all_demos (directory)
58   dirinfo = dir (directory);
59   flist = {dirinfo.name};
60   for i = 1:numel (flist)
61     f = flist{i};
62     if (length (f) > 2 && strcmp (f((end-1):end), ".m"))
63       f = fullfile (directory, f);
64       if (has_demos (f))
65         try
66           demo (f);
67         catch
68           printf ("error: %s\n\n", lasterror().message)
69         end_try_catch
70         if (i != numel (flist))
71           input ("Press <enter> to continue: ", "s");
72         endif
73       endif
74     endif
75   endfor
76 endfunction
77
78 function retval = has_demos (f)
79   fid = fopen (f);
80   if (f < 0)
81     error ("rundemos: fopen failed: %s", f);
82   else
83     str = fscanf (fid, "%s");
84     fclose (fid);
85     retval = findstr (str, "%!demo");
86   endif
87 endfunction
88
89 %!error rundemos ("foo", 1);