Esempio n. 1
0
        // Recursively walk an array of tokens
        public SCPInode Find(string[] tokens)
        {
            if (tokens == null || tokens.Length == 0)
            {
                return(null);
            }

            SCPInode child = Children.FirstOrDefault(s => s.Name == tokens[0]);

            if (tokens.Length == 1)
            {
                return(child);
            }
            else
            {
                return(child?.Find(tokens.Skip(1).ToArray()));
            }
        }
Esempio n. 2
0
        // Override the Add-method, build parsetree here.
        public new void Add(Setting s)
        {
            base.Add(s);

            // Build parse tree
            SCPInode node;
            SCPInode parent = root;

            string[] keywords = s.SCPI.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < keywords.Length; i++)
            {
                node = parent.Find(keywords[i]);

                if (node == null)
                {
                    // If this is the last keyword, add the setting. Else add a new SCPInode.
                    if (i + 1 == keywords.Length)
                    {
                        node = s;
                    }
                    else
                    {
                        node = new SCPInode();
                    }

                    node.Name   = keywords[i];
                    node.Parent = parent;

                    parent.Children.Add(node);
                }
                else
                {
                    // Node is not null. If this is the last keyword, it means this setting has already been defined.
                    if (i == keywords.Length - 1)
                    {
                        throw new Exception("Node already defined");
                    }
                }

                parent = node;
            }
        }