private async void FormatButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(tbInput.Text) || string.IsNullOrWhiteSpace(tbOutput.Text)) { return; } var btn = sender as Button; btn.IsEnabled = false; btn.Content = "正在处理"; try { using var input = new FileStream(tbInput.Text, FileMode.Open, FileAccess.Read, FileShare.Read); var ini = await IniDocument.ParseAsync(input); using var output = new FileStream(tbOutput.Text, FileMode.Create, FileAccess.Write, FileShare.Read); using var sw = new StreamWriter(output); var options = new SortOptions { Digit = string.IsNullOrWhiteSpace(tbDigit.Text) ? (int?)null : int.Parse(tbDigit.Text), First = string.IsNullOrWhiteSpace(tbDigit.Text) ? 0 : int.Parse(tbFirst.Text), Prefix = tbPrefix.Text, Sort = cbSort.IsChecked, SortTargetKey = tbSortTargetKey.Text, SummaryKey = tbSummaryKey.Text, KeyConstraint = tbKeyConstraint.Text, ValueConstraint = tbValueConstraint.Text }; var result = SortHelper.Sort(ini, options); if (!string.IsNullOrEmpty(tbTargetSectionName.Text)) { await sw.WriteLineAsync($"[{tbTargetSectionName.Text}]"); } result.ToList().ForEach(i => sw.WriteLine(i)); await sw.FlushAsync(); } #if !DEBUG catch (Exception ex) { using var logfs = new FileStream("program.err.log", FileMode.Create, FileAccess.Write, FileShare.Read); using var sw = new StreamWriter(logfs); await sw.WriteLineAsync(ex.ToString()); await sw.WriteLineAsync("全部堆栈跟踪"); await sw.WriteLineAsync(ex.StackTrace); await sw.FlushAsync(); MessageBox.Show(ex.ToString(), "发生异常:"); } #endif finally { btn.IsEnabled = true; btn.Content = "开始格式化"; } }
static async void Main( FileInfo input_file, string output_file, int start_num, int?digit, string prefix, bool nologo, bool sort, string sort_key, string output_section, string output_key, string constraint_key, string constraint_value) { Console.WriteLine("Hello World"); using var fs = new FileStream(input_file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); using var ofs = new FileStream(output_file, FileMode.Create, FileAccess.Write, FileShare.Read); using var sw = new StreamWriter(ofs); var options = new SortOptions { First = start_num, Digit = digit, Prefix = prefix, Sort = sort, SortTargetKey = sort_key, SummaryKey = output_key, KeyConstraint = constraint_key, ValueConstraint = constraint_value }; var result = SortHelper.Sort(await IniDocument.ParseAsync(fs), options); if (!string.IsNullOrEmpty(output_section)) { await sw.WriteLineAsync($"[{output_section}]"); } result.ToList().ForEach(i => sw.WriteLine(i)); }
public static async Task ScreeningAsync(SectionScreenOptions options, IConsole console) { IniDocument ini, result; try { // 0. 判断程序是否具备执行条件 if (options.Sections is null || options.Sections.Length < 1)// 未设置目标键或目标键为空 { console.Error.WriteLine($"{nameof(options.Sections)} are NULL or Empty"); return; } // 1. 读入文件 ini = await IniDocument.ParseAsync(options.Input); // 2. 筛选并保留目标键 // 输出 result.Sections = options.MatchCase ? ini.Sections.Where(section => options.Sections.Contains(section.Name)).ToArray() : ini.Sections.Where(section => options.Sections.Contains(section.Name, StringComparer.OrdinalIgnoreCase)).ToArray(); await result.DeparseAsync(options.Output); } finally { console.Out.WriteLine("All Done!"); } }
private static async Task WorkAsync(this IniMergeOptions options, IConsole console) { // 0. 读入ini var mainIni = new IniBuilder(await IniDocument.ParseAsync(options.Input)); // 1. 获取[#Include]节 if (mainIni.TryGetSection("#Include") is IniSectionBuilder include) { // 2. 通过遍历节中内容来获取INI文件 foreach (var kvp in include.Content) { if (!kvp.HasData) { continue; } var subIni = await IniDocument.ParseAsync(new FileInfo(Path.Combine(options.WorkDir.FullName, kvp.Value)).Open(FileMode.Open, FileAccess.Read, FileShare.Read)); foreach (var subSection in subIni.Sections) { if (mainIni.TryGetSection(subSection.Name) is IniSectionBuilder mainSection) { foreach (var subKvp in subSection.Content) { if (mainSection.TryGetKey(subKvp.Key) is IniKeyValuePair mainKvp) { mainKvp.Value = subKvp.Value; } else { mainSection.Add(subKvp); } } } else { mainIni.Add(subSection); } } } } else { console.Fatal($"不存在[#Include]节!"); } await mainIni.ToDocument().DeparseAsync(options.Output); }
public static async Task WorkAsync(IniSortSectionOptions options, IConsole console) { var ini = await IniDocument.ParseAsync(options.Input); IEnumerable <IniSection> Result = ini.Sections; // 约束筛选 var result = new List <IniKeyValuePair>(); // 结果 int num = options.First; if (!string.IsNullOrEmpty(options.KeyConstraint)) { console.Info($"已启用键约束"); if (!string.IsNullOrEmpty(options.ValueConstraint)) { console.Info($"已启用值约束"); Result = ini.Sections.Where(i => i.TryGetKey(options.KeyConstraint)?.Value.ToString().Equals(options.ValueConstraint) ?? false); } else { console.Info($"已启用键约束, 但未启用值约束"); Result = ini.Sections.Where(i => i.TryGetKey(options.KeyConstraint, out _)); } } if (options.Sort) { console.Info($"已启用排序"); if (options.SortTargetKeys is null) { console.Warn($"排序列表不存在, 将按Section名排序"); Result = Result.OrderBy(i => i.Name); } if (options.SortTargetKeys.Length > 0) { console.Info($"排序列表为空, 将按Section名排序"); Result = Result.OrderBy(i => i.Name); } else { foreach (var key in options.SortTargetKeys) { console.Trace($"正在根据键 {key} 的值排序"); Result = Result.Where(i => i.TryGetKey(key, out _)).OrderBy(i => i.TryGetKey(key)?.Value.ToString()); } } } if (string.IsNullOrEmpty(options.PrefixKey)) { if (string.IsNullOrEmpty(options.SummaryKey)) { foreach (var i in Result) { var key = $"{options.Prefix ?? string.Empty}{num++.ToString().PadLeft(options.Digit, '0')}"; var value = i.Name; result.Add(IniKeyValuePair.CreateDataLine(key, value)); } } else { foreach (var i in Result) { var key = $"{options.Prefix ?? string.Empty}{num++.ToString().PadLeft(options.Digit, '0')}"; var value = i.Name; var summary = i.TryGetKey(options.SummaryKey)?.Value ?? string.Empty; result.Add(IniKeyValuePair.CreateFullLine(key, value, summary)); } } } else { foreach (var i in Result) { var prefix = string.Empty; if (string.IsNullOrEmpty(options.Prefix)) { prefix = i.TryGetKey(options.PrefixKey)?.Value.ToString().Trim(); } else { prefix = options.Prefix.Replace("%s", i.TryGetKey(options.PrefixKey)?.Value.ToString().Trim()); } var key = $"{prefix}{num++.ToString().PadLeft(options.Digit, '0')}"; var value = i.Name; var summary = i.TryGetKey(options.SummaryKey)?.Value ?? string.Empty; result.Add(IniKeyValuePair.CreateFullLine(key, value, summary)); } } console.Info($"输出结果"); if (!string.IsNullOrEmpty(options.OutputSection)) { console.Out.WriteLine($"[{options.OutputSection}]"); await options.Output?.WriteAsync($"[{options.OutputSection}]"); } foreach (var item in result) { console.Out.WriteLine(item.ToString()); await options.Output?.WriteAsync(item.ToString()); } await options.Output?.FlushAsync(); }