/// <summary> /// Seek to the specified date. Will throw if date not found. /// </summary> /// <param name="Date"></param> public void SeekToDate(DateTime Date) { int NumRowsToSkip = (Date - _FirstDate).Days; In.Seek(FirstLinePosition, SeekOrigin.Begin); while (!In.EndOfStream && NumRowsToSkip > 0) { In.ReadLine(); NumRowsToSkip--; } int SavedPosition = In.Position; StringCollection Words = new StringCollection(); if (GetNextLine(In, ref Words)) { // Make sure we found the date. object[] Values = ConvertWordsToObjects(Words, ColumnTypes); DateTime RowDate = GetDateFromValues(Values); if (RowDate != Date) { throw new Exception("Non consecutive dates found in file: " + _FileName); } } else { throw new Exception("End of file reached while trying to find date " + Date.ToShortDateString() + " in file " + _FileName); } // All ok - restore position. In.Seek(SavedPosition, SeekOrigin.Begin); }
/// <summary> /// Open the file ready for reading. /// </summary> public void Open(string FileName) { if (FileName == "") { return; } if (!File.Exists(FileName)) { throw new Exception("Cannot find file: " + FileName); } _FileName = FileName; CSV = Path.GetExtension(FileName).ToLower() == ".csv"; _Constants.Clear(); In = new StreamReaderRandomAccess(_FileName); ReadApsimHeader(In); FirstLinePosition = In.Position; // Read in first line. StringCollection Words = new StringCollection(); GetNextLine(In, ref Words); ColumnTypes = DetermineColumnTypes(In, Words); // Get first date. object[] Values = ConvertWordsToObjects(Words, ColumnTypes); _FirstDate = GetDateFromValues(Values); // Now we need to seek to the end of file and find the last full line in the file. In.Seek(0, SeekOrigin.End); if (In.Position >= 1000 && In.Position - 1000 > FirstLinePosition) { In.Seek(-1000, SeekOrigin.End); In.ReadLine(); // throw away partial line. } else { In.Seek(FirstLinePosition, SeekOrigin.Begin); } while (GetNextLine(In, ref Words)) { } In.Seek(FirstLinePosition, SeekOrigin.Begin); // Get the date from the last line. if (Words.Count == 0) { throw new Exception("Cannot find last row of file: " + FileName); } Values = ConvertWordsToObjects(Words, ColumnTypes); _LastDate = GetDateFromValues(Values); }
public void TestBomParsing(string inputFile) { string[] expected = new string[4] { "x,y", "0,1", "1,2", "2,4" }; long[] endOfLinePositions = new long[expected.Length]; using (Stream stream = GetResourceStream(inputFile)) { StreamReaderRandomAccess reader = new StreamReaderRandomAccess(stream); for (int i = 0; i < expected.Length; i++) { string line = reader.ReadLine(); Assert.AreEqual(expected[i], line); endOfLinePositions[i] = reader.Position; } // Seek to the end of each line, and read a line, and ensure // that we get the expected result. This should probably be a // separate test. for (int i = 1; i < expected.Length - 1; i++) { reader.Seek(endOfLinePositions[i], SeekOrigin.Begin); string input = reader.ReadLine(); Assert.AreEqual(expected[i + 1], input); } } }
public void TestPositionAfterSeek(string input, int seekPosition, SeekOrigin origin, int expectedPosition) { using (Stream stream = CreateStream(input)) { StreamReaderRandomAccess reader = new StreamReaderRandomAccess(stream); reader.Seek(seekPosition, SeekOrigin.Begin); Assert.AreEqual(expectedPosition, reader.Position); } }
public void TestSeek(string input, int position, SeekOrigin origin, string expectedOutput) { using (Stream stream = CreateStream(input)) { StreamReaderRandomAccess reader = new StreamReaderRandomAccess(stream); reader.Seek(position, origin); string actualOutput = reader.ReadLine(); Assert.AreEqual(expectedOutput, actualOutput); } }