private static void ParseWeek(StringIterator Stream, out int WeekId, out String WeekData) { WeekId = 0; WeekData = ""; var idString = ""; while (!Stream.AtEnd && Stream.Next >= '0' && Stream.Next <= '9') { idString += (char)Stream.Next; Stream.Advance(); } if (String.IsNullOrEmpty(idString)) return; var dataString = ""; while (!Stream.AtEnd && Stream.Next != ';') { dataString += (char)Stream.Next; Stream.Advance(); } Stream.Advance(); WeekId = Convert.ToInt32(idString); WeekData = dataString; }
public void LoadData() { try { Weeks = new List<WeekData>(); if (!System.IO.File.Exists("data.txt")) { Weeks.Add(new WeekData()); return; } var text = System.IO.File.ReadAllText("data.txt").Trim(); var stream = new StringIterator(text); if (!stream.AtEnd && stream.Next == '@') { stream.Advance(); while (!stream.AtEnd && stream.Next != '@') stream.Advance(); stream.Advance(); } while (!stream.AtEnd) { var weekId = 0; var weekData = ""; SkipWhitespace(stream); ParseWeek(stream, out weekId, out weekData); while (weekId >= Weeks.Count) Weeks.Add(new WeekData()); Weeks[weekId].EffectiveChanges = weekData; } } catch (Exception e) { System.Windows.MessageBox.Show(String.Format("An error has occured while loading saved data.\n{0}\nLoading will be aborted to preserve your existing data. To discard your saved data, delete 'data.txt' found in the folder where you installed Leave Wizard. It is likely that your existing data has been corrupted, but it might be possible to save it.", e.Message), "Critical error!", System.Windows.MessageBoxButton.OK); throw e; } }
public void Propogate(WeekData PreviousWeek) { this.PreviousWeek = PreviousWeek; if (PreviousWeek == null) { SaturdayDate = DateTime.FromOADate(0); PayPeriod = 0; Week = 1; Year = 2015; Substitutes = new List<Substitute>(); Regulars = new List<RegularCarrier>(); DailySchedules = new DailySchedule[7]; for (int i = 0; i < 7; ++i) DailySchedules[i] = new DailySchedule(); PendingJDays = new List<Tuple<DayOfWeek, LeaveEntry>>(); LeavePolicy = new LeavePolicy { DaysPerSub = 5, AmazonRoutes = 0 }; } else { LeavePolicy = PreviousWeek.LeavePolicy; Year = PreviousWeek.Year; Week = PreviousWeek.Week + 1; if (Week == 3) { PayPeriod = PreviousWeek.PayPeriod + 1; Week = 1; } else PayPeriod = PreviousWeek.PayPeriod; SaturdayDate = PreviousWeek.SaturdayDate + TimeSpan.FromDays(7); Substitutes = new List<Substitute>(PreviousWeek.Substitutes); Regulars = new List<RegularCarrier>(PreviousWeek.Regulars); PendingJDays = new List<Tuple<DayOfWeek, LeaveEntry>>(); DailySchedules = PreviousWeek.DailySchedules.Select(ds => ds.CloneForNewWeek()).ToArray(); for ( var day = 0; day < 7; ++day) { foreach (var rd in DailySchedules[day].ReliefDays) if (rd.LeaveType == "J") PendingJDays.Add(Tuple.Create((DayOfWeek)day, rd)); DailySchedules[day].ReliefDays.RemoveAll(rd => rd.LeaveType == "J"); } if (PreviousWeek.PendingJDays != null) foreach (var pendingJ in PreviousWeek.PendingJDays) DailySchedules[(int)pendingJ.Item1].ReliefDays.Add(pendingJ.Item2); } PrepareSundayRoutes(); if (String.IsNullOrEmpty(EffectiveChanges)) EffectiveChanges = ""; var iter = new StringIterator(EffectiveChanges); try { while (!iter.AtEnd) { SkipWhitespace(iter); if (iter.Next == 'S') { var sub = ParseSub(iter); var existing = Substitutes.Find(s => s.Name == sub.Name); if (existing != null) Substitutes.Remove(existing); Substitutes.Add(sub); } else if (iter.Next == 'R') { var regular = ParseRegular(iter); var existing = Regulars.Find(r => r.Name == regular.Name); if (existing != null) Regulars.Remove(existing); Regulars.Add(regular); } else if (iter.Next == 'L') { bool denied = false; iter.Advance(); SkipWhitespace(iter); if (iter.Next == 'D') { iter.Advance(); SkipWhitespace(iter); denied = true; } var name = ParseName(iter); SkipWhitespace(iter); var day = ParseDay(iter); SkipWhitespace(iter); var type = ParseLeave(iter); iter.Advance(); var leave = new LeaveEntry { Carrier = name, LeaveType = type, Substitute = "DUMMY" }; if (denied) DailySchedules[(int)day].DeniedLeave.Add(leave); else { DailySchedules[(int)day].ReliefDays.Add(leave); //Leave supercedes a route assignment. foreach (var reliefDay in DailySchedules[(int)day].ReliefDays.Where(rd => rd.Substitute == name)) reliefDay.Substitute = "DUMMY"; } } else if (iter.Next == 'B') { iter.Advance(); SkipWhitespace(iter); var dateString = ParseName(iter); SkipWhitespace(iter); var year = ParseRoute(iter); SkipWhitespace(iter); var payPeriod = ParseRoute(iter); SkipWhitespace(iter); var week = ParseRoute(iter); this.SaturdayDate = Convert.ToDateTime(dateString); this.Year = year; this.PayPeriod = payPeriod; this.Week = week; } else if (iter.Next == 'P') { iter.Advance(); SkipWhitespace(iter); var daysPerSub = ParseRoute(iter); SkipWhitespace(iter); var amazonRoutes = ParseRoute(iter); LeavePolicy = new LeavePolicy { DaysPerSub = daysPerSub, AmazonRoutes = amazonRoutes }; PrepareSundayRoutes(); } else if (iter.Next == 'A') { iter.Advance(); SkipWhitespace(iter); var regular = ParseName(iter); SkipWhitespace(iter); var sub = ParseName(iter); SkipWhitespace(iter); var day = ParseDay(iter); if (sub != "DUMMY") foreach (var reliefDay in DailySchedules[(int)day].ReliefDays) if (reliefDay.Substitute == sub) reliefDay.Substitute = "DUMMY"; foreach (var reliefDay in DailySchedules[(int)day].ReliefDays) if (reliefDay.Carrier == regular) reliefDay.Substitute = sub; } else if (iter.Next == 'H') { iter.Advance(); SkipWhitespace(iter); var day = ParseDay(iter); Holiday(day); } else if (iter.Next == 'D') { iter.Advance(); if (iter.Next == 'S') { iter.Advance(); SkipWhitespace(iter); var name = ParseName(iter); Substitutes.RemoveAll(s => s.Name == name); } else if (iter.Next == 'R') { iter.Advance(); SkipWhitespace(iter); var name = ParseName(iter); Regulars.RemoveAll(r => r.Name == name); } else if (iter.Next == 'L') { iter.Advance(); SkipWhitespace(iter); var name = ParseName(iter); SkipWhitespace(iter); var day = ParseDay(iter); DailySchedules[(int)day].ReliefDays.RemoveAll(r => r.Carrier == name); } else if (iter.Next == 'D') { iter.Advance(); SkipWhitespace(iter); var name = ParseName(iter); SkipWhitespace(iter); var day = ParseDay(iter); DailySchedules[(int)day].DeniedLeave.RemoveAll(r => r.Carrier == name); } else if (iter.Next == 'H') { iter.Advance(); SkipWhitespace(iter); var day = ParseDay(iter); ClearHoliday(day); } } else { iter.Advance(); } } } catch (Exception e) { } foreach (var ds in DailySchedules) ds.ReliefDays = new List<LeaveEntry>(ds.ReliefDays.OrderBy(rd => rd.Carrier)); //Substitutes = new List<Substitute>(Substitutes.OrderBy(s => s.Name)); Regulars = new List<RegularCarrier>(Regulars.OrderBy(r => r.Route)); }
private void SkipWhitespace(StringIterator Iter) { while (!Iter.AtEnd && (Iter.Next == ' ' || Iter.Next == '\n' || Iter.Next == '\t' || Iter.Next == '\r')) Iter.Advance(); }
private Substitute ParseSub(StringIterator Iter) { var sub = new Substitute(); Iter.Advance(); SkipWhitespace(Iter); sub.Name = ParseName(Iter); SkipWhitespace(Iter); if (Iter.Next == 'M') { Iter.Advance(); SkipWhitespace(Iter); sub.Matrix[0] = ParseRoute(Iter); SkipWhitespace(Iter); sub.Matrix[1] = ParseRoute(Iter); SkipWhitespace(Iter); sub.Matrix[2] = ParseRoute(Iter); } return sub; }
private int ParseRoute(StringIterator Iter) { var numberString = ""; while (!Iter.AtEnd && Iter.Next >= '0' && Iter.Next <= '9') { numberString += (char)Iter.Next; Iter.Advance(); } return Convert.ToInt32(numberString); }
private RegularCarrier ParseRegular(StringIterator Iter) { var regular = new RegularCarrier(); Iter.Advance(); SkipWhitespace(Iter); regular.Name = ParseName(Iter); SkipWhitespace(Iter); regular.Route = ParseRoute(Iter); return regular; }
private string ParseName(StringIterator Iter) { var nameString = ""; Iter.Advance(); while (!Iter.AtEnd && Iter.Next != '"') { nameString += (char)Iter.Next; Iter.Advance(); } Iter.Advance(); return nameString; }
private string ParseLeave(StringIterator Iter) { var leave = ""; while (!Iter.AtEnd && !IsWhitespace(Iter)) { leave += (char)Iter.Next; Iter.Advance(); } return leave; }
private DayOfWeek ParseDay(StringIterator Iter) { var dayName = ""; for (var i = 0; i < 3; ++i) { if (Iter.AtEnd) continue; dayName += (char)Iter.Next; Iter.Advance(); } for (int i = 0; i < Constants.DayNames.Length; ++i) { if (dayName == Constants.DayNames[i]) return (DayOfWeek)i; } return DayOfWeek.Saturday; }
private bool IsWhitespace(StringIterator Iter) { return (Iter.Next == ' ' || Iter.Next == '\n' || Iter.Next == '\t' || Iter.Next == '\r'); }