Ejemplo n.º 1
0
        private void ParseIniFile(TextReader reader)
        {
            Section currentSection = null;

            for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                //Blank line
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                line = line.Trim();

                //Comment
                if (line.StartsWith(";"))
                {
                    continue;
                }

                //Section
                Match sectionMatch = SectionPattern.Match(line);
                if (sectionMatch.Success)
                {
                    string sectionName = sectionMatch.Groups[1].Value;
                    if (this.Any(section => section.Name.Equals(sectionName, _comparison)))
                    {
                        throw new NotSupportedException(string.Format("Duplicate section found - '{0}'", sectionName));
                    }
                    currentSection = new Section(sectionName);
                    Add(currentSection);
                    continue;
                }

                //Property
                Match propertyMatch = PropertyPattern.Match(line);
                if (propertyMatch.Success)
                {
                    string propertyName  = propertyMatch.Groups[1].Value;
                    string propertyValue = propertyMatch.Groups[2].Value;

                    if (currentSection == null)
                    {
                        throw new NotSupportedException(string.Format("Property '{0}' is not part of any section", propertyName));
                    }
                    if (currentSection.Any(property => property.Key.Equals(propertyName, _comparison)))
                    {
                        throw new NotSupportedException(string.Format("Key '{0}' already exists in section '{1}'", propertyName, currentSection.Name));
                    }

                    currentSection.Add(propertyName, propertyValue);
                    continue;
                }

                throw new NotSupportedException(string.Format("Unrecognized line '{0}'", line));
            }
        }
Ejemplo n.º 2
0
        private static IniItem TryCreateSection(string line)
        {
            Match match = SectionPattern.Match(line);

            if (!match.Success)
            {
                return(null);
            }
            var section = new Section(match.Groups[3].Value);

            section.Padding.Left        = match.Groups[1].Length;
            section.Padding.InsideLeft  = match.Groups[2].Length;
            section.Padding.InsideRight = match.Groups[4].Length;
            section.Padding.Right       = match.Groups[5].Length;
            return(section);
        }