diff --git a/DotNetDBF.Enumerable/DotNetDBF.Enumerable.csproj b/DotNetDBF.Enumerable/DotNetDBF.Enumerable.csproj index f2a1eb1..4fbf4d0 100644 --- a/DotNetDBF.Enumerable/DotNetDBF.Enumerable.csproj +++ b/DotNetDBF.Enumerable/DotNetDBF.Enumerable.csproj @@ -16,7 +16,7 @@ gits True True - 5.1.0 + 6.0.0.1 True True diff --git a/DotNetDBF.sln.DotSettings b/DotNetDBF.sln.DotSettings index 54b8d3a..01258ab 100644 --- a/DotNetDBF.sln.DotSettings +++ b/DotNetDBF.sln.DotSettings @@ -1,7 +1,9 @@  DBT + False <data><IncludeFilters /><ExcludeFilters /></data> <data /> True True + True True \ No newline at end of file diff --git a/DotNetDBF/DBFField.cs b/DotNetDBF/DBFField.cs index dda8c2d..5286b04 100644 --- a/DotNetDBF/DBFField.cs +++ b/DotNetDBF/DBFField.cs @@ -43,30 +43,30 @@ public DBFField() { } - public DBFField(string aFieldName, NativeDbType aType) + public DBFField(string fieldName, NativeDbType type) { - Name = aFieldName; - DataType = aType; + Name = fieldName; + DataType = type; } - public DBFField(string aFieldName, - NativeDbType aType, - int aFieldLength) + public DBFField(string fieldName, + NativeDbType type, + int fieldLength) { - Name = aFieldName; - DataType = aType; - FieldLength = aFieldLength; + Name = fieldName; + DataType = type; + FieldLength = fieldLength; } - public DBFField(string aFieldName, - NativeDbType aType, - int aFieldLength, - int aDecimalCount) + public DBFField(string fieldName, + NativeDbType type, + int fieldLength, + int decimalCount) { - Name = aFieldName; - DataType = aType; - FieldLength = aFieldLength; - DecimalCount = aDecimalCount; + Name = fieldName; + DataType = type; + FieldLength = fieldLength; + DecimalCount = decimalCount; } public int Size => SIZE; @@ -162,20 +162,21 @@ @param Length of the field as int. "Field length should be a positive number"); } - if (DataType == NativeDbType.Date || DataType == NativeDbType.Memo || DataType == NativeDbType.Logical) + switch (DataType) { - throw new NotSupportedException( - "Cannot set length on this type of field"); - } - - if (DataType == NativeDbType.Char && value > 255) - { - fieldLength = value % 256; - decimalCount = (byte) (value / 256); - return; + case NativeDbType.Date: + case NativeDbType.Memo: + case NativeDbType.Logical: + throw new NotSupportedException( + "Cannot set length on this type of field"); + case NativeDbType.Char when value > 255: + fieldLength = value % 256; + decimalCount = (byte) (value / 256); + return; + default: + fieldLength = value; + break; } - - fieldLength = value; } } @@ -217,16 +218,16 @@ @param Size of the decimal field. } } - public bool Read(BinaryReader aReader) + public bool Read(BinaryReader reader) { - var t_byte = aReader.ReadByte(); /* 0 */ + var t_byte = reader.ReadByte(); /* 0 */ if (t_byte == DBFFieldType.EndOfField) { //System.out.println( "End of header found"); return false; } - aReader.Read(fieldName, 1, 10); /* 1-10 */ + reader.Read(fieldName, 1, 10); /* 1-10 */ fieldName[0] = t_byte; for (var i = 0; i < fieldName.Length; i++) @@ -239,16 +240,16 @@ public bool Read(BinaryReader aReader) } } - dataType = aReader.ReadByte(); /* 11 */ - reserv1 = aReader.ReadInt32(); /* 12-15 */ - fieldLength = aReader.ReadByte(); /* 16 */ - decimalCount = aReader.ReadByte(); /* 17 */ - reserv2 = aReader.ReadInt16(); /* 18-19 */ - workAreaId = aReader.ReadByte(); /* 20 */ - reserv3 = aReader.ReadInt16(); /* 21-22 */ - setFieldsFlag = aReader.ReadByte(); /* 23 */ - aReader.Read(reserv4, 0, 7); /* 24-30 */ - indexFieldFlag = aReader.ReadByte(); /* 31 */ + dataType = reader.ReadByte(); /* 11 */ + reserv1 = reader.ReadInt32(); /* 12-15 */ + fieldLength = reader.ReadByte(); /* 16 */ + decimalCount = reader.ReadByte(); /* 17 */ + reserv2 = reader.ReadInt16(); /* 18-19 */ + workAreaId = reader.ReadByte(); /* 20 */ + reserv3 = reader.ReadInt16(); /* 21-22 */ + setFieldsFlag = reader.ReadByte(); /* 23 */ + reader.Read(reserv4, 0, 7); /* 24-30 */ + indexFieldFlag = reader.ReadByte(); /* 31 */ return true; } @@ -260,25 +261,25 @@ @param os OutputStream @throws IOException if any stream related issues occur. */ - public void Write(BinaryWriter aWriter) + public void Write(BinaryWriter writer) { // Field Name - aWriter.Write(fieldName); /* 0-10 */ - aWriter.Write(new byte[11 - fieldName.Length], + writer.Write(fieldName); /* 0-10 */ + writer.Write(new byte[11 - fieldName.Length], 0, 11 - fieldName.Length); // data type - aWriter.Write(dataType); /* 11 */ - aWriter.Write(reserv1); /* 12-15 */ - aWriter.Write((byte) fieldLength); /* 16 */ - aWriter.Write(decimalCount); /* 17 */ - aWriter.Write(reserv2); /* 18-19 */ - aWriter.Write(workAreaId); /* 20 */ - aWriter.Write(reserv3); /* 21-22 */ - aWriter.Write(setFieldsFlag); /* 23 */ - aWriter.Write(reserv4); /* 24-30*/ - aWriter.Write(indexFieldFlag); /* 31 */ + writer.Write(dataType); /* 11 */ + writer.Write(reserv1); /* 12-15 */ + writer.Write((byte) fieldLength); /* 16 */ + writer.Write(decimalCount); /* 17 */ + writer.Write(reserv2); /* 18-19 */ + writer.Write(workAreaId); /* 20 */ + writer.Write(reserv3); /* 21-22 */ + writer.Write(setFieldsFlag); /* 23 */ + writer.Write(reserv4); /* 24-30*/ + writer.Write(indexFieldFlag); /* 31 */ } /** @@ -289,13 +290,13 @@ and the stream "pointer" is supposed to be positioned properly. @param in DataInputStream @return Returns the created DBFField object. - @throws IOException If any stream reading problems occures. + @throws IOException If any stream reading problems occurs. */ - internal static DBFField CreateField(BinaryReader aReader) + internal static DBFField CreateField(BinaryReader reader) { var field = new DBFField(); - if (field.Read(aReader)) + if (field.Read(reader)) { return field; } diff --git a/DotNetDBF/DBFFieldType.cs b/DotNetDBF/DBFFieldType.cs index 52a3281..60f3dee 100644 --- a/DotNetDBF/DBFFieldType.cs +++ b/DotNetDBF/DBFFieldType.cs @@ -36,15 +36,15 @@ public static class DBFFieldType { public const byte EndOfData = 0x1A; //^Z End of File public const byte EndOfField = 0x0D; //End of Field - public const byte False = 0x46; //F in Ascci + public const byte False = 0x46; //F in Ascii public const byte Space = 0x20; //Space in ascii public const byte True = 0x54; //T in ascii public const byte UnknownByte = 0x3F; //Unknown Bool value public const string Unknown = "?"; //Unknown value - public static DbType FromNative(NativeDbType aByte) + public static DbType FromNative(NativeDbType @byte) { - switch (aByte) + switch (@byte) { case NativeDbType.Char: return DbType.AnsiStringFixedLength; @@ -60,7 +60,7 @@ public static DbType FromNative(NativeDbType aByte) return DbType.AnsiString; default: throw new DBFException( - $"Unsupported Native Type {aByte}"); + $"Unsupported Native Type {@byte}"); } } diff --git a/DotNetDBF/DBFHeader.cs b/DotNetDBF/DBFHeader.cs index 14066c5..2384f2f 100644 --- a/DotNetDBF/DBFHeader.cs +++ b/DotNetDBF/DBFHeader.cs @@ -20,6 +20,7 @@ original author (javadbf): anil@linuxense.com 2004/03/31 namespace DotNetDBF { + [Obsolete("Use DBFSignature instead", error:true)] public static class DBFSigniture { public const byte NotSet = 0, @@ -28,6 +29,14 @@ public static class DBFSigniture DBase3WithMemo = DBase3 | WithMemo; } + public static class DBFSignature + { + public const byte NotSet = 0, + WithMemo = 0x80, + DBase3 = 0x03, + DBase3WithMemo = DBase3 | WithMemo; + } + [Flags] public enum MemoFlags : byte { @@ -37,35 +46,32 @@ public enum MemoFlags : byte public class DBFHeader { public const byte HeaderRecordTerminator = 0x0D; - - private byte _day; /* 3 */ + + internal byte Signature { get; set; } /* 0 */ + internal byte Year { set; get; } /* 1 */ + internal byte Month { set; get; } /* 2 */ + internal byte Day { set; get; } /* 3 */ + internal int NumberOfRecords { set; get; } /* 4-7 */ + internal short HeaderLength { set; get; } /* 8-9 */ + internal short RecordLength { set; get; } /* 10-11 */ + private short _reserv1; /* 12-13 */ + private byte _incompleteTransaction; /* 14 */ private byte _encryptionFlag; /* 15 */ - private DBFField[] _fieldArray; /* each 32 bytes */ private int _freeRecordThread; /* 16-19 */ - private short _headerLength; /* 8-9 */ - private byte _incompleteTransaction; /* 14 */ - private byte _languageDriver; /* 29 */ - private byte _mdxFlag; /* 28 */ - private byte _month; /* 2 */ - private int _numberOfRecords; /* 4-7 */ - private short _recordLength; /* 10-11 */ - private short _reserv1; /* 12-13 */ private int _reserv2; /* 20-23 */ private int _reserv3; /* 24-27 */ - private short reserv4; /* 30-31 */ - private byte _signature; /* 0 */ - private byte _year; /* 1 */ + private byte _mdxFlag; /* 28 */ + internal byte LanguageDriver { get; set; } /* 29 */ + private short _reserv4; /* 30-31 */ + internal DBFField[] FieldArray { set; get; } /* each 32 bytes */ + public DBFHeader() { - _signature = DBFSigniture.DBase3; + Signature = DBFSignature.DBase3; } - internal byte Signature - { - get => _signature; - set => _signature = value; - } + internal short Size => (short) (sizeof(byte) + sizeof(byte) + sizeof(byte) + sizeof(byte) + @@ -81,7 +87,7 @@ internal byte Signature sizeof(byte) + sizeof(byte) + sizeof(short) + - (DBFField.SIZE * _fieldArray.Length) + + (DBFField.SIZE * FieldArray.Length) + sizeof(byte)); internal short RecordSize @@ -89,80 +95,25 @@ internal short RecordSize get { var tRecordLength = 0; - for (var i = 0; i < _fieldArray.Length; i++) + for (var i = 0; i < FieldArray.Length; i++) { - tRecordLength += _fieldArray[i].FieldLength; + tRecordLength += FieldArray[i].FieldLength; } return (short) (tRecordLength + 1); } } - internal short HeaderLength - { - set => _headerLength = value; - - get => _headerLength; - } - - internal DBFField[] FieldArray - { - set => _fieldArray = value; - - get => _fieldArray; - } - - internal byte Year - { - set => _year = value; - - get => _year; - } - - internal byte Month - { - set => _month = value; - - get => _month; - } - - internal byte Day - { - set => _day = value; - - get => _day; - } - - internal int NumberOfRecords - { - set => _numberOfRecords = value; - - get => _numberOfRecords; - } - - internal short RecordLength - { - set => _recordLength = value; - - get => _recordLength; - } - - internal byte LanguageDriver - { - get => _languageDriver; - set => _languageDriver = value; - } - internal void Read(BinaryReader dataInput) { - _signature = dataInput.ReadByte(); /* 0 */ - _year = dataInput.ReadByte(); /* 1 */ - _month = dataInput.ReadByte(); /* 2 */ - _day = dataInput.ReadByte(); /* 3 */ - _numberOfRecords = dataInput.ReadInt32(); /* 4-7 */ + Signature = dataInput.ReadByte(); /* 0 */ + Year = dataInput.ReadByte(); /* 1 */ + Month = dataInput.ReadByte(); /* 2 */ + Day = dataInput.ReadByte(); /* 3 */ + NumberOfRecords = dataInput.ReadInt32(); /* 4-7 */ - _headerLength = dataInput.ReadInt16(); /* 8-9 */ - _recordLength = dataInput.ReadInt16(); /* 10-11 */ + HeaderLength = dataInput.ReadInt16(); /* 8-9 */ + RecordLength = dataInput.ReadInt16(); /* 10-11 */ _reserv1 = dataInput.ReadInt16(); /* 12-13 */ _incompleteTransaction = dataInput.ReadByte(); /* 14 */ @@ -171,8 +122,8 @@ internal void Read(BinaryReader dataInput) _reserv2 = dataInput.ReadInt32(); /* 20-23 */ _reserv3 = dataInput.ReadInt32(); /* 24-27 */ _mdxFlag = dataInput.ReadByte(); /* 28 */ - _languageDriver = dataInput.ReadByte(); /* 29 */ - reserv4 = dataInput.ReadInt16(); /* 30-31 */ + LanguageDriver = dataInput.ReadByte(); /* 29 */ + _reserv4 = dataInput.ReadInt16(); /* 30-31 */ var v_fields = new List(); @@ -184,30 +135,30 @@ internal void Read(BinaryReader dataInput) field = DBFField.CreateField(dataInput); } - _fieldArray = v_fields.ToArray(); + FieldArray = v_fields.ToArray(); //System.out.println( "Number of fields: " + _fieldArray.length); } internal void Write(BinaryWriter dataOutput) { - dataOutput.Write(_signature); /* 0 */ + dataOutput.Write(Signature); /* 0 */ var tNow = DateTime.Now; - _year = (byte) (tNow.Year - 1900); - _month = (byte) (tNow.Month); - _day = (byte) (tNow.Day); + Year = (byte) (tNow.Year - 1900); + Month = (byte) (tNow.Month); + Day = (byte) (tNow.Day); - dataOutput.Write(_year); /* 1 */ - dataOutput.Write(_month); /* 2 */ - dataOutput.Write(_day); /* 3 */ + dataOutput.Write(Year); /* 1 */ + dataOutput.Write(Month); /* 2 */ + dataOutput.Write(Day); /* 3 */ //System.out.println( "Number of records in O/S: " + numberOfRecords); - dataOutput.Write(_numberOfRecords); /* 4-7 */ + dataOutput.Write(NumberOfRecords); /* 4-7 */ - _headerLength = Size; - dataOutput.Write(_headerLength); /* 8-9 */ + HeaderLength = Size; + dataOutput.Write(HeaderLength); /* 8-9 */ - _recordLength = RecordSize; - dataOutput.Write(_recordLength); /* 10-11 */ + RecordLength = RecordSize; + dataOutput.Write(RecordLength); /* 10-11 */ dataOutput.Write(_reserv1); /* 12-13 */ dataOutput.Write(_incompleteTransaction); /* 14 */ @@ -217,13 +168,12 @@ internal void Write(BinaryWriter dataOutput) dataOutput.Write(_reserv3); /* 24-27 */ dataOutput.Write(_mdxFlag); /* 28 */ - dataOutput.Write(_languageDriver); /* 29 */ - dataOutput.Write(reserv4); /* 30-31 */ + dataOutput.Write(LanguageDriver); /* 29 */ + dataOutput.Write(_reserv4); /* 30-31 */ - for (var i = 0; i < _fieldArray.Length; i++) + foreach (var field in FieldArray) { - //System.out.println( "Length: " + _fieldArray[i].getFieldLength()); - _fieldArray[i].Write(dataOutput); + field.Write(dataOutput); } dataOutput.Write(HeaderRecordTerminator); /* n+1 */ diff --git a/DotNetDBF/DotNetDBF.csproj b/DotNetDBF/DotNetDBF.csproj index f40b33c..9281507 100644 --- a/DotNetDBF/DotNetDBF.csproj +++ b/DotNetDBF/DotNetDBF.csproj @@ -3,7 +3,7 @@ net35;netstandard1.3 True sn.snk - 5.1.0.0 + 6.0.0.1 Ekon Benefits Copyright 2009-2017 This is a basic file parser for reading and writing xBase DBF files particularlly Clipper. Code originally derived from javadbf. diff --git a/DotNetDBF/MemoValue.cs b/DotNetDBF/MemoValue.cs index 263cb1c..0b630a4 100644 --- a/DotNetDBF/MemoValue.cs +++ b/DotNetDBF/MemoValue.cs @@ -107,25 +107,24 @@ public string Value { reader.BaseStream.Seek(_block * _base.BlockSize, SeekOrigin.Begin); - var tStringBuilder = new StringBuilder(); - int tIndex; - var tSoftReturn = _base.CharEncoding.GetString(new byte[] {0x8d, 0x0a}); + var builder = new StringBuilder(); + int termIndex; + var softReturn = _base.CharEncoding.GetString(new byte[] {0x8d, 0x0a}); - byte[] tData; do { - tData = reader.ReadBytes(_base.BlockSize); - if ((tData.Length == 0)) + var data = reader.ReadBytes(_base.BlockSize); + if ((data.Length == 0)) { throw new DBTException("Missing Data for block or no 1a memo terminator"); } - var tString = _base.CharEncoding.GetString(tData); - tIndex = tString.IndexOf(MemoTerminator, StringComparison.Ordinal); - if (tIndex != -1) - tString = tString.Substring(0, tIndex); - tStringBuilder.Append(tString); - } while (tIndex == -1); - _value = tStringBuilder.ToString().Replace(tSoftReturn, string.Empty); + var stringVal = _base.CharEncoding.GetString(data); + termIndex = stringVal.IndexOf(MemoTerminator, StringComparison.Ordinal); + if (termIndex != -1) + stringVal = stringVal.Substring(0, termIndex); + builder.Append(stringVal); + } while (termIndex == -1); + _value = builder.ToString().Replace(softReturn, string.Empty); } _loaded = true;