// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text.RegularExpressions; namespace E { public class AIC { public string HYST { get; set; } public string NAME { get; set; } public string UNIT { get; set; } public string DESCR { get; set; } public string DISPMAX { get; set; } public string DISPMIN { get; set; } public string DEC { get; set; } public string NORM_TR { get; set; } public string H2_R_FCL { get; set; } public string H1_R_FCL { get; set; } public string L1_R_FCL { get; set; } public string L2_R_FCL { get; set; } public string PROC_SEC { get; set; } public string CLASS { get; set; } public string H1_LIM2 { get; set; } public string H1_LIM1 { get; set; } public override string ToString() { return string.Format("[AIC HYST={0}, NAME={1}, UNIT={2}, DESCR={3}, DISPMAX={4}, DISPMIN={5}, DEC={6}, NORM_TR={7}, H2_R_FCL={8}, H1_R_FCL={9}, L1_R_FCL={10}, L2_R_FCL={11}, PROC_SEC={12}, CLASS={13}, H1_LIM2={14}, H1_LIM1={15}]", HYST, NAME, UNIT, DESCR, DISPMAX, DISPMIN, DEC, NORM_TR, H2_R_FCL, H1_R_FCL, L1_R_FCL, L2_R_FCL, PROC_SEC, CLASS, H1_LIM2, H1_LIM1); } } public abstract class FileProcessor { public delegate void ObjectHandler(object o); private static Regex typdef = new Regex(@"^\w+\s+(\w+)$"); private static Regex propdef = new Regex(@"^\s*:(\w+)\s+(\S+)$"); public void Process(string fnm, ObjectHandler oh) { using(StreamReader sr = new StreamReader(fnm)) { object currobj = new object(); string line; while((line = sr.ReadLine()) != null) { Match m1 = typdef.Match(line); if(m1.Success) { currobj = CreateObject(m1.Groups[1].Value); } else { Match m2 = propdef.Match(line); if(m2.Success) { SetProperty(currobj, m2.Groups[1].Value, m2.Groups[2].Value); } else { if(line.Trim().Length == 0) { oh(currobj); } else { // ???? } } } } } } public abstract object CreateObject(string typnam); public abstract void SetProperty(object o, string nam, string val); } public class IfFileProcessor : FileProcessor { public override object CreateObject(string typnam) { if(typnam == "AIC") { return new AIC(); } else { throw new Exception("Unsupported type : " + typnam); } } public override void SetProperty(object o, string nam, string val) { if(o is AIC) { AIC aic = (AIC)o; if(nam == "HYST") { aic.HYST = val; } else if(nam == "NAME") { aic.NAME = val; } else if(nam == "UNIT") { aic.UNIT = val; } else if(nam == "DESCR") { aic.DESCR = val; } else if(nam == "DISPMAX") { aic.DISPMAX = val; } else if(nam == "DISPMIN") { aic.DISPMIN = val; } else if(nam == "DEC") { aic.DEC = val; } else if(nam == "NORM_TR") { aic.NORM_TR = val; } else if(nam == "H2_R_FCL") { aic.H2_R_FCL = val; } else if(nam == "H1_R_FCL") { aic.H1_R_FCL = val; } else if(nam == "L1_R_FCL") { aic.L1_R_FCL = val; } else if(nam == "L2_R_FCL") { aic.L2_R_FCL = val; } else if(nam == "PROC_SEC") { aic.PROC_SEC = val; } else if(nam == "CLASS") { aic.CLASS = val; } else if(nam == "H1_LIM2") { aic.H1_LIM2 = val; } else if(nam == "H1_LIM1") { aic.H1_LIM1 = val; } else { throw new Exception("Unknown property : " + nam); } } } } public class SwitchFileProcessor : FileProcessor { public override object CreateObject(string typnam) { switch(typnam) { case "AIC": return new AIC(); default: throw new Exception("Unsupported type : " + typnam); } } public override void SetProperty(object o, string nam, string val) { if(o is AIC) { AIC aic = (AIC)o; switch(nam) { case "HYST": aic.HYST = val; break; case "NAME": aic.NAME = val; break; case "UNIT": aic.UNIT = val; break; case "DESCR": aic.DESCR = val; break; case "DISPMAX": aic.DISPMAX = val; break; case "DISPMIN": aic.DISPMIN = val; break; case "DEC": aic.DEC = val; break; case "NORM_TR": aic.NORM_TR = val; break; case "H2_R_FCL": aic.H2_R_FCL = val; break; case "H1_R_FCL": aic.H1_R_FCL = val; break; case "L1_R_FCL": aic.L1_R_FCL = val; break; case "L2_R_FCL": aic.L2_R_FCL = val; break; case "PROC_SEC": aic.PROC_SEC = val; break; case "CLASS": aic.CLASS = val; break; case "H1_LIM2": aic.H1_LIM2 = val; break; case "H1_LIM1": aic.H1_LIM1 = val; break; default: throw new Exception("Unknown property : " + nam); } } } } public class ReflectionFileProcessor : FileProcessor { private IDictionary propcache = new Dictionary(); public override object CreateObject(string typnam) { return Activator.CreateInstance(Type.GetType("E." + typnam)); } public override void SetProperty(object o, string nam, string val) { string key = o.GetType().FullName + "#" + nam; PropertyInfo pi; if(propcache.ContainsKey(key)) { pi = propcache[key]; } else { pi = o.GetType().GetProperty(nam); if(pi == null) throw new Exception("Unknown property : " +nam); propcache.Add(key, pi); } pi.SetValue(o, val, null); } } public class DelegateLookupFileProcessor : FileProcessor { private delegate object ObjectCreator(); private delegate void PropertySetter(object o,string val); private IDictionary typs = new Dictionary(); private IDictionary> props = new Dictionary>(); public DelegateLookupFileProcessor() { typs.Add("AIC", ()=>new AIC()); props.Add("E.AIC", new Dictionary()); props["E.AIC"].Add("HYST", (o,val) => ((AIC)o).HYST = val); props["E.AIC"].Add("NAME", (o,val) => ((AIC)o).NAME = val); props["E.AIC"].Add("UNIT", (o,val) => ((AIC)o).UNIT = val); props["E.AIC"].Add("DESCR", (o,val) => ((AIC)o).DESCR = val); props["E.AIC"].Add("DISPMAX", (o,val) => ((AIC)o).DISPMAX = val); props["E.AIC"].Add("DISPMIN", (o,val) => ((AIC)o).DISPMIN = val); props["E.AIC"].Add("DEC", (o,val) => ((AIC)o).DEC = val); props["E.AIC"].Add("NORM_TR", (o,val) => ((AIC)o).NORM_TR = val); props["E.AIC"].Add("H2_R_FCL", (o,val) => ((AIC)o).H2_R_FCL = val); props["E.AIC"].Add("H1_R_FCL", (o,val) => ((AIC)o).H1_R_FCL = val); props["E.AIC"].Add("L1_R_FCL", (o,val) => ((AIC)o).L1_R_FCL = val); props["E.AIC"].Add("L2_R_FCL", (o,val) => ((AIC)o).L2_R_FCL = val); props["E.AIC"].Add("PROC_SEC", (o,val) => ((AIC)o).PROC_SEC = val); props["E.AIC"].Add("CLASS", (o,val) => ((AIC)o).CLASS = val); props["E.AIC"].Add("H1_LIM2", (o,val) => ((AIC)o).H1_LIM2 = val); props["E.AIC"].Add("H1_LIM1", (o,val) => ((AIC)o).H1_LIM1 = val); } public override object CreateObject(string typnam) { return typs[typnam](); } public override void SetProperty(object o, string nam, string val) { props[o.GetType().FullName][nam](o, val); } } public class Program { public static void Print(object o) { Console.WriteLine(o); } public static void Main(string[] args) { FileProcessor fp1 = new IfFileProcessor(); fp1.Process(@"\work\tony.dat", Print); FileProcessor fp2 = new SwitchFileProcessor(); fp2.Process(@"\work\tony.dat", Print); FileProcessor fp3 = new ReflectionFileProcessor(); fp3.Process(@"\work\tony.dat", Print); FileProcessor fp4 = new DelegateLookupFileProcessor(); fp4.Process(@"\work\tony.dat", Print); } } }