home *** CD-ROM | disk | FTP | other *** search
- unit t1;
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ExtCtrls;
-
- type
- { declare a fixed-length string }
- bkstr = string[70];
-
- TForm1 = class(TForm)
- ListBox1: TListBox;
- Panel1: TPanel;
- ExitBtn: TButton;
- SaveBtn: TButton;
- LoadBtn: TButton;
- ClearDisplayBtn: TButton;
- Panel2: TPanel;
- AddObsBtn: TButton;
- ShowObsBtn: TButton;
- FreeObsBtn: TButton;
- procedure FormCreate(Sender: TObject);
- procedure ExitBtnClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure AddObsBtnClick(Sender: TObject);
- procedure ShowObsBtnClick(Sender: TObject);
- procedure FreeObsBtnClick(Sender: TObject);
- procedure SaveBtnClick(Sender: TObject);
- procedure LoadBtnClick(Sender: TObject);
- procedure ClearDisplayBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure ShowObList;
- end;
-
-
- { BookOb class }
- BookOb = class(TObject)
- name : bkstr;
- author : bkstr;
- constructor Create( aName, anAuthor : bkstr );
- constructor CreateFromStream( fs : TFileStream );
- destructor Destroy; override;
- function Describe : bkstr; virtual;
- procedure WriteToStream( fs : TFileStream );
- procedure ReadFromStream(fs: TFileStream);
- end;
-
- { BookObList is a special BookOb-managing TList }
- BookObList = class(TList)
- constructor Create;
- destructor Destroy; override;
- procedure FreeObs;
- procedure AddBookOb( n, a : bkstr );
- procedure SaveObList( fname : string );
- procedure LoadObList(fname: string);
- end;
-
- var
- Form1: TForm1;
- ObList : BookObList; { a List to hold our objects }
-
- const
- SAVEFILE = 'Test.sav';
-
- implementation
-
- {$R *.DFM}
- // ===========================================
- // TFORM1 Start...
- // ===========================================
-
- procedure TForm1.ShowObList;
- { Show each ob's ID (its name and author) in the ListBox }
- var
- i :integer;
- begin
- { ListBox1.Clear; }
- if ObList.Count = 0 then
- ListBox1.Items.Add( 'No books in the list!' )
- else
- for i := 0 to ObList.Count - 1 do
- ListBox1.Items.Add( BookOb(ObList.Items[i]).Describe );
- end;
-
- { Event-handlers }
- procedure TForm1.AddObsBtnClick(Sender: TObject);
- begin
- if ObList.Count = 0 then
- begin
- ObList.AddBookOb( 'The Moonstone', 'Wilkie Collins' );
- ObList.AddBookOb( 'The Shining', 'Stephen King' );
- ObList.AddBookOb( 'Black Holes and Time Warps', 'Kip S. Thorne' );
- ShowObList;
- end
- else
- ListBox1.Items.Add( 'List already contains objects! ' );
- end;
-
- procedure TForm1.ShowObsBtnClick(Sender: TObject);
- begin
- ShowObList;
- end;
-
- procedure TForm1.FreeObsBtnClick(Sender: TObject);
- begin
- ObList.FreeObs;
- ListBox1.Items.Add( 'Objects have been freed' );
- end;
-
- procedure TForm1.SaveBtnClick(Sender: TObject);
- begin
- ObList.SaveObList( SAVEFILE );
- ListBox1.Items.Add( 'Saved' );
- end;
-
- procedure TForm1.LoadBtnClick(Sender: TObject);
- begin
- ObList.LoadObList( SAVEFILE );
- ShowObList;
- end;
-
- procedure TForm1.ClearDisplayBtnClick(Sender: TObject);
- begin
- ListBox1.Clear;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- // When the Form is created, the ObList must be created too!
- begin
- ObList := BookObList.Create;
- end;
-
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- ObList.FreeObs;
- ObList.Free;
- end;
-
- procedure TForm1.ExitBtnClick(Sender: TObject);
- begin
- Close;
- end;
- // ...TFORM1 End
-
-
- // ===========================================
- // BOOKOB Start...
- // ===========================================
- function BookOb.Describe : bkstr;
- { Concatenate name and author - an illustrative, rather than a useful method! }
- begin
- result := Format( '[General Book] %s by %s', [name,author]);
- end;
-
- procedure BookOb.WriteToStream( fs : TFileStream );
- begin
- fs.WriteBuffer(author, sizeof(author) );
- fs.WriteBuffer(name, sizeof(name ) );
- end;
-
- procedure BookOb.ReadFromStream( fs : TFileStream );
- begin
- fs.ReadBuffer(author, sizeof(author) );
- fs.ReadBuffer(name, sizeof(name ) );
- end;
-
- // constructor
- constructor BookOb.Create( aName, anAuthor : bkstr );
- begin
- inherited Create;
- Form1.ListBox1.Items.Add( 'CONSTRUCTOR: BookOb.Create' );
- name := aName;
- author := anAuthor;
- end;
-
- // alternative constructor.
- // creates itself using data read from stream
- constructor BookOb.CreateFromStream(fs: TFileStream);
- begin
- inherited Create;
- Form1.ListBox1.Items.Add( 'CONSTRUCTOR: BookOb.CreateFromStream' );
- ReadFromStream( fs );
- end;
-
- // destructor
- destructor BookOb.Destroy;
- begin
- Form1.ListBox1.Items.Add( 'DESTRUCTOR: BookOb.Destroy' );
- name := '';
- author := '';
- inherited Destroy;
- end;
-
- // ...BOOKOB End
-
-
-
- // ===========================================
- // BOOKOBLIST Start...
- // ===========================================
- procedure BookObList.SaveObList( fname : string );
- var
- fs : TFileStream;
- i : integer;
- begin
- fs := TFileStream.Create(fname, fmCreate );
- try
- for i := 0 to ObList.Count - 1 do
- BookOb(ObList[i]).WriteToStream( fs );
- finally;
- fs.Free;
- end;
- end;
-
- procedure BookObList.LoadObList( fname : string );
- var
- fs : TFileStream;
- begin
- ObList.FreeObs;
- fs := TFileStream.Create(fname, fmOpenRead );
- try
- Form1.ListBox1.Items.Add( Format('fs.Position = %d, fs.Size = %d',
- [fs.Position,fs.Size] ));
- while fs.Position < fs.Size do
- ObList.Add( BookOb.CreateFromStream(fs) ) ;
- finally;
- fs.Free;
- end;
- end;
-
- procedure BookObList.FreeObs;
- { Free the objects in the ObList }
- var
- i : integer;
- begin
- for i := 0 to ObList.Count - 1 do
- if ObList.Items[i] <> nil then
- begin
- Form1.ListBox1.Items.Add( 'Call to FREE Ob: ' +
- BookOb(ObList.Items[i]).Describe );
- BookOb(ObList.Items[i]).Free;
- end;
- ObList.Clear;
- end;
-
- procedure BookObList.AddBookOb(n, a: bkstr);
- begin
- ObList.Add( BookOb.Create( n, a ));
- end;
-
- // constructor
- constructor BookObList.Create;
- begin
- inherited Create;
- end;
-
- // destructor
- destructor BookObList.Destroy;
- begin
- inherited Destroy;
- end;
- // ...BOOKOBLIST End
-
-
-
-
-
- end.
-