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

  1. {$V-,N+,E+,X+}
  2.  
  3. { EXAMPLE5.PAS - Demonstrates accessing a file using the key info stored by
  4.                  Btrieve.
  5.  
  6.   Requires Turbo Pascal version 6.0, 7.0
  7. }
  8.  
  9. USES
  10.   {$IFDEF WINDOWS}
  11.   WinCrt,
  12.   {$ELSE}
  13.   Crt,
  14.   {$ENDIF}
  15.   {$IFDEF VER70}
  16.   WinDos,
  17.   {$ELSE}
  18.   Dos,
  19.   {$ENDIF}
  20.   BtvConst,
  21.   Btv;
  22.  
  23.  
  24. TYPE
  25.   DataRec = record
  26.     Int   : Integer;
  27.     Str   : String[15];
  28.   end;
  29.  
  30.   ErrorType = Object(ErrorDisplay)
  31.     Function    Display(Error     : Integer;
  32.                         ErrorMsg  : String;
  33.                         OpCode    : Integer;
  34.                         OpCodeMsg : String;
  35.                         FileName  : PathStr
  36.                         ): ErrorAction;             Virtual;
  37.   end;
  38.  
  39.  
  40. VAR
  41.   F   : BtrieveFile;
  42.   Buff: DataRec;
  43.   Seg0: Integer;
  44.   Seg1: String[15];
  45.   ErrHandler  : ErrorHandler;
  46.   ErrDisplay  : ErrorType;
  47.  
  48.  
  49. { Heres our error display object  }
  50. Function ErrorType.Display(Error     : Integer;
  51.                            ErrorMsg  : String;
  52.                            OpCode    : Integer;
  53.                            OpCodeMsg : String;
  54.                            FileName  : PathStr
  55.                            ): ErrorAction;
  56.   begin
  57.     ClrScr;
  58.     Writeln('Btrieve IO error for ' + FileName);
  59.     Writeln(Error,  ' - ', ErrorMsg);
  60.     Writeln(Opcode, ' - ', OpCodeMsg);
  61.     Writeln('Press any key ....');
  62.     ReadKey;
  63.     ClrScr;
  64.     Display := erAbort;
  65.   end;
  66.  
  67.  
  68. BEGIN
  69.   { first make a error display }
  70.   ErrDisplay.Init;
  71.   { make an error handler, it needs a display object  }
  72.   ErrHandler.Init(@ErrDisplay);
  73.  
  74.   ClrScr;
  75.   Writeln('Creating a file called TEST5.DAT');
  76.  
  77.   { init the file passing it the error handler and
  78.     address of our data buffer
  79.   }
  80.   F.Init('TEST5.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  81.  
  82.   { the first thing to do is define the key  }
  83.   { key is an integer }
  84.   F.AddKeySegment(1, 2, bExtended + bSegmented, bInteger, 0, bNormal);
  85.   { key is a Pascal type String }
  86.   { notice there is no justification, when we reopen the file we
  87.     wouldn't have that info, since that is not a Btrieve function
  88.   }
  89.   F.AddKeySegment(3, 16, bExtended, bLstring, 0, bNormal);
  90.   { create the file }
  91.   F.Create(bNormal, SizeOf(Buff), 1024, 0, bNormal);
  92.   F.Open(bNormal, '');
  93.   { add some records }
  94.   F.ClearBuffer;
  95.   Buff.Int := 0;
  96.   Buff.Str := 'ZERO';
  97.   F.Insert;
  98.   Buff.Int := 1;
  99.   Buff.Str := 'ONE';
  100.   F.Insert;
  101.   Buff.Int := 2;
  102.   Buff.Str := 'TWO';
  103.   F.Insert;
  104.   Buff.Int := 3;
  105.   Buff.Str := 'THREE';
  106.   F.Insert;
  107.   Buff.Int := 4;
  108.   Buff.Str := 'FOUR';
  109.   F.Insert;
  110.   Buff.Int := 5;
  111.   Buff.Str := 'FIVE';
  112.   F.Insert;
  113.   Buff.Int := 6;
  114.   Buff.Str := 'SIX';
  115.   F.Insert;
  116.   { close the file and dispose the object }
  117.   F.Close;
  118.   F.Done;
  119.  
  120.   { reopen the file }
  121.   F.Init('TEST5.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  122.   F.Open(bNormal, '');
  123.   { read and display a couple records }
  124.   F.ClearBuffer;
  125.   Seg0 := 3;
  126.   Seg1 := 'THREE';
  127.   { read by first key (Only key in this case)}
  128.   F.SetKeyPath(0);
  129.   { make sure key segments are in correct order }
  130.   F.MakeKey(@Seg0, @Seg1, nil,nil,nil,nil);
  131.   F.Get(bGetEqual, bNoLock);
  132.   Writeln('READING : ', Seg0, ' ', Seg1);
  133.   Writeln('FOUND   : ', Buff.Int, ' ', Buff.Str);
  134.   Writeln;
  135.  
  136.   Seg0 := 6;
  137.   Seg1 := 'SIX';
  138.   F.MakeKey(@Seg0, @Seg1, nil,nil,nil,nil);
  139.   F.Get(bGetEqual, bNoLock);
  140.   Writeln('READING : ', Seg0, ' ', Seg1);
  141.   Writeln('FOUND   : ', Buff.Int, ' ', Buff.Str);
  142.  
  143.   F.Close;
  144.   F.Done;
  145. END.
  146.  
  147.