00001
00002 using System;
00003 using System.IO;
00004 using System.Collections.Generic;
00005 using Foodolini.BusinessLogic;
00006
00007 namespace Foodolini.Tools
00008 {
00009
00010
00011 public class MealMasterFormat : RecipeImporter
00012 {
00013
00014 public override Recipe ReadData (System.IO.FileStream fs)
00015 {
00016 string filename = Path.GetFileName(fs.Name);
00017 StreamReader sourceFile = null;
00018 try{
00019 sourceFile = new StreamReader(filename, System.Text.Encoding.GetEncoding("ISO-8859-1"));
00020 }
00021 catch(FileNotFoundException ex){
00022
00023 }
00024 finally{
00025 fs.Close();
00026 }
00027
00028 List<Recipe> recipes = new List<Recipe>();
00029
00030 int recipeCount = 0;
00031 string line;
00032 bool inRecipe = false;
00033
00034 List<string> steps = new List<string>();
00035
00036
00037 while(sourceFile.Peek() != -1){
00038 line = sourceFile.ReadLine();
00039 if(line.IndexOf("-----") == 0 & !inRecipe){
00040
00041 inRecipe = true;
00042 recipeCount += 1;
00043
00044 steps.Clear();
00045 }
00046 else if(line.Equals("-----") & inRecipe){
00047
00048
00049 recipes[recipeCount-1].Directions = steps;
00050 inRecipe = false;
00051 }
00052 else if(inRecipe){
00053
00054 if(line.IndexOf("Title:") == 6)
00055
00056 recipes.Add(new Recipe(line.Substring(13)));
00057 else if(line.IndexOf("Categories:") == 1){
00058
00059 string[] s = line.Substring(13).Replace(" ", "").Split(new char[] {','});
00060 List<string> listOfCategories = new List<string>();
00061 for(int i = 0; i < s.Length; i++)
00062 listOfCategories.Add(s[i]);
00063 recipes[recipeCount-1].Categories = listOfCategories;
00064 }
00065 else if(line.IndexOf("Yield:") == 6){
00066
00067 string[] s = SortYieldArray(line);
00068
00069 if(s.Length == 2)
00070 s[0] = ((Double.Parse(s[0]) + Double.Parse(s[1])) / 2).ToString();
00071
00072 recipes[recipeCount-1].Servings = Double.Parse(s[0]);
00073 }
00074 else{
00075 bool isIngredient = false;
00076 int j = 0;
00077
00078 foreach (char c in line){
00079 if(Char.IsDigit(c) & j > 1 & j > 7){
00080
00081 isIngredient = true;
00082 break;
00083 }
00084 if(j > 7)
00085 break;
00086 j++;
00087 }
00088
00089 if(isIngredient){
00090
00091
00092
00093 string ing = line.Substring(12, 27);
00094
00095
00096
00097
00098
00099
00100 }
00101 else{
00102
00103 string step = line;
00104 while(line.Length > 1)
00105 line = sourceFile.ReadLine();
00106 steps.Add(step);
00107 }
00108 }
00109 }
00110 }
00111 Console.WriteLine("Done, " + recipeCount + " recipes counted");
00112 Recipe recipe = new Recipe("HEJSA");
00113 return recipe;
00114 }
00115
00116
00120 private string[] SortYieldArray(string s){
00121
00122 string[] tempArray = s.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
00123 List<string> tempList = new List<string>();
00124
00125 for(int i = 0; i < tempArray.Length; i++){
00126 try{
00127 if(tempArray[i].IndexOf("/") != -1)
00128 Console.WriteLine("YOO! WTF MAN!?");
00129 double t = Double.Parse(tempArray[i]);
00130 tempList.Add(tempArray[i]);
00131 }
00132 catch{
00133 continue;
00134 }
00135 }
00136
00137
00138 string[] values = new string[tempList.Count];
00139 tempList.CopyTo(values);
00140
00141
00142 return values;
00143 }
00144 }
00145 }