private void VersionCheckAndUpgrade() { string line = fileReader.ReadLine(); if (line == null) { // fresh file WriteHeader(); return; } int fileVersion = -1; if (!line.StartsWith(VersionHeader) || !int.TryParse(line.Substring(VersionHeader.Length), out fileVersion)) { throw new FormatException("The history file has an invalid header."); } if (fileVersion < HistoryManagerVersion) { BackupFile(); } switch (fileVersion) { case 0: /*do upgrade stuff*/ goto case 1; case 1: break; // lastest version default: throw new FormatException("Not recognized header version"); } }
public void PositionedStreamReaderBufferSize() { using (var memStream = new MemoryStream()) { // Setting streams up var writer = new StreamWriter(memStream); string[] values = new[] { new string('1', 1024) + '\n', // 1025: 1 over buffer size new string('1', 1023) + '\n', // 1024: exactly the buffer size, but 1 over the 1024 line block due to the previous new string('1', 1022) + '\n', // 1023: 1 less then the buffer size, should now match the line block again new string('1', 1024) }; foreach (var val in values) { writer.Write(val); } writer.Flush(); memStream.Seek(0, SeekOrigin.Begin); var reader = new PositionedStreamReader(memStream); int pos = 0; foreach (var val in values) { var line = reader.ReadLine(); pos += val.Length; Assert.AreEqual(val.TrimEnd(new[] { '\r', '\n' }), line); Assert.AreEqual(pos, reader.ReadPosition); } } }
public void PositionedStreamReaderLineEndings() { using (var memStream = new MemoryStream()) { // Setting streams up var writer = new StreamWriter(memStream); string[] values = { "11\n", "22\n", "33\n", "44\r", "55\r", "66\r", "77\r\n", "88\r\n", "99\r\n", "xx\n", "\r", "yy\n", "\r", "zz\n", "\r", "a\r", "b\n", "c\r\n", "d\n", "\r", "e" }; foreach (var val in values) { writer.Write(val); } writer.Flush(); memStream.Seek(0, SeekOrigin.Begin); var reader = new PositionedStreamReader(memStream); int pos = 0; foreach (var val in values) { var line = reader.ReadLine(); pos += val.Length; Assert.AreEqual(val.TrimEnd(new[] { '\r', '\n' }), line); Assert.AreEqual(pos, reader.ReadPosition); } } }