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

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