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

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

improvements of some *.pro

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