Re: "include" a file [message #66987 is a reply to message #66977] |
Fri, 26 June 2009 19:37   |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Fri, 26 Jun 2009 12:50:56 -0700 (PDT), JD Smith wrote:
...
> This works just fine, collapsing multi-line commands, and executing
> them, at the cost of temporarily polluting the current scope with
> "_inc_" variables (these are left undefined after the @include). You
...
Here is another idea to avoid the "_inc_" variables within the current
scope. (Therefore you have to accept the existence of a common
block.) I changed JDs code a little bit and put it into the following
functions:
function include_command
;-----------------------
;
common include_function,status,_inc_lines,_inc_wh,_inc_start,_inc_i
;
_inc_parts=_inc_lines[_inc_wh[_inc_i]]
if _inc_wh[_inc_i] gt _inc_start then $
_inc_parts=strjoin( $
reform((stregex(_inc_lines[_inc_start:_inc_wh[_inc_i]-1],$
' *(.*) *\$(;.*)?[ \t]*$', $
/SUBEXPR,/EXTRACT))[1,*])) + _inc_parts
_inc_start=_inc_wh[_inc_i]+1L
_inc_i++
;
return,_inc_parts
end
function include_status,include_file
;-----------------------------------
;
common include_function,status,_inc_lines,_inc_wh,_inc_start,_inc_i
;
if n_elements(status) le 0 then status=0
;
if status eq 0 then begin
_inc_lines=replicate('',file_lines(include_file))
openr,_inc_un,include_file,/get_lun
readf,_inc_un,_inc_lines
free_lun,_inc_un
_inc_wh=where(~stregex(_inc_lines,'\$(;.*)?[\t]*$',/BOOLEAN) ,_inc_cnt)
_inc_start=0L
_inc_i=0L
status=1
end
;
if _inc_i ge n_elements(_inc_wh) then begin
; Clean-up the arrays in the common block
temp=temporary(_inc_lines)
temp=temporary(_inc_wh)
status=0
end
;
return,status
end
After that you can use the following command within your program or
command line:
while include_status('commands.pro') do $
if not execute(include_command()) then message,/cont,'Error.'
Here "commands.pro" is the include file with the commands to execute.
There are no unwanted variables anymore.
If you prefer an easier command than the long while-command, you can
do the file reading part of the include_status function within another
procedure and use something like
include_file,'commands.pro'
@include_execute
With the modified while-command written into the file
"include_execute.pro".
Heinz
|
|
|