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

Last change on this file since 371 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1;+
2;
3; @file_comments
4; write an IDL function, compile it and execute it.
5; useful to avoid the use of execute
6;
7; @param command {in}{required} {type=scalar string}
8; defining the result to be given back by the function.
9; (see examples)
10;
11; @keyword FILENAMEIN {in} {default=for_createfunc.pro}
12; name of the function to be created.
13;
14; @keyword KWDLIST {in} {type=vector string}
15; to specify a list of keywords that must be included in the
16; function definition.
17; Warning: the string must start with a ','
18; for example: KWDLIST=', TOTO=toto'
19;
20; @keyword _EXTRA
21; Used to pass keywords to the created function.
22;
23; @restrictions
24; - arguments can be given only through keywords;
25; - ends the function name with '.pro' if needed.
26;
27; @examples
28;
29;   IDL> print, createfunc('3*2', FILENAMEIN='test')
30;   IDL> print, createfunc('3*two', FILENAMEIN='test' $
31;   IDL>                          , KWDLIST='two = two', TWO=2)
32;
33; @history
34; Sebastien Masson (smasson\@lodyc.jussieu.fr)
35;                      May 2005
36;
37; @version
38; $Id$
39;
40;-
41FUNCTION createfunc, command, FILENAMEIN=filenamein $
42               , KWDLIST=kwdlist, _EXTRA=ex
43;
44  compile_opt idl2, hidden, strictarrsubs
45;
46  usage="result=createfunc(command, FILENAMEIN=filenamein , KWDLIST=kwdlist, _EXTRA=ex)"
47;
48  IF n_elements(command) NE 1 THEN BEGIN
49    dummy = report(['Input parameter command empty', $
50                    'Usage : ' + usage])
51    stop
52  ENDIF
53; define filename if needed
54  if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $
55  ELSE filename = filenamein
56; get the name of the function (not the name of the file containing the function)
57   shortfilename =  file_basename(filename, '.pro')
58; check if the directory exists
59   dirname = isadirectory(file_dirname(filename) $
60                          , title = 'Redefine '+shortfilename+'.pro directory')
61   IF size(dirname, /type) NE 7 THEN return, -1
62;
63   filename = dirname + shortfilename + '.pro'
64; create the file
65   if NOT keyword_set(kwdlist) then kwdlist = ''
66   kwdlist = kwdlist + ', _EXTRA = ex'
67   IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' +  kwdlist
68   putfile, filename, ['function ' + shortfilename + kwdlist $
69                       , 'compile_opt idl2, hidden, strictarrsubs' $
70                       , 'res = ' + command $
71                       , 'return, res' $
72                       , 'end']
73; is dirname in !path?
74   cd, current = here
75   pathlist = [here, strsplit(!path, path_sep(/search_path),/extract)]
76   inpath = total((file_search(dirname, /fully_qualify_path))[0] EQ pathlist)
77   IF inpath EQ 0 THEN !path = dirname + path_sep(/search_path) + !path
78; update the list of .pro and .sav in !PATH
79   path_cache, /rebuild
80; do we really use shortfilename?
81   list = find(shortfilename, /onlypro, /firstfound)
82   IF list[0] NE filename THEN BEGIN ; it is ok if filename is the first one
83     dummy = report(['Several files ' + shortfilename + ' are found in the !path and' $
84                     , list[0] + ' we be used instead of', filename, 'We stop...'], /simple)
85     stop
86   ENDIF
87; compile it
88   resolve_routine, shortfilename, /is_function
89; execute it
90   res = call_function(shortfilename, _EXTRA = ex)
91;
92   return, res
93end
Note: See TracBrowser for help on using the repository browser.