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

Last change on this file since 186 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 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        if !version.os_family EQ 'unix' then begin
84          spawn, 'cat '+file,  res
85          if res[0] NE '' then return, res ELSE return, ''
86        endif
87       
88        if (!version.release+0. ge 5.5) then begin
89          f = call_function('file_search', file, count = c)
90        endif else begin
91          f = findfile(file,count=c)
92        endelse
93        if c eq 0 then begin
94          err = 1
95          return,''
96        endif
97 
98        if n_elements(line) eq 0 and (!version.release+0. ge 5.6) then begin
99          lines = file_lines(file)
100          if lines eq 0 then begin
101            if not keyword_set(quiet) then print,' No lines in file.'
102            err = 2
103            return,-1
104          endif
105          minlines = 0
106        endif else minlines=1
107 
108        get_lun, lun
109        on_ioerror, err
110        openr, lun, file
111 
112        if n_elements(lines) ne 0 then begin
113          s = strarr(lines)
114          readf,lun,s
115        endif else begin
116          s = [' ']
117          t = ''
118          while not eof(lun) do begin
119            readf, lun, t
120            s = [s,t]
121          endwhile
122        endelse
123 
124        close, lun
125        free_lun, lun
126        if n_elements(s) eq minlines then begin
127          if not keyword_set(quiet) then print,' No lines in file.'
128          err = 2
129          return,-1
130        endif
131        if minlines eq 1 then s=s[1:*]
132 
133        err = 0
134        return, s
135 
136err:    if !err eq -168 then begin
137          if not keyword_set(quiet) then print,' Non-standard text file format.'
138          free_lun, lun
139          return, s
140        endif
141        if not keyword_set(quiet) then print,$
142          ' Error in getfile: File '+file+' not opened.'
143        free_lun, lun
144        err = 1
145        return, -1
146 
147        end
Note: See TracBrowser for help on using the repository browser.