public DbaseFileHeader(DateTime lastUpdated, DbaseCodePage codePage, DbaseRecordCount recordCount,
                        DbaseSchema schema)
 {
     LastUpdated = lastUpdated.RoundToDay();
     CodePage    = codePage;
     RecordCount = recordCount;
     Schema      = schema;
 }
Exemple #2
0
        public void ParseValueMustBeSupported()
        {
            var allSupported = Array.ConvertAll(DbaseCodePage.All, _ => _.ToByte());
            var value        = new Generator <byte>(_fixture)
                               .First(candidate => !Array.Exists(allSupported, supported => supported == candidate));

            Assert.Throws <ArgumentException>(() => DbaseCodePage.Parse(value));
        }
Exemple #3
0
        public void ToStringReturnsExpectedValue()
        {
            var value = _fixture.Create <byte>().AsDbaseCodePageValue();
            var sut   = DbaseCodePage.Parse(value);

            var result = sut.ToString();

            Assert.Equal(value.ToString(), result);
        }
Exemple #4
0
        public void TryParseWithSupportedValueReturnsExpectedResult()
        {
            var supported = _fixture.Create <DbaseCodePage>();

            var result = DbaseCodePage.TryParse(supported.ToByte(), out DbaseCodePage parsed);

            Assert.True(result);
            Assert.Equal(supported, parsed);
        }
Exemple #5
0
        public void TryParseWithUnsupportedValueReturnsExpectedResult()
        {
            var allSupported = Array.ConvertAll(DbaseCodePage.All, _ => _.ToByte());
            var value        = new Generator <byte>(_fixture)
                               .First(candidate => !Array.Exists(allSupported, supported => supported == candidate));

            var result = DbaseCodePage.TryParse(value, out DbaseCodePage parsed);

            Assert.False(result);
            Assert.Null(parsed);
        }
        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));
        }