Created
March 12, 2015 13:45
-
-
Save Descalon/704023ae6c583b613225 to your computer and use it in GitHub Desktop.
New proposition file reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool Foo(byte[] txt) | |
{ | |
var ms = new MemoryStream(txt); | |
using (var sr = new StreamReader(ms)) | |
{ | |
DynamicElement.ChangedElements.Clear(); | |
DynamicElement.TrainElements.Clear(); | |
while (!sr.EndOfStream) | |
{ | |
var line = sr.ReadLine(); | |
if (string.IsNullOrEmpty(line)) | |
continue; | |
if (line == "trains:") { | |
HandleTrainInput(sr); | |
continue; | |
} | |
if (line == "DMI:") | |
{ | |
HandleDMIInput(sr); | |
continue; | |
} | |
var values = line.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries); | |
var changedElement = new ErtmsChangedElement | |
{ | |
Number = values[0], | |
Type = (ErtmsType) ParseInt32("type", values[1]), | |
Status = (int) ParseFloat("status", values[2]), | |
Length = ParseFloat("length", values[3]), | |
KmStart = ParseInt32("start", values[4]), | |
KmEnd = ParseInt32("end", values[5]) | |
}; | |
if (changedElement.Type == ErtmsType.Sein) { | |
var tempVal = Convert.ToInt32(changedElement.Number); | |
if (ElementDictionary.ContainsKey(tempVal)) { | |
changedElement.Name = ElementDictionary[tempVal].Name; | |
} | |
} | |
DynamicElement.ChangedElements.Add(changedElement); | |
} | |
} | |
return false; | |
} | |
private static void HandleDMIInput(TextReader sr) | |
{ | |
while (true) | |
{ | |
var line = sr.ReadLine(); | |
if (string.IsNullOrEmpty(line)) | |
return; | |
var values = line.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries); | |
DMIElement.DMIDistance.Add(ParseFloat("Distance", values[0])); | |
DMIElement.DMICode.Add(ParseInt32("Code", values[1])); | |
DMIElement.DMIValue.Add(ParseFloat("Value", values[2])); | |
} | |
} | |
private static void HandleTrainInput(TextReader sr) | |
{ | |
while (true) { | |
var line = sr.ReadLine(); | |
if (string.IsNullOrEmpty(line)) | |
return; | |
var values = line.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries); | |
var trainElement = new ErtmsTrainElement | |
{ | |
TrainNumber = ParseInt32("trainNumber", values[0]), | |
OccNumber = ParseInt32("trainOccNumber", values[1]), | |
Position = ParseFloat("trainPos", values[2]), | |
Speed = ParseFloat("trainSpeed", values[3]), | |
Power = ParseFloat("trainPower", values[4]) | |
}; | |
if (values.Length > 5) { | |
trainElement.ATBSpeed = ParseInt32("atbSpeed", values[5]); | |
trainElement.ATBBrake = ParseInt32("atbBrake", values[6]); | |
} | |
if (values.Length > 7) trainElement.Acceleration = ParseFloat("accel", values[7]); | |
if (values.Length > 8) { | |
trainElement.Figuur = ParseInt32("ertms-figuur", values[8]); | |
trainElement.EBD = ParseFloat("ertms-ebd", values[9]); | |
trainElement.EBI = ParseFloat("ertms-ebi", values[10]); | |
trainElement.P = ParseFloat("ertms-p", values[11]); | |
trainElement.I = ParseFloat("ertms-i", values[12]); | |
trainElement.Target = ParseFloat("ertms-target", values[13]); | |
trainElement.Snelheid = ParseFloat("ertms-snelheid", values[14]); | |
trainElement.Ingreep = ParseInt32("ertms-ingreep", values[15]); | |
trainElement.RS = ParseFloat("ertms-RS", values[16]); | |
trainElement.DistToNext = ParseFloat("ertms-disttonext", values[17]); | |
} | |
DynamicElement.TrainElements.Add(trainElement); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment