benchtopparser/BenchtopParser/TransducerVerify.cs

90 lines
3.5 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
public Dictionary<int, Transducer> transducers = new Dictionary<int, Transducer>();
public int indent = 26;
/// Split the string, and clean up whitespace, returns the Name and Value
public String[] SplitLine(String line) {
2022-07-26 05:30:36 +00:00
return new[] {
2022-07-26 04:22:31 +00:00
line[..indent].TrimEnd(),
line[(indent + 1)..]
};
}
public void SetIndent(int id, String line) {
var endOfCurrentTransducerKey = $"Transducer {id}".Length;
var restOfLine = line.Substring(endOfCurrentTransducerKey);
var additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count();
// length 1 based, remove 1, then add additional spaces
2022-07-26 05:30:36 +00:00
this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
2022-07-26 04:22:31 +00:00
}
public TransducerVerify(String transducer_verify_value) {
var separator = "===============================================================";
var documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList();
// Check if proper document
var title = documentLines[0];
documentLines.RemoveAt(0);
if (title != "|| Transducer Verify Report ||") {
throw new Exception("Invalid Transducer Verify Report, please provide a Transducer Verify Report file data");
}
var countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
var numTransducers = countDict[separator];
var currentTransducer = 0; // ONE BASED - will add 1 later at check step, but first transducer is 1.
// Find the the indexes of each transducer start and stop
for (int i = 0; i < documentLines.Count(); i++) {
var line = documentLines[i];
if (line == $"TRANSDUCER{currentTransducer + 1}") {
currentTransducer++;
continue; // Skip TITLE line and separator
}
if (line == separator) {
continue;
}
if (line == "") {
// end of current transducer
continue;
}
// Add new Transducer if not present
if (transducers != null && !this.transducers.ContainsKey(currentTransducer)) {
transducers.Add(currentTransducer, new Transducer(currentTransducer));
// Calculating indent
SetIndent(currentTransducer, line);
transducers[currentTransducer].name = SplitLine(line)[1];
continue;
}
// Add transducer parameters
var _line = SplitLine(line);
if (transducers != null) {
transducers[currentTransducer].parameters[_line[0]] = _line[1];
} else {
throw new Exception("Config list is null, something broke");
}
}
}
}
public class Transducer {
public int id;
public String? name;
public Dictionary<String, String> parameters = new Dictionary<String, String>();
public Transducer(int id) {
this.id = id;
}
2022-07-26 05:30:36 +00:00
}
}