Skip to content

Commit

Permalink
Refactoring format-string
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirdock committed May 27, 2019
1 parent 46601f4 commit bb5e7bb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 110 deletions.
21 changes: 12 additions & 9 deletions Rename.9/Helper/SettingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ class SettingHelper
{
internal static List<string> Formates => GetFormates();
private static List<string> 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<string> FormatesWithoutNumeration = new List<string>
{
Number,
Position,
Directory
};

private static List<string> GetFormates()
{
return formates ?? InitFormates();
}

private static List<string> InitFormates()
{
formates = new List<string>
{
Number,
Position,
Directory
};
formates.AddRange(Enumerable.Range(1, 99).Reverse().ToArray().Select(x => x.ToString()).ToList());
formates = new List<string>(FormatesWithoutNumeration);

formates.AddRange(Enumerable.Range(1, MaxNumber -1).Reverse().Select(x => x.ToString()).ToList());
return formates;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Rename.9/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]
132 changes: 70 additions & 62 deletions Rename.9/View/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,84 +179,91 @@ private void RenameFiles(List<FileInfo> 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<string> text = Properties.Settings.Default.formatString.Split(new string[] { SettingHelper.Separator.ToString() }, StringSplitOptions.RemoveEmptyEntries);

List<string> formates = SettingHelper.Formates;

string fullname = string.Empty;
bool firstIndex = true;
List<string> 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

Expand Down
45 changes: 7 additions & 38 deletions Rename.9/View/Form_Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> text = txtFormat.Text.Split(new string[] { SettingHelper.Separator.ToString() }, StringSplitOptions.RemoveEmptyEntries);
text = txtFormat.Text.StartsWith(SettingHelper.Separator.ToString()) ? text : text.Skip(1);

List<string> 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

Expand Down

0 comments on commit bb5e7bb

Please sign in to comment.