//--------------------------------------------------------------------- public static List<ExpectedLine> ReadLines(string path) { List<ExpectedLine> lines = new List<ExpectedLine>(); int prevLineNum = 0; LineReader reader = new FileLineReader(path); string line; while ((line = reader.ReadLine()) != null) { Regex pattern = new Regex(@"^(\d+):(.*)"); Match match = pattern.Match(line); if (! match.Success) throw new LineReaderException(reader, "Line does not start with a number and colon"); int number = int.Parse(match.Groups[1].Value); string text = match.Groups[2].Value; if (number == 0) throw new LineReaderException(reader, "The expected line number must be > 0"); ExpectedLine expectedLine = new ExpectedLine(number, text); if (prevLineNum > 0 && expectedLine.Number <= prevLineNum) { throw new LineReaderException(reader, "Expected line number ({0}) \u2264 expected line number ({1}) on previous line", expectedLine.Number, prevLineNum); } lines.Add(expectedLine); prevLineNum = expectedLine.Number; } reader.Close(); return lines; }
//--------------------------------------------------------------------- public static List<Location> ReadLocations(string path) { List<Location> sites = new List<Location>(); Util.FileLineReader reader = new Util.FileLineReader(path); string line; while ((line = reader.ReadLine()) != null) { string[] rowAndCol = line.Split(null); Assert.AreEqual(2, rowAndCol.Length); uint row = uint.Parse(rowAndCol[0]); uint col = uint.Parse(rowAndCol[1]); Location loc = new Location(row, col); sites.Add(loc); } reader.Close(); return sites; }
//--------------------------------------------------------------------- public static List <Location> ReadLocations(string path) { List <Location> sites = new List <Location>(); Util.FileLineReader reader = new Util.FileLineReader(path); string line; while ((line = reader.ReadLine()) != null) { string[] rowAndCol = line.Split(null); Assert.AreEqual(2, rowAndCol.Length); uint row = uint.Parse(rowAndCol[0]); uint col = uint.Parse(rowAndCol[1]); Location loc = new Location(row, col); sites.Add(loc); } reader.Close(); return(sites); }
public void DirNotFound() { string subDir = System.IO.Path.Combine(dataDir, "subdir-that-should-not-exist"); if (System.IO.Directory.Exists(subDir)) System.IO.Directory.Delete(subDir, true); string filename = System.IO.Path.Combine(subDir, "filename.txt"); FileLineReader sr = new FileLineReader(filename); }
//--------------------------------------------------------------------- private FileLineReader MakeReader(string filename) { string path = System.IO.Path.Combine(dataDir, filename); FileLineReader reader = new FileLineReader(path); Assert.AreEqual(path, reader.Path); return reader; }
public void NullPath() { FileLineReader sr = new FileLineReader(null); }
public void FileNotFound() { string filename = System.IO.Path.Combine(dataDir, "file-that-should-not-exist.txt"); System.IO.File.Delete(filename); FileLineReader sr = new FileLineReader(filename); }
public void EmptyPath() { FileLineReader sr = new FileLineReader(""); }
//--------------------------------------------------------------------- // Read a 2-D boolean array from a file. // File format: // blank lines (empty or just whitespace) & comment lines are // ignored. Comment line has "#" as first non-whitespace // character. // first data line: true-chars ={CHARS} // whitespace allowed before & after keyword "true-chars" // all characters after = and before EOL are true chars // 0 or more row data lines: row #? ={CHARS} // whitespace allowed before & after keyword "row", // # is 0 or more digits; allows for numbering for user's // benefit, but #'s are not interpreted by program // row's data are all chars after "=" upto EOL // all rows must have same number of data chars public static bool[,] Read2DimArray(string path) { LineReader reader = new FileLineReader(path); reader.SkipBlankLines = true; reader.SkipCommentLines = true; string trueChars = null; string line = reader.ReadLine(); if (line != null) { Regex pattern = new Regex(@"^\s*true-chars\s*=(.*)$"); Match match = pattern.Match(line); if (match.Success) trueChars = match.Groups[1].Value; } if (trueChars == null) throw new LineReaderException(reader, "Expected a line starting with \"true-chars =\""); // Read row data-lines. try { Regex pattern = new Regex(@"^\s*row\s*\d*\s*=(.*)$"); List<string> rows = new List<string>(); int expectedRowLength = 0; while ((line = reader.ReadLine()) != null) { Match match = pattern.Match(line); if (! match.Success) throw new LineReaderException(reader, "Expected a line starting with \"row =\" or \"row # =\""); string row = match.Groups[1].Value; rows.Add(row); if (rows.Count == 1) { expectedRowLength = row.Length; } else if (row.Length != expectedRowLength) { string pluralSuffix = expectedRowLength == 1 ? "" : "s"; throw new LineReaderException(reader, "Expected {0} character{1} after the \"=\"", expectedRowLength, pluralSuffix); } } // while lines from stream return Make2DimArray(rows.ToArray(), trueChars); } catch (LineReaderException) { throw; } catch (System.Exception e) { throw new LineReaderException(reader, e.Message); } }