benchtopparser/BenchtopParser/TransducerVerify.cs

82 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BenchtopParser {
public class TransducerVerify {
2022-07-26 04:22:31 +00:00
2022-07-27 00:13:20 +00:00
public Dictionary<int, Transducer> Transducers = new();
public int Indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety
2022-07-26 04:22:31 +00:00
/// Split the string, and clean up whitespace, returns the Name and Value
2022-07-27 00:13:20 +00:00
public string[] SplitLine(string line) {
2022-07-26 05:30:36 +00:00
return new[] {
2022-07-27 00:13:20 +00:00
line[..Indent].TrimEnd(),
line[(Indent + 1)..]
2022-07-26 04:22:31 +00:00
};
}
2022-07-27 00:13:20 +00:00
public void SetIndent(int id, string line) {
2022-07-26 05:56:22 +00:00
int endOfCurrentTransducerKey = $"Transducer {id}".Length;
2022-07-27 00:13:20 +00:00
string restOfLine = line[endOfCurrentTransducerKey..];
2022-07-26 05:56:22 +00:00
int additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count();
2022-07-26 04:22:31 +00:00
// length 1 based, remove 1, then add additional spaces
2022-07-27 00:13:20 +00:00
Indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
2022-07-26 04:22:31 +00:00
}
2022-07-26 06:04:19 +00:00
public TransducerVerify() {
}
2022-07-27 02:33:45 +00:00
public TransducerVerify(string transducerVerifyValue) {
2022-07-27 00:13:20 +00:00
const string separator = "===============================================================";
2022-07-27 02:33:45 +00:00
List<string> documentLines = transducerVerifyValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
2022-07-26 04:22:31 +00:00
// Check if proper document
2022-07-26 05:56:22 +00:00
string title = documentLines[0];
2022-07-26 04:22:31 +00:00
documentLines.RemoveAt(0);
if (title != "|| Transducer Verify Report ||") {
throw new Exception("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data");
}
2022-07-27 00:13:20 +00:00
Dictionary<string, int> countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
2022-07-26 05:56:22 +00:00
int currentTransducer = 0; // ONE BASED - will add 1 later at check step, but first transducer is 1.
2022-07-26 04:22:31 +00:00
// Find the the indexes of each transducer start and stop
2022-07-27 00:13:20 +00:00
foreach (var line in documentLines)
{
2022-07-26 04:22:31 +00:00
if (line == $"TRANSDUCER{currentTransducer + 1}") {
currentTransducer++;
continue; // Skip TITLE line and separator
}
2022-07-27 00:13:20 +00:00
if (line is separator or "") {
2022-07-26 04:22:31 +00:00
// end of current transducer
continue;
}
// Add new Transducer if not present
2022-07-27 00:13:20 +00:00
if (!Transducers.ContainsKey(currentTransducer)) {
Transducers.Add(currentTransducer, new Transducer(currentTransducer));
2022-07-26 04:22:31 +00:00
// Calculating indent
SetIndent(currentTransducer, line);
2022-07-27 00:13:20 +00:00
Transducers[currentTransducer].Name = SplitLine(line)[1];
2022-07-26 04:22:31 +00:00
continue;
}
// Add transducer parameters
2022-07-27 00:13:20 +00:00
string[] lineParts = SplitLine(line);
Transducers[currentTransducer].Parameters[lineParts[0]] = lineParts[1];
2022-07-26 04:22:31 +00:00
}
}
}
public class Transducer {
2022-07-27 00:13:20 +00:00
public int Id;
public string? Name;
public Dictionary<string, string> Parameters = new();
2022-07-26 04:22:31 +00:00
public Transducer(int id) {
2022-07-27 00:13:20 +00:00
Id = id;
2022-07-26 04:22:31 +00:00
}
}
}