Exemplo n.º 1
0
        private static void AddComment(IniBlock block, string value)
        {
            if (value == null)
            {
                return;
            }

            if (block.Comments != null)
            {
                block.Comments += Environment.NewLine;
            }

            block.Comments += value;
        }
Exemplo n.º 2
0
 private static void AppendBlock(StringBuilder builder, IniBlock iniBlock)
 {
     builder.AppendLine($"[{iniBlock.Header}]");
     iniBlock.Properties.ForEach(item => builder.AppendLine($"{item.Key}={item.Value}"));
 }
Exemplo n.º 3
0
        public List <IniBlock> Read()
        {
            List <IniBlock> data = new List <IniBlock>();

            IniBlock currentBlock             = new IniBlock();
            int      currentOptionIndex       = 0;
            bool     currentBlockCommentedOut = false;

            using (FileStream stream = new FileStream(this.File, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamReader streamReader = new StreamReader(stream, Encoding.Default))
                {
                    while (!streamReader.EndOfStream)
                    {
                        string line = streamReader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        int    commentIndex;
                        string newComment = null;

                        // check for commented out block
                        if (this.ReadWriteComments && line.Length > 2 && line[0] == TOKEN_BLOCK_START && line[1] == TOKEN_COMMENT)
                        {
                            line = line.Substring(2);

                            // add additional comments
                            commentIndex = line.IndexOf(TOKEN_COMMENT);
                            if (commentIndex != -1)
                            {
                                // add comment
                                newComment = line.Substring(commentIndex + 1);

                                // remove comment from data
                                line = line.Substring(0, commentIndex).TrimEnd();
                            }

                            if (line[line.Length - 1] == TOKEN_BLOCK_END)
                            {
                                // add current block
                                if (currentBlock.Name != null)
                                {
                                    data.Add(currentBlock);
                                }

                                // new block
                                string blockName = line.Substring(0, line.Length - 1).Trim();
                                currentBlock = CreateBlock(blockName, new IniOptions(), currentBlock.Name == null ? currentBlock.Comments : null);

                                currentBlockCommentedOut = true;

                                AddComment(currentBlock, newComment);
                                continue;
                            }
                        }

                        // handle comments
                        commentIndex = line.IndexOf(TOKEN_COMMENT);
                        if (commentIndex != -1)
                        {
                            // add comment
                            if (this.ReadWriteComments)
                            {
                                newComment = line.Substring(commentIndex + 1);
                            }

                            // remove comment from data
                            line = line.Substring(0, commentIndex).TrimEnd();
                        }
                        else
                        {
                            line = line.Trim();
                        }

                        if (line.Length == 0)
                        {
                            AddComment(currentBlock, newComment);
                            continue;
                        }

                        if (line[0] == TOKEN_BLOCK_START)
                        {
                            // add current block
                            if (currentBlock.Name != null)
                            {
                                data.Add(currentBlock);
                            }

                            if (line[line.Length - 1] == TOKEN_BLOCK_END)
                            {
                                // new block
                                string blockName = line.Substring(1, line.Length - 2).Trim();
                                currentBlock = CreateBlock(blockName, new IniOptions(), currentBlock.Name == null ? currentBlock.Comments : null);
                            }
                            else
                            {
                                // reset block
                                currentBlock = CreateBlock(null, null, null);
                            }

                            currentOptionIndex       = 0;
                            currentBlockCommentedOut = false;
                        }
                        else if (currentBlockCommentedOut)
                        {
                            // add comment
                            AddComment(currentBlock, line);
                        }
                        else if (currentBlock.Name != null)
                        {
                            // new value for block
                            int valueIndex = line.IndexOf(TOKEN_MAPPING_VALUE);
                            if (valueIndex != -1)
                            {
                                // retrieve name and value from data
                                string optionName  = line.Substring(0, valueIndex).Trim();
                                string optionValue = line.Substring(valueIndex + 1, line.Length - valueIndex - 1).Trim();

                                currentBlock.Options.Add(optionName, new IniOption
                                {
                                    Value = optionValue,
                                    Index = currentOptionIndex
                                });
                                ++currentOptionIndex;
                            }
                            else
                            {
                                // entry without value
                                currentBlock.Options.Add(line, new IniOption
                                {
                                    Value = string.Empty,
                                    Index = currentOptionIndex
                                });
                                ++currentOptionIndex;
                            }
                        }

                        // add comment to current block
                        AddComment(currentBlock, newComment);
                    }
                }

            // add final block
            if (currentBlock.Name != null)
            {
                data.Add(currentBlock);
            }

            return(data);
        }