home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / TBTREE16.ZIP / EXAM7.PAS < prev    next >
Pascal/Delphi Source File  |  1989-07-12  |  9KB  |  197 lines

  1. (* EXAM7.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     Exam0,
  8.     Btree,
  9.     Compare,
  10.     FileBuff,
  11.     FileDecs,
  12.     Files,
  13.     Logical,
  14.     LRecList,
  15.     Numbers,
  16.     Page;
  17.  
  18. var
  19.     dataFile,
  20.     indexFile1,
  21.     indexFile2 : FnString;                      (* holds file name strings *)
  22.  
  23.     testRec : TestRecord;                (* variable to hold a data record *)
  24.  
  25.     lrLst : LrList;         (* variable which user declares and passes in to
  26.                                LRLIST unit.  LRLIST will build a list of
  27.                                logical records which fulfill a query criteria.
  28.                                (Actually the list will be built by a call
  29.                                to the BTREE unit which in turn uses the LRLIST
  30.                                unit.                                         *)
  31.  
  32.     lrNum : LrNumber;       (* a couple of variable used in this demo program
  33.                                to keep track of logical record number
  34.                                currently being processed.                    *)
  35.  
  36.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  37.  
  38.  
  39. (* This procedure will be called prior to termination of the program whether
  40.    there is an error or not.  This is a demonstration of a good technique to
  41.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  42.    close the files should be included.  This is also a place top handle runtime
  43.    error since I do not attempt to deal with errors or maintain global error
  44.    variables in TBTree.                                                      *)
  45.  
  46. {$F+} procedure MyExit; {$F-}
  47.  
  48.     begin
  49.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  50.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  51.                                                       follow along           *)
  52.  
  53.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  54.                                         the program the buffer must be written
  55.                                         to disk or some changes will be lost.
  56.                                         This will cause major problems and lost
  57.                                         data!!!  This is not really required in
  58.                                         in this program since nothing is
  59.                                         modified.  A logical record list is
  60.                                         created, but it happens to be small
  61.                                         enough that a temporary file is not
  62.                                         needed.  If a file was needed, it would
  63.                                         have been destroyed on the call to
  64.                                         DestroyLrList anyway.                *)
  65.  
  66.     Writeln('Closing Files ...');                  (* just a note so you can
  67.                                                       follow along           *)
  68.     CloseAllFiles;               (* Close the files to clean up.  It is
  69.                                     important to realize that CloseAllFiles
  70.                                     DOES NOT write the buffer to disk, it only
  71.                                     deals with the files open list.          *)
  72.  
  73.     end;
  74.  
  75.  
  76. (* This routine should be called before anything else happens.  It calls
  77.    the various routines to set up parameters according to values applicable
  78.    to your particular application.                                           *)
  79.  
  80. procedure SetUp;
  81.  
  82.     begin
  83.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  84.                                 of pages in the buffer.  If this is not done a
  85.                                 default of one page is used.  This will cause
  86.                                 poor performance but will not cause any
  87.                                 runtime errors                               *)
  88.  
  89.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  90.                                 of files the FILEBUFF unit can have open at
  91.                                 once.  See FILEBUFF unit for details.        *)
  92.  
  93.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  94.                                      buffered in the page buffer and will only
  95.                                      written to disk when they are swapped out
  96.                                      or explicitly written out by a user
  97.                                      request                                 *)
  98.  
  99.     end;
  100.  
  101.  
  102. (* This procedure initializes the file names as required.  Also, if will
  103.    create the files if they do not exist.  Therefore, you can use one
  104.    initialization routine whether the file exists or must be created.        *)
  105.  
  106. procedure InitFiles;
  107.  
  108.     begin
  109.  
  110.     dataFile := 'myFile1.dat';
  111.     if not FileExists(dataFile) then
  112.         begin
  113.         CreateDataFile(dataFile,SizeOf(TestRecord));
  114.         end;
  115.  
  116.     indexFile1 := 'testByte.idx';
  117.     if not FileExists(indexFile1) then
  118.         begin
  119.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  120.         end;
  121.  
  122.     indexFile2 := 'testStrg.idx';
  123.     if not FileExists(indexFile2) then
  124.         begin
  125.         CreateIndexFile(indexfile2,SizeOf(TestString),STRINGVALUE);
  126.         end;
  127.     end;
  128.  
  129. (*****************************************************************************)
  130. (*                                                                           *)
  131. (*                         M A I N      P R O G R A M                        *)
  132. (*                                                                           *)
  133. (*****************************************************************************)
  134.  
  135. begin
  136.  
  137. Writeln('Setting up termination routine ...');     (* just a note so you can
  138.                                                       follow along           *)
  139.  
  140. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  141. ExitProc := @MyExit;
  142.  
  143. Writeln('Initializing Parameters ...');            (* just a note so you can
  144.                                                       follow along           *)
  145.  
  146. SetUp;              (* set file open buffer size and page buffer size limits *)
  147.                     (* set immediate disk write parameter as well            *)
  148.  
  149.  
  150. InitFiles;
  151.  
  152.      (* recreate the index files *)
  153. CreateIndexFile(indexFile1,SizeOf(Byte),BYTEVALUE);
  154.  
  155. CreateIndexFile(indexFile2,SizeOf(TestString),STRINGVALUE);
  156.  
  157. Writeln('Looking for all logical records presently in use for our data file');
  158.                                                    (* just a note so you can
  159.                                                       follow along           *)
  160.  
  161. GetValidLogicalRecords(dataFile,lrLst);        (* get a list of all existing
  162.                                                    data records in this file *)
  163.  
  164. Writeln('Found them - rebuilding the indexes ...');
  165.  
  166.     (* The following loop will place the appropriate data in the two indexes.
  167.        This is an easy way to build an index from an existing data file.  This
  168.        same technique can be used to rebuild an index if it somehow becomes
  169.        damaged.                                                              *)
  170.  
  171.  
  172. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  173.                                  to the front of the list.                   *)
  174.  
  175. while lrNum <> 0 do
  176.     begin                   (* we need to fetch the record and print it out  *)
  177.  
  178.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  179.                       lrNum,                      (* logical record to fetch *)
  180.                       testRec);                     (* place to put the data *)
  181.  
  182.     InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
  183.     InsertValueInBTree(indexFile2,lrNum,testRec.randString);
  184.  
  185.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  186.  
  187.     end;
  188.  
  189. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  190.                            is important so that there aren't a bunch of
  191.                            temporary files upon termination.  Also, lists
  192.                            take up data space and also space in the page
  193.                            buffer                                            *)
  194.  
  195. end.
  196.  
  197.