Пример #1
0
        public IEnumerable <T> ReadAll()
        {
            using (var reader = File.OpenText(_path))
            {
                int lineNo     = 0;
                T   parsedLine = null;
                while (reader.Peek() >= 0)
                {
                    try
                    {
                        lineNo++;

                        string line = reader.ReadLine();

                        if (string.IsNullOrWhiteSpace(line))
                        {
                            InvalidFormatException.EmptyOrNull();
                        }

                        parsedLine = Parse(line);
                    }
                    catch (Exception e)
                    {
                        throw InvalidFormatException.Create(lineNo, e);
                    }

                    yield return(parsedLine);
                }
            }
        }
Пример #2
0
        protected override UserDetail Parse(string line)
        {
            var lineItems = line.Split(_userAndFollowsSplit, StringSplitOptions.RemoveEmptyEntries);

            if (lineItems.Length != 2)
            {
                throw InvalidFormatException.Create(line);
            }

            try
            {
                return(new UserDetail()
                {
                    User = lineItems[0],
                    Follows = ParseFollows(lineItems[1])
                });
            }
            catch (Exception e)
            {
                throw InvalidFormatException.Create(line, e);
            }
        }
Пример #3
0
        protected override Tweet Parse(string line)
        {
            int index = line.IndexOf("> ");

            if (index < 0)
            {
                InvalidFormatException.Create(line);
            }

            User creator = line.Substring(0, index);

            index += 2;

            if (index > line.Length)
            {
                InvalidFormatException.Create(line);
            }

            string text = line.Substring(index, line.Length - index);

            return(new Tweet(creator, text));
        }