Beispiel #1
0
            public StreamAggregator(Stream baseStream)
            {
                _baseStream = baseStream;

                var lines = new StreamReader(_baseStream).ReadAllLines().ToArray();

                _sections = SectionsTreeBuilder.BuildTree(SectionsFinder.GetSections(lines));
            }
Beispiel #2
0
        public static IEnumerable <ConfigKVP> ExtractAll(StreamReader reader)
        {
            var iniFile = reader.ReadAllLines().ToArray();

            foreach (var section in SectionsFinder.GetSections(iniFile))
            {
                foreach (var kvp in exctractFromSection(section))
                {
                    yield return(kvp);
                }
            }

            /////////////////////////////////

            IEnumerable <ConfigKVP> exctractFromSection(SectionsFinder.SectionInfo section)
            {
                var lineEnumerator = section.Body.GetEnumerator();

                while (lineEnumerator.MoveNext())
                {
                    var line = lineEnumerator.Current;
                    var key  = extractKey();
                    if (key != null)
                    {
                        yield return(extractKVP());
                    }

                    //////////////////////////////////

                    string extractKey()
                    {
                        var canHaveKey  = line.Contains(Constants.KVP_SEPERATOR);
                        var supposedKey = canHaveKey
                            ? line
                                          .Split(Constants.KVP_SEPERATOR)[0]
                                          .SkipWhile(c => char.IsWhiteSpace(c))
                                          .SkipFromEndWhile(c => char.IsWhiteSpace(c))
                                          .Aggregate()
                            : null;
                        var isKeyCorrect = canHaveKey &&
                                           supposedKey.IsNotEmpty() &&
                                           supposedKey.All(c => char.IsLetterOrDigit(c) || c == '_') &&
                                           !char.IsDigit(supposedKey.First());

                        return(isKeyCorrect
                            ? supposedKey
                            : null);
                    }

                    ConfigKVP extractKVP()
                    {
                        var kvp = isOneLineKVP()
                                  ? extractSinglelineKVP(key, line)
                                  : extractMultilineKVP(key, lineEnumerator);

                        return(new ConfigKVP(section.Section, kvp.Key, kvp.Value, kvp.Commentary));

                        //////////////////////////////////

                        bool isOneLineKVP()
                        {
                            return(!line.Contains(Constants.MULTILINE_VALUE_MARK) || !line.Contains(Constants.BLOCK_MARK));
                        }
                    }
                }
            }
        }