示例#1
0
 internal IniConfigurationSection(IniConfigurationSection collection)
     : base(collection)
 {
     this.Name           = collection.Name;
     this.sectionComment = collection.sectionComment;
     this.comments       = new Dictionary <string, string>(collection.comments);
 }
示例#2
0
        private static void WriteSection(StringBuilder sb, IniConfigurationSection section, IniConfigurationFormat format)
        {
            IniConfigurationSection n = new IniConfigurationSection(section);

            if (section.Name != String.Empty && sb.Length > 0)
            {
                sb.AppendLine();
            }
            if (format.HasFlag(IniConfigurationFormat.PreserveComment))
            {
                WriteComment(sb, section.GetComment());
            }
            if (section.Name != String.Empty)
            {
                sb.Append('[');
                sb.Append(section.Name);
                sb.AppendLine("]");
            }
            string[] keys = new string[section.AllKeys.Length];
            Array.Copy(section.AllKeys, keys, keys.Length);
            if (format.HasFlag(IniConfigurationFormat.SortKey))
            {
                Array.Sort(keys);
            }
            foreach (string key in keys)
            {
                if (format.HasFlag(IniConfigurationFormat.PreserveComment))
                {
                    WriteComment(sb, n.GetComment(key));
                    n.RemoveComment(key);
                }
                foreach (string value in section.GetValues(key))
                {
                    var hasQuote    = format.HasFlag(IniConfigurationFormat.ForceQuote) || value.IndexOf('"') >= 0;
                    var quotedValue = hasQuote ? value.Replace("\"", "\\\"") : value;
                    var isNewLine   = false;
                    sb.Append(key);
                    sb.Append('=');
                    if (hasQuote)
                    {
                        sb.Append('"');
                    }
                    foreach (string line in quotedValue.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None))
                    {
                        if (isNewLine)
                        {
                            sb.Append('\\');
                            sb.AppendLine();
                        }
                        else
                        {
                            isNewLine = true;
                        }
                        sb.Append(line);
                    }
                    if (hasQuote)
                    {
                        sb.Append('"');
                    }
                    sb.AppendLine();
                }
            }
        }
示例#3
0
        /// <summary>
        /// Parse an INI-formatted formatted string and creates a new instance of the <see cref="IniConfiguration"/> class with the parsed keys and values.
        /// </summary>
        /// <param name="data">An INI-formatted formatted string.</param>
        /// <returns>A key-value collection that contains key-value pairs parsed from the specified string.</returns>
        public static IniConfiguration Parse(string data)
        {
            IniConfiguration        iniData        = new IniConfiguration();
            IniConfigurationSection currentSection = iniData.DefaultSection;

            using (StringReader reader = new StringReader(data)) {
                StringBuilder cb = new StringBuilder();
                StringBuilder sb = new StringBuilder();
                while (reader.Peek() > 0)
                {
                    sb.Remove(0, sb.Length);
                    do
                    {
                        string line = reader.ReadLine();
                        if (line.Length > 0)
                        {
                            if (line[line.Length - 1] != '\\')
                            {
                                sb.Append(line);
                                break;
                            }
                            sb.Append(Environment.NewLine);
                            sb.Append(line.Substring(0, line.Length - 1).TrimStart());
                        }
                    } while (reader.Peek() > 0);

                    string entry = sb.ToString().Trim();
                    if (entry.Length == 0)
                    {
                        continue;
                    }
                    if (entry[0] == ';' || entry[0] == '#')
                    {
                        cb.AppendLine(entry.Substring(1));
                    }
                    else if (entry[0] == '[' && entry[entry.Length - 1] == ']')
                    {
                        currentSection = iniData.AddSection(entry.Substring(1, entry.Length - 2));
                        if (cb.Length > 0)
                        {
                            currentSection.SetComment(cb.ToString().TrimEnd());
                        }
                        cb.Remove(0, cb.Length);
                    }
                    else
                    {
                        int equalSignPos = entry.IndexOf('=');
                        if (equalSignPos >= 0)
                        {
                            string key   = entry.Substring(0, equalSignPos).TrimEnd();
                            string value = Regex.Unescape(entry.Substring(equalSignPos + 1).TrimStart());
                            if (value.Length > 1 && value[0] == '"' && value[value.Length - 1] == '"')
                            {
                                value = value.Substring(1, value.Length - 2);
                            }
                            currentSection.Add(key, value);
                            if (cb.Length > 0)
                            {
                                currentSection.SetComment(key, cb.ToString().TrimEnd());
                            }
                        }
                        cb.Remove(0, cb.Length);
                    }
                }
                return(iniData);
            }
        }