source: trunk/SRC/Utilities/createfunc.pro @ 133

Last change on this file since 133 was 128, checked in by pinsard, 18 years ago

improvements of Utilities/*.pro header

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5; @file_comments
6; write an idl function, compile it and execute it.
7; usefull to avoid the use of execute
8;
9; @param command {in}{required} a scalar string defining the result to be
10; given back by the function. (see examples)
11;
12; @keyword FILENAMEIN {in} {default=for_createfunc.pro}
13; name of the function to be created.
14;
15; @keyword KWDLIST {in} a vector string. to specify a list of keywords that
16;      must be included in the function definition. Warning: the string
17;      must start with a ',' for example: KWDLIST = ', TOTO = toto'
18;
19; @keyword _EXTRA used to pass your keywords to the created function.
20;
21; @restrictions
22; - arguments can be given only through keywords;
23; - ends the function name with '.pro' if needed.
24;
25; @examples
26; IDL> print, createfunc('3*2', filename='test')
27; IDL> print, createfunc('3*two', filename = 'test' $
28; IDL>                          , kwdlist ='two = two', two = 2)
29;
30; @history Sebastien Masson (smasson\@lodyc.jussieu.fr)
31;                      May 2005
32;
33; @version $Id$
34;
35;-
36;------------------------------------------------------------
37;------------------------------------------------------------
38;------------------------------------------------------------
39FUNCTION createfunc, command, FILENAMEIN = filenamein $
40               , KWDLIST = kwdlist, _EXTRA = ex
41;
42  compile_opt idl2, hidden, strictarrsubs
43;
44  IF n_elements(command) NE 1 THEN stop
45; define filename if needed
46  if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $
47  ELSE filename = filenamein
48; get the name of the function (not the name of the file containing the function)
49   shortfilename =  file_basename(filename, '.pro')
50; check if the directory exists
51   dirname = isadirectory(file_dirname(filename) $
52                          , title = 'Redefine '+shortfilename+'.pro directory')
53   IF size(dirname, /type) NE 7 THEN return, -1
54;
55   filename = dirname + shortfilename + '.pro'
56; create the file
57   if NOT keyword_set(kwdlist) then kwdlist = ''
58   kwdlist = kwdlist + ', _EXTRA = ex'
59   IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' +  kwdlist
60   putfile, filename, ['function ' + shortfilename + kwdlist $
61                       , 'compile_opt idl2, hidden, strictarrsubs' $
62                       , 'res = ' + command $
63                       , 'return, res' $
64                       , 'end']
65; go in dirname directory
66   cd, dirname, current = old_dir
67; compile it
68   resolve_routine, shortfilename, /is_function
69   cd, old_dir
70; execute it
71   res = call_function(shortfilename, _EXTRA = ex)
72;
73   return, res
74end
Note: See TracBrowser for help on using the repository browser.