private void ReadRecords(List <Type> records, Stream stream) { StdfFileReader reader = null; try { reader = new StdfFileReader(stream); foreach (Type recordType in records) { StdfRecord record = reader.ReadRecord(); Assert.IsInstanceOf(recordType, record); } } catch (StdfException e) { Console.WriteLine(e.StackTrace); Assert.Fail(e.Message); } finally { if (reader != null) { reader.Dispose(); } } }
private void ReadRecords(RecordInfo[] info, Stream stream) { StdfFileReader reader = null; try { reader = new StdfFileReader(stream); foreach (RecordInfo stdfInfo in info) { reader.RegisterDelegate(stdfInfo.Type, stdfInfo.Subtype, delegate(object o, RecordReadEventArgs e) { StdfRecord record = e.Record; Assert.AreEqual(stdfInfo.Type, record.Type); Assert.AreEqual(stdfInfo.Subtype, record.Subtype); }); } reader.Read(); } catch (StdfException e) { Console.WriteLine(e.StackTrace); Assert.Fail(e.Message); } finally { if (reader != null) { reader.Dispose(); } } }
/// <summary> /// Gets the extents containing the specified record /// </summary> public Extents GetExtents(StdfRecord record) { //TODO: optimized search (binary?) var candidate = _ExtentsList.TakeWhile(e => e.StartOffset <= record.Offset) .LastOrDefault(); return((candidate.EndOffset >= record.Offset) ? candidate : null); }
public void LengthCalculationWithEmptyRecords() { StdfRecord record = StdfRecordFactory.Instance.CreateRecord(typeof(FarRecord)); Assert.AreEqual(2, record.Length); record = StdfRecordFactory.Instance.CreateRecord(typeof(AtrRecord)); Assert.AreEqual(5, record.Length); record = StdfRecordFactory.Instance.CreateRecord(typeof(MrrRecord)); Assert.AreEqual(8, record.Length); }
/// <summary> /// Removes a delegate to be notified when the specified record is read. /// </summary> /// <param name="recordType">The Type of the record to remove</param> /// <exception cref="ObjectDisposedException">If the object is already disposed.</exception> public void RemoveDelegate(Type recordType) { if (disposed) { throw disposedException; } CheckRecordType(recordType); StdfRecordAttribute attribute = StdfRecord.GetStdfRecordAttribute(recordType); RemoveDelegate(attribute.Type, attribute.Subtype); }
/// <summary> /// Registers a delegate for a specific <code>StdfRecord</code>. /// </summary> /// <param name="recordType">The record <code>Type</code> for which the delegated is registered.</param> /// <param name="recordReadDelegate">The delegate to register.</param> /// <exception cref="ArgumentException">If the passed <code>Type</code> is not a subclass of <code>StdfRecord</code>.</exception> /// <exception cref="AttributeNotDefinedException">If the <code>StdfRecordAttribute</code> is not defined for the passed record Type.</exception> /// <exception cref="ObjectDisposedException">If the object is already disposed.</exception> public void RegisterDelegate(Type recordType, RecordReadEventHandler recordReadDelegate) { if (disposed) { throw disposedException; } CheckRecordType(recordType); StdfRecordAttribute attribute = StdfRecord.GetStdfRecordAttribute(recordType); RegisterDelegate(attribute.Type, attribute.Subtype, recordReadDelegate); }
private static void DumpRecord(StdfRecord r) { var type = r.GetType(); if (!_Dumpers.TryGetValue(type, out var dumper)) { dumper = CreateDumperForType(type); _Dumpers[type] = dumper; } dumper(r); }
//~StdfFileWriter() //{ // Dispose(false); //} /// <summary> /// Writes a record through the <see cref="StdfFileWriter"/>. /// </summary> /// <param name="record">The record to write.</param> /// <exception cref="ObjectDisposedException">If the object is already disposed.</exception> public void WriteRecord(StdfRecord record) { if (disposed) { throw disposedException; } if (record == null) { throw new ArgumentNullException("record", "Object cannot be null"); } record.Write(writer); }
public void GetFields() { StdfRecordFactory factory = StdfRecordFactory.Instance; StdfRecord record = factory.CreateRecord(0, 20); Assert.AreEqual(typeof(AtrRecord), record.GetType()); IList <Type> fieldTypes = StdfRecordUtil.GetIFieldTypes(record); Assert.AreEqual(2, fieldTypes.Count); foreach (Type t in fieldTypes) { Debug.WriteLine(t.ToString()); Assert.AreEqual(typeof(IField), t.GetInterface(typeof(IField).Name)); } }
private StdfRecord ReadRecord(StdfHeader h, RecordReadEventHandler d) { StdfRecord record = factory.CreateRecord(h.Type, h.Subtype); record.Length = h.Lenght; record.Read(reader); RecordReadEventArgs e = new RecordReadEventArgs(record); OnRecordRead(e); if (d != null) { d(this, e); } return(record); }
/// <summary> /// Reads each record sequentially until the end of the stream raising the event and notifying all registered delegates. /// </summary> /// <returns>Returns the read record or null if the end of the stream is reached or an error occurred.</returns> /// <exception cref="ObjectDisposedException">If the object is already disposed.</exception> public StdfRecord ReadRecord() { if (disposed) { throw disposedException; } if (reader.PeekChar() == -1) { return(null); } StdfRecord record = null; try { record = ReadRecord(ReadRecordHeader(), null); } catch (RecordNotFoundException) { SkipRecord(); } return(record); }
public void TestWriting() { InitializeTestRecord(); MockStdfFileWriter writer = new MockStdfFileWriter(CpuType.Sun386); writer.WriteRecord(hbr); writer.Reset(); StdfFileReader reader = new StdfFileReader(writer.Stream); StdfRecord record = reader.ReadRecord(); Assert.IsInstanceOf(typeof(FarRecord), record); record = reader.ReadRecord(); Assert.IsInstanceOf(typeof(HbrRecord), record); HbrRecord readRecord = record as HbrRecord; Assert.IsNotNull(readRecord); Assert.AreEqual(hbr.HeadNumber, readRecord.HeadNumber); Assert.AreEqual(hbr.SiteNumber, readRecord.SiteNumber); Assert.AreEqual(hbr.Name, readRecord.Name); Assert.AreEqual(hbr.Number, readRecord.Number); Assert.AreEqual(hbr.PartsCount, readRecord.PartsCount); Assert.AreEqual(hbr.PassFail, readRecord.PassFail); }
internal static UnknownRecord ConvertFromPlr(StdfRecord record, Endian endian) { Plr plr = (Plr)record; using (MemoryStream stream = new MemoryStream()) { // Writing the PLR backwards BinaryWriter writer = new BinaryWriter(stream, endian, true); // Get GroupIndexes length, which must be consistent with all other arrays, except for the last one present, which may be truncated if (plr.GroupIndexes == null) { throw new InvalidOperationException(String.Format(Resources.NonNullableField, 1, typeof(Plr))); } int groupCount = plr.GroupIndexes.Length; if (groupCount > UInt16.MaxValue) { throw new InvalidOperationException(String.Format(Resources.ArrayTooLong, UInt16.MaxValue, 1, typeof(Plr))); } bool fieldsWritten = false; // Field 7: ReturnStatesLeft if (plr.ReturnStatesLeft != null) { // Check for larger group length (writing has definitely not occurred yet) if (plr.ProgramStatesLeft.Length > groupCount) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 7)); } // Write the field writer.WriteStringArray(plr.ReturnStatesLeft); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write string[] arr = new string[groupCount]; Array.ForEach <string>(arr, delegate(string a) { a = ""; }); writer.WriteStringArray(arr); } // Field 6: ProgramStatesLeft if (plr.ProgramStatesLeft != null) { // Check for larger, or not equal group length ifwriting has occurred if ((plr.ProgramStatesLeft.Length > groupCount) || (fieldsWritten && (plr.ProgramStatesLeft.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 6)); } // Write the field writer.WriteStringArray(plr.ProgramStatesLeft); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write string[] arr = new string[groupCount]; Array.ForEach <string>(arr, delegate(string a) { a = ""; }); writer.WriteStringArray(arr); } // Field 5: ReturnStatesRight if (plr.ReturnStatesRight != null) { // Check for larger, or not equal group length ifwriting has occurred if ((plr.ReturnStatesRight.Length > groupCount) || (fieldsWritten && (plr.ReturnStatesRight.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 5)); } // Write the field writer.WriteStringArray(plr.ReturnStatesRight); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write string[] arr = new string[groupCount]; Array.ForEach <string>(arr, delegate(string a) { a = ""; }); writer.WriteStringArray(arr); } // Field 4: ProgramStatesRight if (plr.ProgramStatesRight != null) { // Check for larger, or not equal group length ifwriting has occurred if ((plr.ProgramStatesRight.Length > groupCount) || (fieldsWritten && (plr.ProgramStatesRight.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 4)); } // Write the field writer.WriteStringArray(plr.ProgramStatesRight); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write string[] arr = new string[groupCount]; Array.ForEach <string>(arr, delegate(string a) { a = ""; }); writer.WriteStringArray(arr); } // Field 3: GroupRadixes if (plr.GroupRadixes != null) { // Check for larger, or not equal group length ifwriting has occurred if ((plr.GroupRadixes.Length > groupCount) || (fieldsWritten && (plr.GroupRadixes.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 3)); } // Write the field writer.WriteByteArray(plr.GroupRadixes); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write byte[] arr = new byte[groupCount]; Array.ForEach <byte>(arr, delegate(byte a) { a = Byte.MinValue; }); writer.WriteByteArray(arr); } // Field 2: GroupModes if (plr.GroupModes != null) { // Check for larger, or not equal group length ifwriting has occurred if ((plr.GroupModes.Length > groupCount) || (fieldsWritten && (plr.GroupModes.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 2)); } // Write the field writer.WriteUInt16Array(plr.GroupModes); fieldsWritten = true; } else if (fieldsWritten) { // Fill an array of missing values and write ushort[] arr = new ushort[groupCount]; Array.ForEach <ushort>(arr, delegate(ushort a) { a = UInt16.MinValue; }); writer.WriteUInt16Array(arr); } // Field 1: GroupIndexes // Check for larger, or not equal group length ifwriting has occurred if ((plr.GroupIndexes.Length > groupCount) || (fieldsWritten && (plr.GroupIndexes.Length != groupCount))) { throw new InvalidOperationException(String.Format(Resources.SharedLengthViolation, 1)); } // Write the field writer.WriteUInt16Array(plr.GroupIndexes); fieldsWritten = true; // Field 0: Group Count writer.WriteUInt16((ushort)groupCount); // Reverse bytes in stream long length = stream.Length; if (length > UInt16.MaxValue) { throw new InvalidOperationException(Resources.RecordTooLong); } byte[] sa = stream.ToArray(); Array.Reverse(sa, 0, (int)length); return(new UnknownRecord(plr.RecordType, sa, endian)); } }
/// <summary> /// Writes a record through the <see cref="StdfFileWriter"/>. /// </summary> /// <param name="record">The record to write.</param> /// <exception cref="ObjectDisposedException">If the object is already disposed.</exception> public void WriteRecord(StdfRecord record) { writer.WriteRecord(record); }
private void ReadRecord(Type recordType, Stream stream) { StdfRecordAttribute attribute = StdfRecord.GetStdfRecordAttribute(recordType); ReadRecords(new RecordInfo[] { new RecordInfo(attribute.Type, attribute.Subtype) }, stream); }
internal RecordReadEventArgs(StdfRecord record) { this.record = record; attribute = StdfRecord.GetStdfRecordAttribute(this.record.GetType()); }
internal static UnknownRecord ConvertFromGdr(StdfRecord record, Endian endian) { Gdr gdr = (Gdr)record; using (MemoryStream stream = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(stream, endian, false); writer.WriteUInt16((ushort)gdr._GenericData.Length); //TODO: support padding bytes? Most modern parser doesn't care. //TODO: faster if statements via RuntimeTypeHandle for (int i = 0; i < gdr._GenericData.Length; i++) { object o = gdr._GenericData[i]; if (o is byte) { writer.WriteByte((byte)1); writer.WriteByte((byte)o); } else if (o is ushort) { writer.WriteByte((byte)2); writer.WriteUInt16((ushort)o); } else if (o is uint) { writer.WriteByte((byte)3); writer.WriteUInt32((uint)o); } else if (o is sbyte) { writer.WriteByte((byte)4); writer.WriteSByte((sbyte)o); } else if (o is short) { writer.WriteByte((byte)5); writer.WriteInt16((short)o); } else if (o is int) { writer.WriteByte((byte)6); writer.WriteInt32((int)o); } else if (o is float) { writer.WriteByte((byte)7); writer.WriteSingle((float)o); } else if (o is double) { writer.WriteByte((byte)8); writer.WriteDouble((double)o); } else if (o is string) { writer.WriteByte((byte)10); writer.WriteString((string)o); } else if (o is byte[]) { writer.WriteByte((byte)11); writer.WriteByteArray((byte[])o); } else if (o is BitArray) { writer.WriteByte((byte)12); writer.WriteBitArray((BitArray)o); } else { throw new InvalidOperationException(string.Format(Resources.UnsupportedGdrDataType, o.GetType())); } //TODO: how to deal with nibble? } return(new UnknownRecord(gdr.RecordType, stream.ToArray(), endian)); } }