// 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.Globalization; using System.IO; using System.Linq; using System.Xml; namespace E { public static class Utils { public static string ToCapital(this string s) { return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower(); } } public class StructuredItem { public string WeekDay { get; set; } public string Course { get; set; } public string CourseType { get; set; } public string StartTime { get; set; } public string EndTime { get; set; } public string Weeks { get; set; } public string Location { get; set; } public override string ToString() { return "(" + WeekDay + ";" + Course + ";" + CourseType + ";" + StartTime + ";" + EndTime + ";" + Weeks + ";" + Location + ")"; } } public class OrganizedItem { public string Course { get; set; } public string CourseType { get; set; } public string Time { get; set; } public string Location { get; set; } public override string ToString() { return "(" + Course + ";" + CourseType + ";" + Time + ";" + Location + ")"; } } public class Convert { public static List LoadRaw(string fnm) { List res = new List(); using(StreamReader sr = new StreamReader(fnm)) { string line; while((line = sr.ReadLine()) != null) { res.Add(line); } } return res; } public static List RawToStructured(List data) { List res = new List(); string day = null; int ix = 0; while(ix < data.Count) { if(data[ix].EndsWith("dag")) { day = data[ix]; ix++; } else { StructuredItem itm = new StructuredItem(); itm.WeekDay = day; itm.Course = data[ix]; ix++; itm.CourseType = data[ix]; ix++; itm.StartTime = data[ix]; ix++; itm.EndTime = data[ix]; ix++; itm.Weeks = data[ix]; ix++; itm.Location = data[ix]; ix++; res.Add(itm); } } return res; } private static readonly string[] daynames = (new CultureInfo("da-DK")).DateTimeFormat.DayNames; public static Dictionary>> StructuredToOrganized(List data) { Dictionary>> res = new Dictionary>>(); foreach(StructuredItem itm in data) { //DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), itm.WeekDay.ToCapital()); DayOfWeek day = (DayOfWeek)Array.FindIndex(daynames, dn => dn.Equals(itm.WeekDay)); OrganizedItem newitm = new OrganizedItem(); newitm.Course = itm.Course; newitm.CourseType = itm.CourseType; newitm.Time = itm.StartTime + " - " + itm.EndTime; newitm.Location = itm.Location; foreach(string wintval in itm.Weeks.Split(',')) { string[] wparts = wintval.Split('-'); int wstart; int wend; if(wparts.Length == 1) { wstart = int.Parse(wparts[0]); wend = wstart; } else { wstart = int.Parse(wparts[0]); wend = int.Parse(wparts[1]); } for(int w = wstart; w <= wend; w++) { if(!res.ContainsKey(w)) { res.Add(w, new Dictionary>()); } if(!res[w].ContainsKey(day)) { res[w].Add(day, new List()); } res[w][day].Add(newitm); } } } return res; } public static XmlDocument OrganizedToXml(Dictionary>> data) { XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("plan")); foreach(int w in data.Keys.OrderBy(w => w)) { XmlElement welm = doc.CreateElement("week"); welm.SetAttribute("no", w.ToString()); doc.DocumentElement.AppendChild(welm); foreach(DayOfWeek day in data[w].Keys.OrderBy(d => d)) { XmlElement dayelm = doc.CreateElement("day"); dayelm.SetAttribute("name", day.ToString()); welm.AppendChild(dayelm); foreach(OrganizedItem itm in data[w][day]) { XmlElement itmelm = doc.CreateElement("item"); dayelm.AppendChild(itmelm); XmlElement elm1 = doc.CreateElement("course"); elm1.AppendChild(doc.CreateTextNode(itm.Course)); itmelm.AppendChild(elm1); XmlElement elm2 = doc.CreateElement("course-type"); elm2.AppendChild(doc.CreateTextNode(itm.CourseType)); itmelm.AppendChild(elm2); XmlElement elm3 = doc.CreateElement("time"); elm3.AppendChild(doc.CreateTextNode(itm.Time)); itmelm.AppendChild(elm3); XmlElement elm4 = doc.CreateElement("location"); elm4.AppendChild(doc.CreateTextNode(itm.Location)); itmelm.AppendChild(elm4); } } } return doc; } } public class Program { public static void Main(string[] args) { List raw = Convert.LoadRaw(@"C:\cbs.txt"); foreach(string s in raw) { Console.WriteLine(s); } List structured = Convert.RawToStructured(raw); foreach(StructuredItem itm in structured) { Console.WriteLine(itm); } Dictionary>> organized = Convert.StructuredToOrganized(structured); foreach(int w in organized.Keys.OrderBy(w => w)) { Console.WriteLine("Week: " + w); foreach(DayOfWeek day in organized[w].Keys.OrderBy(d => d)) { Console.WriteLine(" Day: " + day); foreach(OrganizedItem itm in organized[w][day]) { Console.WriteLine(" " + itm); } } } XmlDocument doc = Convert.OrganizedToXml(organized); doc.Save(Console.Out); } } }