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

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

replace some print by some report in some .pro (continuation) + 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 the file in the all !path directories (use <pro>find</pro>)
40;       when using /find keyword. Use <proidl>spawn</proidl>, 'cat...' for
41;       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;
54FUNCTION getfile, filein, error=err, help=hlp, quiet=quiet, lines=lines, find = find
55;
56  compile_opt idl2, strictarrsubs
57;
58
59        if (n_params(0) lt 1) or keyword_set(hlp) then begin
60          print,' Read a text file into a string array.'
61          print,' s = getfile(f)'
62          print,'   f = text file name.      in'
63          print,'   s = string array.        out'
64          print,' Keywords:'
65          print,'   ERROR=err  error flag: 0=ok, 1=file not opened,'
66          print,'     2=no lines in file.'
67          print,'   /QUIET means give no error message.'
68          print,'   LINES=n  Number of lines to read (def=all).'
69          print,'     Much faster if number of lines is known.'
70          print,'     Automatic for IDL 5.6 or later.'
71          return, -1
72        endif
73;
74        if keyword_set(find) then begin
75          file = find(filein)
76          file = file[0]
77          if file EQ 'NOT FOUND' then begin
78            print, ' Error in getfile: File '+filein+' not found.'
79            return, -1
80          endif
81        ENDIF ELSE file = filein
82
83        if (!version.release+0. ge 5.5) then begin
84          f = call_function('file_search', file, count = c)
85        endif else begin
86          f = findfile(file,count=c)
87        endelse
88        if c eq 0 then begin
89          err = 1
90          return,''
91        endif
92
93        if n_elements(line) eq 0 and (!version.release+0. ge 5.6) then begin
94          lines = file_lines(file)
95          if lines eq 0 then begin
96            if not keyword_set(quiet) then print,' No lines in file.'
97            err = 2
98            return,-1
99          endif
100          minlines = 0
101        endif else minlines=1
102
103        get_lun, lun
104        on_ioerror, err
105        openr, lun, file
106
107        if n_elements(lines) ne 0 then begin
108          s = strarr(lines)
109          readf,lun,s
110        endif else begin
111          s = [' ']
112          t = ''
113          while not eof(lun) do begin
114            readf, lun, t
115            s = [s,t]
116          endwhile
117        endelse
118
119        close, lun
120        free_lun, lun
121        if n_elements(s) eq minlines then begin
122          if not keyword_set(quiet) then print,' No lines in file.'
123          err = 2
124          return,-1
125        endif
126        if minlines eq 1 then s=s[1:*]
127
128        err = 0
129        return, s
130
131err:    if !err eq -168 then begin
132          if not keyword_set(quiet) then print,' Non-standard text file format.'
133          free_lun, lun
134          return, s
135        endif
136        if not keyword_set(quiet) then print,$
137          ' Error in getfile: File '+file+' not opened.'
138        free_lun, lun
139        err = 1
140        return, -1
141
142        end
Note: See TracBrowser for help on using the repository browser.