ed.ex :: file backup routine

new topic     » topic index » view thread      » older message » newer message

I believe this routine is fairly "robust", but I tend to get lost in loops. Any suggestions for improvement will certainly be appreciated.

The routine simply appends the tilde symbol to the current file name. If a backup file with that name already exists, you will be given the option of over writing the existing back up file, or renaming the file. If you choose to rename your file, and the file name you enter already exists, you will continue to be prompted, until you enter one not found in the current directory.

Rots 'a Ruck! Use at your own risk...

procedure get_escape(boolean help) 
 
-- if you wish to have the backup hot-key displayed in the top line menu: 
-- add insert wherever you wish into list of "first_bold" routines: 
-- If you have a small monitor, it is not necessary to display the 
-- hot-key in order to use it.  
                 
                first_bold("~ ") 
		first_bold("help ") 
		first_bold("find ") 
		first_bold("replace ") 
 
 
-- add the symbol "~" to the string of "hot" keys in the following line: 
-- the routine you add next will be launched by pressing "ESC" + "~" 
-- 
command = key_gets("hcqswnedfrlm~", {}) & ' ' 
-- 
-- insert the following elsif clause into the 
-- subsequent block of code for the 
-- ESCAPE hot-key actions: 
 
elsif command[1] = '~' then 
	integer BAD = 0, GOOD = 1, status = BAD 
		 
	    if not file_exists(file_name) then  
		-- in "ed.ex" parlance "write" file: 
		save_file(file_name) 
		stop = FALSE 
	    end if  
		 
	    sequence backup_file_name = file_name & "~"  
	    if not file_exists(backup_file_name) then 
		status = eu_filesys:copy_file(file_name, backup_file_name, 0)  
	    end if 
		 
	    while file_exists(backup_file_name) do 
		set_top_line(" ") 
		puts(SCREEN, "Backup file already exists - overwrite? ") 
		answer = key_gets("yn", {}) 
		if find('y', answer) then 
			-- overwrite existing back-up file: 
			status = eu_filesys:copy_file(file_name, backup_file_name, 1) 
		exit 
		else 
		    loop do 
			set_top_line("new file name for " & backup_file_name & ": ") 
			backup_file_name = delete_trailing_white(key_gets("", file_history)) 
			status = eu_filesys:copy_file(file_name, backup_file_name, 0)  
			until status = GOOD 
		    end loop 
		exit 
		end if 
	    end while 
 
	    if status = GOOD then 
		set_top_line("file back-up succeeded! ")  
	    else 
		-- just in case. This shouldn't happen. 
		set_top_line("file back-up FAILED! ")  
	    end if 

Regards, Kenneth Rhodes

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu