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