public AnonymousDbaseRecordEnumerator(DbaseFileHeader header, BinaryReader reader)
 {
     _header  = header ?? throw new ArgumentNullException(nameof(header));
     _reader  = reader ?? throw new ArgumentNullException(nameof(reader));
     _current = null;
     _state   = State.Initial;
     _number  = RecordNumber.Initial;
 }
Exemple #2
0
        public void Write(DbaseRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            record.Write(Writer);
        }
Exemple #3
0
        public void Write(DbaseRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(DbaseBinaryWriter));
            }

            record.Write(Writer);
        }
            public bool MoveNext()
            {
                if (_state == State.Ended)
                {
                    return(false);
                }

                if (_state == State.Initial)
                {
                    _state = State.Started;
                }
                else
                {
                    if (_header.RecordCount == _number)
                    {
                        _current = null;
                        _state   = State.Ended;
                        return(false);
                    }

                    _number = _number.Next();
                }

                try
                {
                    var record = _header.CreateDbaseRecord();
                    if (record.TryRead(_reader))
                    {
                        _current = record;
                    }
                    else
                    {
                        _current = null;
                        _state   = State.Ended;
                    }
                }
                catch (Exception)
                {
                    _current = null;
                    _state   = State.Ended;
                    throw;
                }

                return(_state == State.Started);
            }
Exemple #5
0
        public static DbaseRecord[] GenerateDbaseRecords(this IFixture fixture, DbaseField[] fields,
                                                         DbaseRecordCount count)
        {
            var records   = new DbaseRecord[count.ToInt32()];
            var inspector = new GenerateValueInspector(fixture);

            for (var index = 0; index < records.Length; index++)
            {
                var record = new AnonymousDbaseRecord(fields);
                foreach (var value in record.Values)
                {
                    value.Inspect(inspector);
                }

                records[index] = record;
            }

            return(records);
        }
Exemple #6
0
        public AnonymousDbaseRecordEnumeratorOverStreamWithExpectedBytesTests()
        {
            var fixture = new Fixture();

            fixture.CustomizeWordLength();
            fixture.CustomizeDbaseFieldName();
            fixture.CustomizeDbaseFieldLength();
            fixture.CustomizeDbaseDecimalCount();
            fixture.CustomizeDbaseField();
            fixture.CustomizeDbaseCodePage();
            fixture.CustomizeDbaseRecordCount();
            fixture.CustomizeDbaseSchema();

            _record1 = new FakeDbaseRecord {
                Id = { Value = fixture.Create <int>() }
            };
            _record2 = new FakeDbaseRecord {
                Id = { Value = fixture.Create <int>() }
            };
            var header = new DbaseFileHeader(
                fixture.Create <DateTime>(),
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(2),
                new FakeDbaseSchema());
            var  stream = new MemoryStream();
            long position;

            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                header.Write(writer);
                writer.Flush();
                position = stream.Position;
                _record1.Write(writer);
                _record2.Write(writer);
                writer.Write(DbaseRecord.EndOfFile);
                writer.Flush();
            }

            stream.Position = position;

            _reader = new DisposableBinaryReader(stream, Encoding.UTF8, false);
            _sut    = header.CreateAnonymousDbaseRecordEnumerator(_reader);
        }
Exemple #7
0
        public DbaseRecordEnumeratorWithLongerStreamTests()
        {
            var fixture = new Fixture();

            fixture.CustomizeWordLength();
            fixture.CustomizeDbaseFieldName();
            fixture.CustomizeDbaseFieldLength();
            fixture.CustomizeDbaseDecimalCount();
            fixture.CustomizeDbaseField();
            fixture.CustomizeDbaseCodePage();
            fixture.CustomizeDbaseRecordCount();
            fixture.CustomizeDbaseSchema();

            _record = new FakeDbaseRecord {
                Id = { Value = fixture.Create <int>() }
            };
            var header = new DbaseFileHeader(
                fixture.Create <DateTime>(),
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new FakeDbaseSchema());
            var  stream = new MemoryStream();
            long position;

            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                header.Write(writer);
                writer.Flush();
                position = stream.Position;
                _record.Write(writer);
                writer.Write(DbaseRecord.EndOfFile);
                writer.Write(fixture.CreateMany <byte>(10).ToArray());
                writer.Flush();
            }

            stream.Position = position;

            _reader = new DisposableBinaryReader(stream, Encoding.UTF8, false);
            _sut    = header.CreateDbaseRecordEnumerator <FakeDbaseRecord>(_reader);
        }
Exemple #8
0
        public void WriteManyHasExpectedResult()
        {
            var expectedHeader  = _fixture.Create <DbaseFileHeader>();
            var expectedRecords =
                _fixture.GenerateDbaseRecords(expectedHeader.Schema.Fields, expectedHeader.RecordCount);

            using (var stream = new MemoryStream())
            {
                using (var sut = new DbaseBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    foreach (var expectedRecord in expectedRecords)
                    {
                        sut.Write(expectedRecord);
                    }
                }

                // Assert
                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var actualHeader  = DbaseFileHeader.Read(reader);
                    var actualRecords = new DbaseRecord[actualHeader.RecordCount.ToInt32()];
                    for (var index = 0; index < actualRecords.Length; index++)
                    {
                        var actualRecord = new AnonymousDbaseRecord(actualHeader.Schema.Fields);
                        actualRecord.Read(reader);
                        actualRecords[index] = actualRecord;
                    }

                    var actualEndOfFile = reader.ReadByte();

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecords, actualRecords, new DbaseRecordEqualityComparer());
                    Assert.Equal(DbaseRecord.EndOfFile, actualEndOfFile);
                }
            }
        }