public static DbaseField Read(BinaryReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var name = new DbaseFieldName(reader.ReadRightPaddedString(11, char.MinValue)); var typeOfField = reader.ReadByte(); if (!Enum.IsDefined(typeof(DbaseFieldType), typeOfField)) { var values = Enum.GetValues(typeof(DbaseFieldType)); var supportedValues = string.Join( ",", Enumerable .Range(0, values.Length) .Select(index => values.GetValue(index).ToString() + "[" + ((byte)values.GetValue(index)).ToString() + "]") ); throw new DbaseFileHeaderException( $"The field type {typeOfField} of field {name} is not supported ({supportedValues})."); } var fieldType = (DbaseFieldType)typeOfField; var offset = new ByteOffset(reader.ReadInt32()); var length = new DbaseFieldLength(reader.ReadByte()); var decimalCount = new DbaseDecimalCount(reader.ReadByte()); reader.ReadBytes(14); return(new DbaseField(name, fieldType, offset, length, decimalCount)); }
public void GetHashCodeIgnoresCasing() { var value = new string(_fixture.CreateMany <char>(_fixture.Create <int>().AsDbaseFieldNameLength()).ToArray()); var lower = new DbaseFieldName(value.ToLowerInvariant()); var upper = new DbaseFieldName(value.ToUpperInvariant()); Assert.True(lower.GetHashCode().Equals(upper.GetHashCode())); }
public void ImplicitConversionToStringReturnsExpectedValue() { var value = new string('a', _fixture.Create <int>().AsDbaseFieldNameLength()); var sut = new DbaseFieldName(value); string result = sut; Assert.Equal(value, result); }
public DbaseField(DbaseFieldName name, DbaseFieldType fieldType, ByteOffset offset, DbaseFieldLength length, DbaseDecimalCount decimalCount) { if (!Enum.IsDefined(typeof(DbaseFieldType), fieldType)) { throw new ArgumentException( $"The field type {fieldType} of field {name} is not supported.", nameof(fieldType)); } switch (fieldType) { case DbaseFieldType.Character: if (decimalCount.ToInt32() != 0) { throw new ArgumentException( $"The character field {name} decimal count ({decimalCount}) must be set to 0.", nameof(decimalCount)); } break; case DbaseFieldType.DateTime: if (length.ToInt32() != 15) { throw new ArgumentException($"The datetime field {name} length ({length}) must be set to 15.", nameof(length)); } if (decimalCount.ToInt32() != 0) { throw new ArgumentException( $"The datetime field {name} decimal count ({decimalCount}) must be set to 0.", nameof(decimalCount)); } break; case DbaseFieldType.Number: if (length > DbaseDouble.MaximumLength) { throw new ArgumentException( $"The number field {name} length ({length}) must be less than or equal to {DbaseDouble.MaximumLength}.", nameof(length)); } if (decimalCount.ToInt32() != 0) { if (length < DbaseDouble.MinimumLength) { throw new ArgumentException( $"The number field {name} length ({length}) must be at least {DbaseDouble.MinimumLength}.", nameof(length)); } if (decimalCount > DbaseDouble.MaximumDecimalCount) { throw new ArgumentException( $"The number field {name} decimal count ({decimalCount}) must be less than or equal to {DbaseDouble.MaximumDecimalCount}.", nameof(decimalCount)); } if (decimalCount.ToInt32() > length.ToInt32() - 2) { throw new ArgumentException( $"The number field {name} decimal count ({decimalCount}) must be 2 less than its length ({length}).", nameof(decimalCount)); } } break; case DbaseFieldType.Float: if (length > DbaseSingle.MaximumLength) { throw new ArgumentException( $"The float field {name} length ({length}) must be less than or equal to {DbaseSingle.MaximumLength}.", nameof(length)); } if (decimalCount.ToInt32() != 0) { if (length < DbaseSingle.MinimumLength) { throw new ArgumentException( $"The number field {name} length ({length}) must be at least {DbaseSingle.MinimumLength}.", nameof(length)); } if (decimalCount > DbaseSingle.MaximumDecimalCount) { throw new ArgumentException( $"The float field {name} decimal count ({decimalCount}) must be less than or equal to {DbaseSingle.MaximumDecimalCount}.", nameof(decimalCount)); } if (decimalCount.ToInt32() > length.ToInt32() - 2) { throw new ArgumentException( $"The float field {name} decimal count ({decimalCount}) must be 2 less than its length ({length}).", nameof(decimalCount)); } } break; case DbaseFieldType.Logical: if (decimalCount.ToInt32() != 0) { throw new ArgumentException( $"The logical field {name} decimal count ({decimalCount}) must be set to 0.", nameof(decimalCount)); } if (length.ToInt32() != 1) { throw new ArgumentException( $"The logical field {name} length ({length}) must be set to 1.", nameof(length)); } break; } Name = name; FieldType = fieldType; Offset = offset; Length = length; DecimalCount = decimalCount; if (FieldType == DbaseFieldType.Number || FieldType == DbaseFieldType.Float) { PositiveIntegerDigits = DecimalCount.ToInt32() != 0 ? new DbaseIntegerDigits( Length .Minus(DecimalCount.ToLength()) .Minus(DecimalSeparatorLength) .ToInt32() ) : new DbaseIntegerDigits(Length.ToInt32()); NegativeIntegerDigits = PositiveIntegerDigits != new DbaseIntegerDigits(0) ? PositiveIntegerDigits .Minus(SignLength) : new DbaseIntegerDigits(0); } else { PositiveIntegerDigits = new DbaseIntegerDigits(0); NegativeIntegerDigits = new DbaseIntegerDigits(0); } }
public static DbaseField CreateLogicalField(DbaseFieldName name) => new DbaseField(name, DbaseFieldType.Logical, ByteOffset.Initial, new DbaseFieldLength(1), new DbaseDecimalCount(0));
public static DbaseField CreateSingleField(DbaseFieldName name, DbaseFieldLength length, DbaseDecimalCount decimalCount) => new DbaseField(name, DbaseFieldType.Float, ByteOffset.Initial, length, decimalCount);
public static DbaseField CreateDateTimeField(DbaseFieldName name) => new DbaseField(name, DbaseFieldType.DateTime, ByteOffset.Initial, new DbaseFieldLength(15), new DbaseDecimalCount(0));
public static DbaseField CreateInt16Field(DbaseFieldName name, DbaseFieldLength length) => new DbaseField(name, DbaseFieldType.Number, ByteOffset.Initial, length, new DbaseDecimalCount(0));
public static DbaseField CreateStringField(DbaseFieldName name, DbaseFieldLength length) => new DbaseField(name, DbaseFieldType.Character, ByteOffset.Initial, length, new DbaseDecimalCount(0));
// public static DbaseField CreateDecimalField(DbaseFieldName name, DbaseFieldLength length, // DbaseDecimalCount decimalCount) // { // return new DbaseField(name, DbaseFieldType.Number, ByteOffset.Initial, length, decimalCount); // } public static DbaseField CreateDoubleField(DbaseFieldName name, DbaseFieldLength length, DbaseDecimalCount decimalCount) { return(new DbaseField(name, DbaseFieldType.Number, ByteOffset.Initial, length, decimalCount)); }
public static DbaseField CreateNumberField(DbaseFieldName name, DbaseFieldLength length, DbaseDecimalCount decimalCount) => new DbaseField(name, DbaseFieldType.Number, ByteOffset.Initial, length, decimalCount);