unit fmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    Edit1: TEdit;
    RadioButton1: TRadioButton;
    CheckBox1: TCheckBox;
    RadioGroup1: TRadioGroup;
    ListBox1: TListBox;
    Button3: TButton;
    Memo1: TMemo;
    btnBinaryToText: TButton;
    btnTextToBinary: TButton;
    Button4: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure btnBinaryToTextClick(Sender: TObject);
    procedure btnTextToBinaryClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    procedure MyCreateNotifyEvent(Reader: TReader;
      ComponentClass: TComponentClass; var Component: TComponent);
  public
    { Public declarations }
    procedure DeletePanel; 
  end;

var
  Form1: TForm1;

implementation

uses uFileStreamTx;

{$R *.dfm}

const
  sFileName = 'testps.cks';
  bkFileName = 'testps1.cks';

procedure TForm1.Button1Click(Sender: TObject);
var
  iCount : Integer;
  ams : TFileStream;
begin
  ams := TFileStream.Create(sFileName, fmCreate	or fmOpenReadWrite);
  try
    for iCount := 0 to Self.ComponentCount - 1 do    // Iterate
    begin
      ams.WriteDescendent(Self.Components[iCount], Self);
    end;    // for
  finally // wrap up
    FreeAndNil(ams);
  end;    // try/finally
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ams : TFileStream;
  aReader : TReader;
begin
  DeletePanel;
  RegisterClass(TPanel);

  ams := TFileStream.Create(sFileName, fmOpenRead);
  aReader := TReader.Create(ams, 4096);
  aReader.OnCreateComponent := MyCreateNotifyEvent;
  try
//    ams.ReadComponent(Self.Panel1);
    aReader.ReadRootComponent(Self.Panel1);
  finally // wrap up
    FreeAndNil(ams);
//    FreeAndNil(aReader);
  end;    // try/finally
end;

procedure TForm1.DeletePanel;
begin
  FreeAndNil(Self.Panel1);
end;

procedure TForm1.MyCreateNotifyEvent(Reader: TReader;
  ComponentClass: TComponentClass; var Component: TComponent);
begin
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  ams : TFileStream;
begin
  ams := TFileStream.Create(sFileName, fmCreate	or fmOpenReadWrite);
  try
    ams.WriteComponent(Self);
  finally // wrap up
    FreeAndNil(ams);
  end;    // try/finally
end;

procedure TForm1.btnBinaryToTextClick(Sender: TObject);
var
  afs : TFileStream;
  ass : TStringStream;
begin
  if (FileExists(sFileName)) then
  begin
    afs := TFileStream.Create(sFileName, fmOpenRead);
    ass := TStringStream.Create('');
    try
      ObjectBinaryToText(afs, ass);
      Memo1.Lines.Text := ass.DataString;
    finally
      FreeAndNil(ass);
      FreeAndNil(afs);
    end;
  end;
end;

procedure TForm1.btnTextToBinaryClick(Sender: TObject);
var
  afs : TFileStream;
  ass : TStringStream;
begin
  afs := TFileStream.Create(bkFileName, fmCreate	or fmOpenReadWrite);
  ass := TStringStream.Create(Memo1.Lines.Text);
  try
    ObjectTextToBinary(ass, afs);
  finally
    FreeAndNil(afs);
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  aftx : TFileStreamTx;
begin
  if (Self.OpenDialog1.Execute) then
  begin
    aftx := TFileStreamTx.Create(Self.OpenDialog1.FileName, fmOpenRead);
    try
      Memo1.Lines.Text := aftx.ReadLine;
    finally // wrap up
      FreeAndNil(aftx);
    end;    // try/finally
  end;
end;

end.
