示例#1
0
        /// <summary>
        /// Find data element with specific name.
        /// </summary>
        /// <remarks>
        /// We will search starting at our level in the tree, then moving
        /// to children from our level, then walk up node by node to the
        /// root of the tree.
        /// </remarks>
        /// <param name="name">Name to search for</param>
        /// <returns>Returns found data element or null.</returns>
        public DataElement find(string name)
        {
            string [] names = name.Split(new char[] { '.' });

            if (names.Length == 1)
            {
                // Make sure it's not us :)
                if (this.name == names[0])
                {
                    return(this);
                }

                // Check our children
                foreach (DataElement elem in EnumerateElementsUpTree())
                {
                    if (elem.name == names[0])
                    {
                        return(elem);
                    }
                }

                // Can't locate!
                return(null);
            }

            foreach (DataElement elem in EnumerateElementsUpTree())
            {
                if (elem.fullName == name)
                {
                    return(elem);
                }

                if (!(elem is DataElementContainer))
                {
                    continue;
                }

                DataElement ret = ((DataElementContainer)elem).QuickNameMatch(names);
                if (ret != null)
                {
                    return(ret);
                }
            }

            DataElement root = getRoot();

            if (root == this)
            {
                return(null);
            }

            return(root.find(name));
        }
示例#2
0
            public string UpdateRefName(DataElement parent, DataElement elem, string name)
            {
                if (parent == null || name == null)
                {
                    return(name);
                }

                // Expect parent and element to be in the source object graph
                System.Diagnostics.Debug.Assert(InSourceGraph(parent));

                if (elem == null)
                {
                    elem = parent.find(name);
                }
                else
                {
                    System.Diagnostics.Debug.Assert(InSourceGraph(elem));
                }

                return(rename.Contains(elem) ? this.name : name);
            }
示例#3
0
        /// <summary>
        /// Caluclate the offset in bytes between two data elements.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns>Returns the offset in bits between two elements.  Return can be negative.</returns>
        protected long calculateOffset(DataElement from, DataElement to)
        {
            DataElementContainer commonAncestor = null;
            long fromPosition = 0;
            long toPosition   = 0;

            if (isRelativeOffset)
            {
                if (!string.IsNullOrEmpty(relativeTo))
                {
                    DataElement relative = from.find(relativeTo);
                    if (relative == null)
                    {
                        throw new PeachException(string.Format("Error, offset relation from element '{0}' couldn't locate relative to element '{1}'.", from.fullName, relativeTo));
                    }
                    from = relative;
                }

                commonAncestor = findCommonRoot(from, to);

                if (commonAncestor == null)
                {
                    throw new PeachException("Error, unable to calculate offset between '" +
                                             from.fullName + "' and '" + to.fullName + "'.");
                }

                BitStream stream = commonAncestor.Value;
                if (from != commonAncestor)
                {
                    if (!stream.HasDataElement(from.fullName))
                    {
                        throw new PeachException("Error, unable to calculate offset between '" +
                                                 from.fullName + "' and '" + to.fullName + "'.");
                    }

                    fromPosition = stream.DataElementPosition(from);
                }

                if (!stream.HasDataElement(to.fullName))
                {
                    throw new PeachException("Error, unable to calculate offset between '" +
                                             from.fullName + "' and '" + to.fullName + "'.");
                }

                toPosition = stream.DataElementPosition(to);
            }
            else
            {
                commonAncestor = findCommonRoot(from, to);
                if (commonAncestor == null)
                {
                    throw new PeachException("Error, unable to calculate offset between '" +
                                             from.fullName + "' and '" + to.fullName + "'.");
                }

                BitStream stream = commonAncestor.Value;
                fromPosition = 0;

                if (!stream.HasDataElement(to.fullName))
                {
                    throw new PeachException("Error, unable to calculate offset between '" +
                                             from.fullName + "' and '" + to.fullName + "'.");
                }

                toPosition = stream.DataElementPosition(to);
            }

            return(toPosition - fromPosition);
        }