예제 #1
0
파일: PPM.Import.cs 프로젝트: bangush/Pyxis
        private static Canvas Parse(TextReader text)
        {
            var    parsers = new List <Parser <char, IPPMPart> >(new[] { FileType, ImageSize, Scale, PixelData });
            string line;
            var    index    = 0;
            var    current  = parsers[index];
            var    builder  = new CanvasBuilder(3);
            bool   complete = false;

            while ((line = text.ReadLine()) != null)
            {
                if (complete)
                {
                    break;
                }

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                if (line.StartsWith('#'))
                {
                    // Skip comment lines;
                    continue;
                }

                var parsed = current.ParseOrThrow(line);
                var result = parsed.Process(builder);
                switch (result)
                {
                case ParseResult.NextParser:
                    current = parsers[++index];
                    break;

                case ParseResult.Complete:
                    complete = true;
                    break;

                case ParseResult.Failure:
                    var error = parsed.GetLastFailureMessage();
                    if (!string.IsNullOrEmpty(error))
                    {
                        throw new FileLoadException(error);
                    }

                    throw new FileLoadException();

                case ParseResult.Continue:
                    continue;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(builder.GetCanvas());
        }
예제 #2
0
        private void SetCanvas()
        {
            var Instructions = new Dictionary <ConsoleCommand, string> {
                { ConsoleCommand.ObjectType, "C" },
                { ConsoleCommand.X1, "20" },
                { ConsoleCommand.Y1, "4" }
            };

            Canvas = CanvasBuilder.GetCanvas <List <DataLine> >(Instructions);
        }
예제 #3
0
        public void GetCanvas()
        {
            var Instructions = new Dictionary <ConsoleCommand, string> {
                { ConsoleCommand.ObjectType, "C" },
                { ConsoleCommand.X1, "20" },
                { ConsoleCommand.Y1, "4" }
            };

            List <DataLine> Actual = CanvasBuilder.GetCanvas <List <DataLine> >(Instructions);

            List <DataLine> Expected = new List <DataLine>()
            {
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "-", 22)
                },
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "|").Insert(1, " ", 20).Insert(21, "|")
                },
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "|").Insert(1, " ", 20).Insert(21, "|")
                },
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "|").Insert(1, " ", 20).Insert(21, "|")
                },
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "|").Insert(1, " ", 20).Insert(21, "|")
                },
                new DataLine()
                {
                    Line = new StringBuilder().Insert(0, "-", 22)
                },
            };

            Expected.ForEach(x => Assert.AreEqual(x.Line.ToString(), Actual[Expected.IndexOf(x)].Line.ToString()));
        }