public string Parse(string pattern) { if (string.IsNullOrEmpty(pattern)) { return(string.Empty); } StringBuilder sb = new StringBuilder(pattern); if (WindowText != null) { sb.Replace(ReplacementVariables.t.ToPrefixString(), WindowText.Replace(' ', '_')); } string width = string.Empty, height = string.Empty; if (Picture != null) { width = Picture.Width.ToString(); height = Picture.Height.ToString(); } sb.Replace(ReplacementVariables.width.ToPrefixString(), width); sb.Replace(ReplacementVariables.height.ToPrefixString(), height); DateTime dt = DateTime.Now; if (CustomDate != DateTime.MinValue) { dt = CustomDate; } sb.Replace(ReplacementVariables.mon2.ToPrefixString(), CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(dt.Month)) .Replace(ReplacementVariables.mon.ToPrefixString(), CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dt.Month)) .Replace(ReplacementVariables.y.ToPrefixString(), dt.Year.ToString()) .Replace(ReplacementVariables.mo.ToPrefixString(), Helpers.AddZeroes(dt.Month)) .Replace(ReplacementVariables.d.ToPrefixString(), Helpers.AddZeroes(dt.Day)); string hour; if (sb.ToString().Contains(ReplacementVariables.pm.ToPrefixString())) { hour = Helpers.HourTo12(dt.Hour); } else { hour = Helpers.AddZeroes(dt.Hour); } sb.Replace(ReplacementVariables.h.ToPrefixString(), hour) .Replace(ReplacementVariables.mi.ToPrefixString(), Helpers.AddZeroes(dt.Minute)) .Replace(ReplacementVariables.s.ToPrefixString(), Helpers.AddZeroes(dt.Second)) .Replace(ReplacementVariables.ms.ToPrefixString(), Helpers.AddZeroes(dt.Millisecond, 3)) .Replace(ReplacementVariables.w2.ToPrefixString(), CultureInfo.InvariantCulture.DateTimeFormat.GetDayName(dt.DayOfWeek)) .Replace(ReplacementVariables.w.ToPrefixString(), CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dt.DayOfWeek)) .Replace(ReplacementVariables.pm.ToPrefixString(), (dt.Hour >= 12 ? "PM" : "AM")); sb.Replace(ReplacementVariables.unix.ToPrefixString(), DateTime.UtcNow.ToUnix().ToString()); if (sb.ToString().Contains(ReplacementVariables.i.ToPrefixString())) { AutoIncrementNumber++; sb.Replace(ReplacementVariables.i.ToPrefixString(), AutoIncrementNumber.ToString()); } sb.Replace(ReplacementVariables.un.ToPrefixString(), Environment.UserName); sb.Replace(ReplacementVariables.uln.ToPrefixString(), Environment.UserDomainName); sb.Replace(ReplacementVariables.cn.ToPrefixString(), Environment.MachineName); if (Type == NameParserType.Text) { sb.Replace(ReplacementVariables.n.ToPrefixString(), Environment.NewLine); } string result = sb.ToString(); result = result.ReplaceAll(ReplacementVariables.rn.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Numbers).ToString()); result = result.ReplaceAll(ReplacementVariables.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString()); if (Type == NameParserType.FolderPath) { result = Helpers.GetValidFolderPath(result); } else if (Type == NameParserType.FileName) { result = Helpers.GetValidFileName(result); } else if (Type == NameParserType.FilePath) { result = Helpers.GetValidFilePath(result); } else if (Type == NameParserType.URL) { result = Helpers.GetValidURL(result); } if (MaxNameLength > 0 && result.Length > MaxNameLength) { result = result.Remove(MaxNameLength); } return(result); }
public string Parse(string pattern) { if (string.IsNullOrEmpty(pattern)) { return(string.Empty); } StringBuilder sb = new StringBuilder(pattern); if (WindowText != null) { string windowText = WindowText.Replace(' ', '_'); if (MaxTitleLength > 0 && windowText.Length > MaxTitleLength) { windowText = windowText.Remove(MaxTitleLength); } sb.Replace(ReplCodeMenuEntry.t.ToPrefixString(), windowText); } if (ProcessName != null) { sb.Replace(ReplCodeMenuEntry.pn.ToPrefixString(), ProcessName); } string width = string.Empty, height = string.Empty; if (Picture != null) { width = Picture.Width.ToString(); height = Picture.Height.ToString(); } sb.Replace(ReplCodeMenuEntry.width.ToPrefixString(), width); sb.Replace(ReplCodeMenuEntry.height.ToPrefixString(), height); DateTime dt = DateTime.Now; if (CustomTimeZone != null) { dt = TimeZoneInfo.ConvertTime(dt, CustomTimeZone); } sb.Replace(ReplCodeMenuEntry.mon2.ToPrefixString(), CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(dt.Month)) .Replace(ReplCodeMenuEntry.mon.ToPrefixString(), CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dt.Month)) .Replace(ReplCodeMenuEntry.yy.ToPrefixString(), dt.ToString("yy")) .Replace(ReplCodeMenuEntry.y.ToPrefixString(), dt.Year.ToString()) .Replace(ReplCodeMenuEntry.mo.ToPrefixString(), Helpers.AddZeroes(dt.Month)) .Replace(ReplCodeMenuEntry.d.ToPrefixString(), Helpers.AddZeroes(dt.Day)); string hour; if (sb.ToString().Contains(ReplCodeMenuEntry.pm.ToPrefixString())) { hour = Helpers.HourTo12(dt.Hour); } else { hour = Helpers.AddZeroes(dt.Hour); } sb.Replace(ReplCodeMenuEntry.h.ToPrefixString(), hour) .Replace(ReplCodeMenuEntry.mi.ToPrefixString(), Helpers.AddZeroes(dt.Minute)) .Replace(ReplCodeMenuEntry.s.ToPrefixString(), Helpers.AddZeroes(dt.Second)) .Replace(ReplCodeMenuEntry.ms.ToPrefixString(), Helpers.AddZeroes(dt.Millisecond, 3)) .Replace(ReplCodeMenuEntry.w2.ToPrefixString(), CultureInfo.InvariantCulture.DateTimeFormat.GetDayName(dt.DayOfWeek)) .Replace(ReplCodeMenuEntry.w.ToPrefixString(), CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dt.DayOfWeek)) .Replace(ReplCodeMenuEntry.pm.ToPrefixString(), (dt.Hour >= 12 ? "PM" : "AM")); sb.Replace(ReplCodeMenuEntry.unix.ToPrefixString(), DateTime.UtcNow.ToUnix().ToString()); if (sb.ToString().Contains(ReplCodeMenuEntry.i.ToPrefixString()) || sb.ToString().Contains(ReplCodeMenuEntry.ib.ToPrefixString()) || sb.ToString().Contains(ReplCodeMenuEntry.ib.ToPrefixString().Replace('b', 'B')) || sb.ToString().Contains(ReplCodeMenuEntry.iAa.ToPrefixString()) || sb.ToString().Contains(ReplCodeMenuEntry.iAa.ToPrefixString().Replace("Aa", "aA")) || sb.ToString().Contains(ReplCodeMenuEntry.ia.ToPrefixString()) || sb.ToString().Contains(ReplCodeMenuEntry.ia.ToPrefixString().Replace('a', 'A')) || sb.ToString().Contains(ReplCodeMenuEntry.ix.ToPrefixString()) || sb.ToString().Contains(ReplCodeMenuEntry.ix.ToPrefixString().Replace('x', 'X'))) { AutoIncrementNumber++; // Base try { foreach (Tuple <string, int[]> entry in ListEntryWithValues(sb.ToString(), ReplCodeMenuEntry.ib.ToPrefixString(), 2)) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(entry.Item2[0], Helpers.AlphanumericInverse), entry.Item2[1])); } foreach (Tuple <string, int[]> entry in ListEntryWithValues(sb.ToString(), ReplCodeMenuEntry.ib.ToPrefixString().Replace('b', 'B'), 2)) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(entry.Item2[0], Helpers.Alphanumeric), entry.Item2[1])); } } catch { } // Alphanumeric Dual Case (Base 62) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.iAa.ToPrefixString())) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(62, Helpers.Alphanumeric), entry.Item2)); } sb.Replace(ReplCodeMenuEntry.iAa.ToPrefixString(), AutoIncrementNumber.ToBase(62, Helpers.Alphanumeric)); // Alphanumeric Dual Case (Base 62) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.iAa.ToPrefixString().Replace("Aa", "aA"))) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(62, Helpers.AlphanumericInverse), entry.Item2)); } sb.Replace(ReplCodeMenuEntry.iAa.ToPrefixString().Replace("Aa", "aA"), AutoIncrementNumber.ToBase(62, Helpers.AlphanumericInverse)); // Alphanumeric Single Case (Base 36) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.ia.ToPrefixString())) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(36, Helpers.Alphanumeric), entry.Item2).ToLowerInvariant()); } sb.Replace(ReplCodeMenuEntry.ia.ToPrefixString(), AutoIncrementNumber.ToBase(36, Helpers.Alphanumeric).ToLowerInvariant()); // Alphanumeric Single Case Capital (Base 36) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.ia.ToPrefixString().Replace('a', 'A'))) { sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber.ToBase(36, Helpers.Alphanumeric), entry.Item2).ToUpperInvariant()); } sb.Replace(ReplCodeMenuEntry.ia.ToPrefixString().Replace('a', 'A'), AutoIncrementNumber.ToBase(36, Helpers.Alphanumeric).ToUpperInvariant()); // Hexadecimal (Base 16) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.ix.ToPrefixString())) { sb.Replace(entry.Item1, AutoIncrementNumber.ToString("x" + entry.Item2.ToString())); } sb.Replace(ReplCodeMenuEntry.ix.ToPrefixString(), AutoIncrementNumber.ToString("x")); // Hexadecimal Capital (Base 16) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.ix.ToPrefixString().Replace('x', 'X'))) { sb.Replace(entry.Item1, AutoIncrementNumber.ToString("X" + entry.Item2.ToString())); } sb.Replace(ReplCodeMenuEntry.ix.ToPrefixString().Replace('x', 'X'), AutoIncrementNumber.ToString("X")); // Number (Base 10) foreach (Tuple <string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.i.ToPrefixString())) { sb.Replace(entry.Item1, AutoIncrementNumber.ToString("d" + entry.Item2.ToString())); } sb.Replace(ReplCodeMenuEntry.i.ToPrefixString(), AutoIncrementNumber.ToString("d")); } sb.Replace(ReplCodeMenuEntry.un.ToPrefixString(), Environment.UserName); sb.Replace(ReplCodeMenuEntry.uln.ToPrefixString(), Environment.UserDomainName); sb.Replace(ReplCodeMenuEntry.cn.ToPrefixString(), Environment.MachineName); if (Type == NameParserType.Text) { sb.Replace(ReplCodeMenuEntry.n.ToPrefixString(), Environment.NewLine); } string result = sb.ToString(); foreach (Tuple <string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.rn.ToPrefixString())) { result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Numbers).ToString())); } foreach (Tuple <string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.ra.ToPrefixString())) { result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString())); } foreach (Tuple <string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.rx.ToPrefixString())) { result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Hexadecimal.ToLowerInvariant()).ToString())); } foreach (Tuple <string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.rx.ToPrefixString().Replace('x', 'X'))) { result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Hexadecimal.ToUpperInvariant()).ToString())); } result = result.ReplaceAll(ReplCodeMenuEntry.rn.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Numbers).ToString()); result = result.ReplaceAll(ReplCodeMenuEntry.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString()); result = result.ReplaceAll(ReplCodeMenuEntry.rx.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Hexadecimal.ToLowerInvariant()).ToString()); result = result.ReplaceAll(ReplCodeMenuEntry.rx.ToPrefixString().Replace('x', 'X'), () => Helpers.GetRandomChar(Helpers.Hexadecimal.ToUpperInvariant()).ToString()); result = result.ReplaceAll(ReplCodeMenuEntry.guid.ToPrefixString().ToLowerInvariant(), () => Guid.NewGuid().ToString().ToLowerInvariant()); result = result.ReplaceAll(ReplCodeMenuEntry.guid.ToPrefixString().ToUpperInvariant(), () => Guid.NewGuid().ToString().ToUpperInvariant()); if (Type == NameParserType.FolderPath) { result = Helpers.GetValidFolderPath(result); } else if (Type == NameParserType.FileName) { result = Helpers.GetValidFileName(result); } else if (Type == NameParserType.FilePath) { result = Helpers.GetValidFilePath(result); } else if (Type == NameParserType.URL) { result = Helpers.GetValidURL(result); } if (MaxNameLength > 0 && result.Length > MaxNameLength) { result = result.Remove(MaxNameLength); } return(result); }