home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / EXAM6.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-07-25  |  8.2 KB  |  194 lines

  1. (* EXAMPLE6.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     Btree,
  8.     Compare,
  9.     FileBuff,
  10.     FileDecs,
  11.     Files,
  12.     LRecList,
  13.     Numbers,
  14.     Page,
  15.     Logical;
  16.  
  17. const
  18.     TESTSTRINGSIZE = 10;          (* size of strings (part of test record) *)
  19.  
  20. type
  21.     TestString = String[TESTSTRINGSIZE];
  22.  
  23.     TestRecord = record                 (* record definition for data file *)
  24.                  randByte : Byte;
  25.                  randString : TestString;
  26.                  end;
  27.  
  28. var
  29.     dataFile,
  30.     indexFile1,
  31.     indexFile2 : FnString;                      (* holds file name strings *)
  32.  
  33.     testRec : TestRecord;                (* variable to hold a data record *)
  34.  
  35.     lrLst : LrList;         (* variable which user declares and passes in to
  36.                                LRLIST unit.  LRLIST will build a list of
  37.                                logical records which fulfill a query criteria.
  38.                                (Actually the list will be built by a call
  39.                                to the BTREE unit which in turn uses the LRLIST
  40.                                unit.                                         *)
  41.  
  42.     lrNum : LrNumber;       (* a couple of variable used in this demo program
  43.                                to keep track of logical record number
  44.                                currently being processed.                    *)
  45.  
  46.     tempString : TestString; (* these variables are used to pass in the
  47.                                 selection criteria for retrievals.  A variable
  48.                                 or typed constants must be used.  Constants do
  49.                                 not work since the call is by reference and
  50.                                 not by value                                 *)
  51.  
  52.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  53.  
  54.  
  55. (* This procedure will be called prior to termination of the program whether
  56.    there is an error or not.  This is a demonstration of a good technique to
  57.    use in conjunction with BTree10.  Calls to write the buffer to disk and
  58.    close the files should be included.  This is also a place top handle runtime
  59.    error since I do not attempt to deal with errors or maintain global error
  60.    variables in BTree10.                                                     *)
  61.  
  62. {$F+} procedure MyExit; {$F-}
  63.  
  64.     begin
  65.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  66.                                                       follow along           *)
  67.  
  68.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  69.                                         the program the buffer must be written
  70.                                         to disk or some changes will be lost.
  71.                                         This will cause major problems and lost
  72.                                         data!!!  This is not really required in
  73.                                         in this program since nothing is
  74.                                         modified.  A logical record list is
  75.                                         created, but it happens to be small
  76.                                         enough that a temporary file is not
  77.                                         needed.  If a file was needed, it would
  78.                                         have been destroyed on the call to
  79.                                         DestroyLrList anyway.                *)
  80.  
  81.     Writeln('Closing Files ...');                  (* just a note so you can
  82.                                                       follow along           *)
  83.     CloseAllFiles;               (* Close the files to clean up.  It is
  84.                                     important to realize that CloseAllFiles
  85.                                     DOES NOT write the buffer to disk, it only
  86.                                     deals with the files open list.          *)
  87.  
  88.     end;
  89.  
  90.  
  91. (* This routine should be called before anything else happens.  It calls
  92.    the various routines to set up parameters according to values applicable
  93.    to your particular application.                                           *)
  94.  
  95. procedure SetUp;
  96.  
  97.     begin
  98.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  99.                                 of pages in the buffer.  If this is not done a
  100.                                 default of one page is used.  This will cause
  101.                                 poor performance but will not cause any
  102.                                 runtime errors                               *)
  103.  
  104.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  105.                                 of files the FILEBUFF unit can have open at
  106.                                 once.  See FILEBUFF unit for details.        *)
  107.  
  108.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  109.                                      buffered in the page buffer and will only
  110.                                      written to disk when they are swapped out
  111.                                      or explicitly written out by a user
  112.                                      request                                 *)
  113.  
  114.     end;
  115.  
  116.  
  117. (* This procedure adjusts the file names as required.  It appends the proper
  118.    extension to the name.                                                    *)
  119.  
  120. procedure InitFiles;
  121.  
  122.     begin
  123.  
  124.     dataFile := 'myFile1';
  125.     FixFileName(dataFile,DATA);
  126.  
  127.     indexFile1 := 'testByte';
  128.     FixFileName(indexFile1,INDEX);
  129.  
  130.     indexFile2 := 'testStrg';
  131.     FixFileName(indexFile2,INDEX);
  132.  
  133.     end;
  134.  
  135.  
  136.  
  137. begin
  138.  
  139. Writeln('Setting up termination routine ...');     (* just a note so you can
  140.                                                       follow along           *)
  141.  
  142. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  143. ExitProc := @MyExit;
  144.  
  145. Writeln('Initializing Parameters ...');            (* just a note so you can
  146.                                                       follow along           *)
  147.  
  148. SetUp;              (* set file open buffer size and page buffer size limits *)
  149.                     (* set immediate disk write parameter as well            *)
  150.  
  151.  
  152. InitFiles;                                   (* create the files and indexes *)
  153.  
  154. Writeln('Looking for logical records which match selection criterion and',
  155.          ' building logical record list ... this may take a minute ...' );
  156.                                                    (* just a note so you can
  157.                                                       follow along           *)
  158. tempString := 'C';       (* We want to find all records which begin with 'C' *)
  159.  
  160. GetSubstringFromBTree(indexFile2,tempString,EN,lrLst);
  161.                                                    (* build list of logical
  162.                                                       record which end
  163.                                                       in 'C'                 *)
  164.  
  165.     (* The following loop will find data records associated with the
  166.        record numbers found in the newly created logical record list.        *)
  167.  
  168. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  169.                                  to the front of the list.                   *)
  170.  
  171. while lrNum <> 0 do
  172.     begin                   (* we need to fetch the record and print it out  *)
  173.  
  174.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  175.                       lrNum,                      (* logical record to fetch *)
  176.                       testRec,                      (* place to put the data *)
  177.                       SizeOf(testRec));       (* number of bytes to retrieve *)
  178.  
  179.     Writeln(testRec.randByte,'      ',testRec.randString);
  180.  
  181.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  182.  
  183.     end;
  184.  
  185. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  186.  
  187. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  188.                            is important so that there aren't a bunch of
  189.                            temporary files upon termination.  Also, lists
  190.                            take up data space and also space in the page
  191.                            buffer                                            *)
  192.  
  193. end.
  194.