; $Id$ ;----------------------------------------------------------------------- ;+ ; NAME: ; SAFE_OPENW ; ; PURPOSE: ; Safely open files while preventing unintentional overwrites. ; Requires user input to overwrite preexisting files. No input ; required to create files with unused filenames. ; Syntax and operation very similar to native OPENW in IDL. ; ; CATEGORY: ; ; CALLING SEQUENCE: ; SAFE_OPENW, LUN, FILENAME [, KEYWORDS] ; ; INPUTS: ; LUN - Logical unit number for file (INTEGER). ; Can be undefined if /GET_LUN keyword is used ; FILENAME - String name for file to open. ; ; KEYWORD PARAMETERS: ; Same keywords as OPENW. e.g. /GET_LUN will find an unused LUN ; ; OUTPUTS: ; ; SUBROUTINES: ; ; REQUIREMENTS: ; ; NOTES: ; ; EXAMPLE: ; ; MODIFICATION HISTORY: ; cdh, 29 Aug 2011: VERSION 1.00 ; ;- ; Copyright (C) 2011, Christopher Holmes, UC Irvine ; This software is provided as is without any warranty whatsoever. ; It may be freely used, copied or distributed for non-commercial ; purposes. This copyright notice must be kept with any copy of ; this software. If this software shall be used commercially or ; sold as part of a larger package, please contact the author. ; Bugs and comments should be directed to cdholmes@post.harvard.edu ; with subject "IDL routine safe_openw" ;----------------------------------------------------------------------- pro safe_openw, lun, fileName, _extra=_extra ; Check whether file exists FileExists = File_Test( Filename ) if ( FileExists ) then begin ; Print a warning print, '' print, 'WARNING! Output file already exists: ', Filename overwrite = '' ; Prompt user whether to continue read, overwrite, prompt='Overwrite existing file? (default: no) ' ; Check if user said yes, exit otherwise if ( (overwrite ne 'y' ) and $ (overwrite ne 'Y' ) and $ (overwrite ne 'yes') ) then begin lun = -1L return endif endif ; Open the file with write access openw, lun, fileName, _extra=_extra end