[chronojump] encoderConfiguration "unnamed_copy_copy_copy" -> "unnamed_copy3"



commit 5d54760be754bd31888b7b93a1eb38d53a487ed2
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon May 15 12:07:56 2017 +0200

    encoderConfiguration "unnamed_copy_copy_copy" -> "unnamed_copy3"

 src/gui/chronojump.cs              |    2 +
 src/gui/encoder.cs                 |    2 +-
 src/gui/encoderConfiguration.cs    |    6 ++--
 src/sqlite/encoderConfiguration.cs |   68 ++++++++++++++++++++++++++++++++++--
 4 files changed, 71 insertions(+), 7 deletions(-)
---
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 6481d63..5ac448d 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -653,6 +653,8 @@ public partial class ChronoJumpWindow
                pingThread.Start();
 
                //moveStartTestInitial();
+
+               //SqliteEncoderConfiguration.IfNameExistsAddSuffixDoTests();
        }
 
 
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index c08a58d..a8ff4fa 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -1539,7 +1539,7 @@ public partial class ChronoJumpWindow
                                //if user has deleted this econfSO, create it again
                                if(econfSO.uniqueID == -1)
                                {
-                                       string name = 
SqliteEncoderConfiguration.IfNameExistsAddSuffix(Catalog.GetString("Unnamed"), Catalog.GetString("copy"));
+                                       string name = 
SqliteEncoderConfiguration.IfNameExistsAddSuffix(Catalog.GetString("Unnamed"), "_" + 
Catalog.GetString("copy"));
 
                                        econfSO = new EncoderConfigurationSQLObject(
                                                        -1,                             //uniqueID
diff --git a/src/gui/encoderConfiguration.cs b/src/gui/encoderConfiguration.cs
index 5bb7a78..25a15b8 100644
--- a/src/gui/encoderConfiguration.cs
+++ b/src/gui/encoderConfiguration.cs
@@ -668,7 +668,7 @@ public class EncoderConfigurationWindow
                                        else if(econfSO.name != null && econfSO.name != "")
                                        {
                                                //add more suffixes until name is unique
-                                               econfSO.name = 
SqliteEncoderConfiguration.IfNameExistsAddSuffix(econfSO.name, Catalog.GetString("copy"));
+                                               econfSO.name = 
SqliteEncoderConfiguration.IfNameExistsAddSuffix(econfSO.name, "_" + Catalog.GetString("copy"));
 
                                                SqliteEncoderConfiguration.MarkAllAsUnactive(false, 
encoderGI);
                                                econfSO.active = true;
@@ -785,7 +785,7 @@ public class EncoderConfigurationWindow
                         * if name has changed, then check if newname already exists on database
                         * if exists add _copy recursively
                         */
-                       newName = SqliteEncoderConfiguration.IfNameExistsAddSuffix(newName, 
Catalog.GetString("copy"));
+                       newName = SqliteEncoderConfiguration.IfNameExistsAddSuffix(newName, "_" + 
Catalog.GetString("copy"));
                }
                //update entry_save_name if needed
                if(newName != entry_save_name.Text)
@@ -824,7 +824,7 @@ public class EncoderConfigurationWindow
                        //add a suffix
                        econfSO.name += "_"  + Catalog.GetString("copy");
                        //add more suffixes until name is unique
-                       econfSO.name = SqliteEncoderConfiguration.IfNameExistsAddSuffix(econfSO.name, 
Catalog.GetString("copy"));
+                       econfSO.name = SqliteEncoderConfiguration.IfNameExistsAddSuffix(econfSO.name, "_" + 
Catalog.GetString("copy"));
 
                        SqliteEncoderConfiguration.MarkAllAsUnactive(false, encoderGI);
                        econfSO.active = true;
diff --git a/src/sqlite/encoderConfiguration.cs b/src/sqlite/encoderConfiguration.cs
index db7224d..41b7440 100644
--- a/src/sqlite/encoderConfiguration.cs
+++ b/src/sqlite/encoderConfiguration.cs
@@ -86,16 +86,78 @@ class SqliteEncoderConfiguration : Sqlite
                closeIfNeeded(dbconOpened);
        }
 
+       /*
+        * IfNameExistsAddSuffix starts --------------------->
+        */
+       /*
+        * this method check if a name exists.
+        * if exists, add a suffix, like _copy
+        * but if the string already ends with _copy add a number: _copy2
+        * but if the number already exists, like _copy21, convert into _copy22
+        * always check that the new string exists.
+        *
+        * The main reason of this method is not to have a:
+        * unnamed_copy_copy_copy_copy (that's very ugly and breaks the interface), and have instead:
+        * unnamed_copy4
+        */
        public static string IfNameExistsAddSuffix(string name, string suffix)
        {
-               if(Sqlite.Exists(false, Constants.EncoderConfigurationTable, name))
+               Sqlite.Open();
+               if(Sqlite.Exists(true, Constants.EncoderConfigurationTable, name))
                {
                        do {
-                               name += "_" + suffix;
-                       } while (Sqlite.Exists(false, Constants.EncoderConfigurationTable, name));
+                               name = ifNameExistsAddSuffixDo(name, suffix);
+                       } while (Sqlite.Exists(true, Constants.EncoderConfigurationTable, name));
                }
+               Sqlite.Close();
                return name;
        }
+       private static string ifNameExistsAddSuffixDo(string str, string suffix)
+       {
+               //suffixStarts will point to the start of suffix (the last suffix if there's > 1)
+               int suffixStarts = str.LastIndexOf(suffix);
+
+               // 1) if there's no suffix on str: add it
+               if(suffixStarts == -1)
+                       return str + suffix;
+
+               // 2) check if there's a number at the end of suffix
+               int numberShouldStart = suffixStarts + suffix.Length;
+               string strBeforeNum = str.Substring(0, numberShouldStart);
+               string strNum = str.Substring(numberShouldStart);
+
+               //Console.WriteLine("suffixStarts: " + suffixStarts.ToString() + "; numberShouldStart: " + 
numberShouldStart + "; strNum: " + strNum);
+
+               // 2.a) there's nothing after the suffix, write a "2"
+               if(strNum.Length == 0)
+                       return str + "2";
+
+               // 2.b) after the last suffix there's something but is not a whole number, add suffix again
+               // eg: unnamed_copyk will be unnamed_copyk2
+               // but unnamed_copyk2 will be unnamed_copyk22 ...
+               if(! Util.IsNumber(strNum, false))
+                       return str + "2";
+
+               // 2.c) after the suffix, there's a whole number, add +1 to this number
+               return strBeforeNum + (Convert.ToInt32(strNum) +1);
+       }
+       public static void IfNameExistsAddSuffixDoTests()
+       {
+               string suffix = "_copy";
+               string [] tests = {
+                       "_copy2", "_copy75", "_copy",
+                       "unnamed_copy2", "unnamed_copy75", "unnamed_copy",
+                       "lalala_copy", "_copy2_copy2", "hello_good_morning_copy",
+                       "how are you 21", "_copy2k", "_copy2k2" };
+
+               foreach (string test in tests)
+                       LogB.Information(test + " -> " + ifNameExistsAddSuffixDo(test, suffix));
+       }
+
+       /*
+        * <-------------------------- IfNameExistsAddSuffix ends
+        */
+
 
        //called on capture, recalculate, load
        public static void UpdateActive(bool dbconOpened, Constants.EncoderGI encoderGI, EncoderConfiguration 
econf)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]