/// <summary> /// Create a CmdA from a string array /// </summary> /// <param name="script">An array of strings </param> /// <returns>A valid Cmd or null</returns> public static CmdA CreateFromStrings(string[] script) { /* * 0 1 2 3 .. * A=AcftType;Runway[;Pref.Runway[;Strict]] # Type 1 – default Alt Mode => AGL based * A=AcftType;Airway # Type 2 – default Alt Mode => MSL based * A=AcftType;MsgRelative;Alt;GS # Type 3 – default Alt Mode => MSL based; StartPos random; * A=AcftType;MsgAbsolute;Alt;GS;Lat;Lon;Hdg # Type 4 – default Alt Mode => MSL based; StartPos defined; */ // must be 3 at least if (script.Length < 3) { return(null); // ERROR exit } var acType = script[1]; var fType = AsFlightType(script[2]); if (fType == FlightT.Invalid) { return(null); // ERROR exit } var cmd = new CmdA(acType, fType); switch (fType) { case FlightT.Runway: {// this is usually not used but to have it complete if (script.Length > 3) { // pref runway cmd.RunwayPreference = script[3].Trim( ).ToUpperInvariant( ); // optional, preferred RWY if (script.Length > 4) { // pref runway strict cmd.RunwayPrefStrict = script[4].Trim( ).ToUpperInvariant( ) == "S"; // optional, preferred RWY is strict } } return(cmd); } case FlightT.Airway: return(cmd); // no further parameters case FlightT.MsgRelative: { if (script.Length < 5) { return(null); // ERROR exit } bool pass = true; pass &= double.TryParse(script[3], out double alt); pass &= double.TryParse(script[4], out double gs); if (!pass) { return(null); // ERROR exit - number conversion error } cmd.StartAlt_ftMsl = alt; cmd.StartGS_kn = gs; return(cmd); } case FlightT.MsgAbsolute: { if (script.Length < 8) { return(null); // ERROR exit } bool pass = true; pass &= double.TryParse(script[3], out double alt); pass &= double.TryParse(script[4], out double gs); pass &= double.TryParse(script[5], out double lat); pass &= double.TryParse(script[6], out double lon); pass &= double.TryParse(script[7], out double hdg); if (!pass) { return(null); // ERROR exit - number conversion error } cmd.StartAlt_ftMsl = alt; cmd.StartGS_kn = gs; cmd.StartPos_latlon = new LatLon(lat, lon); cmd.StartBrg_degm = hdg; return(cmd); } default: return(null); } }
/// <summary> /// Reads one Script file /// NOTE: Does not check for command inconsistencies whatsoever /// only a limited format check is done /// </summary> /// <param name="filename">The file to read</param> /// <returns>A CmdList either populated or empty on Error</returns> public static CmdList ReadCmdScript(string filename) { var list = new CmdList( ); if (!File.Exists(filename)) { return(list); // ERROR return an empty list } using (var sr = new StreamReader(filename)) { bool expectCmdA = true; do { string buffer = sr.ReadLine( ).Trim( ); string[] x = buffer.Split(new char[] { '#' }); // cut comments if (x.Length > 0 && x[0].Length > 0) { // use first part only string[] e = x[0].Split(new char[] { '=', ';' }); if (e.Length < 2) { continue; // at least 2 items should be there... Cmd and Arg1 } if (expectCmdA) { // start of script expected - cmd A accepted only var a = CmdA.CreateFromStrings(e); if (a == null) { return(list); // ERROR in CmdA } list.Enqueue(a); expectCmdA = false; // we have it } else { // rest of script switch (e[0].Substring(0, 1)) { case "D": var d = CmdD.CreateFromStrings(e); if (d != null) { list.Enqueue(d); } break; case "T": var t = CmdT.CreateFromStrings(e); if (t != null) { list.Enqueue(t); } break; case "H": var h = CmdH.CreateFromStrings(e); if (h != null) { list.Enqueue(h); } break; case "G": var g = CmdG.CreateFromStrings(e); if (g != null) { list.Enqueue(g); } break; case "S": var s = CmdS.CreateFromStrings(e); if (s != null) { list.Enqueue(s); } break; case "V": var v = CmdV.CreateFromStrings(e); if (v != null) { list.Enqueue(v); } break; case "M": var m = CmdM.CreateFromStrings(e); if (m != null) { list.Enqueue(m); } break; default: // not supported .. break; } } } } while (!sr.EndOfStream); } list.Enqueue(new CmdE( )); // MUST be the last one return(list); }