You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
11 KiB
370 lines
11 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Data; |
|
using System.Globalization; |
|
using System.IO; |
|
using System.Text; |
|
|
|
namespace Kingo.Mobile.Shape2KOTool.KoDataBase |
|
{ |
|
// Token: 0x0200004F RID: 79 |
|
internal class DBFReader |
|
{ |
|
// Token: 0x060002B2 RID: 690 RVA: 0x0001084F File Offset: 0x0000EA4F |
|
public DBFReader() |
|
{ |
|
this.DBFFields = new List<DBFReader.DBFFieldInfo>(); |
|
} |
|
|
|
// Token: 0x170000E9 RID: 233 |
|
// (get) Token: 0x060002B3 RID: 691 RVA: 0x00010870 File Offset: 0x0000EA70 |
|
// (set) Token: 0x060002B4 RID: 692 RVA: 0x00010887 File Offset: 0x0000EA87 |
|
public int FiledCount { get; private set; } |
|
|
|
// Token: 0x170000EA RID: 234 |
|
// (get) Token: 0x060002B5 RID: 693 RVA: 0x00010890 File Offset: 0x0000EA90 |
|
// (set) Token: 0x060002B6 RID: 694 RVA: 0x000108A7 File Offset: 0x0000EAA7 |
|
public DBFReader.DBFFileInfo FileInfo { get; set; } |
|
|
|
// Token: 0x170000EB RID: 235 |
|
// (get) Token: 0x060002B7 RID: 695 RVA: 0x000108B0 File Offset: 0x0000EAB0 |
|
// (set) Token: 0x060002B8 RID: 696 RVA: 0x000108C7 File Offset: 0x0000EAC7 |
|
public List<DBFReader.DBFFieldInfo> DBFFields { get; set; } |
|
|
|
// Token: 0x170000EC RID: 236 |
|
// (get) Token: 0x060002B9 RID: 697 RVA: 0x000108D0 File Offset: 0x0000EAD0 |
|
// (set) Token: 0x060002BA RID: 698 RVA: 0x000108E7 File Offset: 0x0000EAE7 |
|
public DataTable DataTable { get; private set; } |
|
|
|
// Token: 0x060002BB RID: 699 RVA: 0x000108F0 File Offset: 0x0000EAF0 |
|
public void Open(string dbfFilePath) |
|
{ |
|
this.fileStream = File.Open(dbfFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
|
byte[] array = new byte[32]; |
|
this.fileStream.Read(array, 0, array.Length); |
|
this.FileInfo = new DBFReader.DBFFileInfo |
|
{ |
|
HeaderLength = BitConverter.ToUInt16(array, 8), |
|
RecordCount = BitConverter.ToUInt32(array, 4), |
|
RecordLength = BitConverter.ToUInt16(array, 10), |
|
LanguageDriverID = array[29] |
|
}; |
|
this.FiledCount = (int)(this.FileInfo.HeaderLength / 32 - 1); |
|
this.DataTable = new DataTable(); |
|
for (int i = 0; i < this.FiledCount; i++) |
|
{ |
|
byte[] array2 = new byte[32]; |
|
this.fileStream.Read(array2, 0, array2.Length); |
|
DBFReader.DBFFieldInfo dbffieldInfo = new DBFReader.DBFFieldInfo |
|
{ |
|
FieldName = this.GetString(this.CopyByte(array2, 0, 10)), |
|
FieldType = (char)array2[11], |
|
Decimal = array2[17], |
|
Length = array2[16] |
|
}; |
|
if (dbffieldInfo.FieldName.ToLower() == "objectid") |
|
{ |
|
dbffieldInfo.FieldName = "OBJECTID_1"; |
|
} |
|
this.DBFFields.Add(dbffieldInfo); |
|
this.DataTable.Columns.Add(dbffieldInfo.FieldName, DBFReader.GetFieldType(dbffieldInfo)); |
|
} |
|
this.fileStream.ReadByte(); |
|
this.fileStream.ReadByte(); |
|
} |
|
|
|
// Token: 0x060002BC RID: 700 RVA: 0x00010A80 File Offset: 0x0000EC80 |
|
public static Type GetFieldType(DBFReader.DBFFieldInfo pDBFFieldInfo) |
|
{ |
|
Type typeFromHandle = typeof(string); |
|
switch (pDBFFieldInfo.FieldType) |
|
{ |
|
case 'B': |
|
case 'C': |
|
case 'G': |
|
case 'M': |
|
typeFromHandle = typeof(string); |
|
break; |
|
case 'D': |
|
typeFromHandle = typeof(DateTime); |
|
break; |
|
case 'F': |
|
case 'N': |
|
if (pDBFFieldInfo.Decimal == 0) |
|
{ |
|
typeFromHandle = typeof(int); |
|
} |
|
else |
|
{ |
|
typeFromHandle = typeof(double); |
|
} |
|
break; |
|
case 'L': |
|
typeFromHandle = typeof(bool); |
|
break; |
|
} |
|
return typeFromHandle; |
|
} |
|
|
|
// Token: 0x060002BD RID: 701 RVA: 0x00010B3B File Offset: 0x0000ED3B |
|
public void Reset() |
|
{ |
|
this.fileStream.Position = (long)(this.FileInfo.HeaderLength + 1); |
|
this.EOF = false; |
|
} |
|
|
|
// Token: 0x060002BE RID: 702 RVA: 0x00010B60 File Offset: 0x0000ED60 |
|
public void Close() |
|
{ |
|
if (this.fileStream != null) |
|
{ |
|
this.fileStream.Close(); |
|
this.fileStream = null; |
|
} |
|
} |
|
|
|
// Token: 0x060002BF RID: 703 RVA: 0x00010B90 File Offset: 0x0000ED90 |
|
private DateTime GetDataTime(byte[] value) |
|
{ |
|
string @string = this.GetString(value); |
|
DateTime result; |
|
if (string.IsNullOrEmpty(@string)) |
|
{ |
|
result = DateTime.MinValue; |
|
} |
|
else |
|
{ |
|
result = DateTime.ParseExact(@string, "yyyyMMdd", CultureInfo.CurrentCulture); |
|
} |
|
return result; |
|
} |
|
|
|
// Token: 0x060002C0 RID: 704 RVA: 0x00010BD0 File Offset: 0x0000EDD0 |
|
private int GetINT32(byte[] value) |
|
{ |
|
return BitConverter.ToInt32(value, 0); |
|
} |
|
|
|
// Token: 0x060002C1 RID: 705 RVA: 0x00010BEC File Offset: 0x0000EDEC |
|
private int GetINT32(byte value) |
|
{ |
|
return Convert.ToInt32(value); |
|
} |
|
|
|
// Token: 0x060002C2 RID: 706 RVA: 0x00010C04 File Offset: 0x0000EE04 |
|
private byte[] CopyByte(byte[] value, int start, int end) |
|
{ |
|
byte[] array = new byte[end - start + 1]; |
|
for (int i = start; i <= end; i++) |
|
{ |
|
array[i - start] = value[i]; |
|
} |
|
return array; |
|
} |
|
|
|
// Token: 0x060002C3 RID: 707 RVA: 0x00010C40 File Offset: 0x0000EE40 |
|
private string GetString(byte[] value) |
|
{ |
|
string text = string.Empty; |
|
if (value != null && value.Length > 0) |
|
{ |
|
if (this.FileInfo.LanguageDriverID == 0) |
|
{ |
|
text = Encoding.UTF8.GetString(value).Trim(); |
|
} |
|
else |
|
{ |
|
text = Encoding.Default.GetString(value).Trim(); |
|
} |
|
string text2 = text; |
|
char[] trimChars = new char[1]; |
|
text = text2.TrimEnd(trimChars); |
|
} |
|
return text; |
|
} |
|
|
|
// Token: 0x060002C4 RID: 708 RVA: 0x00010CBC File Offset: 0x0000EEBC |
|
private bool GetBoolean(byte[] value) |
|
{ |
|
char c = (char)value[0]; |
|
return c == 'Y' || c == 'y' || c == 'T' || c == 't'; |
|
} |
|
|
|
// Token: 0x170000ED RID: 237 |
|
// (get) Token: 0x060002C5 RID: 709 RVA: 0x00010CF8 File Offset: 0x0000EEF8 |
|
// (set) Token: 0x060002C6 RID: 710 RVA: 0x00010D0F File Offset: 0x0000EF0F |
|
public bool EOF { get; private set; } |
|
|
|
// Token: 0x060002C7 RID: 711 RVA: 0x00010D18 File Offset: 0x0000EF18 |
|
public DataRow GetNextRecord() |
|
{ |
|
byte[] array = new byte[(int)this.FileInfo.RecordLength]; |
|
this.fileStream.Read(array, 0, array.Length); |
|
if (this.fileStream.Position + (long)((ulong)this.FileInfo.RecordLength) > this.fileStream.Length) |
|
{ |
|
this.EOF = true; |
|
} |
|
DataRow dataRow = this.DataTable.NewRow(); |
|
int num = 0; |
|
foreach (DBFReader.DBFFieldInfo dbffieldInfo in this.DBFFields) |
|
{ |
|
int end = num + (int)dbffieldInfo.Length - 1; |
|
switch (dbffieldInfo.FieldType) |
|
{ |
|
case 'B': |
|
case 'C': |
|
case 'E': |
|
case 'G': |
|
case 'H': |
|
case 'I': |
|
case 'J': |
|
case 'K': |
|
case 'M': |
|
goto IL_1B1; |
|
case 'D': |
|
dataRow[dbffieldInfo.FieldName] = this.GetDataTime(this.CopyByte(array, num, end)); |
|
break; |
|
case 'F': |
|
case 'N': |
|
{ |
|
string @string = this.GetString(this.CopyByte(array, num, end)); |
|
if (string.IsNullOrEmpty(@string)) |
|
{ |
|
dataRow[dbffieldInfo.FieldName] = 0; |
|
} |
|
else if (dbffieldInfo.Decimal == 0) |
|
{ |
|
dataRow[dbffieldInfo.FieldName] = Convert.ToInt32(@string); |
|
} |
|
else |
|
{ |
|
dataRow[dbffieldInfo.FieldName] = Convert.ToDouble(@string); |
|
} |
|
break; |
|
} |
|
case 'L': |
|
dataRow[dbffieldInfo.FieldName] = this.GetBoolean(this.CopyByte(array, num, end)); |
|
break; |
|
default: |
|
goto IL_1B1; |
|
} |
|
IL_1D0: |
|
num += (int)dbffieldInfo.Length; |
|
continue; |
|
IL_1B1: |
|
dataRow[dbffieldInfo.FieldName] = this.GetString(this.CopyByte(array, num, end)); |
|
goto IL_1D0; |
|
} |
|
return dataRow; |
|
} |
|
|
|
// Token: 0x060002C8 RID: 712 RVA: 0x00010F44 File Offset: 0x0000F144 |
|
public Dictionary<string, int> Distinct(string fieldName) |
|
{ |
|
int num = (int)this.FileInfo.HeaderLength; |
|
DBFReader.DBFFieldInfo dbffieldInfo = null; |
|
foreach (DBFReader.DBFFieldInfo dbffieldInfo2 in this.DBFFields) |
|
{ |
|
if (dbffieldInfo2.FieldName == fieldName) |
|
{ |
|
dbffieldInfo = dbffieldInfo2; |
|
break; |
|
} |
|
num += (int)dbffieldInfo2.Length; |
|
} |
|
if (dbffieldInfo == null) |
|
{ |
|
throw new Exception(fieldName + "字段不存在"); |
|
} |
|
Dictionary<string, int> dictionary = new Dictionary<string, int>(); |
|
this.fileStream.Position = (long)num; |
|
int num2 = 0; |
|
while ((long)num2 < (long)((ulong)this.FileInfo.RecordCount)) |
|
{ |
|
byte[] array = new byte[(int)dbffieldInfo.Length]; |
|
this.fileStream.Read(array, 0, (int)dbffieldInfo.Length); |
|
string @string = this.GetString(array); |
|
if (dictionary.ContainsKey(@string)) |
|
{ |
|
Dictionary<string, int> dictionary2; |
|
string key; |
|
(dictionary2 = dictionary)[key = @string] = dictionary2[key] + 1; |
|
} |
|
else |
|
{ |
|
dictionary.Add(@string, 1); |
|
} |
|
if ((long)num2 < (long)((ulong)(this.FileInfo.RecordCount - 1U))) |
|
{ |
|
this.fileStream.Seek((long)(this.FileInfo.RecordLength - (ushort)dbffieldInfo.Length), SeekOrigin.Current); |
|
} |
|
num2++; |
|
} |
|
return dictionary; |
|
} |
|
|
|
// Token: 0x060002C9 RID: 713 RVA: 0x000110D4 File Offset: 0x0000F2D4 |
|
public DataTable GetAllRecord() |
|
{ |
|
this.Reset(); |
|
while (!this.EOF) |
|
{ |
|
DataRow nextRecord = this.GetNextRecord(); |
|
this.DataTable.Rows.Add(nextRecord); |
|
} |
|
this.Reset(); |
|
return this.DataTable; |
|
} |
|
|
|
// Token: 0x040001CB RID: 459 |
|
private FileStream fileStream = null; |
|
|
|
// Token: 0x02000050 RID: 80 |
|
internal class DBFFileInfo |
|
{ |
|
// Token: 0x170000EE RID: 238 |
|
// (get) Token: 0x060002CA RID: 714 RVA: 0x00011124 File Offset: 0x0000F324 |
|
// (set) Token: 0x060002CB RID: 715 RVA: 0x0001113B File Offset: 0x0000F33B |
|
public uint RecordCount { get; set; } |
|
|
|
// Token: 0x170000EF RID: 239 |
|
// (get) Token: 0x060002CC RID: 716 RVA: 0x00011144 File Offset: 0x0000F344 |
|
// (set) Token: 0x060002CD RID: 717 RVA: 0x0001115B File Offset: 0x0000F35B |
|
public ushort HeaderLength { get; set; } |
|
|
|
// Token: 0x170000F0 RID: 240 |
|
// (get) Token: 0x060002CE RID: 718 RVA: 0x00011164 File Offset: 0x0000F364 |
|
// (set) Token: 0x060002CF RID: 719 RVA: 0x0001117B File Offset: 0x0000F37B |
|
public ushort RecordLength { get; set; } |
|
|
|
// Token: 0x170000F1 RID: 241 |
|
// (get) Token: 0x060002D0 RID: 720 RVA: 0x00011184 File Offset: 0x0000F384 |
|
// (set) Token: 0x060002D1 RID: 721 RVA: 0x0001119B File Offset: 0x0000F39B |
|
public byte LanguageDriverID { get; set; } |
|
} |
|
|
|
// Token: 0x02000051 RID: 81 |
|
internal class DBFFieldInfo |
|
{ |
|
// Token: 0x170000F2 RID: 242 |
|
// (get) Token: 0x060002D3 RID: 723 RVA: 0x000111AC File Offset: 0x0000F3AC |
|
// (set) Token: 0x060002D4 RID: 724 RVA: 0x000111C3 File Offset: 0x0000F3C3 |
|
public string FieldName { get; set; } |
|
|
|
// Token: 0x170000F3 RID: 243 |
|
// (get) Token: 0x060002D5 RID: 725 RVA: 0x000111CC File Offset: 0x0000F3CC |
|
// (set) Token: 0x060002D6 RID: 726 RVA: 0x000111E3 File Offset: 0x0000F3E3 |
|
public char FieldType { get; set; } |
|
|
|
// Token: 0x170000F4 RID: 244 |
|
// (get) Token: 0x060002D7 RID: 727 RVA: 0x000111EC File Offset: 0x0000F3EC |
|
// (set) Token: 0x060002D8 RID: 728 RVA: 0x00011203 File Offset: 0x0000F403 |
|
public byte Length { get; set; } |
|
|
|
// Token: 0x170000F5 RID: 245 |
|
// (get) Token: 0x060002D9 RID: 729 RVA: 0x0001120C File Offset: 0x0000F40C |
|
// (set) Token: 0x060002DA RID: 730 RVA: 0x00011223 File Offset: 0x0000F423 |
|
public byte Decimal { get; set; } |
|
} |
|
} |
|
}
|
|
|