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

  1. (* EXAM8.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.     tempByte : Byte;        (* this variable is used to pass in the selection
  37.                                criteria for retrievals.  A variable or typed
  38.                                constants must be used.  Constants do not work
  39.                                since the call is by reference and not by
  40.                                value                                         *)
  41.  
  42.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  43.  
  44.  
  45. (* This procedure will be called prior to termination of the program whether
  46.    there is an error or not.  This is a demonstration of a good technique to
  47.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  48.    close the files should be included.  This is also a place top handle runtime
  49.    error since I do not attempt to deal with errors or maintain global error
  50.    variables in TBTREE.                                                      *)
  51.  
  52. {$F+} procedure MyExit; {$F-}
  53.  
  54.     begin
  55.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  56.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  57.                                                       follow along           *)
  58.  
  59.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  60.                                         the program the buffer must be written
  61.                                         to disk or some changes will be lost.
  62.                                         This will cause major problems and lost
  63.                                         data!!!  This is not really required in
  64.                                         in this program since nothing is
  65.                                         modified.  A logical record list is
  66.                                         created, but it happens to be small
  67.                                         enough that a temporary file is not
  68.                                         needed.  If a file was needed, it would
  69.                                         have been destroyed on the call to
  70.                                         DestroyLrList anyway.                *)
  71.  
  72.     Writeln('Closing Files ...');                  (* just a note so you can
  73.                                                       follow along           *)
  74.  
  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.  
  165. tempByte := 255;         (* since we are checking for existence, this is not
  166.                             required.  tempByte must still be included in the
  167.                             call however.  It will be ignored.               *)
  168.  
  169.  
  170. GetValuesFromBTree(indexFile1,tempByte,EX,lrLst);  (* build list of logical
  171.                                                       record numbers which
  172.                                                       exist (ctriterion is
  173.                                                       EX which means exists *)
  174.  
  175.     (* The following loop will fetch the data records associated with the
  176.        record numbers found in the newly created logical record list.  Each
  177.        of the records will be printed.  Since the list was built using
  178.        indexFile1 which is the index corresponding to the randByte field in
  179.        testRec, the records will be printed in ascending order by
  180.        testRec.randByte.  If the opposite order was desired the list should
  181.        be traversed in reverse by using GetLastLr and GetPrevLr.             *)
  182.  
  183.  
  184. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  185.                                  the front of the list.                      *)
  186.  
  187. while lrNum <> 0 do
  188.     begin
  189.  
  190.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  191.                       lrNum,              (* logical record number from list *)
  192.                       testRec);                     (* place to put the data *)
  193.  
  194.     Writeln(lrNum,'        ',testRec.randByte,'       ',testrec.randString);
  195.  
  196.     DeleteFromLrList(lrLst);
  197.  
  198.     lrNum := GetCurrLr(lrLst);         (* advance the cursor to next in list *)
  199.     end;
  200.  
  201.     (* The following two calls are for demo purposes to show a couple of
  202.        utilities available.                                                  *)
  203.  
  204. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  205.  
  206. Writeln('Number of files presently open = ',GetNumberOpenFiles);
  207.  
  208.  
  209. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  210.                            is important so that there aren't a bunch of
  211.                            temporary files upon termination.  Also, lists
  212.                            take up data space and also space in the page
  213.                            buffer                                            *)
  214.  
  215.  
  216. end.
  217.