关于FastDBF库读写ArcGis dbf文件的小bug

时间:2023-03-08 16:26:05
关于FastDBF库读写ArcGis dbf文件的小bug

该库托管于GitHub,地址:https://github.com/SocialExplorer/FastDBF

贡献者应该都是老外,所以……

1、解析文件头,字段名部分如果有中文命名字段会出错

在DbfHeader类的Read(BinaryReader reader)方法

                //char[] buffer = new char[11];
//buffer = reader.ReadChars(11);
//string sFieldName = new string(buffer);
//yang:ReadChars(11)读取中文字段名时会出错,已改为ReadBytes(11)//Encoding注意UTF-8与GBK
byte[] bytes = reader.ReadBytes();
string sFieldName = Encoding.Default.GetString(bytes);
int nullPoint = sFieldName.IndexOf((char));
if (nullPoint != -)
sFieldName = sFieldName.Substring(, nullPoint);

2、在DbfColumn类,没有设定字段类型可能存在的Float

 public enum DbfColumnType
{ /// <summary>
/// Character less than 254 length
/// ASCII text less than 254 characters long in dBASE.
///
/// Character fields can be up to 32 KB long (in Clipper and FoxPro) using decimal
/// count as high byte in field length. It's possible to use up to 64KB long fields
/// by reading length as unsigned.
///
/// </summary>
Character = , /// <summary>
/// Number Length: less than 18
/// ASCII text up till 18 characters long (include sign and decimal point).
///
/// Valid characters:
/// "0" - "9" and "-". Number fields can be up to 20 characters long in FoxPro and Clipper.
/// </summary>
/// <remarks>
/// We are not enforcing this 18 char limit.
/// </remarks>
Number = , /// <summary>
/// L Logical Length: 1 Boolean/byte (8 bit)
///
/// Legal values:
/// ? Not initialised (default)
/// Y,y Yes
/// N,n No
/// F,f False
/// T,t True
/// Logical fields are always displayed using T/F/?. Some sources claims
/// that space (ASCII 20h) is valid for not initialised. Space may occur, but is not defined.
/// </summary>
Boolean = , /// <summary>
/// D Date Length: 8 Date in format YYYYMMDD. A date like 0000-00- 00 is *NOT* valid.
/// </summary>
Date = , /// <summary>
/// M Memo Length: 10 Pointer to ASCII text field in memo file 10 digits representing a pointer to a DBT block (default is blanks).
/// </summary>
Memo = , /// <summary>
/// B Binary (dBASE V) Like Memo fields, but not for text processing.
/// </summary>
Binary = , /// <summary>
/// I Integer Length: 4 byte little endian integer (FoxPro)
/// </summary>
Integer = ,
/// <summary>
///yang:添加 F Float
/// </summary>
Float = ,
}
 public char ColumnTypeChar
{
get
{
switch(mType)
{
case DbfColumnType.Number:
return 'N'; case DbfColumnType.Character:
return 'C'; case DbfColumnType.Binary:
return 'B'; case DbfColumnType.Boolean:
return 'L'; case DbfColumnType.Date:
return 'D'; case DbfColumnType.Integer:
return 'I'; case DbfColumnType.Memo:
return 'M';
//yang:新加Float字段类型
case DbfColumnType.Float:
return 'F'; } throw new Exception("Unrecognized field type!"); }
}
public static DbfColumnType GetDbaseType(char c)
{
switch(c.ToString().ToUpper())
{
case "C": return DbfColumnType.Character;
case "N": return DbfColumnType.Number;
case "B": return DbfColumnType.Binary;
case "L": return DbfColumnType.Boolean;
case "D": return DbfColumnType.Date;
case "I": return DbfColumnType.Integer;
case "M": return DbfColumnType.Memo;
//yang:新加Float字段类型
case "F": return DbfColumnType.Number;
} throw new NotSupportedException(String.Format("{0} does not have a corresponding dbase type.", c)); }