/// <summary> /// Saves data to specified place using <paramref name="saver"/> interface. /// </summary> /// <param name="saveFromFile">Specified file with data.</param> /// <param name="saver">Interaface for saving.</param> /// <exception cref="ArgumentNullException">Throws when <paramref name="saveFromFile"/>, <paramref name="saver"/> or ProccesInfo event is null.</exception> /// <exception cref="Exception">Throws when some exeptions occurs.</exception> public void SaveData(string saveFromFile, IDataSaver saver) { if (saveFromFile is null) { throw new ArgumentNullException(String.Format("{0} can't be null.", saveFromFile)); } if (saver is null) { throw new ArgumentNullException(String.Format("{0} can't be null.", saver)); } if (ProcessInfo is null) { throw new ArgumentNullException(String.Format("You must be subscribed on event {0}.", ProcessInfo)); } int currentLineNumber = 0; int totalLinesCount = 0; try { using (StreamReader reader = new StreamReader(saveFromFile)) { while (reader.ReadLine() != null) { totalLinesCount++; } } using (StreamReader reader = new StreamReader(saveFromFile, Encoding.Default)) { string currentLine = null; while ((currentLine = reader.ReadLine()) != null) { currentLineNumber++; saver.SaveRow(RowRepresentation.ParseToRowRepresentation(currentLine)); ProcessInfo?.Invoke(currentLineNumber, totalLinesCount - currentLineNumber); } } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Parses string row to the RowRepresentation. /// </summary> /// <param name="row">Specified row.</param> /// <returns>Parsed result.</returns> public static RowRepresentation ParseToRowRepresentation(string row) { RowRepresentation rowRepresentation = new RowRepresentation(); string[] values = row.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries); rowRepresentation.Date = DateTime.Parse(values[0]); rowRepresentation.LatinSequence = values[1]; rowRepresentation.CyrillicSequence = values[2]; rowRepresentation.IntegerNumber = Int32.Parse(values[3]); rowRepresentation.FloatNumber = float.Parse(values[4]); return(rowRepresentation); }