private static void ValidateExpectedSize(byte[] value, IAb1DataItem item) { int expectedSize = item.Size * item.Entry.ElementCount; if (expectedSize != value.Length) { throw new InvalidItemSizeException(expectedSize, value.Length, item.Name); } }
/// <summary> /// Returns the bytes containing the item value. Note that the data offset field contains the bytes starting /// at the high-order byte of the 32 bit field. /// </summary> /// <param name="item"></param> /// <returns></returns> private static byte[] GetLocalItemValue(IAb1DataItem item) { var result = new byte[item.Entry.DataSize]; for (int i = 0; i < item.Entry.DataSize; i++) { result[i] = (byte)(item.Entry.DataOffset >> (24 - 8 * i)); } return(result); }
/// <summary> /// Attempts to create a data item. /// </summary> /// <param name="entry"></param> /// <returns></returns> public static IAb1DataItem TryCreateDataItem(Ab1DirectoryEntry entry) { IAb1DataItem item = SupportedItems.FirstOrDefault(dataItem => dataItem.Type == entry.ElementTypeCode); if (item == null) { return(null); } item = item.Create(); item.Entry = entry; return(item); }
/// <summary> /// Returns the item value. /// </summary> /// <param name="item"></param> /// <exception cref="InvalidItemSizeException">Thrown if the item size does not match the expected size.</exception>" /// <returns></returns> private byte[] GetItemValue(IAb1DataItem item) { if (item == null) { throw new ArgumentNullException("item"); } // // Based on the Ab1 specifications, if a item size is greater than 4 bytes the offset points to the data // otherwise it contains the data. // byte[] result = item.Entry.DataSize <= Constants.MaxLocalItemByteSize ? GetLocalItemValue(item) : GetRemoteItemValue(item); ValidateExpectedSize(result, item); return(result); }
/// <summary> /// Parser data. /// </summary> /// <param name="context"></param> public void ParseData(IParserContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (context.Header.MajorVersion != MajorVersion) { throw new InvalidFileVersionException(MajorVersion, context.Header.MajorVersion); } Context = context; Context.Header.DirectoryEntries.ForEach( entry => { IAb1DataItem item = DataItemFactory.TryCreateDataItem(entry); if (item == null) { return; } item.Accept(this); }); }
/// <summary> /// Retrives the matching xml tag of type and id. /// </summary> /// <param name="item"></param> /// <returns></returns> private AB_RootDataTag RetrieveTag(IAb1DataItem item) { return(this.tagsByType[item.Name].First(tag => tag.ID == item.Entry.TagNumber.ToString(CultureInfo.InvariantCulture) && tag.Name == item.Entry.TagName)); }
private static void ValidateExpectedSize(byte[] value, IAb1DataItem item) { int expectedSize = item.Size * item.Entry.ElementCount; if (expectedSize != value.Length) throw new InvalidItemSizeException(expectedSize, value.Length, item.Name); }
/// <summary> /// Returns the bytes containing the item value. Note that the data offset field contains the bytes starting /// at the high-order byte of the 32 bit field. /// </summary> /// <param name="item"></param> /// <returns></returns> private static byte[] GetLocalItemValue(IAb1DataItem item) { var result = new byte[item.Entry.DataSize]; for (int i = 0; i < item.Entry.DataSize; i++) { result[i] = (byte)(item.Entry.DataOffset >> (24 - 8 * i)); } return result; }
/// <summary> /// Returns the bytes containing the item value based on the data offset pointer and data size. /// </summary> /// <param name="item"></param> /// <returns></returns> private byte[] GetRemoteItemValue(IAb1DataItem item) { Context.Reader.BaseStream.Position = item.Entry.DataOffset; byte[] result = Context.Reader.ReadBytes(item.Entry.DataSize); return result; }
/// <summary> /// Returns the item value. /// </summary> /// <param name="item"></param> /// <exception cref="InvalidItemSizeException">Thrown if the item size does not match the expected size.</exception>" /// <returns></returns> private byte[] GetItemValue(IAb1DataItem item) { if (item == null) throw new ArgumentNullException("item"); // // Based on the Ab1 specifications, if a item size is greater than 4 bytes the offset points to the data // otherwise it contains the data. // byte[] result = item.Entry.DataSize <= Constants.MaxLocalItemByteSize ? GetLocalItemValue(item) : GetRemoteItemValue(item); ValidateExpectedSize(result, item); return result; }
/// <summary> /// Returns the bytes containing the item value based on the data offset pointer and data size. /// </summary> /// <param name="item"></param> /// <returns></returns> private byte[] GetRemoteItemValue(IAb1DataItem item) { Context.Reader.BaseStream.Position = item.Entry.DataOffset; byte[] result = Context.Reader.ReadBytes(item.Entry.DataSize); return(result); }