/// <summary> /// Reads the passed file to verify that each row adheres to the format defined by the /// FieldStarts Set -- assuming that the file has at least one space separating each field. /// </summary> /// <param name="PathSpec">The file to validate</param> /// <returns>True if the file format is consistent, else false</returns> private bool ValidateFormatConsistency(string PathSpec) { using (StreamReader Rdr = new StreamReader(PathSpec)) { string Inline; while ((Inline = Rdr.ReadLine()) != null) { Inline = Inline.Replace("\t", TabReplacementString); foreach (int FieldStart in FieldStarts) { if (FieldStart == 0) { continue; } if (Inline.Length < FieldStart - 1) { break; // supports ragged files } if (Inline[FieldStart - 1] != ' ') { return(false); // no space where format says space should be } } } } return(true); }
/// <summary> /// Scans the passed file and determines the format from the data -- assuming that fields are separated by /// at least one space. (Refer to the constructor for additional information). Initializes the "FieldStarts" /// field. /// </summary> /// <param name="PathSpec">The file to analyze</param> private void DetermineFieldStartsFromFile(string PathSpec) { FieldStarts.Add(0); // first field is always at position zero using (StreamReader Rdr = new StreamReader(PathSpec)) { string Inline; while ((Inline = Rdr.ReadLine()) != null) { foreach (Match m in Regex.Matches(Inline.Replace("\t", TabReplacementString), StartOfFieldPattern)) { FieldStarts.Add(m.Index + 1); } } } FieldStarts.Add(int.MaxValue); // simplifies the parsing later on }