private int GetByteSize(Element element) { switch (element.TypeOfElement) { case ElementType.Collection: CollectionElement collection = element as CollectionElement ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement."); return(GetByteSize(collection)); case ElementType.Vector: VectorElement vector = element as VectorElement ?? throw new InvalidDataException("Element with type Vector is not a VectorElement."); return(GetByteSize(vector)); case ElementType.Scalar: ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement."); return(GetByteSize(scalar)); default: return(0); } }
/// <summary> /// Updates the value of the scalar element identified by the given tag /// or adds a new scalar element if one does not already exist. /// </summary> /// <param name="tag">The tag which identifies the scalar element to be updated.</param> /// <param name="type">The physical type of the value contained in the scalar element.</param> /// <param name="bytes">The value to be entered into the scalar element.</param> /// <returns>The scalar element which was updated, or a new scalar element if one did not already exist.</returns> public ScalarElement AddOrUpdateScalar(Guid tag, PhysicalType type, byte[] bytes) { ScalarElement scalarElement = GetOrAddScalar(tag); scalarElement.TypeOfValue = type; scalarElement.SetValue(bytes, 0); return(scalarElement); }
/// <summary> /// Gets the scalar element identified by the given tag /// or adds a new scalar element if one does not already exist. /// </summary> /// <param name="tag">The tag which identifies the scalar element to be retrieved.</param> /// <returns>The scalar element identified by the tag, or a new scalar element if one did not already exist.</returns> public ScalarElement GetOrAddScalar(Guid tag) { ScalarElement?scalarElement = GetScalarByTag(tag); if (scalarElement == null) { scalarElement = new ScalarElement(); scalarElement.TagOfElement = tag; AddElement(scalarElement); } return(scalarElement); }
private int GetByteSize(ScalarElement scalar) => scalar.TypeOfValue.GetByteSize();
private void WriteScalar(BinaryWriter writer, ScalarElement scalar) { writer.Write(scalar.GetValue()); }
private void WriteCollection(BinaryWriter writer, CollectionElement collection) { int linkPosition = (int)writer.BaseStream.Position + 4 + 28 * collection.Size; writer.Write(collection.Size); foreach (Element element in collection.Elements) { bool isEmbedded = IsEmbedded(element); writer.Write(element.TagOfElement.ToByteArray()); writer.Write((byte)element.TypeOfElement); writer.Write((byte)element.TypeOfValue); writer.Write(isEmbedded ? (byte)1 : (byte)0); writer.Write((byte)0); if (!isEmbedded) { int padSize = GetPaddedByteSize(element); writer.Write(linkPosition); writer.Write(padSize); linkPosition += padSize; } else { ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Embedded element is not a scalar element."); WriteScalar(writer, scalar); for (int i = element.TypeOfValue.GetByteSize(); i < 8; i++) { writer.Write((byte)0); } } } foreach (Element element in collection.Elements) { if (IsEmbedded(element)) { continue; } switch (element.TypeOfElement) { case ElementType.Collection: CollectionElement nestedCollection = element as CollectionElement ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement."); WriteCollection(writer, nestedCollection); break; case ElementType.Vector: VectorElement vector = element as VectorElement ?? throw new InvalidDataException("Element with type Vector is not a VectorElement."); WriteVector(writer, vector); break; case ElementType.Scalar: ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement."); WriteScalar(writer, scalar); break; } int byteSize = GetByteSize(element); int padSize = GetPaddedByteSize(element); for (int i = 0; i < padSize - byteSize; i++) { writer.Write((byte)0); } } }