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

Last change on this file since 239 was 239, checked in by smasson, 17 years ago

cleaning + minor bugfix related to the path definition

  • Property svn:keywords set to Id
File size: 2.9 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}
8; a scalar string 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}
15; a vector string. 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; IDL> print, createfunc('3*2', filename='test')
29; IDL> print, createfunc('3*two', filename = 'test' $
30; IDL>                          , kwdlist ='two = two', two = 2)
31;
32; @history
33; Sebastien Masson (smasson\@lodyc.jussieu.fr)
34;                      May 2005
35;
36; @version
37; $Id$
38;
39;-
40;
41FUNCTION createfunc, command, FILENAMEIN = filenamein $
42               , KWDLIST = kwdlist, _EXTRA = ex
43;
44  compile_opt idl2, hidden, strictarrsubs
45;
46  IF n_elements(command) NE 1 THEN stop
47; define filename if needed
48  if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $
49  ELSE filename = filenamein
50; get the name of the function (not the name of the file containing the function)
51   shortfilename =  file_basename(filename, '.pro')
52; check if the directory exists
53   dirname = isadirectory(file_dirname(filename) $
54                          , title = 'Redefine '+shortfilename+'.pro directory')
55   IF size(dirname, /type) NE 7 THEN return, -1
56;
57   filename = dirname + shortfilename + '.pro'
58; create the file
59   if NOT keyword_set(kwdlist) then kwdlist = ''
60   kwdlist = kwdlist + ', _EXTRA = ex'
61   IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' +  kwdlist
62   putfile, filename, ['function ' + shortfilename + kwdlist $
63                       , 'compile_opt idl2, hidden, strictarrsubs' $
64                       , 'res = ' + command $
65                       , 'return, res' $
66                       , 'end']
67; is dirname in !path?
68   cd, current = here
69   pathlist = [here, strsplit(!path, path_sep(/search_path),/extract)]
70   inpath = total((file_search(dirname))[0] EQ pathlist)
71   IF inpath EQ 0 THEN !path = !path + path_sep(/search_path) + inpath
72; update the list of .pro and .sav in !PATH
73   path_cache, /rebuild
74; do we really use shortfilename?
75   list = find(shortfilename, /onlypro, /firstfound)
76   IF list[0] NE filename THEN BEGIN ; it is ok if filename is the first one
77     dummy = report(['Several files ' + shortfilename + ' are found in the !path and' $
78                     , list[0] + ' we be used instead of', filename, 'We stop...'], /simple)
79     stop
80   ENDIF
81; compile it
82   resolve_routine, shortfilename, /is_function
83; execute it
84   res = call_function(shortfilename, _EXTRA = ex)
85;
86   return, res
87end
Note: See TracBrowser for help on using the repository browser.