2

I'm following this tutorial to the letter for saving and loading my VirtualStringTree: http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus My problem now is that when I have saved and loaded, my data is corrupted. Chars are changed and added. So does anyone know what the problem is?

Eszee
  • 264
  • 2
  • 14
  • 2
    You need to read and write more bytes than for that example in Lazarus. To be more specific, you have to `Write(Data.Column, Len * SizeOf(Char));` and `Read(Data.Column, Len * SizeOf(Char));` since strings are 2 bytes per char in your Delphi version. Still, I would leave that example and either make helper functions for string writing and reading, or use a ready made `TBinaryWriter`. – TLama May 12 '14 at 10:34
  • You almost have it there. You did give me the info to look for the right code. Tnx – Eszee May 12 '14 at 11:24

1 Answers1

2

I found this Link and changed the awnser to fit my code.

Data.Symbol[1]  //Simply points to a char. So I changed it to 'char'

Read:

Stream.Read(Len, SizeOf(Len));
SetLength(Data.Column0, Len);
Stream.Read(PChar(Data.Column0)^, Len * SizeOf(Char));

//Copy/Paste this code for all Columns

Write:

Len := Length(Data.Column0);
Stream.Write(Len, SizeOf(Len));
Stream.Write(PChar(Data.Column0)^, Length(Data.Column0) * SizeOf(Char));

//Copy/Paste this code for all Columns

Second answer by TLama:

Read:

var
  TreeData: PTreeData;
  BinaryReader: TBinaryReader;
begin
  TreeData := Sender.GetNodeData(Node);

  BinaryReader := TBinaryReader.Create(Stream);
  try
    TreeData.Column0 := BinaryReader.ReadString;
    TreeData.Column1 := BinaryReader.ReadString;
    TreeData.Column2 := BinaryReader.ReadString;
  finally
    BinaryReader.Free;
  end;
end;

Write:

var
  TreeData: PTreeData;
  BinaryWriter: TBinaryWriter;
begin
  TreeData := Sender.GetNodeData(Node);

  BinaryWriter := TBinaryWriter.Create(Stream);
  try
    BinaryWriter.Write(TreeData.Column0);
    BinaryWriter.Write(TreeData.Column1);
    BinaryWriter.Write(TreeData.Column2);
  finally
    BinaryWriter.Free;
  end;
end;

I implemented TLama's answer because I really can't be bothered with all the pointers. This code looks much better, is a lot easier to read, takes up less space and does the same.

So to conclude. When you search the web for saving your VST data, your gonna get a lot of junk code. The first answer will fix that code for you. The second will make you smile :)

Community
  • 1
  • 1
Eszee
  • 264
  • 2
  • 14
  • 1
    With binary reader and writer I think you could write something [`like this`](http://pastebin.com/VpyaZ0T6). – TLama May 12 '14 at 12:03
  • @TLama That looks good. Much more comprehensive then my code. I'm gonna see if I can implement it. Thank you for that. – Eszee May 13 '14 at 09:41
  • @Tlama I Implemented it. If you like I can accept your answer, should you post it ;) Thank you verry much. – Eszee May 13 '14 at 09:49
  • Feel free to include that into your answer ;-) – TLama May 13 '14 at 10:09