// Static Methods public static EventHistory Parse(string[] lines, ref int index) { EventHistory eventHistory = new EventHistory(); // Parse the report header eventHistory.Header = Header.Parse(lines, ref index); // Skip to the next nonblank line EventFile.SkipBlanks(lines, ref index); // Parse event histories eventHistory.Histories = EventHistoryRecord.ParseRecords(lines, ref index); return(eventHistory); }
private static List <EventHistoryRecord> ParseEventHistoryRecords(string[] lines, ref int index) { List <EventHistoryRecord> histories = new List <EventHistoryRecord>(); EventHistoryRecord eventHistory; string currentLine; List <Token> tokens; List <Token> headers; Dictionary <Token, Token> fields; Token fieldHeader; Token field; int eventNumber; DateTime dateTime; double faultLocation; double current; double frequency; int group; int shot; string date; string time; // Parse header headers = Split(lines[index++]); // Skip to the next nonblank line SkipBlanks(lines, ref index); while (index < lines.Length) { currentLine = lines[index]; // Empty line indicates end of event histories if (string.IsNullOrWhiteSpace(currentLine)) { break; } // Create a new event history record eventHistory = new EventHistoryRecord(); // Parse fields tokens = Split(currentLine); // Initialize date and time variables date = null; time = null; fields = new Dictionary <Token, Token>(); foreach (Token token in tokens) { fieldHeader = headers.MinBy(header => token.Distance(header)); fields.AddOrUpdate(fieldHeader, token, (key, value) => value.JoinWith(token)); } foreach (Token header in headers) { if (fields.TryGetValue(header, out field)) { switch (header.Text.ToUpper()) { case "#": case "REC_NUM": // Parse the field as an event number if (int.TryParse(field.Text, out eventNumber)) { eventHistory.EventNumber = eventNumber; } break; case "DATE": // Parse the field as a date value date = field.Text; // If both date and time have been provided, parse them as a DateTime if ((object)time != null && TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime)) { eventHistory.Time = dateTime; } break; case "TIME": // Parse the field as a time value time = field.Text; // If both date and time have been provided, parse them as a DateTime if ((object)date != null && TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime)) { eventHistory.Time = dateTime; } break; case "EVENT": // Parse the field as an event type eventHistory.EventType = field.Text; break; case "LOCAT": case "LOCATION": // Parse the field as a fault location value if (double.TryParse(field.Text, out faultLocation)) { eventHistory.FaultLocation = faultLocation; } break; case "CURR": // Parse the field as a current magnitude if (double.TryParse(field.Text, out current)) { eventHistory.Current = current; } break; case "FREQ": // Parse the field as a frequency value if (double.TryParse(field.Text, out frequency)) { eventHistory.Frequency = frequency; } break; case "GRP": case "GROUP": // Parse the field as a group number if (int.TryParse(field.Text, out group)) { eventHistory.Group = group; } break; case "SHOT": // Parse the field as a shot number if (int.TryParse(field.Text, out shot)) { eventHistory.Shot = shot; } break; case "TARGETS": // Parse the field as targets eventHistory.Targets = field.Text; break; } } } // Add history record to the list of histories histories.Add(eventHistory); // Advance to the next line index++; } return(histories); }
private static List<EventHistoryRecord> ParseEventHistoryRecords(string[] lines, ref int index) { List<EventHistoryRecord> histories = new List<EventHistoryRecord>(); EventHistoryRecord eventHistory; string currentLine; List<Token> tokens; List<Token> headers; Dictionary<Token, Token> fields; Token fieldHeader; Token field; int eventNumber; DateTime dateTime; double faultLocation; double current; double frequency; int group; int shot; string date; string time; // Parse header headers = Split(lines[index++]); // Skip to the next nonblank line SkipBlanks(lines, ref index); while (index < lines.Length) { currentLine = lines[index]; // Empty line indicates end of event histories if (string.IsNullOrWhiteSpace(currentLine)) break; // Create a new event history record eventHistory = new EventHistoryRecord(); // Parse fields tokens = Split(currentLine); // Initialize date and time variables date = null; time = null; fields = new Dictionary<Token, Token>(); foreach (Token token in tokens) { fieldHeader = headers.MinBy(header => token.Distance(header)); fields.AddOrUpdate(fieldHeader, token, (key, value) => value.JoinWith(token)); } foreach (Token header in headers) { if (fields.TryGetValue(header, out field)) { switch (header.Text.ToUpper()) { case "#": case "REC_NUM": // Parse the field as an event number if (int.TryParse(field.Text, out eventNumber)) eventHistory.EventNumber = eventNumber; break; case "DATE": // Parse the field as a date value date = field.Text; // If both date and time have been provided, parse them as a DateTime if ((object)time != null && TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime)) eventHistory.Time = dateTime; break; case "TIME": // Parse the field as a time value time = field.Text; // If both date and time have been provided, parse them as a DateTime if ((object)date != null && TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime)) eventHistory.Time = dateTime; break; case "EVENT": // Parse the field as an event type eventHistory.EventType = field.Text; break; case "LOCAT": case "LOCATION": // Parse the field as a fault location value if (double.TryParse(field.Text, out faultLocation)) eventHistory.FaultLocation = faultLocation; break; case "CURR": // Parse the field as a current magnitude if (double.TryParse(field.Text, out current)) eventHistory.Current = current; break; case "FREQ": // Parse the field as a frequency value if (double.TryParse(field.Text, out frequency)) eventHistory.Frequency = frequency; break; case "GRP": case "GROUP": // Parse the field as a group number if (int.TryParse(field.Text, out group)) eventHistory.Group = group; break; case "SHOT": // Parse the field as a shot number if (int.TryParse(field.Text, out shot)) eventHistory.Shot = shot; break; case "TARGETS": // Parse the field as targets eventHistory.Targets = field.Text; break; } } } // Add history record to the list of histories histories.Add(eventHistory); // Advance to the next line index++; } return histories; }