Пример #1
0
        internal IniDocumentBuilder(string[] headerLines = null, string[] footerLines = null, bool isEnabled = true, params Section[] sections)
        {
            _header    = Comment.Builder(headerLines).AsType(CommentType.Header).Build();
            _footer    = Comment.Builder(footerLines).AsType(CommentType.Footer).Build();
            _isEnabled = isEnabled;

            for (int i = 0; i < sections.Length; i++)
            {
                AppendSection(sections[i]);
            }
        }
Пример #2
0
        /// <summary>
        /// Essentially creates a clone of the given section if its valid. Can be used to modify an existing Section.
        /// </summary>
        /// <param name="section">The section to modify.</param>
        internal SectionBuilder(Section section)
        {
            if (section != null)
            {
                _name      = section.Name;
                _isEnabled = section.IsEnabled;
                _comment   = Comment.Builder(section.Comment).Build();

                for (int i = 0; i < section.Properties().Count; i++)
                {
                    _properties.Add(Property.Builder(section.Properties()[i]).Build());
                }
            }
        }
Пример #3
0
        private void GetProperty(string line, string sName, ref Comment comment)
        {
            if (line.PropertyIsValidAndUpdated(out var key, out var value, out var isEnabled))
            {
                AppendProperty(sName, Property
                               .Builder()
                               .WithKey(key)
                               .WithValue(value)
                               .WithComment(comment)
                               .IsEnable(isEnabled)
                               .Build());

                comment = Comment.Builder().Build();
            }
        }
Пример #4
0
        public static bool CommentIsValidAndUpdated(
            this string line,
            ref Comment comment)
        {
            if (line.IsComment().isTrue)
            {
                comment = Comment
                          .Builder(comment)
                          .AppendLine(line)
                          .Build();
                return(true);
            }

            return(false);
        }
Пример #5
0
        internal IniDocumentBuilder(IniDocument document)
        {
            if (document != null)
            {
                _header           = Comment.Builder(document.Header).Build();
                _footer           = Comment.Builder(document.Footer).Build();
                _isEnabled        = document.IsEnabled;
                _filePath         = document.FilePath;
                _loadEndedAtIndex = document.LoadEndedAtIndex;

                for (int i = 0; i < document.Sections().Count; i++)
                {
                    _sections.Add(Section.Builder(document.Sections()[i]).Build());
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Creates <see cref="Section"/> from the given text if it's valid.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <returns></returns>
        public SectionBuilder Parse(string text)
        {
            if (!string.IsNullOrWhiteSpace(text))
            {
                var foundSection = false;
                var comment      = Comment.Builder().Build();

                using (var en = text.SplitToLines().Select(l => l.Trim()).Where(l => l != "").GetEnumerator())
                {
                    while (en.MoveNext())
                    {
                        if (en.Current.CommentIsValidAndUpdated(ref comment))
                        {
                            continue;
                        }

                        if (en.Current.SectionIsValidAndUpdated(ref _name, ref _isEnabled))
                        {
                            if (foundSection)
                            {
                                break;
                            }

                            foundSection = true;
                            _comment     = comment;
                            comment      = Comment.Builder().Build();
                        }

                        if (en.Current.PropertyIsValidAndUpdated(out var key, out var value, out var isEnabled))
                        {
                            _properties.Add(Property
                                            .Builder()
                                            .WithKey(key)
                                            .WithValue(value)
                                            .WithComment(comment)
                                            .IsEnable(isEnabled)
                                            .Build());

                            comment = Comment.Builder().Build();
                        }
                    }
                }
            }

            return(this);
        }
Пример #7
0
        /// <summary>
        /// Merges all unique properties from the given <see cref="Section"/>.
        /// </summary>
        /// <param name="other">Merges the unique Properties from this.</param>
        /// <returns>Does nothing if the given value is null or empty.</returns>
        public SectionBuilder Merge(Section other)
        {
            if (other != null)
            {
                _comment = Comment.Builder(_comment).Merge(other.Comment).Build();
                for (int i = 0; i < other.Properties().Count; i++)
                {
                    if (!_properties.PropertyExists(
                            other.Properties()[i].Key,
                            other.Properties()[i].Value).exists)
                    {
                        _properties.Add(other.Properties()[i]);
                    }
                }
            }

            return(this);
        }
Пример #8
0
        /// <summary>
        /// Replaces the current <see cref="Section"/>'s <see cref="Comment"/> with the given value.
        /// </summary>
        /// <param name="comment">Set <see cref="Comment"/> to this.</param>
        /// <returns>Does nothing if the given value is null or empty.</returns>
        public SectionBuilder WithComment(Comment comment)
        {
            _comment = comment ?? Comment.Builder().AsType(CommentType.Comment).Build();

            return(this);
        }
Пример #9
0
        /// <summary>
        /// Read a <see cref="Section"/> from an ini file.
        /// </summary>
        /// <param name="file">The full path of the file to read from.</param>
        /// <param name="name">The name of the <see cref="Section"/> to read.</param>
        /// <returns>Returns the <see cref="Section"/> if its found.</returns>
        public static async Task <Section> ReadSectionAsync(string file, string name)
        {
            if (!file.IsValidFile())
            {
                return(null);
            }

            var section = Section.Builder().Build();
            var comment = Comment.Builder().Build();

            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
                using (var reader = new StreamReader(stream))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        if (line.IsComment().isTrue)
                        {
                            comment = Comment.Builder().AppendLine(line).Build();
                            continue;
                        }

                        var sec = line.IsSection();
                        if (sec.isTrue)
                        {
                            if (section.Name.Equals(name))
                            {
                                return(section);
                            }

                            if (sec.sectionName.Equals(name))
                            {
                                section = Section
                                          .Builder()
                                          .WithName(sec.sectionName)
                                          .WithComment(comment)
                                          .IsEnable(sec.isEnable)
                                          .Build();
                            }

                            comment = Comment.Builder().Build();
                            continue;
                        }

                        var(isTrue, isEnabled, key, value) = line.IsProperty();
                        if (isTrue && section.Name.Equals(name))
                        {
                            section = Section
                                      .Builder(section)
                                      .AppendProperty(Property
                                                      .Builder()
                                                      .WithKey(key)
                                                      .WithValue(value)
                                                      .WithComment(comment)
                                                      .IsEnable(isEnabled)
                                                      .Build())
                                      .Build();
                        }
                        comment = Comment.Builder().Build();
                    }
                }

            return(section.IsEmpty ? null : section);
        }
Пример #10
0
        private (bool endOfLoad, bool continueLoop, bool notSection) GetSection(
            int numberOfSections,
            string line,
            long currentIndex,
            ref string sName,
            ref bool sIsEnabled,
            ref Comment comment)
        {
            if (line.SectionIsValidAndUpdated(ref sName, ref sIsEnabled))
            {
                if (numberOfSections > 0 && _sections.Count == numberOfSections)
                {
                    _loadEndedAtIndex = currentIndex - comment.LineCount;
                    return(true, false, false);
                }

                AppendSection(Section
                              .Builder()
                              .IsEnable(sIsEnabled)
                              .WithName(sName)
                              .WithComment(comment)
                              .Build());

                comment = Comment.Builder().Build();
                return(false, true, false);
            }

            return(false, false, true);
            //else
            //{
            //    continue;
            //}

            //if (line.SectionIsValidAndUpdated(ref sName, ref sIsEnabled))
            //{
            //    if (numberOfSections > 0 && _sections.Count == numberOfSections)
            //    {
            //        _loadEndedAtIndex = currentIndex - comment.LineCount;
            //        return this;
            //    }

            //    AppendSection(Section
            //        .Builder()
            //        .IsEnable(sIsEnabled)
            //        .WithName(sName)
            //        .WithComment(comment)
            //        .Build());

            //    comment = Comment.Builder().Build();
            //    continue;
            //}

            //if (string.IsNullOrWhiteSpace(line))
            //{
            //    continue;
            //}

            //if (line.IsHeader())
            //{
            //    _header = Comment.Builder(_header).AsType(CommentType.Header).AppendLine(line).Build();
            //    continue;
            //}

            //if (line.IsFooter())
            //{
            //    _footer = Comment.Builder(_footer).AsType(CommentType.Footer).AppendLine(line).Build();
            //    continue;
            //}

            //if (line.CommentIsValidAndUpdated(ref comment))
            //{
            //    continue;
            //}

            //if (line.SectionIsValidAndUpdated(ref sName, ref sIsEnabled))
            //{
            //    if (numberOfSections > 0 && _sections.Count == numberOfSections)
            //    {
            //        _loadEndedAtIndex = currentIndex - comment.LineCount;
            //        return this;
            //    }

            //    AppendSection(Section
            //        .Builder()
            //        .IsEnable(sIsEnabled)
            //        .WithName(sName)
            //        .WithComment(comment)
            //        .Build());

            //    comment = Comment.Builder().Build();
            //    continue;
            //}

            //if (line.PropertyIsValidAndUpdated(out var key, out var value, out var isEnabled))
            //{
            //    AppendProperty(sName, Property
            //        .Builder()
            //        .WithKey(key)
            //        .WithValue(value)
            //        .WithComment(comment)
            //        .IsEnable(isEnabled)
            //        .Build());

            //    comment = Comment.Builder().Build();
            //}
        }
Пример #11
0
        /// <summary>
        /// Load the nest given number of sections from the ini configuration file.
        /// <para> Used only when you have already used <see cref="LoadIniAsync"/> and want to load another set of sections from the same ini file.</para>
        /// </summary>
        /// <param name="numberOfSections">The number of sections to load. 0 (default) being all.</param>
        public async Task <IniDocumentBuilder> LoadNextAsync(int numberOfSections = 0)
        {
            if (!_filePath.IsValidFile() || _loadEndedAtIndex < 0)
            {
                return(this);
            }

            _sections.Clear();

            long currentIndex = -1;
            var  comment      = Comment.Builder().Build();
            var  sName        = string.Empty;
            var  sIsEnabled   = true;

            using (var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.None, DefaultBufferSize, DefaultOptions))
                using (var reader = new StreamReader(stream))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        currentIndex++;
                        if (currentIndex < _loadEndedAtIndex)
                        {
                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        if (GetHeader(line))
                        {
                            continue;
                        }

                        if (GetFooter(line))
                        {
                            continue;
                        }

                        if (GetComment(line, ref comment))
                        {
                            continue;
                        }

                        var sec = GetSection(numberOfSections, line, currentIndex, ref sName, ref sIsEnabled, ref comment);
                        if (sec.endOfLoad)
                        {
                            return(this);
                        }
                        if (sec.continueLoop)
                        {
                            continue;
                        }

                        GetProperty(line, sName, ref comment);
                    }
                }

            return(this);
        }