Exemplo n.º 1
0
        private static Node DecodeRoot()
        {
            Stack <Node> nodeStack = new Stack <Node>();
            Node         root      = null;

            byte[] sequence;

            while (!(sequence = readerIn.ReadBytes(8)).IsZero())
            {
                if (root != null)
                {
                    return(null);
                }

                BitArray nodeArray   = new BitArray(sequence);
                Node     decodedNode = nodeArray.Decode();

                if (decodedNode.innerNode)
                {
                    nodeStack.Push(decodedNode);
                }
                else
                {
                    while (nodeStack.Count > 0)
                    {
                        Node inner = nodeStack.Peek();
                        if (inner.AddChildren(decodedNode))
                        {
                            nodeStack.Pop();
                            decodedNode = inner;
                        }
                        else
                        {
                            decodedNode = null;
                            break;
                        }
                    }

                    if (decodedNode != null)
                    {
                        root = decodedNode;
                    }
                }
            }

            if (nodeStack.Count != 0)
            {
                root = null;
            }

            return(root);
        }