From fb7ba61b4b7e4bc0260a288cd07335321f61ad18 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Sun, 20 Jan 2019 00:08:14 +0000 Subject: [PATCH] JAL-3141 Changed suffix position to after the extension (removed extension detection altogether), and changed initial defaults. --- src/jalview/gui/Desktop.java | 2 +- src/jalview/gui/Preferences.java | 2 +- src/jalview/io/BackupFilenameFilter.java | 16 ++++------ src/jalview/io/BackupFilenameParts.java | 19 +++++------- src/jalview/io/BackupFiles.java | 48 ++++++++++-------------------- 5 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index aefcf08..1a1fa45 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1616,7 +1616,7 @@ public class Desktop extends jalview.jbgui.GDesktop // autoSave indicates we already have a file and don't need to ask boolean autoSave = projectFile != null && !saveAs && BackupFiles.getEnabled(); - System.out.println("autoSave="+autoSave+", projectFile='"+projectFile+"', saveAs="+saveAs+", Backups "+(BackupFiles.getEnabled()?"enabled":"disabled")); + //System.out.println("autoSave="+autoSave+", projectFile='"+projectFile+"', saveAs="+saveAs+", Backups "+(BackupFiles.getEnabled()?"enabled":"disabled")); boolean approveSave = false; if (!autoSave) diff --git a/src/jalview/gui/Preferences.java b/src/jalview/gui/Preferences.java index 7258957..5740759 100755 --- a/src/jalview/gui/Preferences.java +++ b/src/jalview/gui/Preferences.java @@ -555,7 +555,7 @@ public class Preferences extends GPreferences enableBackupFiles .setSelected(Cache.getDefault(BackupFiles.ENABLED, true)); suffixTemplate.setText(Cache.getDefault(BackupFiles.SUFFIX, - "-v" + BackupFiles.NUM_PLACEHOLDER)); + "-bak" + BackupFiles.NUM_PLACEHOLDER)); suffixDigitsSpinner .setValue(Cache.getDefault(BackupFiles.SUFFIX_DIGITS, 3)); suffixReverse.setSelected( diff --git a/src/jalview/io/BackupFilenameFilter.java b/src/jalview/io/BackupFilenameFilter.java index db1645a..916d660 100644 --- a/src/jalview/io/BackupFilenameFilter.java +++ b/src/jalview/io/BackupFilenameFilter.java @@ -13,21 +13,16 @@ public class BackupFilenameFilter implements FilenameFilter public int digits; - public String extension; - - public BackupFilenameFilter(String base, String template, int digits, - String extension) + public BackupFilenameFilter(String base, String template, int digits) { this.base = base; this.template = template; this.digits = digits; - this.extension = extension; } @Override public boolean accept(File dir, String filename) { - boolean ret = false; try { File file = new File( @@ -44,18 +39,17 @@ public class BackupFilenameFilter implements FilenameFilter } BackupFilenameParts bffp = new BackupFilenameParts(filename, base, - template, digits, extension); - ret = bffp.isBackupFile(); - return ret; + template, digits); + return bffp.isBackupFile(); } public static String getBackupFilename(int index, String base, - String template, int digits, String extension) + String template, int digits) { String numString = String.format("%0" + digits + "d", index); String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER, numString); - String backupfilename = base + backupSuffix + extension; + String backupfilename = base + backupSuffix; return backupfilename; } diff --git a/src/jalview/io/BackupFilenameParts.java b/src/jalview/io/BackupFilenameParts.java index b9f766e..93aa602 100644 --- a/src/jalview/io/BackupFilenameParts.java +++ b/src/jalview/io/BackupFilenameParts.java @@ -14,28 +14,23 @@ public class BackupFilenameParts String templateEnd; - String extension; - boolean isBackupFile; - public BackupFilenameParts(File file, String base, String template, int digits, - String extension) + public BackupFilenameParts(File file, String base, String template, int digits) { - this(file.getName(), base, template, digits, extension); + this(file.getName(), base, template, digits); } public BackupFilenameParts(String filename, String base, String template, - int digits, String extension) + int digits) { this.isBackupFile = false; // calculate minimum length of a backup filename int minlength = base.length() + template.length() - - BackupFiles.NUM_PLACEHOLDER.length() + digits - + extension.length(); + - BackupFiles.NUM_PLACEHOLDER.length() + digits ; - if (!(filename.startsWith(base) && filename.endsWith(extension) - && filename.length() >= minlength)) + if (!(filename.startsWith(base) && filename.length() >= minlength)) { // non-starter return; @@ -52,13 +47,13 @@ public class BackupFilenameParts } int startLength = base.length() + templateStart.length(); - int endLength = templateEnd.length() + extension.length(); + int endLength = templateEnd.length(); String numString = filename.substring(startLength, filename.length() - endLength); if (filename.length() >= startLength + digits + endLength && filename.startsWith(base + templateStart) - && filename.endsWith(templateEnd + extension) + && filename.endsWith(templateEnd) && numString.matches("[0-9]+")) { this.base = base; diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 7a026f9..1553a41 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -16,7 +16,7 @@ import java.util.TreeMap; * BackupFiles used for manipulating (naming rolling/deleting) backup/version files when an alignment or project file is saved. * User configurable options are: * BACKUPFILES_ENABLED - boolean flag as to whether to use this mechanism or act as before, including overwriting files as saved. - * BACKUPFILES_SUFFIX - a template to insert just before the file extension. Use '%n' to be replaced by a 0-led SUFFIX_DIGITS long integer. + * BACKUPFILES_SUFFIX - a template to insert after the file extension. Use '%n' to be replaced by a 0-led SUFFIX_DIGITS long integer. * BACKUPFILES_NO_MAX - flag to turn off setting a maximum number of backup files to keep. * BACKUPFILES_ROLL_MAX - the maximum number of backupfiles to keep for any one alignment or project file. * BACKUPFILES_SUFFIX_DIGITS - the number of digits to insert replace %n with (e.g. BACKUPFILES_SUFFIX_DIGITS = 3 would make "001", "002", etc) @@ -48,6 +48,8 @@ public class BackupFiles private static final String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS; + private static final String TEMP_FILE_EXT = ".tmp"; + // file - File object to be backed up and then updated (written over) private File file; @@ -90,7 +92,7 @@ public class BackupFiles // REVERSE_ORDER public BackupFiles(File file) { - this(file, "-v" + NUM_PLACEHOLDER, false, 4, 3, false); + this(file, ".v" + NUM_PLACEHOLDER, false, 4, 3, false); } public BackupFiles(File file, @@ -115,11 +117,11 @@ public class BackupFiles { String tempfilename = file.getName(); File tempdir = file.getParentFile(); - temp = File.createTempFile(tempfilename, ".tmp", tempdir); + temp = File.createTempFile(tempfilename, TEMP_FILE_EXT, tempdir); } else { - temp = File.createTempFile(DEFAULT_TEMP_FILE, ".tmp"); + temp = File.createTempFile(DEFAULT_TEMP_FILE, TEMP_FILE_EXT); } } catch (IOException e) { @@ -200,6 +202,7 @@ public class BackupFiles public boolean renameTempFile() { + System.out.println("RENAMING TEMP FILE '"+tempFile.getName() + "' TO '"+file.getName()+"'"); // DELETEME return tempFile.renameTo(file); } @@ -231,15 +234,6 @@ public class BackupFiles } String filename = file.getName(); String basename = filename; - String extension = ""; - int dotcharpos = filename.lastIndexOf('.'); - // don't split filenames with the last '.' at the very beginning or - // very end of the filename - if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1)) - { - basename = filename.substring(0, dotcharpos); - extension = filename.substring(dotcharpos); // NOTE this includes the '.' - } boolean ret = true; // Create/move backups up one @@ -248,8 +242,7 @@ public class BackupFiles // find existing backup files BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, - digits, - extension); + digits); File[] backupFiles = dirFile.listFiles(bff); int nextIndexNum = 0; String confirmDeleteExtraInfo = null; @@ -261,7 +254,7 @@ public class BackupFiles } else { - TreeMap bfTreeMap = sortBackupFilesAsTreeMap(backupFiles, basename, extension); + TreeMap bfTreeMap = sortBackupFilesAsTreeMap(backupFiles, basename); if (reverseOrder) { @@ -290,7 +283,7 @@ public class BackupFiles // int n = tempMax - m; String backupfilename = dir + File.separatorChar + BackupFilenameFilter.getBackupFilename(n, basename, - suffix, digits, extension); + suffix, digits); File backupfile_n = new File(backupfilename); if (!backupfile_n.exists()) @@ -305,7 +298,7 @@ public class BackupFiles // move the largest (max) rolled file to a temp file and add to the delete list try { - File temp = File.createTempFile(backupfilename, ".tmp", + File temp = File.createTempFile(backupfilename, TEMP_FILE_EXT, dirFile); backupfile_n.renameTo(temp); @@ -362,7 +355,7 @@ public class BackupFiles // Let's make the new backup file!! yay, got there at last! String latestBackupFilename = dir + File.separatorChar + BackupFilenameFilter.getBackupFilename(nextIndexNum, basename, - suffix, digits, extension); + suffix, digits); File latestBackupFile = new File(latestBackupFilename); ret = ret && file.renameTo(latestBackupFile); @@ -419,14 +412,14 @@ public class BackupFiles } } - private TreeMap sortBackupFilesAsTreeMap(File[] backupFiles, String basename, String extension) { + private TreeMap sortBackupFilesAsTreeMap(File[] backupFiles, String basename) { // sort the backup files (based on integer found in the suffix) using a // precomputed Hashmap for speed Map bfHashMap = new HashMap<>(); for (int i = 0; i < backupFiles.length; i++) { File f = backupFiles[i]; - BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, digits, extension); + BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, digits); bfHashMap.put(bfp.indexNum(), f); } TreeMap bfTreeMap = new TreeMap<>(); @@ -497,18 +490,9 @@ public class BackupFiles String filename = file.getName(); String basename = filename; - String extension = ""; - int dotcharpos = filename.lastIndexOf('.'); - // don't split of filenames with the last '.' at the very beginning or - // very end of the filename - if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1)) - { - basename = filename.substring(0, dotcharpos); - extension = filename.substring(dotcharpos); // NOTE this includes the '.' - } // find existing backup files - BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits, extension); + BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits); backupFiles = dirFile.listFiles(bff); // is clone needed? // sort the backup files (based on integer found in the suffix) using a @@ -518,7 +502,7 @@ public class BackupFiles { File f = backupFiles[i]; BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, - digits, extension); + digits); bfHashMap.put(bfp.indexNum(), f); } TreeMap bfTreeMap = new TreeMap<>(); -- 1.7.10.2