source: trunk/STRING/getfile.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
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; OUTPUTS:
18;       s = string array.        out
19; COMMON BLOCKS:
20; NOTES:
21; MODIFICATION HISTORY:
22;       R. Sterner, 20 Mar, 1990
23;
24;       S. Masson (smasson@lodyc.jussieu.fr) 13 Mar, 2000
25;       search te file in the all !path directories (use find.pro)
26;
27; Copyright (C) 1990, Johns Hopkins University/Applied Physics Laboratory
28; This software may be used, copied, or redistributed as long as it is not
29; sold and this copyright notice is reproduced on each copy made.  This
30; routine is provided as is without any express or implied warranties
31; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
32;-
33;-------------------------------------------------------------
34 
35        function getfile, filein, error=err, help=hlp, quiet=quiet
36 
37        if (n_params(0) lt 1) or keyword_set(hlp) then begin
38          print,' Read a text file into a string array.'
39          print,' s = getfile(f)'
40          print,'   f = text file name.      in'
41          print,'   s = string array.        out'
42          print,' Keywords:'
43          print,'   ERROR=err  error flag: 0=ok, 1=file not opened,'
44          print,'     2=no lines in file.'
45          print,'   /QUIET means give no error message.'
46          return, -1
47        endif
48;
49        file = find(filein)
50        file = file[0]
51        if file EQ 'NOT FOUND' then begin
52           print, ' Error in getfile: File '+filein+' not fouond.'
53           return, -1
54        endif
55       
56;
57        get_lun, lun
58        on_ioerror, err
59        openr, lun, file
60 
61        s = [' ']
62        t = ''
63 
64        while not eof(lun) do begin
65          readf, lun, t
66          s = [s,t]
67        endwhile
68 
69        close, lun
70        free_lun, lun
71        if n_elements(s) eq 1 then begin
72          if not keyword_set(quiet) then print,' No lines in file.'
73          err = 2
74          return,-1
75        endif
76 
77        err = 0
78        return, s(1:*)
79 
80err:    if !err eq -168 then begin
81          if not keyword_set(quiet) then print,' Non-standard text file format.'
82          free_lun, lun
83          return, s(1:*)
84        endif
85        if not keyword_set(quiet) then print,$
86          ' Error in getfile: File '+file+' not opened.'
87        free_lun, lun
88        err = 1
89        return, -1
90 
91        end
Note: See TracBrowser for help on using the repository browser.