1. ed.ex :: file backup routine

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 message » categorize

2. Re: ed.ex :: file backup routine

K_D_R said...

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

	    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 

Shouldn't the backup be made before you save the file?

K_D_R said...
	    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? ") 

First you check if the backup name exists, and if not you copy it. But then you check it again. Won't the while loop always be true?

So if the backup name does not exist, won't it prompt you to overwrite anyways? That is, won't it always prompt you to overwrite, no matter what?

K_D_R said...
	    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 

Why use a while loop at all here? The actual looping is done in the loop-do loop. You use a while loop but exit it after the first iteration. (Even this is done inefficiently - you exit at the end of the if-then clause and also at the end of the else clause, instead of having a single exit after the end-if statement.)

From this and the previous clause, I'm wondering if the while loop should be replaced and be turned into the else clause of this if statement.

K_D_R said...
	    if not file_exists(backup_file_name) then 
		status = eu_filesys:copy_file(file_name, backup_file_name, 0)  

Finally, I notice that when you decide not to overwrite the default backup file but choose a new name, you must choose a name that doesn't exist already. You can't pick a name of an existing temporary file that you want to overwrite. You are never given the choice to overwrite except for the very first time. Most of the time you probably wouldn't want to overwrite anyways, but shouldn't ed.ex ask everytime? Just in case?

new topic     » goto parent     » topic index » view message » categorize

3. Re: ed.ex :: file backup routine

K_D_R said...
	integer BAD = 0, GOOD = 1, status = BAD 

Surely

	constant BAD = 0, GOOD = 1 
	integer status = BAD 
new topic     » goto parent     » topic index » view message » categorize

4. Re: ed.ex :: file backup routine

Thanks to Jim and Pete. Basically, I think all the points you made are valid. I will rewrite the routine to address the points made in your comments, and post again.

Regards, Kenneth Rhodes

new topic     » goto parent     » topic index » view message » categorize

5. Re: ed.ex :: file backup routine

jimcbrown said...
K_D_R said...

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

	    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 

Shouldn't the backup be made before you save the file?

In the first instance, there is no file on the disk to backup, therefore the file is saved before proceeding. Granted, it should be a rare case where a backup would be executed without the file having been previously saved.

But your right, in the subsequent routines I should save the current file after backing up the file on the disk.

As I stated in another post, I am re-writing the routines, taking into account the points you and Pete have made.

Thanks again for your comments.

Regards,

new topic     » goto parent     » topic index » view message » categorize

6. Re: ed.ex :: file backup routine

Lets see if this version can pass muster:

 
-- sequence backup_file_history added to the following code block 
-- early in the ed.ex code: 
sequence file_history, command_history, search_history, replace_history,  
backup_file_history --<---- 
 
file_history = {} 
command_history = {} 
search_history = {} 
replace_history = {} 
backup_file_history = {}  --<--- 
 
--------------------------------------------------------------------------- 
 
elsif command[1] = '~' then 
	    status = 0 -- declared as integer at beginning of procedure get_escape 
	    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)  
		save_file(file_name) 
		stop = FALSE 
		set_top_line(" ") 
		set_top_line(file_name & " <backed up as> " & backup_file_name) 
	    else 
		set_top_line(" ") 
		set_top_line("Backup file " & backup_file_name &  " already exists - overwrite?  ") 
		first_bold("y or ") 
		first_bold("n : ") 
		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) 
			save_file(file_name) 
			stop = FALSE 
			set_top_line(file_name & " <backed up as> " & backup_file_name) 
		else 
		    loop do 
			set_top_line("rename or overwrite existing ~ file: " & backup_file_name & " ? :  ") 
			sequence backup_files = dir("*.*~")  
			for x = 3 to length(backup_files) do  
			    backup_file_history = update_history(backup_file_history, backup_files[x][D_NAME]) 
			end for 
 
                        -- this code allows you to select existing back up files or enter 
                        -- a completely new file name: 
			backup_file_name = delete_trailing_white(key_gets("", backup_file_history)) 
			if find(backup_file_name, backup_file_history)then 
			    set_top_line("overwrite file: " & backup_file_name & " ?  ") 
			    first_bold("y or ") 
			    first_bold("n : ") 
			    answer = key_gets("yn", {}) 
			    if find('y', answer) then 
				status = eu_filesys:copy_file(file_name, backup_file_name, 1)  
				save_file(file_name) 
				stop = FALSE 
				set_top_line(file_name & " <backed up as> " &  backup_file_name) 
			    end if 
			 else status = eu_filesys:copy_file(file_name, backup_file_name, 0) 
				save_file(file_name) 
				stop = FALSE 
				set_top_line(" ") 
				set_top_line(file_name & " <backed up as> " &  backup_file_name) 
			 end if 
			until status = 1 
		    end loop 
		end if 
	    end if 

Regards, Kenneth Rhodes

new topic     » goto parent     » topic index » view message » categorize

7. Re: ed.ex :: file backup routine

Looks fine to me. I guess you are intending the contents of the original and backup files to be the same. For some reason I though the backup file would hold the contents that the original file had before it was overwritten.

The loop is ok. There might be a way or two to do it more efficently, but it's pretty good overall.

K_D_R said...

Lets see if this version can pass muster:

 
-- sequence backup_file_history added to the following code block 
-- early in the ed.ex code: 
sequence file_history, command_history, search_history, replace_history,  
backup_file_history --<---- 
 
file_history = {} 
command_history = {} 
search_history = {} 
replace_history = {} 
backup_file_history = {}  --<--- 
 
--------------------------------------------------------------------------- 
 
elsif command[1] = '~' then 
	    status = 0 -- declared as integer at beginning of procedure get_escape 
	    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)  
		save_file(file_name) 
		stop = FALSE 
		set_top_line(" ") 
		set_top_line(file_name & " <backed up as> " & backup_file_name) 
	    else 
		set_top_line(" ") 
		set_top_line("Backup file " & backup_file_name &  " already exists - overwrite?  ") 
		first_bold("y or ") 
		first_bold("n : ") 
		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) 
			save_file(file_name) 
			stop = FALSE 
			set_top_line(file_name & " <backed up as> " & backup_file_name) 
		else 
		    loop do 
			set_top_line("rename or overwrite existing ~ file: " & backup_file_name & " ? :  ") 
			sequence backup_files = dir("*.*~")  
			for x = 3 to length(backup_files) do  
			    backup_file_history = update_history(backup_file_history, backup_files[x][D_NAME]) 
			end for 
 
                        -- this code allows you to select existing back up files or enter 
                        -- a completely new file name: 
			backup_file_name = delete_trailing_white(key_gets("", backup_file_history)) 
			if find(backup_file_name, backup_file_history)then 
			    set_top_line("overwrite file: " & backup_file_name & " ?  ") 
			    first_bold("y or ") 
			    first_bold("n : ") 
			    answer = key_gets("yn", {}) 
			    if find('y', answer) then 
				status = eu_filesys:copy_file(file_name, backup_file_name, 1)  
				save_file(file_name) 
				stop = FALSE 
				set_top_line(file_name & " <backed up as> " &  backup_file_name) 
			    end if 
			 else status = eu_filesys:copy_file(file_name, backup_file_name, 0) 
				save_file(file_name) 
				stop = FALSE 
				set_top_line(" ") 
				set_top_line(file_name & " <backed up as> " &  backup_file_name) 
			 end if 
			until status = 1 
		    end loop 
		end if 
	    end if 

Regards, Kenneth Rhodes

new topic     » goto parent     » topic index » view message » categorize

8. Re: ed.ex :: file backup routine

jimcbrown said...

Looks fine to me. I guess you are intending the contents of the original and backup files to be the same. For some reason I though the backup file would hold the contents that the original file had before it was overwritten.

The loop is ok. There might be a way or two to do it more efficently, but it's pretty good overall.

Thanks for looking at the code again - it had "problems" so I rewrote it again - this time without loops.

My intent is to copy/backup the file on the disk and then save the "live" or current file to disk. My desire is to have a backup trail. I am now thinking about about having the back up routine append "~" to previous backups. In other words, if I back-up myfile.ex, myfile.ex gets written to the file as is, a normal save. The previous version would be copied or moved to myfile.ex, unless there is already a myfile.ex in which case that file would be moved/renamed to myfile.ex~ and so on perhaps 3 or 4 levels deep. The oldest file, say myfile.ex~~ would be trashed/overwritten on the next back-up. LOL, maybe when I can master an elementary "loops". Hope springs eternal.

This is my latest effort:

elsif command[1] = '~' then 
	    status = 0 
	    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 & "~" 
	    sequence backup_files = dir("*.*~") 
	    for x = 3 to length(backup_files) do 
		backup_file_history = update_history(backup_file_history, backup_files[x][D_NAME]) 
	    end for 
	     
	    if not file_exists(backup_file_name) then 
		status = eu_filesys:copy_file(file_name, backup_file_name, 0) 
		save_file(file_name) 
		stop = FALSE 
		set_top_line(" ") 
		set_top_line(file_name & " <backed up as> " & backup_file_name) 
	    else 
		-- overwrite existing file 
		set_top_line(" ") 
		set_top_line("file: " & backup_file_name  & " already exists - overwrite?  ") 
		first_bold("y or ") 
		first_bold("n : ") 
		answer = key_gets("yn", {}) 
		if find('y', answer) then 
		    status = eu_filesys:copy_file(file_name, backup_file_name, 1) 
		    save_file(file_name) 
		    stop = FALSE 
		    set_top_line(" ") 
		    set_top_line(file_name & " <backed up as> " & backup_file_name) 
		else 
		    -- rename backup_file_name to filename not in backup history 
		    set_top_line(" ") 
		    set_top_line("rename " & backup_file_name & " ? :  ") 
		    first_bold("y or ") 
		    first_bold("n : ") 
		    answer = key_gets("yn", {}) 
		    if find('y', answer) then 
			set_top_line(" ") 
			set_top_line("new file name: ") 
			loop  do 
			    answer = delete_trailing_white(key_gets("",backup_file_history)) 
			    until length(answer) != 0 and not find(answer, backup_file_history) 
			end loop 
			backup_file_name = answer 
			backup_file_history = update_history(backup_file_history, answer) 
			stop = TRUE 
			status = eu_filesys:copy_file(file_name, backup_file_name, 0) 
			save_file(file_name) 
			stop = FALSE 
			set_top_line(file_name & " <backed up as> " & backup_file_name) 
		    else 
			-- overwrite an existing backup_file_name from backup_file_history 
			answer = delete_trailing_white(key_gets("",backup_file_history)) 
			if length(answer) !=0 then 
			    backup_file_name = answer 
			    backup_file_history = update_history(backup_file_history, answer) 
			    stop = TRUE 
			end if 
			status = eu_filesys:copy_file(file_name, backup_file_name, 1) 
			save_file(file_name) 
			stop = FALSE 
		    end if  
	    end if 
	end if 

Regards, Kenneth Rhodes

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu