Пример #1
0
        internal static Configuration ReadFromBinaryStream(Stream stream, BinaryReader reader)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (reader == null)
            {
                reader = new BinaryReader(stream);
            }

            var config = new Configuration();

            int sectionCount = reader.ReadInt32();

            for (int i = 0; i < sectionCount; ++i)
            {
                string sectionName  = reader.ReadString();
                int    settingCount = reader.ReadInt32();

                var section = new Section(sectionName);

                ReadCommentsBinary(reader, section);

                for (int j = 0; j < settingCount; j++)
                {
                    var setting = new Setting(reader.ReadString())
                    {
                        RawValue = reader.ReadString()
                    };

                    ReadCommentsBinary(reader, setting);

                    section.Add(setting);
                }

                config.Add(section);
            }

            return(config);
        }
Пример #2
0
        private static void Parse(StringReader reader, Configuration config)
        {
            Section currentSection    = null;
            var     preCommentBuilder = new StringBuilder();

            int newlineLength = Environment.NewLine.Length;

            string line       = null;
            int    lineNumber = 0;

            // Read until EOF.
            while ((line = reader.ReadLine()) != null)
            {
                lineNumber++;

                // Remove all leading/trailing white-spaces.
                line = line.Trim();

                // Skip empty lines.
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                int commentIndex = 0;
                var comment      = ParseComment(line, out commentIndex);

                if (commentIndex == 0)
                {
                    // pre-comment
                    if (!Configuration.IgnorePreComments)
                    {
                        preCommentBuilder.AppendLine(comment);
                    }

                    continue;
                }

                string lineWithoutComment = line;
                if (commentIndex > 0)
                {
                    // inline comment
                    lineWithoutComment = line.Remove(commentIndex).Trim();
                }

                if (lineWithoutComment.StartsWith("[")) // Section
                {
                    currentSection = ParseSection(lineWithoutComment, lineNumber);

                    if (!Configuration.IgnoreInlineComments)
                    {
                        currentSection.Comment = comment;
                    }

                    if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                    {
                        // Remove the last line.
                        preCommentBuilder.Remove(preCommentBuilder.Length - newlineLength, newlineLength);
                        currentSection.PreComment = preCommentBuilder.ToString();
                        preCommentBuilder.Length  = 0; // Clear the SB
                    }

                    config.mSections.Add(currentSection);
                }
                else // Setting
                {
                    var setting = ParseSetting(Configuration.IgnoreInlineComments ? line : lineWithoutComment, lineNumber);

                    if (!Configuration.IgnoreInlineComments)
                    {
                        setting.Comment = comment;
                    }

                    if (currentSection == null)
                    {
                        throw new ParserException(string.Format(
                                                      "The setting '{0}' has to be in a section.",
                                                      setting.Name), lineNumber);
                    }

                    if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                    {
                        // Remove the last line.
                        preCommentBuilder.Remove(preCommentBuilder.Length - newlineLength, newlineLength);
                        setting.PreComment       = preCommentBuilder.ToString();
                        preCommentBuilder.Length = 0; // Clear the SB
                    }

                    currentSection.Add(setting);
                }
            }
        }
Пример #3
0
        // Parses a configuration from a source string.
        // This is the core parsing function.
        private static Configuration Parse(string source)
        {
            // Reset temporary fields.
            mLineNumber = 0;

            Configuration config         = new Configuration();
            Section       currentSection = null;
            var           preComments    = new List <Comment>();

            using (var reader = new StringReader(source))
            {
                string line = null;

                // Read until EOF.
                while ((line = reader.ReadLine()) != null)
                {
                    mLineNumber++;

                    // Remove all leading / trailing white-spaces.
                    line = line.Trim();

                    // Empty line? If so, skip.
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    int commentIndex = 0;
                    var comment      = ParseComment(line, out commentIndex);

                    if (!IgnorePreComments && commentIndex == 0)
                    {
                        // This is a comment line (pre-comment).
                        // Add it to the list of pre-comments.
                        preComments.Add(comment.Value);
                        continue;
                    }
                    else if (!IgnoreInlineComments && commentIndex > 0)
                    {
                        // Strip away the comments of this line.
                        line = line.Remove(commentIndex).Trim();
                    }

                    // Sections start with a '['.
                    if (line.StartsWith("["))
                    {
                        currentSection = ParseSection(line);

                        if (!IgnoreInlineComments)
                        {
                            currentSection.Comment = comment;
                        }

                        if (config.Contains(currentSection.Name))
                        {
                            if (IgnoreDuplicateSections)
                            {
                                continue;
                            }

                            throw new ParserException(string.Format(
                                                          "The section '{0}' was already declared in the configuration.",
                                                          currentSection.Name), mLineNumber);
                        }

                        if (!IgnorePreComments && preComments.Count > 0)
                        {
                            currentSection.mPreComments = new List <Comment>(preComments);
                            preComments.Clear();
                        }

                        config.mSections.Add(currentSection);
                    }
                    else
                    {
                        Setting setting = ParseSetting(line);

                        if (!IgnoreInlineComments)
                        {
                            setting.Comment = comment;
                        }

                        if (currentSection == null)
                        {
                            throw new ParserException(string.Format(
                                                          "The setting '{0}' has to be in a section.",
                                                          setting.Name), mLineNumber);
                        }

                        if (currentSection.Contains(setting.Name))
                        {
                            if (IgnoreDuplicateSettings)
                            {
                                continue;
                            }

                            throw new ParserException(string.Format(
                                                          "The setting '{0}' was already declared in the section.",
                                                          setting.Name), mLineNumber);
                        }

                        if (!IgnorePreComments && preComments.Count > 0)
                        {
                            setting.mPreComments = new List <Comment>(preComments);
                            preComments.Clear();
                        }

                        currentSection.Add(setting);
                    }
                }
            }

            return(config);
        }
Пример #4
0
        private static void Parse(StringReader reader, Configuration config)
        {
            var currentSection    = new Section(Section.DefaultSectionName);
            var preCommentBuilder = new StringBuilder();

            int lineNumber = 0;

            string line;

            // Read until EOF.
            while ((line = reader.ReadLine()) != null)
            {
                lineNumber++;

                // Remove all leading/trailing white-spaces.
                line = line.Trim();

                // Do not process empty lines.
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                var comment = ParseComment(line, out int commentIndex);

                if (commentIndex == 0)
                {
                    // pre-comment
                    if (!Configuration.IgnorePreComments)
                    {
                        preCommentBuilder.AppendLine(comment);
                    }

                    continue;
                }

                string lineWithoutComment = line;
                if (commentIndex > 0)
                {
                    lineWithoutComment = line.Remove(commentIndex).Trim(); // remove inline comment
                }
                if (lineWithoutComment.StartsWith("["))                    // Section
                {
                    // If the first section has been found but settings already exist, add them to the default section.
                    if (currentSection.Name == Section.DefaultSectionName && currentSection.SettingCount > 0)
                    {
                        config.mSections.Add(currentSection);
                    }

                    currentSection = ParseSection(lineWithoutComment, lineNumber);

                    if (!Configuration.IgnoreInlineComments)
                    {
                        currentSection.Comment = comment;
                    }

                    if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                    {
                        // Set the current section's pre-comment, removing the last newline character.
                        currentSection.PreComment = preCommentBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());
                        preCommentBuilder.Length  = 0; // Clear the SB - With .NET >= 4.0: preCommentBuilder.Clear()
                    }

                    config.mSections.Add(currentSection);
                }
                else // Setting
                {
                    var setting = ParseSetting(Configuration.IgnoreInlineComments ? line : lineWithoutComment, lineNumber);

                    if (!Configuration.IgnoreInlineComments)
                    {
                        setting.Comment = comment;
                    }

                    if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                    {
                        // Set the setting's pre-comment, removing the last newline character.
                        setting.PreComment       = preCommentBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());
                        preCommentBuilder.Length = 0; // Clear the SB - With .NET >= 4.0: preCommentBuilder.Clear()
                    }

                    currentSection.Add(setting);
                }
            }
        }
Пример #5
0
        //根据字符串解析配置文件,核心的解析函数
        private static Configuration Parse(string source)
        {
            //重置临时字段
            mLineNumber = 0;

            Configuration config         = new Configuration();
            Section       currentSection = null;
            var           preComments    = new List <Comment>();

            using (var reader = new StringReader(source))
            {
                string line = null;

                // 读取一行,直到结尾(Read until EOF.)
                while ((line = reader.ReadLine()) != null)
                {
                    mLineNumber++;
                    //删除前后空白字符
                    line = line.Trim();

                    //这里扩展核心的换行支持,使用 3个 ... 开头,说明是上一个设置的换行
                    //每一次行都读取下一行试一下,如果有...,就添加
                    if (line.StartsWith("..."))
                    {
                        var text = "\r\n" + line.Substring(3);
                        currentSection[currentSection.SettingCount - 1].Value += text;
                        continue;
                    }
                    //如果是空行跳过
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    int commentIndex = 0;
                    var comment      = ParseComment(line, out commentIndex);

                    if (!mIgnorePreComments && commentIndex == 0)
                    {
                        // 解析注释行,添加到 注释列表中去
                        preComments.Add(comment);
                        continue;
                    }
                    else if (!mIgnoreInlineComments && commentIndex > 0)
                    {
                        // 去掉这一行的注释
                        line = line.Remove(commentIndex).Trim();
                    }

                    //如果开始字符是 [ ,说明是 节(Sections)
                    if (line.StartsWith("["))
                    {
                        #region 节解析
                        currentSection = ParseSection(line);

                        if (!mIgnoreInlineComments)
                        {
                            currentSection.Comment = comment;
                        }

                        if (config.Contains(currentSection.Name))
                        {
                            throw new ParserException(string.Format(
                                                          "The section '{0}' was already declared in the configuration.",
                                                          currentSection.Name), mLineNumber);
                        }

                        if (!mIgnorePreComments && preComments.Count > 0)
                        {
                            currentSection.mPreComments = new List <Comment>(preComments);
                            preComments.Clear();
                        }

                        config.mSections.Add(currentSection);
                        #endregion
                    }
                    else  //否则就是键值设置行
                    {
                        //解析设置行
                        Setting setting = ParseSetting(line);

                        if (!mIgnoreInlineComments)
                        {
                            setting.Comment = comment;
                        }

                        if (currentSection == null)
                        {
                            throw new ParserException(string.Format("The setting '{0}' has to be in a section.", setting.Name), mLineNumber);
                        }

                        if (currentSection.Contains(setting.Name))
                        {
                            throw new ParserException(string.Format("The setting '{0}' was already declared in the section.", setting.Name), mLineNumber);
                        }

                        if (!mIgnorePreComments && preComments.Count > 0)
                        {
                            setting.mPreComments = new List <Comment>(preComments);
                            preComments.Clear();
                        }
                        currentSection.Add(setting);
                    }
                }
            }
            return(config);
        }
Пример #6
0
        internal static Configuration ReadFromString(string source)
        {
            int lineNumber = 0;

            var     config            = new Configuration();
            Section currentSection    = null;
            var     preCommentBuilder = new StringBuilder();

            int newlineLength = Environment.NewLine.Length;

            using (var reader = new StringReader(source))
            {
                string line = null;

                // Read until EOF.
                while ((line = reader.ReadLine()) != null)
                {
                    lineNumber++;

                    // Remove all leading/trailing white-spaces.
                    line = line.Trim();

                    // Skip empty lines.
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    int commentIndex = 0;
                    var comment      = ParseComment(line, out commentIndex);

                    if (!Configuration.IgnorePreComments && commentIndex == 0)
                    {
                        // This is a comment line (pre-comment).
                        preCommentBuilder.AppendLine(comment);
                        continue;
                    }
                    else if (!Configuration.IgnoreInlineComments && commentIndex > 0)
                    {
                        // Strip away the comments of this line.
                        line = line.Remove(commentIndex).Trim();
                    }

                    if (line.StartsWith("[")) // Section
                    {
                        currentSection = ParseSection(line, lineNumber);

                        if (!Configuration.IgnoreInlineComments)
                        {
                            currentSection.Comment = comment;
                        }

                        if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                        {
                            // Remove the last line.
                            preCommentBuilder.Remove(preCommentBuilder.Length - newlineLength, newlineLength);
                            currentSection.PreComment = preCommentBuilder.ToString();
                            preCommentBuilder.Length  = 0; // Clear the SB
                        }

                        config.mSections.Add(currentSection);
                    }
                    else // Setting
                    {
                        var setting = ParseSetting(line, lineNumber);

                        if (!Configuration.IgnoreInlineComments)
                        {
                            setting.Comment = comment;
                        }

                        if (currentSection == null)
                        {
                            throw new ParserException(string.Format(
                                                          "The setting '{0}' has to be in a section.",
                                                          setting.Name), lineNumber);
                        }

                        if (!Configuration.IgnorePreComments && preCommentBuilder.Length > 0)
                        {
                            // Remove the last line.
                            preCommentBuilder.Remove(preCommentBuilder.Length - newlineLength, newlineLength);
                            setting.PreComment       = preCommentBuilder.ToString();
                            preCommentBuilder.Length = 0; // Clear the SB
                        }

                        currentSection.Add(setting);
                    }
                }
            }

            return(config);
        }