public void LessThanOrEqualToDbaseRecordLengthOperatorReturnsExpectedValue(int left, int right, bool expected)
        {
            var sut   = new DbaseRecordLength(left);
            var other = new DbaseRecordLength(right);

            var result = sut <= other;

            Assert.Equal(expected, result);
        }
        public void ToInt16ReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseRecordLengthValue();
            var sut   = new DbaseRecordLength(value);

            var result = sut.ToInt16();

            Assert.Equal(value, result);
        }
        public void CompareToDbaseRecordLengthReturnsExpectedResult(int left, int right, int expected)
        {
            var sut   = new DbaseRecordLength(left);
            var other = new DbaseRecordLength(right);

            var result = sut.CompareTo(other);

            Assert.Equal(expected, result);
        }
Пример #4
0
        public static DbaseFileHeader Read(BinaryReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (reader.ReadByte() != ExpectedDbaseFormat)
            {
                throw new DbaseFileHeaderException("The database file type must be 3 (dBase III).");
            }

            var lastUpdated = new DateTime(reader.ReadByte() + 1900, reader.ReadByte(), reader.ReadByte(), 0, 0, 0,
                                           DateTimeKind.Unspecified);
            var recordCount  = new DbaseRecordCount(reader.ReadInt32());
            var headerLength = reader.ReadInt16();
            var fieldCount   = (headerLength - HeaderMetaDataSize) / FieldMetaDataSize;

            if (fieldCount > DbaseSchema.MaximumFieldCount)
            {
                throw new DbaseFileHeaderException(
                          $"The database file can not contain more than {DbaseSchema.MaximumFieldCount} fields.");
            }

            var recordLength = new DbaseRecordLength(reader.ReadInt16());

            reader.ReadBytes(17);
            var rawCodePage = reader.ReadByte();

            if (!DbaseCodePage.TryParse(rawCodePage, out var codePage))
            {
                throw new DbaseFileHeaderException($"The database code page {rawCodePage} is not supported.");
            }

            reader.ReadBytes(2);
            var fields = new DbaseField[fieldCount];

            for (var recordFieldIndex = 0; recordFieldIndex < fieldCount; recordFieldIndex++)
            {
                fields[recordFieldIndex] = DbaseField.Read(reader);
            }

            // verify field offsets are aligned
            var offset = ByteOffset.Initial;

            foreach (var field in fields)
            {
                if (field.Offset != offset)
                {
                    throw new DbaseFileHeaderException(
                              $"The field {field.Name} does not have the expected offset {offset} but instead {field.Offset}. Please ensure the offset has been properly set for each field and that the order in which they appear in the record field layout matches their offset.");
                }

                offset = field.Offset.Plus(field.Length);
            }

            var schema = new AnonymousDbaseSchema(fields);

            if (recordLength != schema.Length)
            {
                throw new DbaseFileHeaderException(
                          $"The database file record length ({recordLength}) does not match the total length of all fields ({schema.Length}).");
            }

            if (reader.ReadByte() != Terminator)
            {
                throw new DbaseFileHeaderException("The database file header terminator is missing.");
            }

            // skip to first record
            var bytesToSkip = headerLength - (HeaderMetaDataSize + FieldMetaDataSize * fieldCount);

            reader.ReadBytes(bytesToSkip);
            return(new DbaseFileHeader(lastUpdated, codePage, recordCount, schema));
        }