From bb5e7bbe24e284af5c8495c42f736b6e87e77253 Mon Sep 17 00:00:00 2001 From: Klaus Date: Mon, 27 May 2019 20:47:27 +0200 Subject: [PATCH] Refactoring format-string --- Rename.9/Helper/SettingHelper.cs | 21 +++-- Rename.9/Properties/AssemblyInfo.cs | 2 +- Rename.9/View/Form1.cs | 132 +++++++++++++++------------- Rename.9/View/Form_Settings.cs | 45 ++-------- 4 files changed, 90 insertions(+), 110 deletions(-) diff --git a/Rename.9/Helper/SettingHelper.cs b/Rename.9/Helper/SettingHelper.cs index 3b0022b..52eb06d 100644 --- a/Rename.9/Helper/SettingHelper.cs +++ b/Rename.9/Helper/SettingHelper.cs @@ -10,11 +10,18 @@ class SettingHelper { internal static List Formates => GetFormates(); private static List formates; + internal static readonly int MaxNumber = 100; internal static string Number = "n"; internal static string Position = "pos"; internal static string Directory = "dir"; - internal static char Separator = '%'; - + internal static readonly char Separator = '%'; + internal static List FormatesWithoutNumeration = new List + { + Number, + Position, + Directory + }; + private static List GetFormates() { return formates ?? InitFormates(); @@ -22,13 +29,9 @@ private static List GetFormates() private static List InitFormates() { - formates = new List - { - Number, - Position, - Directory - }; - formates.AddRange(Enumerable.Range(1, 99).Reverse().ToArray().Select(x => x.ToString()).ToList()); + formates = new List(FormatesWithoutNumeration); + + formates.AddRange(Enumerable.Range(1, MaxNumber -1).Reverse().Select(x => x.ToString()).ToList()); return formates; } } diff --git a/Rename.9/Properties/AssemblyInfo.cs b/Rename.9/Properties/AssemblyInfo.cs index c66d8f4..d6e2bb5 100644 --- a/Rename.9/Properties/AssemblyInfo.cs +++ b/Rename.9/Properties/AssemblyInfo.cs @@ -33,5 +33,5 @@ // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.4.*")] -[assembly: AssemblyFileVersion("1.4.15")] +[assembly: AssemblyFileVersion("1.4.16")] [assembly: NeutralResourcesLanguageAttribute("")] diff --git a/Rename.9/View/Form1.cs b/Rename.9/View/Form1.cs index 0cd29ff..d24153b 100644 --- a/Rename.9/View/Form1.cs +++ b/Rename.9/View/Form1.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -178,84 +179,91 @@ private void RenameFiles(List infos) #region Format-String wird angepasst private string FormatString(string row, string dirName) { - string[] text = Properties.Settings.Default.formatString.Split(new string[] { SettingHelper.Separator.ToString() }, StringSplitOptions.RemoveEmptyEntries); + IEnumerable text = Properties.Settings.Default.formatString.Split(new string[] { SettingHelper.Separator.ToString() }, StringSplitOptions.RemoveEmptyEntries); List formates = SettingHelper.Formates; - - string fullname = string.Empty; - bool firstIndex = true; + List formatesWithoutNumeration = SettingHelper.FormatesWithoutNumeration; + + StringBuilder fullname = new StringBuilder(); + if (!Properties.Settings.Default.formatString.StartsWith(SettingHelper.Separator.ToString())) + { + fullname.Append(text.FirstOrDefault() ?? string.Empty); + text = text.Skip(1); + } foreach (string line in text) { - if (Properties.Settings.Default.formatString.Substring(0, 1) == SettingHelper.Separator.ToString() || firstIndex) + string format = formates.Where(f => line.Length >= f.Length && line.Substring(0, f.Length) == f).OrderBy(f=>f,new StringComparator()).LastOrDefault(); + if(format == null) { - bool formatFound = false; - foreach(string format in formates) + for(int i = 3; i > 0; i--) { - try - { //Länge verglichen um IndexOutOfRangeException zu vermeiden - if (line.Length >= format.Length && line.Substring(0, format.Length) == format) - { - #region Überprüfen um was für einen Format-String Befehl es sich handelt - if (format == SettingHelper.Number) - { - int temp = pgBar.Value + Properties.Settings.Default.startNumber; - string number = nullvalues(pgBar.Maximum - 1 + Properties.Settings.Default.startNumber, temp) + temp; - fullname += number; - } - else if (format == SettingHelper.Position) - { - char a = txtSplit.Text == "\\t" ? '\t' : '\n'; - fullname += SplitLine(row, a, (int) nbPosition.Value); - } - else if(format == SettingHelper.Directory) - { - fullname += dirName; - } - else //Nummer 1-100 - { - char a = txtSplit.Text == "\\t" ? '\t' : '\n'; - fullname += SplitLine(row, a, Convert.ToInt32(format)); - } - - fullname += line.Substring(format.Length, line.Length - format.Length); //Rest der nach dem %-Befehl steht. Bsp: %nabc also "abc" - #endregion - - formatFound = true; - break; - } + if(line.Length >= i && int.TryParse(line.Substring(0,i), out int result) && result <= SettingHelper.MaxNumber) + { + format = result.ToString(); + } + } + } + + if (format != null) + { + try + { + #region Überprüfen um was für einen Format-String Befehl es sich handelt + if (format == SettingHelper.Number) + { + int temp = pgBar.Value + Properties.Settings.Default.startNumber; + string number = nullvalues(pgBar.Maximum - 1 + Properties.Settings.Default.startNumber, temp) + temp; + fullname.Append(number); + } + else if (format == SettingHelper.Position) + { + char a = txtSplit.Text == "\\t" ? '\t' : '\n'; + fullname.Append(SplitLine(row, a, (int)nbPosition.Value)); } - catch (IndexOutOfRangeException) + else if (format == SettingHelper.Directory) { - //Falls eine Position im Format-String nicht existiert - if (!abort) + fullname.Append(dirName); + } + else //Nummer 1-100 + { + char a = txtSplit.Text == "\\t" ? '\t' : '\n'; + fullname.Append(SplitLine(row, a, Convert.ToInt32(format))); + } + + fullname.Append(line.Substring(format.Length, line.Length - format.Length)); //Rest der nach dem %-Befehl steht. Bsp: %nabc also "abc" + #endregion + } + catch (IndexOutOfRangeException) + { + //Falls eine Position im Format-String nicht existiert + if (!abort) + { + DialogResult? res = MessageHandler.MessagesYesNo(MessageBoxIcon.Error, $"Die Position {nbPosition.Value} mit dem Split-String: {txtSplit.Text} gibt es nicht\nVorgang fortsetzen?"); + + if (res == DialogResult.No) { - DialogResult? res = MessageHandler.MessagesYesNo(MessageBoxIcon.Error, $"Die Position {nbPosition.Value} mit dem Split-String: {txtSplit.Text} gibt es nicht\nVorgang fortsetzen?"); - - if (res == DialogResult.No) - { - res = MessageHandler.MessagesYesNo(MessageBoxIcon.Question, "Restore ausführen?"); - if (res == DialogResult.Yes) - restore(); - throw new AbortException(); - } - else - abort = true; + res = MessageHandler.MessagesYesNo(MessageBoxIcon.Question, "Restore ausführen?"); + if (res == DialogResult.Yes) + restore(); + throw new AbortException(); + } + else + { + abort = true; } - } - } - if (!formatFound) - fullname += "%" + line; + } } - else - fullname += line; - - firstIndex = false; + { + fullname.Append(SettingHelper.Separator).Append(line); + } + + } - return ReplaceSpecialCharacters(fullname); + return ReplaceSpecialCharacters(fullname.ToString()); } #endregion diff --git a/Rename.9/View/Form_Settings.cs b/Rename.9/View/Form_Settings.cs index c1dcfd5..3c870c2 100644 --- a/Rename.9/View/Form_Settings.cs +++ b/Rename.9/View/Form_Settings.cs @@ -222,49 +222,18 @@ private bool saveData() #region Überprüfen ob der FormatString gültig ist private bool FormatStringValid() { - string[] text = txtFormat.Text.Split(new string[] { "%" }, StringSplitOptions.RemoveEmptyEntries); + IEnumerable text = txtFormat.Text.Split(new string[] { SettingHelper.Separator.ToString() }, StringSplitOptions.RemoveEmptyEntries); + text = txtFormat.Text.StartsWith(SettingHelper.Separator.ToString()) ? text : text.Skip(1); List formates = SettingHelper.Formates; - - - bool AllValid = false; - bool IfOnlyNumber = true; - bool firstIndex = true; - - foreach (string item in text) - { - bool StringValid = false; - if (txtFormat.Text.Substring(0, 1) == SettingHelper.Separator.ToString() || firstIndex) - { - for (int count = 0; count < formates.Count && !StringValid; count++) - { - if (item.Length >= formates[count].Length && item.Substring(0, formates[count].Length) == formates[count]) - { - StringValid = true; - if (formates[count] != SettingHelper.Number) - { - IfOnlyNumber = false; - } - } - } - - if (StringValid) - { - AllValid = true; - if (!IfOnlyNumber) - { - break; - } - //Deswegen nicht sofort break, weil es sonst noch "OnlyNumber" sein könnte - } - } - firstIndex = false; - } + bool withoutNumber = text.Any(item => formates.Where(format => format != SettingHelper.Number).Any(format => item.Length >= format.Length && item.Substring(0, format.Length) == format)); + bool onlyNumber = text.Any(item => item.Length >= SettingHelper.Number.Length && item.Substring(0, SettingHelper.Number.Length) == SettingHelper.Number); + - Properties.Settings.Default.OnlyNumber = IfOnlyNumber; + Properties.Settings.Default.OnlyNumber = onlyNumber && !withoutNumber; Properties.Settings.Default.Save(); - return AllValid; + return withoutNumber || onlyNumber; } #endregion