/// <summary> /// Takes records from a specified input file, sorts them as being either valid or invalid, and writes /// them to corresponding output files. To be valid, a given record must have the same number of fields /// as was specified by the user. /// /// If a record is valid, it is written to "<see cref="OutputFileNames.VALID" />". If it is invalid, it /// is written to "<see cref="OutputFileNames.INVALID" />". This will overwrite any previous contents /// in either file, if it already exists, or create a new file if necessary. /// /// If either category does not include any records, its corresponding file will not be created. In /// that case, if such a file previously exists, it will be deleted. /// </summary> /// <param name="pOptions"> /// The user-specified options to use while performing this operation. /// </param> public void SortRecords(RecordSortingOptions pOptions) { InputRecordList input = _inputFileIoManager.ReadFile(pOptions); // Per the requirements, the header row's contents are irrelevant in determining a record's validity. string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; ProduceOutput(Path.Combine(baseDirectory, OutputFileNames.VALID), pOptions.Delimiter, input.Records.Where(x => x.Count == pOptions.NumFields)); ProduceOutput(Path.Combine(baseDirectory, OutputFileNames.INVALID), pOptions.Delimiter, input.Records.Where(x => x.Count != pOptions.NumFields)); }
/// <summary> /// Reads the contents of an input file and returns them in an instance of <see cref="InputRecordList" /// />. /// </summary> /// <param name="pOptions"> /// The user-specified options to be used when reading the file. /// </param> /// <returns> /// An instance of <see cref="InputRecordList" /> representing the contents of the input file. /// </returns> /// <exception cref="InvalidFileFormatException"> /// Thrown when the file's internal contents are determined to be in an invalid format. /// </exception> public InputRecordList ReadFile(RecordSortingOptions pOptions) { File.ReadAllLines(pOptions.Location); using (StreamReader sr = new StreamReader(pOptions.Location)) { string line = sr.ReadLine(); // The requirements specify that the file must begin with a header row. if (line == null) { throw new InvalidFileFormatException(); } string[] headerRow = line.Split(pOptions.Delimiter); var records = new List <IReadOnlyCollection <string> >(); while ((line = sr.ReadLine()) != null) { records.Add(line.Split(pOptions.Delimiter)); } return(new InputRecordList(headerRow, records)); } }