home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR36 / BTV200.ZIP / EXAMPLE2.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-18  |  5KB  |  196 lines

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE2.PAS - demonstrate file creation with segmented keys,
  4.                  supplemental indexes, alternate collating sequences
  5.  
  6.   Requires Turbo Pascal version 6.0, 7.0
  7.  
  8.  
  9.   ***** REQUIRES THE FILE UPPER.ALT *****
  10.  
  11. }
  12. Uses
  13.   {$IFDEF WINDOWS}
  14.   WinCrt,
  15.   {$ELSE}
  16.   Crt,
  17.   {$ENDIF}
  18.   {$IFDEF VER70}
  19.   WinDos,
  20.   {$ELSE}
  21.   Dos,
  22.   {$ENDIF}
  23.   BtvConst,
  24.   Btv;
  25.  
  26.  
  27. type
  28.   ErrorType = Object(ErrorDisplay)
  29.     Function    Display(Error     : Integer;
  30.                         ErrorMsg  : String;
  31.                         OpCode    : Integer;
  32.                         OpCodeMsg : String;
  33.                         FileName  : PathStr
  34.                         ): ErrorAction;             Virtual;
  35.   end;
  36.  
  37.  
  38. var
  39.   F           : BtrieveFile;
  40.   Buff        : record
  41.                   Name    : String[30];
  42.                   Number  : Integer;
  43.                   Comment : String[80];
  44.                 end;
  45.   Name        : String[30];
  46.   Number      : Integer;
  47.   Comment     : String[80];
  48.   ErrHandler  : DefErrorHandler;
  49.   ErrDisplay  : ErrorType;
  50.  
  51.  
  52. { Heres our error display object  }
  53. Function ErrorType.Display(Error     : Integer;
  54.                            ErrorMsg  : String;
  55.                            OpCode    : Integer;
  56.                            OpCodeMsg : String;
  57.                            FileName  : PathStr
  58.                            ): ErrorAction;
  59.   begin
  60.     ClrScr;
  61.     Writeln('Btrieve IO error for ' + FileName);
  62.     Writeln(Error,  ' - ', ErrorMsg);
  63.     Writeln(Opcode, ' - ', OpCodeMsg);
  64.     Writeln('Press any key ....');
  65.     ReadKey;
  66.     Display := erDone;  { just let the program continue }
  67.     ClrScr;
  68.   end;
  69.  
  70. begin
  71.   ClrScr;
  72.   {$IFDEF WINDOWS}
  73.   Writeln('COMPILED FOR WINDOWS');
  74.   {$ENDIF}
  75.   {$IFDEF DPMI}
  76.   Writeln('COMPILED FOR PROTECTED MODE');
  77.   {$ENDIF}
  78.   {$IFDEF MSDOS}
  79.   Writeln('COMPILED FOR REAL MODE');
  80.   {$ENDIF}
  81.   { first make a error display }
  82.   ErrDisplay.Init;
  83.   { now make an error handler, it needs a display object  }
  84.   ErrHandler.Init(@ErrDisplay);
  85.  
  86.   Writeln('Creating a file called TEST2.DAT');
  87.  
  88.   { init the file passing it the error handler and }
  89.   { address of our data buffer                     }
  90.   F.Init('TEST2.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  91.  
  92.   { the first thing to do is define the key }
  93.   { this one will be a segmented key        }
  94.   { Segment #1 is a string that is left justified and padded }
  95.   F.AddKeySegment(1, 31, bExtended + bSegmented, bLstring, 0, bLJustify);
  96.   { Segment #2  is an integer and cannot be justified at all }
  97.   F.AddKeySegment(32, 2, bExtended, bInteger, 0, bNormal);
  98.  
  99.   { now that the key is defined lets create and open it }
  100.   { we will preallocate 15 pages just for fun           }
  101.   F.Create(bPreallocate, SizeOf(Buff), 1024, 15, bNormal);
  102.   F.Open(bNormal, '');
  103.  
  104.   { lets add a couple records  }
  105.   Buff.Name   := 'AAAAAAAAAA';
  106.   Buff.Number := 1;
  107.   Buff.Comment:= 'Record #1';
  108.   F.Insert;
  109.   Write('Adding some records .');
  110.   {$IFNDEF WINDOWS}
  111.   Delay(500);
  112.   {$ENDIF}
  113.  
  114.   Buff.Name   := 'BBBBBBBBBB';
  115.   Buff.Number := 2;
  116.   Buff.Comment:= 'Record #2';
  117.   F.Insert;
  118.   Write('.');
  119.   {$IFNDEF WINDOWS}
  120.   Delay(500);
  121.   {$ENDIF}
  122.  
  123.   Buff.Name   := 'CCCCCCCCCC';
  124.   Buff.Number := 3;
  125.   Buff.Comment:= 'Record #3';
  126.   F.Insert;
  127.   Write('.');
  128.   {$IFNDEF WINDOWS}
  129.   Delay(500);
  130.   {$ENDIF}
  131.  
  132.   Buff.Name   := 'DDDDDDDDDD';
  133.   Buff.Number := 4;
  134.   Buff.Comment:= 'Record #4';
  135.   F.Insert;
  136.   Write('.');
  137.   {$IFNDEF WINDOWS}
  138.   Delay(500);
  139.   {$ENDIF}
  140.  
  141.   Buff.Name   := 'EEEEEEEEEE';
  142.   Buff.Number := 5;
  143.   Buff.Comment:= 'Record #5';
  144.   F.Insert;
  145.   Writeln('.');
  146.  
  147.   { let's see how big the file is }
  148.   Writeln('There are ', F.NumberOfRecords, ' records in the file.');
  149.   Writeln('Press a key...');
  150.   ReadKey;
  151.  
  152.   { how about reading a record by key }
  153.   Writeln('Reading by key');
  154.   { build the key }
  155.   { this will be justified and padded automatically as needed }
  156.   Name    := 'EEEEEEEEEE';
  157.   Number  := 5;
  158.   { make sure these are in the right order  }
  159.   F.MakeKey(@Name, @Number, nil, nil, nil, nil);
  160.   { read it without locks }
  161.   F.Get(bGetEqual, bNoLock);
  162.   Writeln(Buff.Name);
  163.   Writeln(Buff.Number);
  164.   Writeln(Buff.Comment);
  165.   Writeln('Press a key...');
  166.   ReadKey;
  167.  
  168.   Writeln('Adding a supplemental index...');
  169.   Writeln('Press a key...');
  170.   ReadKey;
  171.   { add a supplemental key this is just like adding a normal key  }
  172.   { the new index will have an alternate collating sequence       }
  173.   { this key should not be justified since existing data won't be }
  174.   F.AddSupplKeySegment(34, 81, bExtended + bAltSequence, bLstring, 0, bNormal);
  175.   { if the file already had an alternate collating sequence }
  176.   { we would skip this next call                            }
  177.   F.AddAltSequence('UPPER.ALT');
  178.   F.CreateIndex;
  179.  
  180.   { read with the new index }
  181.   F.SetKeyPath(1);
  182.   { see if UPPER.ALT works }
  183.   Comment := 'ReCoRd #1';
  184.   F.MakeKey(@Comment, nil, nil, nil, nil, nil);
  185.   F.Get(bGetEqual, bNoLock);
  186.   Writeln(Buff.Name);
  187.   Writeln(Buff.Number);
  188.   Writeln(Buff.Comment);
  189.   Writeln('Press a key...');
  190.   ReadKey;
  191.  
  192.   { now get rid of the index  }
  193.   F.DropIndex(1);
  194.   F.Close;
  195. end.
  196.