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

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

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 2.6 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; compile it
75   resolve_routine, shortfilename, /is_function
76; execute it
77   res = call_function(shortfilename, _EXTRA = ex)
78;
79   return, res
80end
Note: See TracBrowser for help on using the repository browser.