source: trunk/SRC/ToBeReviewed/STRING/getfile.pro @ 236

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

improvements/corrections of some *.pro headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1;+
2;
3; @file_comments
4; Read a text file into a string array.
5;
6; @param filein {in}{required}
7; text file name.
8;
9; @keyword ERROR
10; =err  error flag: 0=ok, 1=file not opened,
11; 2=no lines in file.
12;
13; @keyword QUIET
14; means give no error message.
15;
16; @keyword LINES
17; =n  Number of lines to read (def=all).
18; Much faster if number of lines is known.
19; Automatic for IDL 5.6 or later.
20;
21; @keyword FIND
22; search the file in the all !path directories (use
23; find.pro)
24;
25; @keyword HELP
26;
27; @returns
28;
29; @history
30;       R. Sterner, 20 Mar, 1990
31;       R. Sterner, 1999 Apr 14 --- Added LINES=n keyword.
32;       R. Sterner, 2003 Aug 29 --- Automatic lines if IDL 5.6+.
33;       R. Sterner, 2003 Sep 02 --- Check if file exists first.
34;       R. Sterner, 2003 Sep 04 --- Fixed error in number of lines in file.
35;       R. Sterner, 2003 Oct 10 --- Fixed error when no lines.
36;       R. Sterner, 2004 Jan 27 --- Fixed to work in IDL as old as vers 4.
37;
38;       S. Masson (smasson\@lodyc.jussieu.fr) 4 Feb 2002
39;       search te file in the all !path directories (use find.pro)
40;       when using /find keyword. Use spawn, 'cat...' for unix os.
41;
42; Copyright (C) 1990, Johns Hopkins University/Applied Physics Laboratory
43; This software may be used, copied, or redistributed as long as it is not
44; sold and this copyright notice is reproduced on each copy made.  This
45; routine is provided as is without any express or implied warranties
46; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
47;
48; @version
49; $Id$
50;
51;-
52;
53        function getfile, filein, error=err, help=hlp, quiet=quiet, lines=lines, find = find
54;
55  compile_opt idl2, strictarrsubs
56;
57
58        if (n_params(0) lt 1) or keyword_set(hlp) then begin
59          print,' Read a text file into a string array.'
60          print,' s = getfile(f)'
61          print,'   f = text file name.      in'
62          print,'   s = string array.        out'
63          print,' Keywords:'
64          print,'   ERROR=err  error flag: 0=ok, 1=file not opened,'
65          print,'     2=no lines in file.'
66          print,'   /QUIET means give no error message.'
67          print,'   LINES=n  Number of lines to read (def=all).'
68          print,'     Much faster if number of lines is known.'
69          print,'     Automatic for IDL 5.6 or later.'
70          return, -1
71        endif
72;
73        if keyword_set(find) then begin
74          file = find(filein)
75          file = file[0]
76          if file EQ 'NOT FOUND' then begin
77            print, ' Error in getfile: File '+filein+' not found.'
78            return, -1
79          endif
80        ENDIF ELSE file = filein
81
82        if (!version.release+0. ge 5.5) then begin
83          f = call_function('file_search', file, count = c)
84        endif else begin
85          f = findfile(file,count=c)
86        endelse
87        if c eq 0 then begin
88          err = 1
89          return,''
90        endif
91
92        if n_elements(line) eq 0 and (!version.release+0. ge 5.6) then begin
93          lines = file_lines(file)
94          if lines eq 0 then begin
95            if not keyword_set(quiet) then print,' No lines in file.'
96            err = 2
97            return,-1
98          endif
99          minlines = 0
100        endif else minlines=1
101
102        get_lun, lun
103        on_ioerror, err
104        openr, lun, file
105
106        if n_elements(lines) ne 0 then begin
107          s = strarr(lines)
108          readf,lun,s
109        endif else begin
110          s = [' ']
111          t = ''
112          while not eof(lun) do begin
113            readf, lun, t
114            s = [s,t]
115          endwhile
116        endelse
117
118        close, lun
119        free_lun, lun
120        if n_elements(s) eq minlines then begin
121          if not keyword_set(quiet) then print,' No lines in file.'
122          err = 2
123          return,-1
124        endif
125        if minlines eq 1 then s=s[1:*]
126
127        err = 0
128        return, s
129
130err:    if !err eq -168 then begin
131          if not keyword_set(quiet) then print,' Non-standard text file format.'
132          free_lun, lun
133          return, s
134        endif
135        if not keyword_set(quiet) then print,$
136          ' Error in getfile: File '+file+' not opened.'
137        free_lun, lun
138        err = 1
139        return, -1
140
141        end
Note: See TracBrowser for help on using the repository browser.