コード例 #1
0
        /// <summary>
        /// IniFileSectionCollection オブジェクトへセクションを追加する。
        /// </summary>
        /// <param name="dest">IniFileSectionCollection オブジェクト。</param>
        /// <param name="sectionName">セクション名。</param>
        private static void AddSection(IniFileSectionCollection dest, string sectionName)
        {
            Debug.Assert(dest != null);

            IniFileSection section = null;

            try
            {
                section = new IniFileSection(sectionName);
            }
            catch (Exception ex)
            {
                throw new FormatException(@"Invalid section name.", ex);
            }

            try
            {
                dest.Add(section);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                          "The section (\"" + sectionName + "\") is duplicated.",
                          ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// INIファイル形式文字列値を IniFileSectionCollection オブジェクトへパースする。
        /// </summary>
        /// <param name="iniString">INIファイル形式文字列値。</param>
        /// <param name="strict">厳格な形式チェックを行うならば true 。</param>
        /// <returns>IniFileSectionCollection オブジェクト。</returns>
        public static IniFileSectionCollection FromString(
            string iniString,
            bool strict = false)
        {
            if (iniString == null)
            {
                throw new ArgumentNullException(nameof(iniString));
            }

            var ini = new IniFileSectionCollection();

            foreach (var line in ReadLines(iniString))
            {
                var trimmedLine = line.Trim();

                if (trimmedLine.Length == 0 || trimmedLine[0] == ';')
                {
                    continue;
                }

                if (trimmedLine[0] == '[')
                {
                    if (trimmedLine[trimmedLine.Length - 1] == ']')
                    {
                        AddSection(ini, trimmedLine.Substring(1, trimmedLine.Length - 2));
                    }
                }

                var eqIndex = line.IndexOf('=');
                if (eqIndex < 0)
                {
                    if (strict)
                    {
                        throw new FormatException(@"Invalid line.");
                    }
                    continue;
                }

                var name  = line.Substring(0, eqIndex).Trim();
                var value = line.Substring(eqIndex + 1).TrimStart();

                if (ini.Count <= 0)
                {
                    if (strict)
                    {
                        throw new FormatException(
                                  "The item (\"" + name + "\") is found before a section.");
                    }
                    continue;
                }

                AddItemToLastSection(ini, name, value);
            }

            return(ini);
        }
コード例 #3
0
        /// <summary>
        /// IniFileSectionCollection オブジェクトの末尾セクションへアイテムを追加する。
        /// </summary>
        /// <param name="dest">IniFileSectionCollection オブジェクト。</param>
        /// <param name="name">名前。</param>
        /// <param name="value">値。</param>
        private static void AddItemToLastSection(
            IniFileSectionCollection dest,
            string name,
            string value)
        {
            Debug.Assert(dest != null && dest.Count > 0);

            var         section = dest[dest.Count - 1];
            IniFileItem item    = null;

            try
            {
                item = new IniFileItem(name);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                          "The name of the item (in the section \"" +
                          section.Name +
                          "\") is invalid.",
                          ex);
            }

            try
            {
                item.Value = value;
            }
            catch (Exception ex)
            {
                throw new FormatException(
                          "The value of the item (\"" +
                          name +
                          "\" in the section \"" +
                          section.Name +
                          "\") is invalid.",
                          ex);
            }

            try
            {
                dest[dest.Count - 1].Items.Add(item);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                          "The item (\"" +
                          name +
                          "\" in the section \"" +
                          section.Name +
                          "\") is duplicated.",
                          ex);
            }
        }