common/Ui/GenesisToolBox/FrmLogSplit.cs
2026-04-23 17:50:07 +02:00

226 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Xylem.Common.Ui.GenesisToolBox
{
public partial class FrmLogSplit : Form
{
private List<TextFileMatchControl> FileMatchControls = new List<TextFileMatchControl>();
public FrmLogSplit()
{
InitializeComponent();
for (Int32 i = 0; i < 4; i++)
{
var a = new TextFileMatchControl(i);
a.Location = new System.Drawing.Point(10, (i * a.Height) + 15);
FileMatchControls.Add(a);
}
foreach (var item in FileMatchControls)
{
groupBox1.Controls.Add(item);
}
}
private void FrmLogSplit_Load(Object sender, EventArgs e)
{
}
private void btnOpenUpdateDirectory_Click(Object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
tbxFilePathSource.Text = openFileDialog.FileName;
tbxFilePathDestMisc.Text = $"{tbxFilePathSource.Text}_NoMatch";
tbxFilePathCorrupted.Text = $"{tbxFilePathSource.Text}_Corrupted";
foreach (var item in FileMatchControls)
{
item.SetMainFile(tbxFilePathSource.Text);
}
}
private void btnOpenUpdateDirectoryCorrupted_Click(Object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
tbxFilePathCorrupted.Text = openFileDialog.FileName;
tbxFilePathCorrupted.Text = $"{tbxFilePathSource.Text}";
foreach (var item in FileMatchControls)
{
item.SetMainFile(tbxFilePathSource.Text);
}
}
private void btnOpenUpdateDirectoryRest_Click(Object sender, EventArgs e)
{
}
private void btnRun_Click(Object sender, EventArgs e)
{
try
{
var regexMatch = tbxRegExMatch.Text;
var serachDic = new Dictionary<String, TextFileMatchControl>();
Boolean removeLastTab = cbxRemoveLastTab.Checked;
foreach (var item in FileMatchControls)
{
item.PrepareRun();
if (item.IsValid())
{
serachDic.Add(item.Matching(), item);
}
//item.SetMainFile(tbxFilePathSource.Text);
}
var lines = File.ReadAllLines(tbxFilePathSource.Text).ToList();
var sbNoMatch = new StringBuilder();
var sbCorrupted = new StringBuilder();
foreach (var line in lines)
{
var match = false;
var matchingStr = "";
if (removeLastTab)
{
matchingStr = line.Substring(0, line.Trim().LastIndexOf(' '));
}
matchingStr = matchingStr.Replace(" ", "");
if (matchingStr.Length >= 16)
{
matchingStr = matchingStr.Substring(0, matchingStr.Length - 15);
}
matchingStr = matchingStr.Replace("@h", "");
matchingStr = matchingStr.Replace("@f", "");
matchingStr = matchingStr.Replace("-", "");
matchingStr = matchingStr.Replace(":", "");
if (matchingStr.Length >= 26)
{
matchingStr = matchingStr.Substring(26, matchingStr.Length - 26);
}
if (System.Text.RegularExpressions.Regex.IsMatch(matchingStr, @"^[0-9A-F]+$"))
{
var finLine = line;
if (removeLastTab)
{
finLine = line.Substring(0, line.Trim().LastIndexOf(' '));
}
foreach (var item in serachDic)
{
if (finLine.Contains(item.Key))
{
if (item.Value.AddText(finLine))
{
match = true;
}
}
}
if (!match)
{
sbNoMatch.AppendLine(finLine);
}
}
else
{
sbCorrupted.AppendLine(line);
}
}
File.WriteAllText(tbxFilePathDestMisc.Text, sbNoMatch.ToString());
File.WriteAllText(tbxFilePathCorrupted.Text, sbCorrupted.ToString());
Int32 fileLines = (Int32)nudFileLength.Value;
foreach (var item in serachDic)
{
var fileName = item.Value.GetMainFile().Split('.')[0];
var fileType = item.Value.GetMainFile().Split('.')[1];
StringBuilder sb = new StringBuilder();
var lineToWrite = item.Value.getResult();
if (nudFileLength.Value < 1 || lineToWrite.Count <= fileLines)
{
lineToWrite.ForEach(l => sb.AppendLine(l));
File.WriteAllText(item.Value.GetMainFile(), sb.ToString());
}
else
{
for (Int32 i = 0; i < (lineToWrite.Count / fileLines) + 1; i++)
{
sb = new StringBuilder();
if (i == (lineToWrite.Count / fileLines))
{
lineToWrite.GetRange(fileLines * i, lineToWrite.Count - (fileLines * i)).ForEach(l => sb.AppendLine(l));
}
else
{
lineToWrite.GetRange(fileLines * i, fileLines).ForEach(l => sb.AppendLine(l));
}
File.WriteAllText($"{fileName}P{i:D10}.{fileType}", sb.ToString());
}
}
}
MessageBox.Show(@"done");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void tbxFilePathSource_TextChanged(Object sender, EventArgs e)
{
}
private void label1_Click(Object sender, EventArgs e)
{
}
}
}