1 | ;------------------------------------------------------------- |
---|
2 | ;+ |
---|
3 | ; NAME: |
---|
4 | ; GETFILE |
---|
5 | ; PURPOSE: |
---|
6 | ; Read a text file into a string array. |
---|
7 | ; CATEGORY: |
---|
8 | ; CALLING SEQUENCE: |
---|
9 | ; s = getfile(f) |
---|
10 | ; INPUTS: |
---|
11 | ; f = text file name. in |
---|
12 | ; KEYWORD PARAMETERS: |
---|
13 | ; Keywords: |
---|
14 | ; ERROR=err error flag: 0=ok, 1=file not opened, |
---|
15 | ; 2=no lines in file. |
---|
16 | ; /QUIET means give no error message. |
---|
17 | ; |
---|
18 | ; /FIND search te file in the all !path directories (use |
---|
19 | ; find.pro) |
---|
20 | ; |
---|
21 | ; OUTPUTS: |
---|
22 | ; s = string array. out |
---|
23 | ; COMMON BLOCKS: |
---|
24 | ; NOTES: |
---|
25 | ; MODIFICATION HISTORY: |
---|
26 | ; R. Sterner, 20 Mar, 1990 |
---|
27 | ; |
---|
28 | ; S. Masson (smasson@lodyc.jussieu.fr) 4 Feb 2002 |
---|
29 | ; search te file in the all !path directories (use find.pro) |
---|
30 | ; when using /find keyword. Use spawn, 'cat...' for unix os. |
---|
31 | ; |
---|
32 | ; Copyright (C) 1990, Johns Hopkins University/Applied Physics Laboratory |
---|
33 | ; This software may be used, copied, or redistributed as long as it is not |
---|
34 | ; sold and this copyright notice is reproduced on each copy made. This |
---|
35 | ; routine is provided as is without any express or implied warranties |
---|
36 | ; whatsoever. Other limitations apply as described in the file disclaimer.txt. |
---|
37 | ;- |
---|
38 | ;------------------------------------------------------------- |
---|
39 | |
---|
40 | function getfile, filein, error=err, help=hlp, quiet=quiet, find = find |
---|
41 | |
---|
42 | if (n_params(0) lt 1) or keyword_set(hlp) then begin |
---|
43 | print,' Read a text file into a string array.' |
---|
44 | print,' s = getfile(f)' |
---|
45 | print,' f = text file name. in' |
---|
46 | print,' s = string array. out' |
---|
47 | print,' Keywords:' |
---|
48 | print,' ERROR=err error flag: 0=ok, 1=file not opened,' |
---|
49 | print,' 2=no lines in file.' |
---|
50 | print,' /QUIET means give no error message.' |
---|
51 | return, -1 |
---|
52 | endif |
---|
53 | ; |
---|
54 | if keyword_set(find) then begin |
---|
55 | file = find(filein) |
---|
56 | file = file[0] |
---|
57 | if file EQ 'NOT FOUND' then begin |
---|
58 | print, ' Error in getfile: File '+filein+' not fouond.' |
---|
59 | return, -1 |
---|
60 | endif |
---|
61 | ENDIF ELSE file = filein |
---|
62 | if !version.os_family EQ 'unix' then begin |
---|
63 | spawn, 'cat '+file, res |
---|
64 | if res[0] NE '' then return, res ELSE return, '' |
---|
65 | endif |
---|
66 | ; |
---|
67 | get_lun, lun |
---|
68 | on_ioerror, err |
---|
69 | openr, lun, file |
---|
70 | |
---|
71 | s = [' '] |
---|
72 | t = '' |
---|
73 | |
---|
74 | while not eof(lun) do begin |
---|
75 | readf, lun, t |
---|
76 | s = [s,t] |
---|
77 | endwhile |
---|
78 | |
---|
79 | close, lun |
---|
80 | free_lun, lun |
---|
81 | if n_elements(s) eq 1 then begin |
---|
82 | if not keyword_set(quiet) then print,' No lines in file.' |
---|
83 | err = 2 |
---|
84 | return,-1 |
---|
85 | endif |
---|
86 | |
---|
87 | err = 0 |
---|
88 | return, s(1:*) |
---|
89 | |
---|
90 | err: if !err eq -168 then begin |
---|
91 | if not keyword_set(quiet) then print,' Non-standard text file format.' |
---|
92 | free_lun, lun |
---|
93 | return, s(1:*) |
---|
94 | endif |
---|
95 | if not keyword_set(quiet) then print,$ |
---|
96 | ' Error in getfile: File '+file+' not opened.' |
---|
97 | free_lun, lun |
---|
98 | err = 1 |
---|
99 | return, -1 |
---|
100 | |
---|
101 | end |
---|