using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; namespace LASRead.LASFormat { public class RecordCollection : IEnumerable { RecordEnumerator enumerator; public RecordCollection(ref Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader) { enumerator = new RecordEnumerator(ref source, startPosition, maxItems, firstHeader); } public IEnumerator GetEnumerator() { return enumerator; } IEnumerator IEnumerable.GetEnumerator() { return enumerator; } } public class RecordEnumerator : IEnumerator { Stream dataSource; ulong streamStart; ulong currentPosition; uint currentCount; uint maxCount; public RecordEnumerator(ref Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader) { dataSource = source; streamStart = startPosition; currentPosition = startPosition; currentCount = 0; maxCount = maxItems; Current = new Record(firstHeader, (long)startPosition); } object IEnumerator.Current => Current; public Record Current { get; private set; } public void Dispose() { dataSource = null; streamStart = 0; currentPosition = 0; currentCount = 0; maxCount = 0; } public bool MoveNext() { if (currentCount >= maxCount) { return false; } else { long oldPos = dataSource.Position; currentPosition = (ulong)Current.header.HeaderLength + currentPosition; dataSource.Position = (long)currentPosition; Record nextRecord = new Record(Current.header.ParseRecord(dataSource), (long)currentPosition); Current = nextRecord; dataSource.Position = oldPos; currentCount++; return true; } } public void Reset() { throw new NotImplementedException(); } } }