1 // 2 // Copyright (c) 2010-2023 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System; 8 using System.IO; 9 using Antmicro.Renode.Logging; 10 11 namespace Antmicro.Renode.Utilities.RESD 12 { 13 public class SamplesData<T> where T : RESDSample, new() 14 { SamplesData(SafeBinaryReader reader)15 public SamplesData(SafeBinaryReader reader) 16 { 17 this.reader = reader; 18 this.currentSample = new T(); 19 20 currentSample.TryReadMetadata(reader); 21 22 SampleDataOffset = reader.BaseStream.Position; 23 } 24 GetCurrentSample()25 public T GetCurrentSample() 26 { 27 if(!sampleReady) 28 { 29 sampleReady = currentSample.TryReadFromStream(reader); 30 } 31 return currentSample; 32 } 33 Move(int count)34 public bool Move(int count) 35 { 36 if(count == 0) 37 { 38 // if count is zero, we can return sample if either it's ready or we 39 // aren't at the end of the file 40 return sampleReady || !reader.EOF; 41 } 42 43 if(reader.EOF) 44 { 45 return false; 46 } 47 48 if(count < 0) 49 { 50 // although technically possible, we assume it's not supported 51 return false; 52 } 53 54 if(count > 0) 55 { 56 // as GetCurrentSample lazily reads next sample, we have to 57 // take it under account when skipping samples 58 if(!currentSample.Skip(reader, count - (sampleReady ? 1 : 0))) 59 { 60 reader.SeekToEnd(); 61 return false; 62 } 63 sampleReady = false; 64 } 65 66 return !reader.EOF; 67 } 68 69 public long SampleDataOffset { get; } 70 71 private bool sampleReady; 72 private T currentSample; 73 private readonly SafeBinaryReader reader; 74 } 75 } 76