Viewing contents of file '../idllib/contrib/tappin/graffer/graff_get.pro'
function Graff_get, pdefs, f, no_set=no_set, recover=recover, $
                    no_warn=no_warn, previous_name=previous_name

;+
; GRAFF_GET
;	Get a graffer dataset from a file
;
; Usage:
;	iopen = graff_get(pdefs[, f])
;
; Return Value:
;	iopen	int	1 If file was opened; 0 if not.
;
; Argument:
;	pdefs	struct	in/out	The graffer data structure.
;	f	string	input	The file to read.
; Keywords:
;	no_set	input	If set, then don't try to set up the widget
;			values (because the widgets aren't there)
;	recover input	If set, then try to recover the file from the
;			autosave file
;	no_warn	input	If set, then don't print a warning message if
;			file is not opened sucessfully.
;	previous_name string	input	The name of the last file opened.
;
; History:
;	Original: 16/8/95; SJT
;	Remove file argument (use pdefs.name): 17/8/95; SJT
;	Add facilities for string-type data and restore filename: 18/8/95; SJT
;	Change to a function returning 0 or 1 & add NO_WARN key: 11/6/96; SJT
;	Reduce to a wrapper for an ASCII and a binary mode reader:
;	14/1/96; SJT
;-

if (n_params() eq 1) then begin
    junk = findfile(pdefs.dir, count = num)
    if (num ge 1) then path = pdefs.dir $
    else cd, current = path
    if (widget_info(pdefs.ids.graffer, /valid)) then widget_control, $
      pdefs.ids.graffer, sensitive = 0
    f = gr_pickfile(/read, filter = '*.grf', title = 'Graffer Restore', $
                    /must, path = path, pop = pdefs.popflag, current = $
                    previous_name)
    if (widget_info(pdefs.ids.graffer, /valid)) then widget_control, $
      pdefs.ids.graffer, sensitive = 1
    if (f eq '') then return, 0
endif


gr_split_dir, f, dir            ; Split the name and directory after
                                ; opening

pdefs.name = f
pdefs.dir = gr_get_full_dir(dir)

on_ioerror, nofile

recname = dir+'#'+f+'#'
fname = dir+f
rcfile = findfile(recname, count = nrf)

if (nrf eq 1 and not keyword_set(recover)) then icont = gr_recover(0) $
else if (nrf eq 0 and keyword_set(recover)) then icont = gr_recover(1) $
else icont = keyword_set(recover)

if (icont eq -1) then begin
    if (not keyword_set(no_warn)) then  $
      graff_msg, pdefs.ids.message, ['Failed to open file: '+dir+f, $
                                     'User specified cancel']
    return, 0
endif else if (icont eq 0) then begin
    openr, ilu, /get, fname 
endif else begin
    if (not keyword_set(no_warn)) then  $
      graff_msg, pdefs.ids.message, 'Opening autosave file'
    openr, ilu, /get, recname
    fname = recname
endelse

on_ioerror, null

rs = bytarr(7)
readu, ilu, rs                  ; read the overall header
free_lun, ilu
rs = string(rs)

case (rs) of
    'GRAFFER': return, gr_get_bin(pdefs, fname, no_set = $
                                  keyword_set(no_set), no_warn = $
                                  keyword_set(no_warn), resave = $
                                  icont) ; Binary file 
    'Graffer': return, gr_get_asc(pdefs, fname, no_set = $
                                  keyword_set(no_set), no_warn = $
                                  keyword_set(no_warn)) ; ASCII file
    Else: begin
        graff_msg, pdefs.ids.message, "Not a GRAFFER file"
        stop
        return, 0
    end
endcase

Nofile:
if (not keyword_set(no_warn)) then  $
    graff_msg, pdefs.ids.message, ['Failed to open file: '+dir+f, $
                                   !Err_string]

return, 0

end