Пример #1
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);
        }
Пример #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;
        }