using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BenchtopParser { public class TransducerVerify { public Dictionary transducers = new(); public int indent = 26; // Default 26 from seeing files, overridden later in SetIndent for safety /// Split the string, and clean up whitespace, returns the Name and Value public String[] SplitLine(String line) { return new[] { line[..indent].TrimEnd(), line[(indent + 1)..] }; } public void SetIndent(int id, String line) { int endOfCurrentTransducerKey = $"Transducer {id}".Length; string restOfLine = line.Substring(endOfCurrentTransducerKey); int additionalSpaces = restOfLine.TakeWhile(c => c == ' ').Count(); // length 1 based, remove 1, then add additional spaces this.indent = endOfCurrentTransducerKey - 1 + additionalSpaces; } public TransducerVerify() { return; } public TransducerVerify(String transducer_verify_value) { string separator = "==============================================================="; List documentLines = transducer_verify_value.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries).ToList(); // Check if proper document string 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"); } Dictionary 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. // Find the the indexes of each transducer start and stop for (int i = 0; i < documentLines.Count(); i++) { string 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 string[] _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 parameters = new(); public Transducer(int id) { this.id = id; } } }