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

Last change on this file since 325 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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