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

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

modification of headers : mainly blanks around = sign for keywords in declaration of function and pro

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
RevLine 
[2]1;+
[142]2;
3; @file_comments
4; Read a text file into a string array.
5;
6; @param filein {in}{required}
7; text file name.
[223]8;
9; @keyword ERROR
[142]10; =err  error flag: 0=ok, 1=file not opened,
11; 2=no lines in file.
[223]12;
13; @keyword QUIET
[142]14; means give no error message.
[223]15;
[142]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.
[223]20;
21; @keyword FIND
[163]22; search the file in the all !path directories (use
[142]23; find.pro)
24;
25; @keyword HELP
[223]26;
[142]27; @returns
[223]28;
[142]29; @history
[2]30;       R. Sterner, 20 Mar, 1990
[115]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.
[2]37;
[157]38;       S. Masson (smasson\@lodyc.jussieu.fr) 4 Feb 2002
[237]39;       search the file in the all !path directories (use <pro>find</pro>)
[260]40;       when using /find keyword. Use <proidl>SPAWN</proidl>, 'cat...' for
[237]41;       unix os.
[2]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.
[142]48;
49; @version
50; $Id$
51;
[2]52;-
[327]53FUNCTION getfile, filein, error=err $
54                , HELP=hlp, QUIET=quiet, LINES=lines, FIND=find
[2]55;
[115]56  compile_opt idl2, strictarrsubs
57;
[223]58
[262]59if (n_params(0) lt 1) or keyword_set(hlp) then begin
60ras = report([ $
[240]61          ' Read a text file into a string array.', $
[262]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
[115]74;
75        if keyword_set(find) then begin
76          file = find(filein)
77          file = file[0]
78          if file EQ 'NOT FOUND' then begin
[226]79            print, ' Error in getfile: File '+filein+' not found.'
[115]80            return, -1
81          endif
82        ENDIF ELSE file = filein
[223]83
[115]84        if (!version.release+0. ge 5.5) then begin
85          f = call_function('file_search', file, count = c)
[262]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
[223]93
[262]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
[223]103
[262]104        get_lun, lun
105        on_ioerror, err
106        openr, lun, file
[223]107
[262]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
[223]119
[262]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:*]
[223]128
[262]129        err = 0
130        return, s
[223]131
[262]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
[223]142
[262]143        end
Note: See TracBrowser for help on using the repository browser.