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

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