예제 #1
0
        internal static void RenderSectionContents(Sentence section, FormattingOptions options, StringBuilder iniBuilder)
        {
            if (options.HasFlag(FormattingOptions.BlankLineBeforeSection))
            {
                iniBuilder.AppendLine();
            }

            if (section.SectionToken() != Grammar.GlobalSectionName)
            {
                RenderComments(section.Comments(), options, iniBuilder);
                RenderSection(section, options, iniBuilder);
            }
            RenderProperties(section.Properties(), options, iniBuilder);
        }
예제 #2
0
        public IniProperty AddProperty(string name, string value)
        {
            var properties     = Properties.Where(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ToList();
            var propertyExists = properties.Any();

            var addProperty = new Func <IniProperty>(() =>
            {
                var property    = PropertyFactory.CreateProperty(name, value, IniFile.Delimiters);
                var iniProperty = new IniProperty(property, IniFile);

                var contents    = Sentence.Properties().ToList();
                var hasContents = contents.Any();
                if (!hasContents)
                {
                    Sentence.Next = iniProperty.Sentence;
                }
                else
                {
                    contents.Last().Next = iniProperty.Sentence;
                }
                return(iniProperty);
            });

            if (IniFile.DuplicatePropertyHandling == DuplicatePropertyHandling.Disallow)
            {
                if (propertyExists)
                {
                    throw new DuplicatePropertiesException();
                }
                return(addProperty());
            }

            if (IniFile.DuplicatePropertyHandling == DuplicatePropertyHandling.Allow)
            {
                return(addProperty());
            }

            if (IniFile.DuplicatePropertyHandling == DuplicatePropertyHandling.KeepFirst)
            {
                if (propertyExists)
                {
                    return(properties.Single());
                }
                return(addProperty());
            }

            if (IniFile.DuplicatePropertyHandling == DuplicatePropertyHandling.KeepLast)
            {
                if (propertyExists)
                {
                    var property = properties.Single();
                    property.Value = value;
                    return(property);
                }
                return(addProperty());
            }

            if (IniFile.DuplicatePropertyHandling == DuplicatePropertyHandling.Rename)
            {
                if (propertyExists)
                {
                    name += properties.Count + 1;
                    return(addProperty());
                }
                return(addProperty());
            }

            // the compiler will complain without it
            return(null);
        }
예제 #3
0
 public IniProperty this[string name] =>
 Sentence.Properties()
 .Where(x => x.PropertyToken() == name)
 .Select(x => new IniProperty(x, IniFile))
 .SingleOrDefault();