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

Last change on this file since 358 was 358, checked in by pinsard, 16 years ago

small typo

  • 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; =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;-
53FUNCTION getfile, filein, ERROR=err $
54                , HELP=hlp, QUIET=quiet, LINES=lines, FIND=find
55;
56  compile_opt idl2, strictarrsubs
57;
58
59if (n_params(0) lt 1) or keyword_set(hlp) then begin
60ras = report([ $
61          ' Read a text file into a string array.', $
62' s = getfile(f)', $
63'   f = text file name.      in', $
64'   s = string array.        out', $
65' Keywords:', $
66'   ERROR=err  error flag: 0=ok, 1=file not opened,', $
67'     2=no lines in file.', $
68'   /QUIET means give no error message.', $
69'   LINES=n  Number of lines to read (def=all).', $
70'     Much faster if number of lines is known.', $
71'     Automatic for IDL 5.6 or later.'])
72return, -1
73endif
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 found.'
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.