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

  1. (* EXAM10.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.     SetOps,
  18.     Sort;
  19.  
  20. var
  21.     dataFile,
  22.     indexFile1,
  23.     indexFile2 : FnString;                      (* holds file name strings *)
  24.  
  25.     testRec : TestRecord;                (* variable to hold a data record *)
  26.  
  27.     lrLst1,
  28.     lrLst2,
  29.     lrLst3 : LrList;        (* variable which user declares and passes in to
  30.                                LRLIST unit.  LRLIST will build a list of
  31.                                logical records which fulfill a query criteria.
  32.                                (Actually the list will be built by a call
  33.                                to the BTREE unit which in turn uses the LRLIST
  34.                                unit.                                         *)
  35.  
  36.     lrNum : LrNumber;       (* a couple of variable used in this demo program
  37.                                to keep track of logical record number
  38.                                currently being processed.                    *)
  39.  
  40.     tempByte : Byte;        (* this variable is used to pass in the selection
  41.                                criteria for retrievals.  A variable or typed
  42.                                constants must be used.  Constants do not work
  43.                                since the call is by reference and not by
  44.                                value                                         *)
  45.  
  46.     sPtr : SortFieldList;
  47.  
  48.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  49.  
  50.  
  51. (* This procedure will be called prior to termination of the program whether
  52.    there is an error or not.  This is a demonstration of a good technique to
  53.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  54.    close the files should be included.  This is also a place top handle runtime
  55.    error since I do not attempt to deal with errors or maintain global error
  56.    variables in TBTREE.                                                      *)
  57.  
  58. {$F+} procedure MyExit; {$F-}
  59.  
  60.     begin
  61.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  62.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  63.                                                       follow along           *)
  64.  
  65.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  66.                                         the program the buffer must be written
  67.                                         to disk or some changes will be lost.
  68.                                         This will cause major problems and lost
  69.                                         data!!!  This is not really required in
  70.                                         in this program since nothing is
  71.                                         modified.  A logical record list is
  72.                                         created, but it happens to be small
  73.                                         enough that a temporary file is not
  74.                                         needed.  If a file was needed, it would
  75.                                         have been destroyed on the call to
  76.                                         DestroyLrList anyway.                *)
  77.  
  78.     Writeln('Closing Files ...');                  (* just a note so you can
  79.                                                       follow along           *)
  80.  
  81.     CloseAllFiles;               (* Close the files to clean up.  It is
  82.                                     important to realize that CloseAllFiles
  83.                                     DOES NOT write the buffer to disk, it only
  84.                                     deals with the files open list.          *)
  85.  
  86.     end;
  87.  
  88.  
  89. (* This routine should be called before anything else happens.  It calls
  90.    the various routines to set up parameters according to values applicable
  91.    to your particular application.                                           *)
  92.  
  93. procedure SetUp;
  94.  
  95.     begin
  96.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  97.                                 of pages in the buffer.  If this is not done a
  98.                                 default of one page is used.  This will cause
  99.                                 poor performance but will not cause any
  100.                                 runtime errors                               *)
  101.  
  102.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  103.                                 of files the FILEBUFF unit can have open at
  104.                                 once.  See FILEBUFF unit for details.        *)
  105.  
  106.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  107.                                      buffered in the page buffer and will only
  108.                                      written to disk when they are swapped out
  109.                                      or explicitly written out by a user
  110.                                      request                                 *)
  111.  
  112.     end;
  113.  
  114.  
  115. (* This procedure initializes the file names as required.  Also, if will
  116.    create the files if they do not exist.  Therefore, you can use one
  117.    initialization routine whether the file exists or must be created.        *)
  118.  
  119. procedure InitFiles;
  120.  
  121.     begin
  122.  
  123.     dataFile := 'myFile1.dat';
  124.     if not FileExists(dataFile) then
  125.         begin
  126.         CreateDataFile(dataFile,SizeOf(TestRecord));
  127.         end;
  128.  
  129.     indexFile1 := 'testByte.idx';
  130.     if not FileExists(indexFile1) then
  131.         begin
  132.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  133.         end;
  134.  
  135.     indexFile2 := 'testStrg.idx';
  136.     if not FileExists(indexFile2) then
  137.         begin
  138.         CreateIndexFile(indexfile2,SizeOf(TestString),STRINGVALUE);
  139.         end;
  140.     end;
  141.  
  142.  
  143. (*****************************************************************************)
  144. (*                                                                           *)
  145. (*                         M A I N      P R O G R A M                        *)
  146. (*                                                                           *)
  147. (*****************************************************************************)
  148.  
  149. begin
  150.  
  151. Writeln('Setting up termination routine ...');     (* just a note so you can
  152.                                                       follow along           *)
  153.  
  154. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  155. ExitProc := @MyExit;
  156.  
  157. Writeln('Initializing Parameters ...');            (* just a note so you can
  158.                                                       follow along           *)
  159.  
  160. SetUp;              (* set file open buffer size and page buffer size limits *)
  161.                     (* set immediate disk write parameter as well            *)
  162.  
  163.  
  164. InitFiles;
  165.  
  166. Writeln('Looking for logical records which match selection criterion and',
  167.          ' building logical record list ... this may take a minute ...' );
  168.                                                    (* just a note so you can
  169.                                                       follow along           *)
  170.  
  171. tempByte := 240;
  172.  
  173.  
  174. GetValuesFromBTree(indexFile1,tempByte,LE,lrLst1); (* build list of logical
  175.                                                       record numbers with
  176.                                                       values <= 240          *)
  177.  
  178. tempByte := 50;
  179.  
  180.  
  181. GetValuesFromBTree(indexFile1,tempByte,GE,lrLst2); (* build list of logical
  182.                                                       record numbers with
  183.                                                       values >= 50          *)
  184.  
  185. Intersection(lrLst1,lrLst2,lrLst3);     (* create new list (lrLst3) which
  186.                                            is the intersection of lrLst1
  187.                                            and lrLst2                       *)
  188.  
  189.     (* The following loop will fetch the data records associated with the
  190.        record numbers found in the newly created logical record list.  Each
  191.        of the records will be printed.  Since the list was built using
  192.        indexFile1 which is the index corresponding to the randByte field in
  193.        testRec, the records will be printed in ascending order by
  194.        testRec.randByte.  If the opposite order was desired the list should
  195.        be traversed in reverse by using GetLastLr and GetPrevLr.             *)
  196.  
  197.  
  198. lrNum := GetFirstLr(lrLst3);  (* get the first record number and set the cursor
  199.                                  the front of the list.                      *)
  200.  
  201. while lrNum <> 0 do
  202.     begin
  203.  
  204.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  205.                       lrNum,              (* logical record number from list *)
  206.                       testRec);                     (* place to put the data *)
  207.  
  208.     Writeln(lrNum,'        ',testRec.randByte,'       ',testrec.randString);
  209.  
  210.     lrNum := GetNextLr(lrLst3);        (* advance the cursor to next in list *)
  211.     end;
  212.  
  213.     (* The following three calls are for demo purposes to show a couple of
  214.        utilities available.                                                  *)
  215.  
  216. Writeln('Total records found matching criteria = ',GetCountLr(lrLst3));
  217.  
  218. Writeln('Number of files presently open = ',GetNumberOpenFiles);
  219.  
  220. DestroyLrList(lrLst1);
  221. DestroyLrList(lrLst2);
  222. DestroyLrList(lrLst3);  (* we're done with list so let's get rid of them.  This
  223.                            is important so that there aren't a bunch of
  224.                            temporary files upon termination.  Also, lists
  225.                            take up data space and also space in the page
  226.                            buffer                                            *)
  227.  
  228. end.
  229.