home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue155 / delphi / Test1 / t1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-05-24  |  6.4 KB  |  273 lines

  1. unit t1;
  2. interface
  3.  
  4. uses
  5.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  6.   StdCtrls, ExtCtrls;
  7.  
  8. type
  9. { declare a fixed-length string                 }
  10.   bkstr = string[70];
  11.  
  12.   TForm1 = class(TForm)
  13.     ListBox1: TListBox;
  14.     Panel1: TPanel;
  15.     ExitBtn: TButton;
  16.     SaveBtn: TButton;
  17.     LoadBtn: TButton;
  18.     ClearDisplayBtn: TButton;
  19.     Panel2: TPanel;
  20.     AddObsBtn: TButton;
  21.     ShowObsBtn: TButton;
  22.     FreeObsBtn: TButton;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure ExitBtnClick(Sender: TObject);
  25.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  26.     procedure AddObsBtnClick(Sender: TObject);
  27.     procedure ShowObsBtnClick(Sender: TObject);
  28.     procedure FreeObsBtnClick(Sender: TObject);
  29.     procedure SaveBtnClick(Sender: TObject);
  30.     procedure LoadBtnClick(Sender: TObject);
  31.     procedure ClearDisplayBtnClick(Sender: TObject);
  32.   private
  33.     { Private declarations }
  34.   public
  35.     { Public declarations }
  36.     procedure ShowObList;
  37.   end;
  38.  
  39.  
  40.   { BookOb class        }
  41.   BookOb = class(TObject)
  42.     name   : bkstr;
  43.     author : bkstr;
  44.     constructor Create( aName, anAuthor : bkstr );
  45.     constructor CreateFromStream( fs : TFileStream );
  46.     destructor Destroy; override;
  47.     function Describe : bkstr;  virtual;
  48.     procedure WriteToStream( fs : TFileStream );
  49.     procedure ReadFromStream(fs: TFileStream);
  50.   end;
  51.  
  52.   { BookObList is a special BookOb-managing TList }
  53.   BookObList = class(TList)
  54.     constructor Create;
  55.     destructor Destroy; override;
  56.     procedure FreeObs;
  57.     procedure AddBookOb( n, a : bkstr );
  58.     procedure SaveObList( fname : string );
  59.     procedure LoadObList(fname: string);
  60.   end;
  61.  
  62. var
  63.   Form1: TForm1;
  64.   ObList : BookObList; { a List to hold our objects }
  65.  
  66. const
  67.    SAVEFILE = 'Test.sav';
  68.  
  69. implementation
  70.  
  71. {$R *.DFM}
  72. // ===========================================
  73. // TFORM1 Start...
  74. // ===========================================
  75.  
  76. procedure TForm1.ShowObList;
  77. { Show each ob's ID (its name and author) in the ListBox }
  78. var
  79.    i :integer;
  80. begin
  81. {  ListBox1.Clear; }
  82.   if ObList.Count = 0 then
  83.     ListBox1.Items.Add( 'No books in the list!' )
  84.   else
  85.   for i := 0 to ObList.Count - 1 do
  86.       ListBox1.Items.Add( BookOb(ObList.Items[i]).Describe );
  87. end;
  88.  
  89. { Event-handlers }
  90. procedure TForm1.AddObsBtnClick(Sender: TObject);
  91. begin
  92.    if ObList.Count = 0 then
  93.    begin
  94.      ObList.AddBookOb( 'The Moonstone', 'Wilkie Collins' );
  95.      ObList.AddBookOb( 'The Shining', 'Stephen King' );
  96.      ObList.AddBookOb( 'Black Holes and Time Warps', 'Kip S. Thorne' );
  97.      ShowObList;
  98.    end
  99.    else
  100.       ListBox1.Items.Add( 'List already contains objects! ' );
  101. end;
  102.  
  103. procedure TForm1.ShowObsBtnClick(Sender: TObject);
  104. begin
  105.   ShowObList;
  106. end;
  107.  
  108. procedure TForm1.FreeObsBtnClick(Sender: TObject);
  109. begin
  110.   ObList.FreeObs;
  111.   ListBox1.Items.Add( 'Objects have been freed' );
  112. end;
  113.  
  114. procedure TForm1.SaveBtnClick(Sender: TObject);
  115. begin
  116.   ObList.SaveObList( SAVEFILE );
  117.   ListBox1.Items.Add( 'Saved' );
  118. end;
  119.  
  120. procedure TForm1.LoadBtnClick(Sender: TObject);
  121. begin
  122.    ObList.LoadObList( SAVEFILE );
  123.    ShowObList;
  124. end;
  125.  
  126. procedure TForm1.ClearDisplayBtnClick(Sender: TObject);
  127. begin
  128.   ListBox1.Clear;
  129. end;
  130.  
  131. procedure TForm1.FormCreate(Sender: TObject);
  132. // When the Form is created, the ObList must be created too!
  133. begin
  134.   ObList := BookObList.Create;
  135. end;
  136.  
  137. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  138. begin
  139.   ObList.FreeObs;
  140.   ObList.Free;
  141. end;
  142.  
  143. procedure TForm1.ExitBtnClick(Sender: TObject);
  144. begin
  145.   Close;
  146. end;
  147. // ...TFORM1 End
  148.  
  149.  
  150. // ===========================================
  151. // BOOKOB Start...
  152. // ===========================================
  153. function BookOb.Describe : bkstr;
  154. { Concatenate name and author - an illustrative, rather than a useful method! }
  155. begin
  156.   result := Format( '[General Book] %s by %s', [name,author]);
  157. end;
  158.  
  159. procedure BookOb.WriteToStream( fs : TFileStream );
  160. begin
  161.   fs.WriteBuffer(author, sizeof(author) );
  162.   fs.WriteBuffer(name, sizeof(name ) );
  163. end;
  164.  
  165. procedure BookOb.ReadFromStream( fs : TFileStream );
  166. begin
  167.   fs.ReadBuffer(author, sizeof(author) );
  168.   fs.ReadBuffer(name, sizeof(name ) );
  169. end;
  170.  
  171. // constructor
  172. constructor BookOb.Create( aName, anAuthor : bkstr );
  173. begin
  174.   inherited Create;
  175.   Form1.ListBox1.Items.Add( 'CONSTRUCTOR: BookOb.Create' );
  176.   name := aName;
  177.   author := anAuthor;
  178. end;
  179.  
  180. // alternative constructor.
  181. // creates itself using data read from stream
  182. constructor BookOb.CreateFromStream(fs: TFileStream);
  183. begin
  184.    inherited Create;
  185.    Form1.ListBox1.Items.Add( 'CONSTRUCTOR: BookOb.CreateFromStream' );
  186.    ReadFromStream( fs );
  187. end;
  188.  
  189. // destructor
  190. destructor BookOb.Destroy;
  191. begin
  192.   Form1.ListBox1.Items.Add( 'DESTRUCTOR: BookOb.Destroy' );
  193.   name := '';
  194.   author := '';
  195.   inherited Destroy;
  196. end;
  197.  
  198. // ...BOOKOB End
  199.  
  200.  
  201.  
  202. // ===========================================
  203. // BOOKOBLIST Start...
  204. // ===========================================
  205. procedure BookObList.SaveObList( fname : string );
  206. var
  207.    fs : TFileStream;
  208.     i : integer;
  209. begin
  210.    fs := TFileStream.Create(fname, fmCreate );
  211.    try
  212.      for i := 0 to ObList.Count - 1 do
  213.          BookOb(ObList[i]).WriteToStream( fs );
  214.    finally;
  215.      fs.Free;
  216.    end;
  217. end;
  218.  
  219. procedure BookObList.LoadObList( fname : string );
  220. var
  221.    fs : TFileStream;
  222. begin
  223.    ObList.FreeObs;
  224.    fs := TFileStream.Create(fname, fmOpenRead );
  225.    try
  226.     Form1.ListBox1.Items.Add( Format('fs.Position = %d, fs.Size = %d',
  227.                                     [fs.Position,fs.Size] ));
  228.    while fs.Position < fs.Size do
  229.        ObList.Add( BookOb.CreateFromStream(fs) ) ;
  230.    finally;
  231.      fs.Free;
  232.    end;
  233. end;
  234.  
  235. procedure BookObList.FreeObs;
  236. { Free the objects in the ObList  }
  237. var
  238.   i : integer;
  239. begin
  240.   for i := 0 to ObList.Count - 1 do
  241.       if ObList.Items[i] <> nil then
  242.       begin
  243.          Form1.ListBox1.Items.Add( 'Call to FREE Ob: ' +
  244.                              BookOb(ObList.Items[i]).Describe );
  245.          BookOb(ObList.Items[i]).Free;
  246.       end;
  247.   ObList.Clear;
  248. end;
  249.  
  250. procedure BookObList.AddBookOb(n, a: bkstr);
  251. begin
  252.   ObList.Add( BookOb.Create( n, a ));
  253. end;
  254.  
  255. // constructor
  256. constructor BookObList.Create;
  257. begin
  258.   inherited Create;
  259. end;
  260.  
  261. // destructor
  262. destructor BookObList.Destroy;
  263. begin
  264.    inherited Destroy;
  265. end;
  266. // ...BOOKOBLIST End
  267.  
  268.  
  269.  
  270.  
  271.  
  272. end.
  273.