예제 #1
0
        //---------------------------------------------------------------------

        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);
        }
예제 #2
0
        //---------------------------------------------------------------------

        //  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);
            }
        }