pro spd_ui_validate_file,filename=filename,statuscode=statuscode,statusmsg=statusmsg,write=write
catch,Error_status
if (Error_status NE 0) then begin
statusmsg = !ERROR_STATE.MSG
statuscode = -99
catch,/cancel
return
endif
statuscode = 0
statusmsg = ''
if ~is_string(filename) then begin
statusmsg='"'+routine_name+'": Malformed filename'
statuscode=-1
return
endif
filename = (expand_path(filename))[0]
traceback = scope_traceback(/structure)
routine_name = traceback[n_elements(traceback)-2].routine
tgt_dirname=file_dirname(filename)
fi=file_info(tgt_dirname)
if keyword_set(write) then begin
if ~fi.exists then begin
statusmsg=string(tgt_dirname,format='("'+routine_name+': Failed. Directory ",A," does not exist.")')
statuscode=-2
return
endif else if ~fi.write then begin
statusmsg=string(tgt_dirname,format='("'+routine_name+': Failed. Directory ",A," is not writeable by you.")')
statuscode=-3
return
endif
fi=file_info(filename)
if (fi.directory) then begin
statusmsg=string(filename,format='("'+routine_name+': Failed: ",A," is a directory.")')
statuscode=-4
return
endif else if (fi.exists AND ~fi.write) then begin
statusmsg=string(filename,format='("'+routine_name+': Failed. File ",A," exists, and is not writeable by you.")')
statuscode=-5
return
end else if (fi.exists) then begin
statusmsg=string(filename,format='("'+routine_name+': File ",A," already exists. Do you wish to overwrite it?")')
answer=dialog_message(statusmsg,/question,/default_no, /center )
if (answer NE 'Yes') then begin
statusmsg=routine_name+': Save cancelled by user.'
statuscode=-6
return
endif
endif
endif else begin
fi=file_info(filename)
if (fi.directory) then begin
statusmsg=string(filename,format='("'+routine_name+': Failed ",A," is a directory.")')
statuscode=-4
return
endif else if (~fi.exists) then begin
statusmsg=string(filename,format='("'+routine_name+': Failed. File ",A," does not exist.")')
statuscode=-7
return
endif
endelse
end