private void button_processFile_Click(object sender, EventArgs e) //open&Read a text file line by line, matchs regex and loads all present info into program state { StreamReader reader = new StreamReader(textBox_filepath.Text); output.AppendText($"Processing file: {textBox_filepath.Text} ..."); output.Update(); Days = new List <DayEntry>(); //reinit to get rid of old state Foods = new List <FoodItem>(); Stopwatch timer = Stopwatch.StartNew(); //test performance String line; //current line Match m; //current regex match for line List <FoodEntry> lfe = new List <FoodEntry>(); //list of Food entries to be init for next MealEntry List <MealEntry> lme = new List <MealEntry>(); //list of Meal entries to be init for next DatEntry FoodEntry fe = null; //current FoodEntry found String activ = ""; //helps set activity for current DayEntry while ((line = reader.ReadLine()) != null) //go through all lines in text file { //line = prepLine(line); if ((m = regexEntry.Match(line)).Success) //matches (1st line)of a Food Entry format: 11g name brand 11/11/11 { fe = new FoodEntry(m); lfe.Add(fe); //output.AppendText(line + " <----- ENTRY\n"); } else if ((m = regexEntryResult.Match(line)).Success) //matches (2nd line)result macro values of a Food Entry format: =11/11/11 { fe.setEntryMacros(m.Groups["Macros"].Value); //output.AppendText(line + " <----- ERESULT\n"); } else if ((m = regexMealResult.Match(line)).Success) //matches MealEntry values after the Food list format: ===1==111//11//11 +-(desc) { lme.Add(new MealEntry(lfe, m)); lfe = new List <FoodEntry>(); //output.AppendText(line + " <----- MEAL\n"); } else if ((m = regexDayResult.Match(line)).Success) //matches DayEntry values after the Meal list format: 22/11/2020--->111||111||111 +-(desc) { Days.Add(new DayEntry(lme, m)); Days[Days.Count - 1].setActiv(activ); lme = new List <MealEntry>(); activ = ""; //output.AppendText(line + " <----- DAY\n"); } else if ((m = regexDayActiv.Match(line)).Success) //matches activity for current format: +SALA or +EXS { activ = m.Groups["Activ"].Value; //output.AppendText(line + " <----- ACTIV\n"); } else if ((m = regexDishResult.Match(line)).Success) //matches Dish definition values after the food list format: >===1 Pizza @eu 11/11/11 { FoodItem dtemp = new Dish(lfe, m); insertOrRefFood(ref dtemp); lfe = new List <FoodEntry>(); //output.AppendText(line + " <----- DISH\n"); } } reader.Close(); Foods.Sort((x, y) => (x._name + x._brand).CompareTo(y._name + y._brand)); //sorts the Foods list alphabetically by Name and Brand timer.Stop(); output.AppendText($"\n...DONE! ({timer.Elapsed})\n"); //prints time required to do to output button_intoDB.Enabled = true; //enables loading data to Database }
} //init with string to be matched #endregion #region Add/Set public void addFoodEntry(FoodEntry foodEntry) { _foodEntries.Add(foodEntry); }