private void ReadTextBox(KeyPressEventArgs e, RichTextBox richtextbox, Info.BoxType boxType) { var c = e.KeyChar; // Do a simple check if the line contains valid characters, 0 - 9, a - f, A - F, + - * / if (!Info.ValidChars_all.Contains((byte)c)) { e.Handled = true; } var tempText_left = richTextBox_dec.Text; var tempText_right = richTextBox_hex.Text; if (e.KeyChar == 0x0D) // seems to be useless { e.Handled = true; richtextbox = ParseLines(richtextbox, boxType); } }
private RichTextBox ParseLines(RichTextBox textfile, Info.BoxType boxType) { var lines = textfile.Text.Split("\n".ToCharArray()); if (lines.Length == 0) { return(textfile); } // lines contains every line in the text box. Only select the second to last. Last should be a new empty line. var lineItems = FindOpAndNumbers(lines[lines.Length - 2], boxType); if (lineItems.Count == 0) { return(textfile); } CheckLineItems(lineItems); DoOperations(lineItems); return(textfile); }
private List <Info.CalcItem> FindOpAndNumbers(string line, Info.BoxType boxType) { var chars = line.ToCharArray(); foreach (var char_ in chars) { if (!Info.ValidChars_all.Contains((byte)char_)) { return(new List <Info.CalcItem>()); } } var items = new List <Info.CalcItem>(); var charsQueue = new List <char>(); var items_temp = new List <List <char> >(); var d = ""; ulong convertedVal = 0; for (int i = 0; i < chars.Length; i++) { // if it finds an operator if (Info.ValidChars_ops.Contains((byte)chars[i])) { // items_temp list = list of all items on a line, such as numbers, operators // incase there was a number before this operator, the char queue would be filled. return it as a new item, then add the operator as another item items_temp.Add(charsQueue); d = ""; foreach (var a in charsQueue) { d = $"{d}{a}"; } convertedVal = 0; if (boxType == Info.BoxType.Hexadecimal) { ulong.TryParse(d, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out convertedVal); } else { ulong.TryParse(d, NumberStyles.Integer, CultureInfo.InvariantCulture, out convertedVal); } // Log($"Parsed: {d}: 0x{convertedVal:X16}, {convertedVal:X16}"); items.Add(new Info.CalcItem { ID = items_temp.Count - 1, Type = Info.ItemType.Integrals, Number = convertedVal, Op = Info.Operation.Nul, OpByte = (char)0x0 }); // reset current chars list charsQueue = new List <char>(); charsQueue.Add(chars[i]); // add this operator to the items list items_temp.Add(charsQueue); // reset current chars list since operators should be only one char long charsQueue = new List <char>(); var op = Info.Operation.Nul; switch ((byte)chars[i]) { case 0x2A: op = Info.Operation.Mul; break; case 0x2B: op = Info.Operation.Add; break; case 0x2F: op = Info.Operation.Div; break; case 0x2D: op = Info.Operation.Sub; break; } items.Add(new Info.CalcItem { ID = items_temp.Count - 1, Type = Info.ItemType.Operation, Op = op, OpByte = chars[i], Number = 0, }); continue; } if (Info.ValidChars_numbers.Contains((byte)chars[i])) { // continue adding chars if it's a number charsQueue.Add(chars[i]); continue; } } // assuming no op was found, or there's a space, or end of line, add the current chars queue items_temp.Add(charsQueue); d = ""; foreach (var a in charsQueue) { d = $"{d}{a}"; } convertedVal = 0; if (boxType == Info.BoxType.Hexadecimal) { ulong.TryParse(d, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out convertedVal); } else { ulong.TryParse(d, NumberStyles.Integer, CultureInfo.InvariantCulture, out convertedVal); } // Log($"Parsed: {d}: 0x{convertedVal:X16}, {convertedVal:X16}"); items.Add(new Info.CalcItem { ID = items_temp.Count - 1, Type = Info.ItemType.Integrals, Number = convertedVal, Op = Info.Operation.Nul, OpByte = (char)0x0 }); // reset current chars list charsQueue = new List <char>(); var lineItems = new List <string>(); // temp: draw current line items var k = -1; foreach (var a in items_temp) { k++; d = ""; foreach (var b in a) { d = $"{d}{b}"; } lineItems.Add(d); } return(items); }