Beispiel #1
0
        /// <summary>
        /// Removes the first section with the given name from the
        /// <see cref="IniContainer.Sections"/> collection.
        /// </summary>
        /// <param name="container">The container</param>
        /// <param name="name">The section name</param>
        /// <returns>True if section removed, otherwise false</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static bool RemoveSection(this IniContainer container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Value cannot be whitespace.", nameof(name));
            }

            var idxToRemove = -1;

            for (var i = 0; i < container.Sections.Count; i++)
            {
                if (container.Sections[i].Name.Equals(name))
                {
                    idxToRemove = i;
                    break;
                }
            }

            if (idxToRemove == -1)
            {
                return(false);
            }

            container.Sections.RemoveAt(idxToRemove);
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a section from the <see cref="IniContainer"/> or adds
        /// a new section if none exists.
        /// </summary>
        /// <param name="container">The container to use</param>
        /// <param name="name">The section name</param>
        /// <returns>The section instance</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static IniSection GetOrAddSection(this IniContainer container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Value cannot be whitespace.", nameof(name));
            }

            var section = container.GetSection(name);

            if (section != null)
            {
                return(section);
            }

            section = new IniSection(name);
            container.Sections.Add(section);

            return(section);
        }
Beispiel #3
0
        /// <summary>
        /// Adds a comment to the <see cref="IniContainer"/>.
        /// </summary>
        /// <param name="container">The container to use</param>
        /// <param name="comment">The comment to add</param>
        /// <returns>The container after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IniContainer AddComment(this IniContainer container, string comment)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.GlobalComments.Add(comment);

            return(container);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a normalized <see cref="IniContainer"/> using the provided one.
        /// </summary>
        /// <param name="normalizer">The normalizer to use</param>
        /// <param name="source">The container to normalize</param>
        /// <returns>The new container with the normalization result</returns>
        public static IniContainer Normalize(this IIniNormalizer normalizer, IniContainer source)
        {
            if (normalizer == null)
            {
                throw new ArgumentNullException(nameof(normalizer));
            }

            var destination = new IniContainer();

            normalizer.NormalizeInto(source, destination);

            return(destination);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the first section with the given name from the
        /// <see cref="IniContainer.Sections"/> collection.
        /// </summary>
        /// <param name="container">The container</param>
        /// <param name="name">The section name</param>
        /// <returns>The first section with the given name, or null</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static IniSection GetSection(this IniContainer container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Value cannot be whitespace.", nameof(name));
            }

            return(container.Sections.FirstOrDefault(e => e.Name.Equals(name)));
        }
Beispiel #6
0
        /// <summary>
        /// Creates a normalized <see cref="IniContainer"/> using the provided one.
        /// </summary>
        /// <param name="normalizer">The normalizer to use</param>
        /// <param name="source">The container to normalize</param>
        /// <param name="destination">The container with the normalization result</param>
        /// <returns>True if instance normalized successfully, otherwise false</returns>
        public static bool TryNormalize(
            this IIniNormalizer normalizer, IniContainer source, out IniContainer destination)
        {
            if (normalizer == null)
            {
                throw new ArgumentNullException(nameof(normalizer));
            }

            var tmpDestination = new IniContainer();

            if (normalizer.TryNormalizeInto(source, tmpDestination))
            {
                destination = tmpDestination;
                return(true);
            }

            destination = null;
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Removes all sections with the given name from the
        /// <see cref="IniContainer.Sections"/> collection.
        /// </summary>
        /// <param name="container">The container</param>
        /// <param name="name">The section name</param>
        /// <returns>True if any section removed, otherwise false</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static bool RemoveSections(this IniContainer container, string name)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Value cannot be whitespace.", nameof(name));
            }

            var indexesToRemove = new List <int>(container.Sections.Count);

            for (var i = container.Sections.Count - 1; i >= 0; i--)
            {
                if (container.Sections[i].Name.Equals(name))
                {
                    indexesToRemove.Add(i);
                }
            }

            if (indexesToRemove.Count == 0)
            {
                return(false);
            }

            foreach (var idx in indexesToRemove)
            {
                container.Sections.RemoveAt(idx);
            }

            return(true);
        }