Exemplo n.º 1
0
        /// <summary>
        /// Read
        /// </summary>
        public bool Read()
        {
            if (null == m_Reader)
            {
                return(false);
            }

            int index = 0;

            for (string line = m_Reader.ReadLine(); line != null; line = m_Reader.ReadLine())
            {
                index += 1;

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

                Current = null;

                if (IniFileComment.TryParse(line, out var c))
                {
                    Current = c;
                }
                else if (IniFileSection.TryParse(line, out var s))
                {
                    Section = s;
                    Current = s;
                }
                else if (IniFileRecord.TryParse(line, out var r))
                {
                    Current = r;
                }

                if (null != Current)
                {
                    return(true);
                }
                else
                {
                    throw new FormatException($"Syntax error at #{index} line");
                }
            }

            return(false);
        }
        /// <summary>
        /// Load from Lines
        /// </summary>
        public static IniDocument Load(IEnumerable <string> lines, StringComparer comparer)
        {
            IniDocument result = new IniDocument(comparer);

            IniDocumentSection section  = null;
            List <string>      comments = new List <string>();

            int index = 0;

            foreach (string line in lines.Select(x => x.Trim()))
            {
                index += 1;

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

                if (IniFileComment.TryParse(line, out var iniComment))
                {
                    comments.Add(iniComment.Value);
                }
                else if (IniFileSection.TryParse(line, out var iniSection))
                {
                    string commentText = string.Join(Environment.NewLine, comments);

                    if (result.TryGetSection(iniSection.Value, out section))
                    {
                        if (!string.IsNullOrEmpty(commentText))
                        {
                            if (string.IsNullOrEmpty(section.Comments))
                            {
                                section.Comments = commentText;
                            }
                            else
                            {
                                section.Comments = string.Join(Environment.NewLine, section.Comments, commentText);
                            }
                        }
                    }
                    else
                    {
                        section = new IniDocumentSection(iniSection.Value, result)
                        {
                            Comments = commentText
                        };
                    }

                    comments.Clear();
                }
                else if (IniFileRecord.TryParse(line, out var iniRecord))
                {
                    if (null == section)
                    {
                        section = new IniDocumentSection("", result);
                    }

                    section.AddRecord(iniRecord.Name, iniRecord.Value).Comments = string.Join(Environment.NewLine, comments);

                    comments.Clear();
                }
                else
                {
                    throw new FormatException($"Syntax error in line {index}");
                }
            }

            return(result);
        }