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

Last change on this file since 495 was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 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; error flag: 0=ok, 1=file not opened, 2=no lines in file.
11;
12; @keyword QUIET
13; means give no error message.
14;
15; @keyword LINES
16; Number of lines to read (def=all).
17; Much faster if number of lines is known.
18; Automatic for IDL 5.6 or later.
19;
20; @keyword FIND
21; search the file in the all !path directories (use <pro>find</pro>)
22;
23; @keyword HELP
24;
25; @returns
26;
27; @history
28;  - R. Sterner, 20 Mar, 1990
29;  - R. Sterner, 1999 Apr 14 --- Added LINES=n keyword.
30;  - R. Sterner, 2003 Aug 29 --- Automatic lines if IDL 5.6+.
31;  - R. Sterner, 2003 Sep 02 --- Check if file exists first.
32;  - R. Sterner, 2003 Sep 04 --- Fixed error in number of lines in file.
33;  - R. Sterner, 2003 Oct 10 --- Fixed error when no lines.
34;  - R. Sterner, 2004 Jan 27 --- Fixed to work in IDL as old as vers 4.
35;  - S. Masson (smasson\@lodyc.jussieu.fr) 4 Feb 2002
36;
37;       search the file in the all !path directories (use <pro>find</pro>)
38;       when using /find keyword. Use <proidl>SPAWN</proidl>, 'cat...' for
39;       unix os.
40;
41; Copyright (C) 1990, Johns Hopkins University/Applied Physics Laboratory
42; This software may be used, copied, or redistributed as long as it is not
43; sold and this copyright notice is reproduced on each copy made.  This
44; routine is provided as is without any express or implied warranties
45; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
46;
47; @version
48; $Id$
49;
50;-
51FUNCTION getfile, filein, ERROR=err $
52                , HELP=hlp, QUIET=quiet, LINES=lines, FIND=find
53;
54  compile_opt idl2, strictarrsubs
55;
56
57if (n_params(0) lt 1) or keyword_set(hlp) then begin
58ras = report([ $
59          ' Read a text file into a string array.', $
60' s = getfile(f)', $
61'   f = text file name.      in', $
62'   s = string array.        out', $
63' Keywords:', $
64'   ERROR=err  error flag: 0=ok, 1=file not opened,', $
65'     2=no lines in file.', $
66'   /QUIET means give no error message.', $
67'   LINES=n  Number of lines to read (def=all).', $
68'     Much faster if number of lines is known.', $
69'     Automatic for IDL 5.6 or later.'])
70return, -1
71endif
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.