file_compile.pro
3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;+
; NAME:
; FILE_COMPILE
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20771
; Craig.Markwardt@nasa.gov
;
; PURPOSE:
; Compile an arbitrary procedure
;
; CALLING SEQUENCE:
; FILE_COMPILE, pathname, ERROR=error, ERRMSG=errmsg
;
; DESCRIPTION:
;
; FILE_COMPILE compiles a file containing an IDL procedure or
; function. After compilation the user may call the procedure or
; function.
;
; If compilation is successful, then ERROR is set to 0. If the
; compilation fails, then, ERROR is set to a non-zero error code,
; and ERRMSG is set to a descriptive error message.
;
; INPUTS:
;
; PATHNAME - scalar string, path name of file containing IDL
; procedure or function. PATHNAME must end in '.pro'.
; The directory containing the file must be readable and
; it must be possible to change to that directory as a
; working directory using CD.
;
; KEYWORDS:
;
; ERROR - upon return, a scalar integer giving status of
; compilation, either 0 for success or non-zero for failure.
;
; ERRMSG - upon return, a scalar string giving a descriptive error
; message.
;
; PRO_NAME - upon return, the name of the procedure, with path name
; and '.pro' suffix removed.
;
; SEE ALSO:
;
; RESOLVE_ROUTINE
;
; MODIFICATION HISTORY:
; Documented, CM, Jun 2009
; Small documentation changes, CM, 2012-10-09
;
; $Id: file_compile.pro,v 1.2 2012/10/17 23:53:58 cmarkwar Exp $
;
;-
; Copyright (C) 2009, 2012, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
pro file_compile, pathname, pro_name=proname, error=err, errmsg=errmsg
err = 1
errmsg = ''
dir = file_dirname(pathname)
proname = file_basename(pathname, '.pro') ;; Remove suffix
filename = file_basename(pathname)
sep = path_sep(/search_path)
if file_test(pathname, /read) EQ 0 then begin
errmsg = 'FILE_COMPILE: '+pathname+' does not exist'
err = -248
return
endif
;; Save current working directory
cd, current=cwd
changed_dir = 0
!error_state.code = 0
;; Bracket this operation by a CATCH block, so that we don't
;; leave the working directory in a bad state
catch, catcherr
if catcherr EQ 0 then begin
;; Temporarily change to the subdirectory of the desired file
changed_dir = 1
cd, dir
resolve_routine, proname, /either, /compile_full_file
err = 0
endif else begin
errmsg = 'FILE_COMPILE: compilation of '+pathname+' failed'
endelse
catch, /cancel
error_state = !error_state ;; Save a temporary copy
;; Restore original working directory
if changed_dir then cd, cwd
if err NE 0 AND error_state.code NE 0 then begin
;; Attempt to capture the error condition returned by
;; RESOLVE_ROUTINE
err = error_state.code
errmsg = error_state.msg
endif
return
end