Ejemplo n.º 1
0
 public Document(string fileName, byte[] buf)
 {
     FileName     = fileName;
     Name         = Path.GetFileName(fileName);
     Length       = buf.Length;
     FirstElement = new ElementUntyped(this);
     FirstElement.Initialise(0, Length);
 }
Ejemplo n.º 2
0
        public Document(string fileName)
        {
            try
            {
                FileName = fileName;
                Name     = Path.GetFileName(fileName);

                Buffer       = File.ReadAllBytes(fileName);
                Length       = Buffer.Length;
                FirstElement = new ElementUntyped(this);
                FirstElement.Initialise(0, Length);
                FirstElement.Name = GetAutoName(this);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Inserts a new element into an ElementUntyped.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src">The element to insert the new element into.</param>
        /// <param name="offset">In bytes, relative to the start of the src element.</param>
        /// <param name="length">In bytes of the new element. This will be rounded down to the largest number of complete items that fit.</param>
        /// <returns></returns>
        protected static Element Insert <T>(Element src, int offset, int length) where T : Element
        {
            // Don't call this on anything other than untyped elements:
            if (!(src is ElementUntyped))
            {
                return(null);
            }

            Document document   = src.Document;
            Element  newElement = (T)Activator.CreateInstance(typeof(T), document);

            newElement.Name = GetAutoName(document);
            int bytesPerItem = newElement.BytesPerItem;

            int actualLength = (length / bytesPerItem) * bytesPerItem; // Round down to the maximum number of complete items

            // Do we have enough bytes within the src element?
            if (actualLength == 0 || src.Length < offset + actualLength)
            {
                throw new ArgumentOutOfRangeException(nameof(length), length,
                                                      $"There are not enough bytes in the buffer to insert type {newElement.TypeName}, a multiple of {bytesPerItem} bytes are required.");
            }

            int bytesBefore = offset;
            int bytesMiddle = actualLength;
            int bytesAfter  = src.Length - offset - actualLength;

            int offsetMiddle = offset;
            int offsetAfter  = offset + actualLength;

            newElement.Initialise(src.Offset + offsetMiddle, bytesMiddle);

            if (offsetMiddle > 0 && bytesAfter > 0)
            {
                ElementUntyped after = new ElementUntyped(document)
                {
                    Name = GetAutoName(document)
                };
                after.Initialise(src.Offset + offsetAfter, bytesAfter);
                newElement.Append(after);
                src.Length = bytesBefore;
                src.Append(newElement);
            }
            else if (offsetMiddle == 0 && bytesAfter > 0)
            {
                if (src.PreviousElement == null)
                {
                    document.FirstElement = newElement;
                }
                src.Offset = src.Offset + offsetAfter;
                src.Length = bytesAfter;
                src.Prepend(newElement);
            }
            else if (offsetMiddle > 0 && bytesAfter == 0)
            {
                src.Length = bytesBefore;
                src.Append(newElement);
            }
            else if (offsetMiddle == 0 && bytesAfter == 0)
            {
                if (src.PreviousElement == null)
                {
                    document.FirstElement = newElement;
                }
                newElement.Name = src.Name;
                src.Append(newElement);
                src.Remove();
            }

            document.RaiseSequenceChanged();
            return(newElement);
        }