Esempio n. 1
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);
        }