home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPTREE16.ZIP / EXAM6.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-12  |  10.4 KB  |  235 lines

  1. (* EXAM6.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.     tempString : TestString; (* these variables are used to pass in the
  37.                                 selection criteria for retrievals.  A variable
  38.                                 or typed constants must be used.  Constants do
  39.                                 not work since the call is by reference and
  40.                                 not by 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.     CloseAllFiles;               (* Close the files to clean up.  It is
  75.                                     important to realize that CloseAllFiles
  76.                                     DOES NOT write the buffer to disk, it only
  77.                                     deals with the files open list.          *)
  78.  
  79.     end;
  80.  
  81.  
  82. (* This routine should be called before anything else happens.  It calls
  83.    the various routines to set up parameters according to values applicable
  84.    to your particular application.                                           *)
  85.  
  86. procedure SetUp;
  87.  
  88.     begin
  89.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  90.                                 of pages in the buffer.  If this is not done a
  91.                                 default of one page is used.  This will cause
  92.                                 poor performance but will not cause any
  93.                                 runtime errors                               *)
  94.  
  95.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  96.                                 of files the FILEBUFF unit can have open at
  97.                                 once.  See FILEBUFF unit for details.        *)
  98.  
  99.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  100.                                      buffered in the page buffer and will only
  101.                                      written to disk when they are swapped out
  102.                                      or explicitly written out by a user
  103.                                      request                                 *)
  104.  
  105.     end;
  106.  
  107.  
  108. (* This procedure initializes the file names as required.  Also, if will
  109.    create the files if they do not exist.  Therefore, you can use one
  110.    initialization routine whether the file exists or must be created.        *)
  111.  
  112. procedure InitFiles;
  113.  
  114.     begin
  115.  
  116.     dataFile := 'myFile1.dat';
  117.     if not FileExists(dataFile) then
  118.         begin
  119.         CreateDataFile(dataFile,SizeOf(TestRecord));
  120.         end;
  121.  
  122.     indexFile1 := 'testByte.idx';
  123.     if not FileExists(indexFile1) then
  124.         begin
  125.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  126.         end;
  127.  
  128.     indexFile2 := 'testStrg.idx';
  129.     if not FileExists(indexFile2) then
  130.         begin
  131.         CreateIndexFile(indexfile2,SizeOf(TestString),STRINGVALUE);
  132.         end;
  133.     end;
  134.  
  135.  
  136. (*****************************************************************************)
  137. (*                                                                           *)
  138. (*                         M A I N      P R O G R A M                        *)
  139. (*                                                                           *)
  140. (*****************************************************************************)
  141.  
  142. begin
  143.  
  144. Writeln('Setting up termination routine ...');     (* just a note so you can
  145.                                                       follow along           *)
  146.  
  147. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  148. ExitProc := @MyExit;
  149.  
  150. Writeln('Initializing Parameters ...');            (* just a note so you can
  151.                                                       follow along           *)
  152.  
  153. SetUp;              (* set file open buffer size and page buffer size limits *)
  154.                     (* set immediate disk write parameter as well            *)
  155.  
  156.  
  157. InitFiles;
  158.  
  159. Writeln('Looking for logical records which match selection criterion and',
  160.          ' building logical record list ... this may take a minute ...' );
  161.                                                    (* just a note so you can
  162.                                                       follow along           *)
  163. tempString := 'C';         (* We want to find all records which end with 'C' *)
  164.  
  165. GetSubstringFromBTree(indexFile2,tempString,EN,lrLst);
  166.                                                    (* build list of logical
  167.                                                       record which end
  168.                                                       in 'C'                 *)
  169.  
  170.     (* The following loop will find data records associated with the
  171.        record numbers found in the newly created logical record list.        *)
  172.  
  173. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  174.                                  to the front of the list.                   *)
  175.  
  176. while lrNum <> 0 do
  177.     begin                   (* we need to fetch the record and print it out  *)
  178.  
  179.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  180.                       lrNum,                      (* logical record to fetch *)
  181.                       testRec);                     (* place to put the data *)
  182.  
  183.     Writeln(testRec.randByte,'      ',testRec.randString);
  184.  
  185.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  186.  
  187.     end;
  188.  
  189. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  190.  
  191. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  192.                            is important so that there aren't a bunch of
  193.                            temporary files upon termination.  Also, lists
  194.                            take up data space and also space in the page
  195.                            buffer                                            *)
  196.  
  197.  
  198. Writeln('Looking for logical records which match selection criterion and',
  199.          ' building logical record list ... this may take a minute ...' );
  200.                                                    (* just a note so you can
  201.                                                       follow along           *)
  202. tempString := 'C';        (* We want to find all records which 'C' in the
  203.                               second character position                      *)
  204.  
  205. GetSubstringAtpositionFromBTree(indexFile2,tempString,2,lrLst);
  206.  
  207.     (* The following loop will find data records associated with the
  208.        record numbers found in the newly created logical record list.        *)
  209.  
  210. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  211.                                  to the front of the list.                   *)
  212.  
  213. while lrNum <> 0 do
  214.     begin                   (* we need to fetch the record and print it out  *)
  215.  
  216.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  217.                       lrNum,                      (* logical record to fetch *)
  218.                       testRec);                     (* place to put the data *)
  219.  
  220.     Writeln(testRec.randByte,'      ',testRec.randString);
  221.  
  222.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  223.  
  224.     end;
  225.  
  226. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  227.  
  228. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  229.                            is important so that there aren't a bunch of
  230.                            temporary files upon termination.  Also, lists
  231.                            take up data space and also space in the page
  232.                            buffer                                            *)
  233.  
  234. end.
  235.