protected void ProcessFile(string fileName, bool useHeader, ProcessLineDelegate proc) { using (FileStream st = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (StreamReader sr = new StreamReader(st)) { Header hdr = null; string line; string[] parts; if (useHeader) { line = sr.ReadLine(); parts = line.Split('\t'); hdr = new Header(parts); } while ((line = sr.ReadLine()) != null) { if (line == string.Empty) { continue; } if (line[0] == '#') { continue; } parts = line.Split('\t'); for (int i = 0; i != parts.Length; ++i) { parts[i] = parts[i].Trim(); } proc(parts, hdr); } } }
/// <summary> /// Main function that iterates over the token list. /// </summary> /// <param name="lambda">Generic lambda function that will be executed whenever an EOL is reached in the script file.</param> public void Interpret(ProcessLineDelegate lambda) { Token current = head; Text.StringBuilder accum = new System.Text.StringBuilder(); while (current != null) { switch (current.kind) { case 1: accum.Append(current.val); break; case 2: //In this case, variable is not an argument, thus just replace it with its value; { string s; _VARS.TryGetValue(current.val, out s); accum.Append(s); } break; case 3: accum.Append(ParseFunction(ref current).val); break; } if (cline < current.line || current.kind == 7) { //Send string if (accum.ToString().Trim() != string.Empty) { try { if (lambda != null) { lambda(accum.ToString(), this); } accum = new System.Text.StringBuilder(); } catch (Exception ex) { throw ex; } } cline = current.line; } if (current.next != null) { current = current.next; } else { break; } } }
/// <summary> /// Construct primitive definitions and pass to provided processors. /// </summary> private static void TraversePrimitives(PrimitiveTopology primitiveTopology, int countVertices, ProcessPointDelegate processPoint, ProcessLineDelegate processLine, ProcessTriangleDelegate processTriangle) { switch (primitiveTopology) { case PrimitiveTopology.PointList: for (var i = 0u; i < countVertices; i++) { processPoint(i); } break; case PrimitiveTopology.LineList: for (var i = 0u; i < countVertices; i += 2) { processLine(i, i + 1); } break; case PrimitiveTopology.LineStrip: for (var i = 0u; i < countVertices - 1; i++) { processLine(i, i + 1); } break; case PrimitiveTopology.TriangleList: for (var i = 0u; i < countVertices; i += 3) { processTriangle(i, i + 1, i + 2); } break; case PrimitiveTopology.TriangleStrip: var flip = false; for (var i = 0u; i < countVertices - 2; i++) { if (flip) { processTriangle(i, i + 2, i + 1); } else { processTriangle(i, i + 1, i + 2); } flip = !flip; } break; default: throw new ArgumentOutOfRangeException(nameof(primitiveTopology), primitiveTopology, default); } }
void ForEachTouch(ProcessLineDelegate processLine) { int touchIndex = 0; while (touchIndex < Input.touchCount) { Touch touch = Input.GetTouch(touchIndex); if (touch.phase == TouchPhase.Began) { Ray ray = Camera.main.ScreenPointToRay(touch.position); RaycastHit2D hit = Physics2D.GetRayIntersection(ray); Line line = lines.Find(delegate(Line obj) { return obj.gameObject == hit.collider.gameObject; }); if (line == lines[nextToTapIndex]) { int tappedTileIndex = (int)((touch.position.x / Screen.width) * TILES_PER_LINE); processLine(line, tappedTileIndex); } } ++touchIndex; } }
/// <summary> /// Run the test! /// </summary> /// <param name="bgw">Background worker instance, so that we can report progress on the UI.</param> public void RunTest(System.ComponentModel.BackgroundWorker bgw) { if (bgw == null) { throw new ArgumentException("Background Worker must be defined!"); } //Some humor: LolTimer _loltimer = new LolTimer(); _loltimer.AutoReset = true; _loltimer.Interval = 2500; _loltimer.Param1 = bgw; _loltimer.Elapsed += new System.Timers.ElapsedEventHandler(_loltimer_Elapsed); _loltimer.Start(); GPIBConnector dc = null; TempChamber tch = null; var T_Queue = new Queue <int>(config.temp); int temperature = 0; var V_Queue = new Queue <float>(config.volt); float voltage = 0.0f; //int t_count = 0, v_count = 0; //counters for temp and volt /* * I am almost certain that there is a better way to create classes on the fly, rather than * issuing a set of conditionals and loops. Until then, I will keep this mess here... */ if (config.volt.Length > 0) { dc = new GPIBConnector(); dc.Connect(0, config.dcaddr, 0); dc.SendData("CURR 2.0"); //set current dc.SendData("VOLT 3.8"); // set volt dc.SendData("OUTPUT:STATE ON"); } if (config.temp.Length > 0) { tch = new TempChamber(config.comport, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One); tch.Open(); if (!tch.modbusStatus.Contains("successful")) { throw new Exception("Cannot connect to Temperature Chamber. Exiting..."); } } // // GPIBConnector SPA = new GPIBConnector(); SPA.Connect(0, config.spaddr, 0); SPA.ResetDevice(); WifiTel.CellDevice dev; if (Enum.IsDefined(typeof(WifiTel.CellDevice), config.phonemodel)) { dev = (WifiTel.CellDevice)Enum.Parse(typeof(WifiTel.CellDevice), config.phonemodel, true); } else { throw new Exception("Invalid Phone Model!"); } WifiTel telnet = new WifiTel((WifiTel.CellDevice)Enum.Parse(typeof(WifiTel.CellDevice), config.phonemodel)); //FIX telnet.AutoConnect(); if (!telnet.StatusMsg.Contains("Online")) { throw new IOException("Wifi Telnet Session Failure!"); } telnet.Login("root", "root"); //Set up variables // string[] spdrate; //Report start _loltimer.Stop(); bgw.ReportProgress(0, "Starting Test..."); //Start Test-Loop if (parser == null) { throw new Exception("How?"); } parser.AddData("i", config.imgindex.ToString()); // StreamWriter logger = new StreamWriter(config.logLoc, true); logger.AutoFlush = true; // parser.AddData("imgpath", @config.savePicLoc); //used for temperature testing, empty string otherwise string envinfo = String.Empty; do { if (tch != null) { temperature = T_Queue.Dequeue(); bgw.ReportProgress(2, String.Format("Settling temperature at {0}\u00B0C", temperature)); tch.SettleTemp(temperature, 0.3f, ref this._Cancel, new StreamWriter(Stream.Null)); parser.AddData("temp", temperature.ToString()); } do { if (dc != null) { voltage = V_Queue.Dequeue(); bgw.ReportProgress(2, String.Format("Setting voltage to {0}V", voltage)); dc.SendData("VOLT " + voltage); dc.SendData("MEAS:VOLT?"); double current_volt = Convert.ToDouble(dc.ReadData()); if (Math.Abs(current_volt - voltage) > 0.02) { throw new Exception("Cannot change voltage!"); } parser.AddData("volt", voltage.ToString()); envinfo = String.Format("{0},{1}", temperature, voltage); } // foreach (int c in config.channel) { foreach (char m in config.mod) { if (m == 'a' || m == 'g') { spdrate = new string[] { "6", "24", "54" } } ; else if (m == 'n') { spdrate = new string[] { "MCS0", "MCS4", "MCS7" } } ; else { spdrate = new string[] { "1", "5.5", "11" } }; //If Quick Test Mode: if (config.quick) { spdrate = spdrate.Take(1).ToArray(); } parser.AddData("channel", c.ToString()); parser.AddData("frequency", this.ConvertToFrequency(c)); foreach (string spd in spdrate) { parser.AddData("speed", spd.ToString()); telnet.InitTx(m, c, spd); //I DON'T LIKE GOTO EITHER, BUT I DON'T WANNA USE 5 CONSECUTIVE BREAK STATEMENTS EITHER if (_Cancel.WaitOne(1000)) { goto Cleanup; //used thread.sleep before, might as well merge them } //Report upcoming test bgw.ReportProgress( 1, String.Format("Running: 802.11{0}, Ch. {1}, {2} Mbps", m, c, spd) ); /* * This delegate type is defined in Parser.cs * Let's hope this works! */ ProcessLineDelegate pdel = new ProcessLineDelegate((s, p) => { SPA.SendData(s.Trim() + @"\n"); Thread.Sleep(80); if (s.Contains('?')) { string res = SPA.ReadData().Trim(); p.AddData("_", res); logger.WriteLine("{0},{1},{2},{3}", envinfo, c, spd, res); } }); parser.Interpret(pdel); } } } } while (V_Queue.Count() > 0); V_Queue = new Queue <float>(config.volt); } while (T_Queue.Count() > 0); /* * HEY LOOK BELOW, IT IS A LABEL * YES, THAT MEANS THERE IS GOTO STATEMENT LURKING AROUND HERE SOMEWHERE... */ //Clean-up -> GOTO STATEMENT FROM THE DATA RATE LOOP Cleanup: //Report completion if (_Cancel.WaitOne(0)) { bgw.ReportProgress(100, "Test Cancelled"); } else { bgw.ReportProgress(100, "Test Complete"); } if (dc != null) { dc.SendData("OUTPUT:STATE OFF"); dc.Dispose(); }//telnet close func! SPA.Dispose(); if (tch != null) { tch.SetTemp(20); tch.Close(); } logger.Close(); }