benchtopparser/BenchtopParser/TransducerVerify.cs

93 lines
3.6 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-26 05:52:37 +00:00
public Dictionary<int, Transducer> transducers = new();
2022-07-26 06:04:19 +00:00
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
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) {
2022-07-26 05:56:22 +00:00
int endOfCurrentTransducerKey = $"Transducer {id}".Length;
string restOfLine = line.Substring(endOfCurrentTransducerKey);
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-26 05:30:36 +00:00
this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces;
2022-07-26 04:22:31 +00:00
}
2022-07-26 06:04:19 +00:00
public TransducerVerify() {
return;
}
2022-07-26 04:22:31 +00:00
public TransducerVerify(String transducer_verify_value) {
2022-07-26 05:56:22 +00:00
string separator = "===============================================================";
List<String> documentLines = transducer_verify_value.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-26 05:56:22 +00:00
Dictionary<String, int> countDict = documentLines.GroupBy(p => p).ToDictionary(p => p.Key, q => q.Count());
int numTransducers = countDict[separator];
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
for (int i = 0; i < documentLines.Count(); i++) {
2022-07-26 05:56:22 +00:00
string line = documentLines[i];
2022-07-26 04:22:31 +00:00
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
2022-07-26 05:56:22 +00:00
string[] _line = SplitLine(line);
2022-07-26 04:22:31 +00:00
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;
2022-07-26 05:52:37 +00:00
public Dictionary<String, String> parameters = new();
2022-07-26 04:22:31 +00:00
public Transducer(int id) {
this.id = id;
}
}
}