Esempio n. 1
0
        public void ReadOneSectionIni()
        {
            StructuredIniFile ini = StructuredIniFile.FromString(OneSectionIni);

            Assert.Equal("thehost", ini["DEFAULT.host"]);
            Assert.Equal("thetoken", ini["DEFAULT.token"]);
        }
Esempio n. 2
0
        public static StructuredIniFile FromStream(Stream inputStream, bool parseInlineComments = true)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }

            var file = new StructuredIniFile();

            using (var reader = new StreamReader(inputStream))
            {
                IniSection section = file._globalSection;

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();

                    if (line.StartsWith(_sectionBegin))
                    {
                        //start new section
                        line    = line.Trim();
                        section = new IniSection(line);
                        file._sections.Add(section);
                    }
                    else if (line.StartsWith(IniComment.CommentSeparator))
                    {
                        //whole line is a comment
                        string comment = line.Substring(1).Trim();
                        section.Add(new IniComment(comment));
                    }
                    else
                    {
                        IniKeyValue ikv = IniKeyValue.FromLine(line, parseInlineComments);
                        if (ikv == null)
                        {
                            continue;
                        }

                        section.Add(ikv);
                        string fullKey = section.Name == null
                     ? ikv.Key
                     : $"{section.Name}{IniSection.SectionKeySeparator}{ikv.Key}";
                        file._fullKeyNameToValue[fullKey] = ikv;
                    }
                }
            }

            return(file);
        }