public DbfFile(String path) { Binary stream = new Binary(File.OpenRead(path)); header = new DbfFileHeader(stream); stream.Seek(header.DataAreaOffset); for (int i = 0, count = header.RecordCount; i < count; i++) { Dictionary<String, String> record = header.ReadRecord(stream); if (record != null) { records.Add(record); } } }
public String ReadValue(Binary stream) { byte[] bytes = new byte[size]; stream.Read(bytes, 0, size); return Encoder.fromBytes(bytes); }
public Dictionary<String, String> ReadRecord(Binary stream) { byte marker = stream.readByte(); if (marker != LIVE_RECORD_MARKER) { return null; } Console.WriteLine(marker); Dictionary<String, String> record = new Dictionary<String, String>(); foreach (DbfSubrecord sr in subrecords) { String value = sr.ReadValue(stream); record.Add(sr.Name, value); } return record; }
public static DbfSubrecord read(Binary stream) { // Console.WriteLine("-Subrecord"); byte nameFirstByte = stream.readByte(); if (nameFirstByte == 0x0D) { return null; } else { DbfSubrecord sr = new DbfSubrecord(); sr.nameBytes[0] = nameFirstByte; for (int i = 1; i < sr.nameBytes.Length; i++) { sr.nameBytes[i] = stream.readByte(); } sr.name = Encoder.fromBytes(sr.nameBytes); if (sr.name[sr.name.Length - 1] == '\0') { sr.name = sr.name.Substring(0, sr.name.Length - 1); } sr.type = stream.readByte(); sr.offsetInRecord = stream.readInt4(); sr.size = stream.readByte(); sr.decimals = stream.readByte(); sr.flags = stream.readByte(); sr.autoincNextValue = stream.readInt4(); sr.autoincStep = stream.readByte(); stream.Read(sr.reservedArea, 0, sr.reservedArea.Length); sr.utf8Size = (sr.type == (byte)'C') ? sr.size * 2 : sr.size; return sr; } }
public DbfFileHeader(Binary stream) { typeSignature = stream.readByte(); lastUpdateYear = stream.readByte(); lastUpdateMonth = stream.readByte(); lastUpdateDay = stream.readByte(); recordCount = stream.readInt4(); dataAreaOffset = stream.readInt2(); recordSize = stream.readInt2(); stream.Read(reservedArea1, 0, reservedArea1.Length); flags = stream.readByte(); codepage = stream.readByte(); stream.Read(reservedArea2, 0, reservedArea2.Length); DbfSubrecord subrecord = DbfSubrecord.read(stream); while (subrecord != null) { subrecords.Add(subrecord); subrecord = DbfSubrecord.read(stream); } }