home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / BTP20.ZIP / CREATE2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-06-10  |  3.7 KB  |  107 lines

  1. PROGRAM Create2;              { (C) 1993 John C. Leon    last updated 6/8/93 }
  2.  
  3. {
  4. Create a Btrieve file with a meaningless and arbitrary file structure and an
  5. owner name, just to demonstrate the BTP unit.
  6.  
  7. Creates a *variable length* file with a 10% free space threshold, with a
  8. record length of 30 for the fixed length portion of a record, with two keys.
  9. The owner access is set to RQ, to require the name for both read and write,
  10. with no encryption.
  11.  
  12. After file creation, we open the file by instantiating an object of the base
  13. class, BFile, in order to add a supplemental index which uses an alternate
  14. collating sequence.  The typical calling convention for the AddSuppIdx
  15. function is used, which builds a linked list of key specs on the heap on the
  16. fly.  The heap objects are deleted internally within the AddSuppIdx function.
  17. Note also that the alternate collating sequence is opened and read into the
  18. data buffer transparently.  This is done within AddSuppIdx by instantiating
  19. an object of type TAltColSeq.  Beautiful illustration of the cooperation
  20. between objects in BTP!
  21.  
  22. Be sure to have the file UPPER.ALT in the current directory when running this
  23. program!
  24. }
  25.  
  26. {$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}
  27.  
  28. USES
  29.    Dos, BTP;
  30.  
  31. VAR
  32.    Counter,
  33.    Counter1   : integer;
  34.    DirInfo    : SearchRec;
  35.    MyFileSpec : PFileSpecObj;
  36.    MyFileName : string;
  37.    MyAltColSeq: PAltColSeq;
  38.    TestFile   : PBFile;
  39.  
  40. BEGIN
  41.    if not IsBtrieveLoaded then
  42.       begin
  43.       writeln('Please load Btrieve before running this program.');
  44.       halt(1);
  45.       end;
  46.  
  47.    FindFirst('Upper.ALT', Archive, DirInfo);
  48.    if doserror <> 0 then
  49.       begin
  50.       writeln('This program requires the UPPER.ALT alternate collating sequence');
  51.       writeln('in the current directory.');
  52.       halt(2);
  53.       end;
  54.    write('Enter name of file to create: ');
  55.    readln(MyFileName);
  56.    if MyFileName = '' then
  57.       begin
  58.       writeln('No filename entered...aborting');
  59.       halt(3);
  60.       end;
  61.    
  62.    MyFileSpec := new(PFileSpecObj, Init(30, 512, 2, VarLength or Free10, 0,
  63.                      NewKeySpec(11, 20, Duplicates or Modifiable or ExtType,
  64.                                 BString,
  65.                      NewKeySpec(1, 10, Duplicates or Modifiable or ExtType,
  66.                                 BString,
  67.                                 nil))));
  68.    
  69.    {Create initially w/no alt col sequence.  We add it only for the supp index.}
  70.    
  71.    BStatus := CreateFile(MyFileName, MyFileSpec^.Specs, '', 'BTP', RQ);
  72.    dispose(MyFileSpec, Done);
  73.    if BStatus <> 0 then
  74.       begin
  75.       writeln('Error creating file.  Status = ', BStatus);
  76.       halt(4);
  77.       end;
  78.  
  79.    for Counter := 1 to length(MyFileName) do
  80.       MyFileName[Counter] := upcase(MyFileName[Counter]);
  81.    writeln('File ', MyFileName, ' created successfully.');
  82.    
  83.    {Now add the supplemental index ... this op requires the file be open,
  84.     so let's instantiate a BFile object, which does it all for us.  Besides,
  85.     the AddSuppIdx function is a member of BFile. }
  86.  
  87.    TestFile := new(PBFile, Init(MyFileName, Normal, 'BTP'));
  88.    TestFile^.AddSuppIdx( NewKeySpec(
  89.                          5, 2, Duplicates or Modifiable or Segmented or
  90.                                AltCol or ExtType, BString,
  91.                          NewKeySpec(
  92.                          16, 2, Duplicates or Modifiable or
  93.                                 AltCol or ExtType, BString,
  94.                          nil)),
  95.                          'UPPER.ALT');
  96.    TestFile^.Close;
  97.    dispose(TestFile, Done);
  98.    
  99.    if (BStatus <> 0) then
  100.       begin
  101.       writeln('Adding of supplemental indexes failed.  Status = ', BStatus);
  102.       halt(5);
  103.       end
  104.       else
  105.       writeln('Supplemental indexes added successfully.');
  106. END.
  107.