コード例 #1
0
        /// <summary>
        /// unpack ini document
        /// </summary>
        /// <param name="unpacker"></param>
        /// <returns></returns>
        public static IDocument IniDocument(this Unpacker unpacker)
        {
            IDocument    document       = new Document();
            List <INode> nodeTag        = new List <INode>();
            Node         currentNode    = null;
            List <INode> currentSection = new List <INode>();

            while (!unpacker.IsEnd && !unpacker.IsLast)
            {
                string line = unpacker.ReadLine();
                if (line.Length >= 2)
                {
                    if (line[0] == '[' && line[line.Length - 1] == ']')
                    {
                        line = line.Substring(1, line.Length - 2);

                        if (currentNode != null)
                        {
                            nodeTag.Add(currentNode);
                        }
                        else
                        {
                            //no section define
                            nodeTag        = currentSection;
                            currentSection = new List <INode>();
                        }
                        if (currentSection.Count != 0)
                        {
                            //clean up this section
                            currentNode.SubNode = currentSection;
                            currentSection      = new List <INode>();
                        }
                        currentNode      = new Node();
                        currentNode.Name = line;
                    }
                    else if (line[0] == ';')
                    {
                        Node node = new Node
                        {
                            IsComment = true,
                            Value     = line.Substring(1)
                        };
                        currentSection.Add(node);
                    }
                    else if (line.Contains("="))
                    {
                        int  index = line.IndexOf('=');
                        Node node  = new Node()
                        {
                            Name  = line.Substring(0, index),
                            Value = line.Substring(index + 1)
                        };
                        currentSection.Add(node);
                    }
                    //ignore line not match format
                }
            }//end while

            if (currentNode != null)
            {
                currentNode.SubNode = currentSection;
                nodeTag.Add(currentNode);
            }
            else
            {
                nodeTag = currentSection;
            }
            document.SubNode = nodeTag;
            return(document);
        }