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

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE3.PAS - demonstrate file creation with variable length records,
  4.                  alternate collating sequences,
  5.                  reading and writing variable length records
  6.  
  7.   Requires Turbo Pascal version 6.0, 7.0
  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.                   Comment : String[80];
  43.                 end;
  44.   Name        : String[30];
  45.   Comment     : String[80];
  46.   ErrHandler  : ErrorHandler;
  47.   ErrDisplay  : ErrorType;
  48.  
  49.  
  50. { Heres our error display object  }
  51. Function ErrorType.Display(Error     : Integer;
  52.                            ErrorMsg  : String;
  53.                            OpCode    : Integer;
  54.                            OpCodeMsg : String;
  55.                            FileName  : PathStr
  56.                            ): ErrorAction;
  57.   begin
  58.     ClrScr;
  59.     Writeln('Btrieve IO error for ' + FileName);
  60.     Writeln(Error,  ' - ', ErrorMsg);
  61.     Writeln(Opcode, ' - ', OpCodeMsg);
  62.     Writeln('Press any key ....');
  63.     ReadKey;
  64.     Display := erAbort; { halt program on any errors }
  65.     ClrScr;
  66.   end;
  67.  
  68. begin
  69.   { first make a error display }
  70.   ErrDisplay.Init;
  71.   { now make an error handler, it needs a display object  }
  72.   ErrHandler.Init(@ErrDisplay);
  73.  
  74.   ClrScr;
  75.   Writeln('Creating a file called TEST3.DAT');
  76.  
  77.   { init the file passing it the error handler and }
  78.   { address of our data buffer                     }
  79.   F.Init('TEST3.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  80.  
  81.   { the first thing to do is define the key }
  82.   { key is name, it is an lString and uses an Alt. collating  }
  83.   { sequence and is left justified and padded                 }
  84.   F.AddKeySegment(1, 31, bExtended + bAltSequence, bLstring, 0, bLJustify);
  85.  
  86.   { define an alternate collating sequence }
  87.   F.AddAltSequence('UPPER.ALT');
  88.   { now that the key is defined lets create and open it }
  89.   { this one has variable length records                }
  90.   { the record size is only the size of fixed portion   }
  91.   F.Create(bVariableLen, 31, 1024, 0, bNormal);
  92.   F.Open(bNormal, '');
  93.  
  94.   if (F.bResult <> bOkay) then Halt;
  95.  
  96.   { lets add a couple records  }
  97.   Buff.Name   := 'AAAAAAAAAA';
  98.   Buff.Comment:= 'XXXXX';
  99.   { need to set the size of the output buffer, this must include the }
  100.   { fixed plus the variable portions                                 }
  101.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  102.   F.Insert;
  103.   Write('Adding some records .');
  104.   {$IFNDEF WINDOWS}
  105.   Delay(500);
  106.   {$ENDIF}
  107.  
  108.   Buff.Name   := 'BBBBBBBBBB';
  109.   Buff.Comment:= 'XXXXXXXXXX';
  110.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  111.   F.Insert;
  112.   Write('.');
  113.   {$IFNDEF WINDOWS}
  114.   Delay(500);
  115.   {$ENDIF}
  116.  
  117.   Buff.Name   := 'CCCCCCCCCC';
  118.   Buff.Comment:= 'XXXXXXXXXXXXXXX';
  119.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  120.   F.Insert;
  121.   Write('.');
  122.   {$IFNDEF WINDOWS}
  123.   Delay(500);
  124.   {$ENDIF}
  125.  
  126.   Buff.Name   := 'DDDDDDDDDD';
  127.   Buff.Comment:= 'XXXXXXXXXXXXXXXXXXXX';
  128.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  129.   F.Insert;
  130.   Write('.');
  131.   {$IFNDEF WINDOWS}
  132.   Delay(500);
  133.   {$ENDIF}
  134.  
  135.   Buff.Name   := 'EEEEEEEEEE';
  136.   Buff.Comment:= 'XXXXXXXXXXXXXXXXXXXXXXXXX';
  137.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  138.   F.Insert;
  139.   Writeln('.');
  140.  
  141.   { now we'll just spin through the file, look at the data and }
  142.   { see how big each record is                                 }
  143.   F.Get(bGetFirst, bNoLock);
  144.  
  145.   While (F.bResult = bOkay) do
  146.   begin
  147.     Writeln(Buff.Name);
  148.     Writeln(Buff.Comment);
  149.     Writeln(F.BytesRead, ' Bytes in this record');
  150.     Writeln;
  151.     F.Get(bGetNext, bNoLock);
  152.   end;
  153.  
  154.   F.Close;
  155. end.
  156.