private static bool ValidationChecks(DBHeader header, string FileName) { string name = Path.GetFileName(FileName) + " " + Directory.GetParent(FileName).Name; if (header == null) { return(false); } if (header.RecordCount == 0) { header.Issues |= DBIssues.NO_RECORDS; } if (header.FieldCount == 0) { header.Issues |= DBIssues.NO_FIELDS; } if (header.RecordCount == 0 || header.RecordSize == 0) { return(false); } return(true); }
private static DBHeader ReadHeader(BinaryReader br, string dbFile) { DBHeader header = null; string signature = br.ReadString(4); if (string.IsNullOrWhiteSpace(signature)) { return(null); } if (signature[0] != 'W') { signature = signature.Reverse(); } switch (signature) { case "WDBC": header = new WDBC(); break; case "WDB2": header = new WDB2(); break; } try { header?.ReadHeader(ref br, signature); } catch { header = null; } return(header); }
public static DataTable Read(string dbFile, string dbdFile, Build build, out DBHeader header) { DataTable table = new DataTable(); // create table structure var typeLookup = BuildStructure(dbdFile, build, out bool defissue); foreach (var t in typeLookup) { table.Columns.Add(t.Key, Type.GetType("System." + t.Value)); } using (var fs = new FileStream(dbFile, FileMode.Open, FileAccess.Read)) using (var buffer = new BufferedStream(fs)) using (var br = new BinaryReader(buffer)) { header = ReadHeader(br, dbFile); if (defissue) { header.Issues |= DBIssues.DEFINITION_ISSUE; // multiple or missing def } if (!ValidationChecks(header, dbFile)) // validate header info { return(table); } int fields = typeLookup.Count(x => !x.Key.StartsWith("padding", WO_CASE)); if (fields != header.FieldCount) { header.Issues |= DBIssues.FIELD_COUNT; // validate field count vs def } // stringtable stuff long pos = br.BaseStream.Position; br.BaseStream.Position = br.BaseStream.Position + (header.RecordCount * header.RecordSize); var stringTable = ReadStringTable(br, br.BaseStream.Position); // get stringtable br.BaseStream.Seek(pos, SeekOrigin.Begin); // add stringtable reader func LoadStringFunc(stringTable); // read data var rowLoader = typeLookup.Values.Select(x => _readerFuncs[x]).ToArray(); // generate a reader collection table.BeginLoadData(); try { for (int i = 0; i < header.RecordCount; i++) { DataRow row = table.NewRow(); row.ItemArray = rowLoader.Select(x => x(br)).ToArray(); table.Rows.Add(row); } } catch (EndOfStreamException) { header.Issues |= DBIssues.FIELD_COUNT; // overflow } // check for unused strings if (!typeLookup.Values.Any(x => x == TypeCode.String) && !stringTable.All(x => string.IsNullOrWhiteSpace(x.Value))) { header.Issues |= DBIssues.UNUSED_STRINGS; } table.EndLoadData(); return(table); } }