]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/temp_name.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / temp_name.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@isr.ist.utl.pt>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {@var{n} = } temp_name ( rootname, quick ) 
18 ## @cindex  
19 ## name = temp_name(rootname, quick=1) - Return a name that is not used
20 ##
21 ## Returns a name, suitable for defining a new function, script or global
22 ## variable, of the form
23 ##
24 ##                 [rootname,number]
25 ##
26 ## Default rootname is "temp_name_"
27 ##
28 ## "quick" is an optional parameter, which defaults to 1. If it is false,
29 ## temp_name() will find the smallest acceptable number for the name.
30 ## Otherwise, a hopefully quicker method is used.
31 ##
32 ## @end deftypefn
33
34 function n = temp_name (rootname, quick)
35
36   persistent warned = false;
37   if (! warned)
38     warned = true;
39     warning ("Octave:deprecated-function",
40              "temp_name has been deprecated, and will be removed in the future. Use `tmpnam' instead.");
41   endif
42
43   ### Keep track of previously asked names
44   persistent cnt = struct ("dummy",0);
45
46   if nargin<1 || !length(rootname), rootname = "temp_name_" ; endif
47
48   if nargin<2, quick = 1; endif
49
50   if quick
51     if ! isfield (cnt, rootname)
52       cnt.(rootname) = 0;
53       c = 0 ;
54     else
55       c = cnt.(rootname) ;
56     endif
57   else
58     c = 0;
59   endif
60
61   n = sprintf ([rootname,"%i"], c);
62
63   while exist (n),
64     c++ ;
65     n = sprintf ([rootname,"%i"], c);
66   endwhile
67
68   if quick
69     cnt.(rootname) = c ;
70   endif
71 endfunction