Exemplo n.º 1
0
        public static IrbisTreeFile ReadLocalFile
        (
            [NotNull] string fileName,
            [NotNull] Encoding encoding
        )
        {
            Sure.NotNullNorEmpty(fileName, "fileName");
            Sure.NotNull(encoding, "encoding");

            using (StreamReader reader = TextReaderUtility.OpenRead
                                         (
                       fileName,
                       encoding
                                         ))
            {
                IrbisTreeFile result = ParseStream(reader);
                result.FileName = Path.GetFileName(fileName);

                return(result);
            }
        }
Exemplo n.º 2
0
        public static IrbisTreeFile ParseStream
        (
            [NotNull] TextReader reader
        )
        {
            Sure.NotNull(reader, "reader");

            IrbisTreeFile result = new IrbisTreeFile();

            List <Item> list = new List <Item>();
            string      line = reader.ReadLine();

            if (ReferenceEquals(line, null))
            {
                goto DONE;
            }
            if (CountIndent(line) != 0)
            {
                Log.Error
                (
                    "IrbisTreeFile::ParseStream: "
                    + "indent != 0"
                );

                throw new FormatException();
            }
            list.Add(new Item(line));

            int currentLevel = 0;

            while ((line = reader.ReadLine()) != null)
            {
                int level = CountIndent(line);
                if (level > currentLevel + 1)
                {
                    Log.Error
                    (
                        "IrbisTreeFile::ParseStream: "
                        + "level > currentLevel + 1"
                    );

                    throw new FormatException();
                }
                currentLevel = level;
                line         = line.TrimStart(Indent);
                Item item = new Item(line)
                {
                    _level = currentLevel
                };
                list.Add(item);
            }

            int maxLevel = list.Max(item => item._level);

            for (int level = 0; level < maxLevel; level++)
            {
                _ArrangeLevel(list, level);
            }

            var roots = list.Where(item => item._level == 0);

            result.Roots.AddRange(roots);

DONE:
            return(result);
        }