2024-11-18 14:27:59 -07:00

96 lines
2.7 KiB
C#

using System;
namespace Adaptation.FileHandlers.Stratus;
#nullable enable
public class Header
{
public Header(string batch, string cassette, string dateTime)
{
Batch = batch;
Cassette = cassette;
DateTime = dateTime;
}
public string Batch { get; }
public string Cassette { get; }
public string DateTime { get; }
internal static string GetBefore(string text, int[] i, string search)
{
string str;
string str1;
int num = text.IndexOf(search, i[0]);
if (num <= -1)
{
str = text.Substring(i[0]);
i[0] = text.Length;
str1 = str.Trim();
}
else
{
str = text.Substring(i[0], num - i[0]);
i[0] = num + search.Length;
str1 = str.Trim();
}
return str1;
}
internal static string GetToEOL(string text, int[] i)
{
string result;
if (text.IndexOf("\n", i[0]) > -1)
result = GetBefore(text, i, "\n");
else
result = GetBefore(text, i, Environment.NewLine);
return result;
}
private static string GetToText(string text, int[] i, string search) =>
text.Substring(i[0], text.IndexOf(search, i[0]) - i[0]).Trim();
internal static void ScanPast(string text, int[] i, string search)
{
int num = text.IndexOf(search, i[0]);
if (num <= -1)
i[0] = text.Length;
else
i[0] = num + search.Length;
}
internal static Header Get(string text, Constant constant, int[] i)
{
Header? result;
string batch;
if (!text.Contains(constant.Batch) || !text.Contains(constant.Started))
batch = string.Empty;
else
{
for (int z = 0; z < int.MaxValue; z++)
{
ScanPast(text, i, constant.Batch);
if (!text.Substring(i[0]).Contains(constant.Batch))
break;
}
batch = GetToText(text, i, constant.Started);
ScanPast(text, i, constant.StartedAt);
}
ScanPast(text, i, constant.Cassette);
string cassette;
if (!text.Substring(i[0]).Contains(constant.Started))
cassette = string.Empty;
else
cassette = GetToText(text, i, constant.Started);
ScanPast(text, i, constant.StartedAt);
string dateTime = GetToEOL(text, i);
if (dateTime.EndsWith("."))
dateTime = dateTime.Remove(dateTime.Length - 1, 1);
result = new(batch: batch,
cassette: cassette,
dateTime: dateTime);
return result;
}
}