00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using Foodolini.BusinessLogic; 00006 00007 namespace Foodolini.Tools 00008 { 00009 public abstract class RecipeImporter 00010 { 00011 public event EventHandler<RecipeImporterEventArgs> RaiseImportEvent; 00012 public abstract Recipe ReadData(System.IO.FileStream fs); 00013 00014 protected void OnRaiseImportEvent(RecipeImporterEventArgs e) 00015 { 00016 EventHandler<RecipeImporterEventArgs> handler = RaiseImportEvent; 00017 if (handler != null) 00018 handler(this, e); 00019 } 00020 00021 00027 protected double ParseQuantity(string[] s) 00028 { 00029 double result; 00030 char[] frac = { '/' }; 00031 if (s.Length > 1) { 00032 double a = double.Parse(s[0]); 00033 string[] f = s[1].Split(frac); 00034 result = a + (double.Parse(f[0]) / double.Parse(f[1])); 00035 } else { 00036 string[] f = s[0].Split(frac); 00037 if (f.Length > 1) 00038 result = double.Parse(f[0]) / double.Parse(f[1]); 00039 else 00040 result = double.Parse(f[0]); 00041 } 00042 return result; 00043 } 00044 } 00045 00046 public class RecipeImporterEventArgs : EventArgs 00047 { 00048 public string Message { get; set; } 00049 public EventCode EvCode { get; set; } 00050 00051 public RecipeImporterEventArgs(string message) : this(message, EventCode.None) { } 00052 00053 public RecipeImporterEventArgs(string message, EventCode evCode) 00054 { 00055 this.Message = message; 00056 this.EvCode = evCode; 00057 } 00058 00059 public enum EventCode 00060 { 00061 None, 00062 ServingsParseError, 00063 QuantityParseError, 00064 DataSourceError 00065 } 00066 } 00067 }