Exemplo n.º 1
0
 static void PrintToFile(Treenode node)
 {
     using (Stream fs = File.Create(@"C:\users\chris.wood\desktop\new folder\output.txt")) {
         using (StreamWriter writer = new StreamWriter(fs)) {
             PrintToFile(writer, new List<Treenode>() { node }, 0);
         }
     }
 }
        public TreenodeViewModel(Treenode node, TreenodeViewModel parent)
        {
            this.Treenode = node;

            this.Parent = parent;
            foreach (Treenode n in Treenode.Children.Cast<Treenode>()) {
                children.Add(new TreenodeViewModel(n, this));
            }
        }
        public bool VisitEnter(Composite composite)
        {
            VisitCount++;

            Treenode node = (Treenode)composite;
            if (predicate(node) && node != lastResult) {
                lastResult = result = node;
            }
            return true;
        }
        public Treenode FindNode(Func<Treenode, bool> predicate)
        {
            this.predicate = predicate;
            result = null;

            root.Accept(this, stack);

            lastResult = result;
            return result;
        }
 private void Reset()
 {
     VisitCount = 0;
     lastResult = null;
     result = null;
 }
 public DepthFirstSearch(Treenode root)
 {
     this.root = root;
     Reset();
 }
Exemplo n.º 7
0
 private void AddNodeChild(Treenode child)
 {
     _NodeChildren.Add(child);
     child.Parent = this;
 }
Exemplo n.º 8
0
 private void AddDataChild(Treenode child)
 {
     _DataChildren.Add(child);
     child.Parent = this;
 }
Exemplo n.º 9
0
        private static Treenode _Read(BinaryReader reader, ref int count)
        {
            Treenode ret = new Treenode();

            count++;

            ret.Flags = (Flags)reader.ReadByte();
            ret.DataType = (DataType)reader.ReadByte();

            if ((ret.Flags & Flags.ExtendedFlags) == Flags.ExtendedFlags) {
                ret.FlagsExtended = (FlagsExtended)reader.ReadInt32();
            }

            int titleLength = reader.ReadInt32();

            if (titleLength > 0) {
                ret.Title = Encoding.ASCII.GetString(reader.ReadBytes(titleLength)).TrimEnd(Treenode.TrimChars);
            }

            if (ret.DataType == DataType.Float) {
                double value = reader.ReadDouble();
            } else if (ret.DataType == DataType.ByteBlock) {
                int stringLength = reader.ReadInt32();

                ret.Data = reader.ReadBytes(stringLength);
            } else if (ret.DataType == DataType.Object) {
                // If the node is an object then the next 4 bytes are an Int32 containing the
                // number of child data nodes that it has (those accessed through the > symbol
                // that is specific to objects). The next node to be read is the first of these
                // which may itself have child nodes. This is how the tree is serialised.

                // TODO Still don't know the purpose of this node
                Treenode node = _Read(reader, ref count);

                // The number of object children access with > rather then the normal +
                int numChildren = reader.ReadInt32();

                while (numChildren > 0) {
                    Treenode child = _Read(reader, ref count);
                    numChildren--;

                    ret.AddDataChild(child);
                }
            } else if (ret.DataType == DataType.PointerCoupling) {
                int coupling = reader.ReadInt32();
            } else if (ret.DataType == DataType.None || ret.DataType == DataType.Undefined) {
                // Do nothing
            } else {
                throw new Exception("Data type was not recognised");
            }

            // If the HasBranch flag is set then this node is followed by an Int32 containing the
            // number of child nodes that it has. The next node to be read is the first of these
            // which may itself have child nodes. This is how the tree is serialised
            if ((ret.Flags & Flags.HasBranch) == Flags.HasBranch) {
                // TODO Still don't know the purpose of this node
                Treenode node = _Read(reader, ref count);

                int numChildren = reader.ReadInt32();

                while (numChildren > 0) {
                    Treenode child = _Read(reader, ref count);
                    numChildren--;

                    ret.AddNodeChild(child);
                }
            }

            return ret;
        }
Exemplo n.º 10
0
 private static void _PrintTree(Treenode t, StreamWriter sw)
 {
     sw.WriteLine(t.FullPath + " " + t.DataAsString);
     foreach (Treenode child in t.Children) {
         _PrintTree(child, sw);
     }
 }
Exemplo n.º 11
0
 public static void PrintTree(Treenode t, Stream s)
 {
     using (StreamWriter sw = new StreamWriter(s)) {
         _PrintTree(t, sw);
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Find a node from a path relative to a specified starting node.
        /// </summary>
        /// <param name="path">The path relative to the relativeTo node of the node to be returned.</param>
        /// <param name="relativeTo">The starting node for the search.</param>
        /// <returns>The Treenode if it is found, null otherwise.</returns>
        public static Treenode NodeFromPath(string path, Treenode relativeTo)
        {
            if (path == null) throw new ArgumentException("path");
            if (relativeTo == null) throw new ArgumentException("relativeTo");

            string[] parts = path.Split(new char[] { '/' });

            if (parts.Length < 1 || parts[1] != relativeTo.Title) {
                // The root node of the search differs
                return null;
            }

            Treenode node = relativeTo;

            // Start on 2 because of the leading forward slash giving an empty string and the starting nodes being the same
            for (int p = 2; p < parts.Length && node != null; p++) {
                node = node[parts[p]];
            }
            return node;
        }
Exemplo n.º 13
0
        public void SelectNode(Treenode target)
        {
            if (RootNode == null) return;

            Stack<Treenode> ancestors = new Stack<Treenode>();

            Treenode parent = target;
            while (parent != null) {
                ancestors.Push(parent);
                parent = parent.Parent;
            }

            TreenodeViewModel item = null;

            while (ancestors.Count > 0) {
                Treenode node = ancestors.Pop();

                ObservableCollection<TreenodeViewModel> items;
                if (item == null) {
                    items = new ObservableCollection<TreenodeViewModel>() { RootNode };
                } else {
                    items = item.Children;
                }

                foreach (TreenodeViewModel n in items) {
                    if (n.RepresentsNode(node)) {
                        item = n;
                        if (item.Parent != null) {
                            item.Parent.IsExpanded = true;
                        }
                        break;
                    }
                }
                if (item == null) {
                    throw new Exception("Cannot find node in tree");
                }
            }
            item.IsSelected = true;
            SelectedItem = item;
        }
Exemplo n.º 14
0
            /// <summary>
            /// This function evaluates Treenodes during a search in accordance with the criteria specified on this form.
            /// </summary>
            /// <param name="node">The Treenode that is currently being evaluated.</param>
            /// <returns>True if the Treenode matches the criteria specified on this form, false otherwise.</returns>
            public bool SearchPredicate(Treenode node)
            {
                if (!svm.FindAllDataTypes && node.DataType != svm.DataType) return false;

                if (!svm.FindAllFlags) {
                    if ((node.Flags & svm.Flags) != svm.Flags) return false;
                    if ((node.FlagsExtended & svm.FlagsExtended) != svm.FlagsExtended) return false;
                }
                return true;
            }