private void AddTextFileRecipes(string filename) { // TODO: Add sorting if (File.Exists(filename)) { var lines = File.ReadAllLines(filename); var recipe = new List <string>(); foreach (var line in lines) { if (!line.StartsWith("#", StringComparison.Ordinal) && !line.StartsWith("//", StringComparison.Ordinal)) { var trimmedLine = line.Trim(); if (trimmedLine.Length == 0) { if (recipe.Count > 0) { this.AddRecipe(recipe); recipe.Clear(); } } else if (line.Contains("=")) { recipe.Add(line); } else { try { var equations = Combiner.EquationsFromParentheses(trimmedLine); this.AddRecipe(equations); } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Error in " + filename, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } } if (recipe.Count > 0) { this.AddRecipe(recipe); recipe.Clear(); } } }
private void ParseRecipeButton_Click(object sender, EventArgs e) { try { // This approach assumes that there will be relatively few comments compared to recipe lines, and that therefore bulk adding and then removing will be faster than adding one-by-one. var newLines = new List <string>(this.recipeInputRichTextBox.Lines); for (int i = newLines.Count - 1; i >= 0; i--) { var line = newLines[i].Trim(); if (line.Length == 0 || line.StartsWith("#", StringComparison.CurrentCulture) || line.StartsWith("//", StringComparison.CurrentCulture)) { newLines.RemoveAt(i); } } bool equations = false; foreach (var line in newLines) { if (line.Contains("=")) { equations = true; break; } } if (!equations) { newLines = new List <string>(Combiner.EquationsFromParentheses(string.Join(string.Empty, newLines))); // Join rather than using newLines[0] in case someone uses line breaks for formatting } var combine = new Combiner(newLines, false); this.CreateInstructions(combine); if (((Control)sender).Tag == null) { // parenthesis this.recipeInputRichTextBox.Text = combine.Gem?.Recipe() ?? string.Empty; } else { // equations this.recipeInputRichTextBox.Text = string.Join(Environment.NewLine, newLines); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }