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