コード例 #1
0
        /// <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);
        }