This one's been bugging me for a few days now, and I can't seem to locate the error.
The program is essentially a simple relational database wherein all the actual data is stored in a Bank field in the Table type, with two other types (FieldGroup and Field) used to hold offset information and fieldnames, etc.
However, my code to save and load the data to the hard drive for further use isn't working properly - the data is garbled somewhere along the line and thus becomes unusable. Whether or not it works depends on the data being used, and if it does work, saving after adding further data to the database will bring up the same error after loading.
Sorry if it's not too clear.
The save function is:
The load function is
I'm wondering if I'm doing something wrong with the way I'm saving and loading the data. I've scoured the code for hours trying to find if I'm forgetting to save an important piece of data, or if I'm correctly creating the Table, FieldGroup and Field types and adding them to their list (although I'm convinced that's not the problem, as the data read in is garbled before I add the object to the list).
Note that I've mainly only encountered a problem with the "Field" data - never with the FieldGroup or Table data (indeed, the banks are being read and stored correctly). The field data ends up with offsets in the tens of thousands, bitsizes far too big and results in garbled data. On one occasion, however, I did get a error reading a databank (trying to access a memory location out of range), but never had enough time to find the exact cause - it's likely part of the field offset problem.
Any help is greatly, greatly appreciated.
The program is essentially a simple relational database wherein all the actual data is stored in a Bank field in the Table type, with two other types (FieldGroup and Field) used to hold offset information and fieldnames, etc.
However, my code to save and load the data to the hard drive for further use isn't working properly - the data is garbled somewhere along the line and thus becomes unusable. Whether or not it works depends on the data being used, and if it does work, saving after adding further data to the database will bring up the same error after loading.
Sorry if it's not too clear.
The save function is:
'Data Saving Function SaveData() Local FileName:String = Input("Enter Filename: ") Local file:TStream = WriteStream(FileName) 'save totals WriteInt(file, TotalTables) WriteInt(file, TotalFieldGroups) WriteInt(file, TotalFields) 'save tables, then field groups, then fields For Local tbl:DBTable = EachIn DBTableList WriteLine(file, tbl.Name) WriteInt(file, tbl.Id) WriteInt(file, tbl.Records) WriteInt(file, tbl.MaxRecords) WriteInt(file, tbl.MaxSize) WriteInt(file, tbl.Mode) WriteInt(file, tbl.RecordSize) For Local a = 0 To tbl.MaxRecords - 1 WriteInt(file, tbl.Offsets[a]) Next 'Bank Sizes WriteInt(file, BankSize(tbl.DataBank)) WriteInt(file, BankSize(tbl.RecordFlags)) WriteBank(tbl.DataBank, file, 0, BankSize(tbl.DataBank)) WriteBank(tbl.RecordFlags, file, 0, BankSize(tbl.RecordFlags)) Next For Local grp:DBFieldGroup = EachIn DBFieldGroupList WriteLine(file, grp.Name) WriteInt(file, grp.Id) WriteInt(file, grp.Size) WriteInt(file, grp.Table) WriteInt(file, grp.Offset) Next For Local fld:DBField = EachIn DBFieldList WriteLine(file, fld.Name) WriteLine(file, fld.StrValue) WriteLine(file, fld.DefaultStr) WriteInt(file, fld.Id) WriteInt(file, fld.DataType) WriteInt(file, fld.Size) WriteInt(file, fld.BitSize) WriteInt(file, fld.Offset) WriteInt(file, fld.Group) WriteInt(file, fld.Table) WriteInt(file, fld.IntValue) WriteInt(file, fld.DefaultInt) WriteInt(file, fld.Record) Next Print "Saved!" CloseStream file End Function
The load function is
Function LoadData() For Local tbl:DBTable = EachIn DBTableList ListRemove(DBTableList, tbl) Next For Local grp:DBFieldGroup = EachIn DBFieldGroupList ListRemove(DBFieldGroupList, grp) Next For Local fld:DBField = EachIn DBFieldList ListRemove(DBFieldList, fld) Next Print "Existing Data is wiped!" Local FileName:String = Input("Enter Filename: ") Local file:TStream = ReadStream(FileName) 'load in totals TotalTables = Readint(file) TotalFieldGroups = Readint(file) TotalFields = Readint(file) 'load tables, then field groups, then fields For Local count = 1 To TotalTables Local tbl:DBTable = New DBTable tbl.Name = ReadLine(file) tbl.Id = Readint(file) tbl.Records = Readint(file) tbl.MaxRecords = Readint(file) tbl.MaxSize = Readint(file) tbl.Mode = Readint(file) tbl.RecordSize = Readint(file) tbl.Offsets = New Int[tbl.MaxRecords] For Local a = 0 To tbl.MaxRecords - 1 tbl.Offsets[a] = Readint(file) Next Local DataBankSize = Readint(file) Local RecordFlagSize = Readint(file) tbl.DataBank = CreateBank(DataBankSize) tbl.RecordFlags = CreateBank(RecordFlagSize) ReadBank(tbl.DataBank, file, 0, DataBankSize) ReadBank(tbl.RecordFlags, file, 0, RecordFlagSize) ListAddLast(DBTableList, tbl) Next For Local count = 1 To TotalFieldGroups Local grp:DBFieldGroup = New DBFieldGroup grp.Name = ReadLine(file) grp.Id = Readint(file) grp.Size = Readint(file) grp.Table = Readint(file) grp.Offset = Readint(file) ListAddLast(DBFieldGroupList, grp) Next For Local count = 1 To TotalFields Local fld:DBField = New DBField fld.Name = ReadLine(file) fld.StrValue = ReadLine(file) fld.DefaultStr = ReadLine(file) fld.Id = Readint(file) fld.DataType = Readint(file) fld.Size = Readint(file) fld.BitSize = Readint(file) fld.Offset = Readint(file) fld.Group = Readint(file) fld.Table = Readint(file) fld.IntValue = Readint(file) fld.DefaultInt = Readint(file) fld.Record = Readint(file) ListAddLast(DBFieldList, fld) Next Print "Loaded!" CloseStream file End Function
I'm wondering if I'm doing something wrong with the way I'm saving and loading the data. I've scoured the code for hours trying to find if I'm forgetting to save an important piece of data, or if I'm correctly creating the Table, FieldGroup and Field types and adding them to their list (although I'm convinced that's not the problem, as the data read in is garbled before I add the object to the list).
Note that I've mainly only encountered a problem with the "Field" data - never with the FieldGroup or Table data (indeed, the banks are being read and stored correctly). The field data ends up with offsets in the tens of thousands, bitsizes far too big and results in garbled data. On one occasion, however, I did get a error reading a databank (trying to access a memory location out of range), but never had enough time to find the exact cause - it's likely part of the field offset problem.
Any help is greatly, greatly appreciated.