コード例 #1
0
ファイル: IniReader.cs プロジェクト: git-thinh/autoken
        /// <summary>
        /// Load a INI file from a stream reader.
        /// </summary>
        /// <param name="reader">StreamReader containing INI file data.</param>
        /// <returns><see cref="IniFile"/> if sucessfully parsed.</returns>
        /// <exception cref="FormatException">If stream do not contain a valid INI file.</exception>
        public static IniFile Load(TextReader reader)
        {
            var            sections       = new IniFile();
            IniFileSection currentSection = null;

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                line = line.Trim();
                if (line == string.Empty || line.StartsWith("#"))
                {
                    continue;
                }

                if (line.StartsWith("["))
                {
                    if (!line.EndsWith("]"))
                    {
                        throw new FormatException("Expected section to end with ']': " + line);
                    }
                    string sectionName = line.Substring(1, line.Length - 2);
                    currentSection = new IniFileSection(sectionName);
                    sections.Add(sectionName, currentSection);
                    continue;
                }

                int pos = line.IndexOf(':');
                if (pos == -1)
                {
                    throw new FormatException("Expected to find colon (header/value separator): " + line);
                }
                if (line.EndsWith(":") && line.IndexOf(':', pos + 1) == -1)
                {
                    throw new FormatException("Line should not end with a colon: " + line);
                }
                if (currentSection == null)
                {
                    throw new FormatException("Missing first section header.");
                }

                string header = line.Substring(0, pos).Trim();
                string value  = line.Substring(pos + 1).Trim();
                currentSection.Add(header, value);
            }

            reader.Close();
            return(sections);
        }
コード例 #2
0
ファイル: IniReader.cs プロジェクト: git-thinh/autoken
 private void Add(string name, IniFileSection section)
 {
     _sections.Add(name, section);
 }