Recent

Author Topic: [SOLVED] VST Integer  (Read 790 times)

Pe3s

  • Full Member
  • ***
  • Posts: 248
[SOLVED] VST Integer
« on: August 03, 2022, 05:40:07 pm »
Hello, I have such a problem with reading the number to VST. I saved to the file with the command
Code: Pascal  [Select][+][-]
  1. len := Length(IntToStr(Data^.Wiek));
  2.   Stream.Write(len, SizeOf(len));
  3.   Stream.Write(PChar(IntToStr(Data^.Wiek)^, len));
  4.  
I have a problem with reading how to correct the code to make it work
reading
Code: Pascal  [Select][+][-]
  1. Stream.Read(len, SizeOf(len));
  2.   SetLength(Data^.Wiek, len);
  3.   Stream.Read(PChar(IntToStr(Data^.Wiek))^, len);  
  4.  
« Last Edit: August 04, 2022, 04:28:49 pm by Pe3s »

GetMem

  • Hero Member
  • *****
  • Posts: 3784
Re: VST Integer
« Reply #1 on: August 03, 2022, 08:54:01 pm »
Can you please be more specific? What result do you expect and what result do you get instead?

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #2 on: August 03, 2022, 09:01:42 pm »
When it reads the data
SetLength + IntToStr is a compiler error how to fix it

Handoko

  • Hero Member
  • *****
  • Posts: 4712
  • My goal: build my own game engine using Lazarus
Re: VST Integer
« Reply #3 on: August 03, 2022, 09:28:37 pm »
That sure will cause an error.

This is SetLength documentation:
https://www.freepascal.org/docs-html/rtl/system/setlength.html

It needs a variable. The thing you provided: IntToStr(xxx) is a string, not variable.

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #4 on: August 04, 2022, 09:06:08 am »
I corrected it to a variable but it still doesn't read the value
Code: Pascal  [Select][+][-]
  1. Stream.Read(len, SizeOf(len));
  2.   FWiek := IntToStr(Data^.Wiek);
  3.   SetLength(FWiek, len);
  4.   Stream.Read(PChar(IntToStr(Data^.Wiek))^, len);
  5.  

bytebites

  • Hero Member
  • *****
  • Posts: 505
Re: VST Integer
« Reply #5 on: August 04, 2022, 09:48:35 am »
Code: Pascal  [Select][+][-]
  1. IntToStr(Data^.Wiek)
This is not variable.

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #6 on: August 04, 2022, 10:10:01 am »
FWiek is variable
Code: Pascal  [Select][+][-]
  1. FWiek := IntToStr(Data^.Wiek);

Handoko

  • Hero Member
  • *****
  • Posts: 4712
  • My goal: build my own game engine using Lazarus
Re: VST Integer
« Reply #7 on: August 04, 2022, 10:26:58 am »
You should provide the whole source code. Without it, my only guess is:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Stream: TStream;
  4.   FWiek: array of Byte;
  5.   len: Integer;
  6. begin
  7. //  Stream := TFileStream.Create('test.txt', fmOpenRead);
  8.   Stream.Read(len, SizeOf(len));
  9.   SetLength(FWiek, len);
  10.   Stream.Read(FWiek, len);
  11.   Data^.Wiek := FWiek;
  12. //  Stream.Free;
  13. end;

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #8 on: August 04, 2022, 10:32:40 am »
Source code
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  9.   Buttons, ExtCtrls, VirtualTrees;
  10.  
  11. type
  12.   PData = ^TData;
  13.   TData = packed record
  14.     Imie, Nazwisko, Miejscowosc: String;
  15.     Wiek: Integer;
  16.   end;
  17.  
  18.   { TForm1 }
  19.  
  20.   TForm1 = class(TForm)
  21.     Button1: TButton;
  22.     Button2: TButton;
  23.     Button3: TButton;
  24.     Button4: TButton;
  25.     Edit1: TEdit;
  26.     Edit2: TEdit;
  27.     Edit3: TEdit;
  28.     Edit4: TEdit;
  29.     ImageList1: TImageList;
  30.     Label1: TLabel;
  31.     Label2: TLabel;
  32.     Label3: TLabel;
  33.     Label4: TLabel;
  34.     SpeedButton1: TSpeedButton;
  35.     SpeedButton2: TSpeedButton;
  36.     VST: TVirtualStringTree;
  37.     procedure Button1Click(Sender: TObject);
  38.     procedure Button4Click(Sender: TObject);
  39.     procedure SpeedButton1Click(Sender: TObject);
  40.     procedure SpeedButton2Click(Sender: TObject);
  41.     procedure VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
  42.     procedure VSTGetNodeDataSize(Sender: TBaseVirtualTree;
  43.       var NodeDataSize: Integer);
  44.     procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  45.       Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
  46.     procedure VSTLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  47.       Stream: TStream);
  48.     procedure VSTMeasureItem(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
  49.       Node: PVirtualNode; var NodeHeight: Integer);
  50.     procedure VSTSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  51.       Stream: TStream);
  52.   private
  53.  
  54.   public
  55.  
  56.   end;
  57.  
  58. var
  59.   Form1: TForm1;
  60.  
  61. implementation
  62.  
  63. {$R *.lfm}
  64.  
  65. { TForm1 }
  66.  
  67. procedure TForm1.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
  68. var
  69.   Data: PData;
  70. begin
  71.   Data := Sender.GetNodeData(Node);
  72.   if Assigned(Data) then begin
  73.     Finalize(Data^);
  74.   end;
  75. end;
  76.  
  77. procedure TForm1.VSTGetNodeDataSize(Sender: TBaseVirtualTree;
  78.   var NodeDataSize: Integer);
  79. begin
  80.   NodeDataSize := SizeOf(TData);
  81. end;
  82.  
  83. procedure TForm1.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  84.   Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
  85. var
  86.   Data: PData;
  87. begin
  88.   Data := VST.GetNodeData(Node);
  89.   case Column of
  90.   0: CellText := Data^.Imie;
  91.   1: CellText := Data^.Nazwisko;
  92.   2: CellText := IntToStr(Data^.Wiek);
  93.   3: CellText := Data^.Miejscowosc;
  94.   end;
  95. end;
  96.  
  97. procedure TForm1.VSTMeasureItem(Sender: TBaseVirtualTree;
  98.   TargetCanvas: TCanvas; Node: PVirtualNode; var NodeHeight: Integer);
  99. begin
  100.   NodeHeight := Round((Screen.PixelsPerInch / 96.0) * 40);
  101. end;
  102.  
  103. procedure TForm1.VSTLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  104.   Stream: TStream);
  105. var
  106.   Data: PData;
  107.   len: Integer;
  108.   FWiek: String;
  109. begin
  110.   Data := VST.GetNodeData(Node);
  111.  
  112.   Stream.Read(len, SizeOf(len));
  113.   SetLength(Data^.Imie, len);
  114.   Stream.Read(PChar(Data^.Imie)^, len);
  115.  
  116.   Stream.Read(len, SizeOf(len));
  117.   SetLength(Data^.Nazwisko, len);
  118.   Stream.Read(PChar(Data^.Nazwisko)^, len);
  119.  
  120.   Stream.Read(len, SizeOf(len));
  121.   FWiek := IntToStr(Data^.Wiek);
  122.   SetLength(FWiek, len);
  123.   Stream.Read(PChar(IntToStr(Data^.Wiek))^, len);
  124.  
  125.   Stream.Read(len, SizeOf(len));
  126.   SetLength(Data^.Miejscowosc, len);
  127.   Stream.Read(PChar(Data^.Miejscowosc)^, len);
  128. end;
  129.  
  130. procedure TForm1.VSTSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  131.   Stream: TStream);
  132. var
  133.   Data: PData;
  134.   len: Integer;
  135. begin
  136.   Data := VST.GetNodeData(Node);
  137.  
  138.   len := Length(Data^.Imie);
  139.   Stream.Write(len, SizeOf(len));
  140.   Stream.Write(PChar(Data^.Imie)^, len);
  141.  
  142.   len := Length(Data^.Nazwisko);
  143.   Stream.Write(len, SizeOf(len));
  144.   Stream.Write(PChar(Data^.Nazwisko)^, len);
  145.  
  146.   len := Length(IntToStr(Data^.Wiek));
  147.   Stream.Write(len, SizeOf(len));
  148.   Stream.Write(PChar(IntToStr(Data^.Wiek))^, len);
  149.  
  150.   len := Length(Data^.Miejscowosc);
  151.   Stream.Write(len, SizeOf(len));
  152.   Stream.Write(PChar(Data^.Miejscowosc)^, len);
  153. end;
  154.  
  155. // dodaj dane
  156. procedure TForm1.Button1Click(Sender: TObject);
  157. var
  158.   Node: PVirtualNode;
  159.   Data: PData;
  160. begin
  161.   VST.NodeDataSize := SizeOf(TData);
  162.   Node := VST.AddChild(nil);
  163.   Data := VST.GetNodeData(Node);
  164.   Data^.Imie := Edit1.Text;
  165.   Data^.Nazwisko := Edit2.Text;
  166.   Data^.Wiek := StrToInt(Edit3.Text);
  167.   Data^.Miejscowosc := Edit4.Text;
  168. end;
  169.  
  170. procedure TForm1.Button4Click(Sender: TObject);
  171. begin
  172.  
  173. end;
  174.  
  175. procedure TForm1.SpeedButton1Click(Sender: TObject);
  176. begin
  177.   VST.LoadFromFile(Application.Location + 'dane.txt');
  178. end;
  179.  
  180. procedure TForm1.SpeedButton2Click(Sender: TObject);
  181. begin
  182.   VST.SaveToFile(Application.Location + 'dane.txt');
  183. end;
  184.  
  185. end.
  186.  

GetMem

  • Hero Member
  • *****
  • Posts: 3784
Re: VST Integer
« Reply #9 on: August 04, 2022, 10:47:40 am »
@Pe3s

Too many Stream.Read and Stream.Write. Try something like this(see attachment, VST5):
Code: Pascal  [Select][+][-]
  1. type
  2.    PData = ^TData;
  3.    TData = record
  4.      FName: String;
  5.      FAge: Integer;
  6.      //..
  7.    end;
  8.  
  9.  
  10. procedure TForm1.LoadFromFile(AFile: String);
  11. var
  12.   FS: TFileStream;
  13.   Node: PVirtualNode;
  14.   Data: PData;
  15.   Cnt, I: Integer;
  16. begin
  17.   if not FileExists(AFile) then
  18.   begin
  19.     MessageDlg('File does not exists', mtInformation, [mbOk], 0);
  20.     Exit;
  21.   end;
  22.  
  23.   VST.BeginUpdate;
  24.   try
  25.     VST.Clear;
  26.     VST.NodeDataSize := SizeOf(TData);
  27.  
  28.     FS := TFileStream.Create(AFile, fmOpenRead or fmShareDenyNone);
  29.     try
  30.       Cnt := FS.ReadWord;
  31.       if Cnt > 0 then
  32.       begin
  33.         I := 1;
  34.         while I <= Cnt do
  35.         begin
  36.           Node := VST.AddChild(nil);
  37.           Data := VST.GetNodeData(Node);
  38.           FS.Read(Data^, SizeOf(TData));
  39.           Inc(I);
  40.         end;
  41.       end;
  42.     finally
  43.       FS.Free;
  44.     end;
  45.   finally
  46.     VST.EndUpdate;
  47.   end;
  48. end;
  49.  
  50. procedure TForm1.SaveToFile(AFile: String);
  51. var
  52.   FS: TFileStream;
  53.   Node: PVirtualNode;
  54.   Data: PData;
  55. begin
  56.   if VST.RootNodeCount = 0 then
  57.   begin
  58.     MessageDlg('The list is empty. Nothing to save.', mtInformation, [mbOk], 0);
  59.     Exit;
  60.   end;
  61.  
  62.   FS := TFileStream.Create(AFile, fmCreate or fmShareDenyWrite);
  63.   try
  64.     FS.WriteWord(VST.RootNodeCount);
  65.     Node := VST.GetFirst;
  66.     while Assigned(Node) do
  67.     begin
  68.       Data := VST.GetNodeData(Node);
  69.       FS.Write(Data^, SizeOf(TData));
  70.       Node := VST.GetNext(Node);
  71.     end;
  72.   finally
  73.     FS.Free;
  74.   end;
  75. end;
  76.  
  77. procedure TForm1.bSaveClick(Sender: TObject);
  78. begin
  79.   if SD.Execute then
  80.     SaveToFile(SD.FileName);
  81. end;
  82.  
  83. procedure TForm1.bLoadClick(Sender: TObject);
  84. begin
  85.   if OD.Execute then
  86.     LoadFromFile(OD.FileName);
  87. end;  


 

bytebites

  • Hero Member
  • *****
  • Posts: 505
Re: VST Integer
« Reply #10 on: August 04, 2022, 11:03:30 am »
^Does it write (long)string correctly?
« Last Edit: August 04, 2022, 11:24:26 am by bytebites »

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #11 on: August 04, 2022, 11:20:31 am »
This is how it correctly records

bytebites

  • Hero Member
  • *****
  • Posts: 505
Re: VST Integer
« Reply #12 on: August 04, 2022, 11:48:23 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.VSTLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  2.   Stream: TStream);
  3.  var
  4.     Data: pdata;
  5.  
  6.   begin
  7.     Data := VST.GetNodeData(Node);
  8.  
  9.     Data^.Imie:=stream.ReadAnsiString;
  10.  
  11.     Data^.Nazwisko:=stream.ReadAnsiString;
  12.  
  13.     Data^.Wiek:=strtoint(stream.ReadAnsiString);
  14.  
  15.     Data^.Miejscowosc:=stream.ReadAnsiString;
  16.  
  17.   end;
  18.  
  19. procedure TForm1.VSTSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  20.   Stream: TStream);
  21. var
  22.   Data: PData;
  23. begin
  24.   Data := VST.GetNodeData(Node);
  25.  
  26.   Stream.WriteAnsiString(Data^.Imie);
  27.  
  28.   Stream.WriteAnsiString(Data^.Nazwisko);
  29.  
  30.   Stream.WriteAnsiString(IntToStr(Data^.Wiek));
  31.  
  32.   Stream.WriteAnsiString(Data^.Miejscowosc);
  33. end;
  34.  
  35.  

Pe3s

  • Full Member
  • ***
  • Posts: 248
Re: VST Integer
« Reply #13 on: August 04, 2022, 04:28:32 pm »
A big thank you to everyone :)

 

TinyPortal © 2005-2018