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

Last change on this file since 153 was 136, checked in by pinsard, 18 years ago

some improvements and corrections in some .pro file according to
aspell and idldoc log file

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