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

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE1.PAS - demonstrate file creation with multiple keys,
  4.                  string justification, error trapping,
  5.                  reading by key value, reading by file position
  6.                  updating records
  7.                  also indicates the platform, DOS, DPMI, Windows
  8.                  uses a disk based message file
  9.  
  10.   Requires Turbo Pascal version 6.0, 7.0
  11. }
  12.  
  13. Uses
  14.   {$IFDEF WINDOWS}
  15.   WinCrt,
  16.   {$ELSE}
  17.   Crt,
  18.   {$ENDIF}
  19.   {$IFDEF VER70}
  20.   WinDos,
  21.   {$ELSE}
  22.   Dos,
  23.   {$ENDIF}
  24.   BtvConst,
  25.   Btv;
  26.  
  27.  
  28. type
  29.   ErrorType = Object(ErrorDisplay)
  30.     Function    Display(Error     : Integer;
  31.                         ErrorMsg  : String;
  32.                         OpCode    : Integer;
  33.                         OpCodeMsg : String;
  34.                         FileName  : PathStr
  35.                         ): ErrorAction;             Virtual;
  36.   end;
  37.  
  38.  
  39. var
  40.   F           : BtrieveFile;
  41.   Buff        : record
  42.                   Name    : String[15];
  43.                   Number  : String[5];
  44.                   ZNumber : Array[0..15] of Char;
  45.                   SNumber : Array[1..10] of Char;
  46.                   Comment : String[80];
  47.                 end;
  48.   Pos         : LongInt;
  49.   Name        : String[15];
  50.   Number      : String[5];
  51.   ZNumber     : Array[0..15] of Char;
  52.   ErrHandler  : DiskErrorHandler;
  53.   ErrDisplay  : ErrorType;
  54.   Major       : Word;
  55.   Minor       : Word;
  56.   Flag        : Char;
  57.   i           : Word;
  58.  
  59.  
  60. { Heres our error display object  }
  61. Function ErrorType.Display(Error     : Integer;
  62.                            ErrorMsg  : String;
  63.                            OpCode    : Integer;
  64.                            OpCodeMsg : String;
  65.                            FileName  : PathStr
  66.                            ): ErrorAction;
  67.   begin
  68.     ClrScr;
  69.     Writeln('Btrieve IO error for ' + FileName);
  70.     Writeln('ERROR CODE #', Error, ' - ', ErrorMsg);
  71.     Writeln('Press any key ....');
  72.     ReadKey;
  73.     Display := erDone;  { just let the program continue }
  74.     ClrScr;
  75.   end;
  76.  
  77. begin
  78.   ClrScr;
  79.   {$IFDEF WINDOWS}
  80.   Writeln('COMPILED FOR WINDOWS');
  81.   {$ENDIF}
  82.   {$IFDEF DPMI}
  83.   Writeln('COMPILED FOR PROTECTED MODE');
  84.   {$ENDIF}
  85.   {$IFDEF MSDOS}
  86.   Writeln('COMPILED FOR REAL MODE');
  87.   {$ENDIF}
  88.  
  89.   { first make a error display }
  90.   ErrDisplay.Init;
  91.   { make a DISK BASED error handler, it needs a display object  }
  92.   ErrHandler.Init(@ErrDisplay, 'BTRIEVE.ERR');
  93.  
  94.   Writeln('Creating a file called TEST1.DAT');
  95.  
  96.   { init the file passing it the error handler and }
  97.   { address of our data buffer                     }
  98.   F.Init('TEST1.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  99.  
  100.   { the first thing to do is define the keys  }
  101.   { key is name, it is an lString, modifiable, has duplicates }
  102.   { and is left justified and padded                          }
  103.   F.AddKeySegment(1, 16, bExtended + bDuplicates + bModifiable,
  104.                   bLstring, 0, bLJustify);
  105.   { key is number, it is an lString, and is right justified }
  106.   F.AddKeySegment(17, 6, bExtended, bLstring, 0, bRJustify);
  107.   { add a ZString segment }
  108.   F.AddKeySegment(23, 16, bExtended, bZstring, 0, bRJustify);
  109.   { add a Btrieve String segment }
  110.   F.AddKeySegment(39, 10, bNormal, bString, 0, bLJustify);
  111.   { now that all the keys are defined lets create and open it             }
  112.   { it will have no special features, but will overwrite any existing one }
  113.   F.Create(bNormal, SizeOf(Buff), 1024, 0, bNormal);
  114.   F.Open(bNormal, '');
  115.  
  116.   { lets add a couple records  }
  117.   F.ClearBuffer;
  118.   Buff.Name   := 'AAAAAAAAAA';
  119.   Buff.Number := '1';   { the object will right justify this  }
  120.   Buff.ZNumber[0] := ' ';
  121.   Buff.ZNumber[1] := '1';
  122.   Buff.ZNumber[2] := #0;
  123.   Buff.SNumber[1]:= ' ';
  124.   Buff.SNumber[2]:= ' ';
  125.   Buff.SNumber[3]:= ' ';
  126.   Buff.SNumber[4]:= ' ';
  127.   Buff.SNumber[5]:= '1';
  128.   Buff.Comment:= 'Record #1';
  129.   F.Insert;
  130.   Write('Adding some records .');
  131.   {$IFNDEF WINDOWS}
  132.   Delay(50);
  133.   {$ENDIF}
  134.  
  135.   F.ClearBuffer;
  136.   Buff.Name   := 'BBBBBBBBBB';
  137.   Buff.Number := '2';
  138.   Buff.ZNumber[0] := '2';
  139.   Buff.ZNumber[1] := #0;
  140.   Buff.SNumber[1]:= ' ';
  141.   Buff.SNumber[2]:= ' ';
  142.   Buff.SNumber[3]:= ' ';
  143.   Buff.SNumber[4]:= ' ';
  144.   Buff.SNumber[5]:= ' ';
  145.   Buff.SNumber[6]:= ' ';
  146.   Buff.SNumber[7]:= ' ';
  147.   Buff.SNumber[8]:= ' ';
  148.   Buff.SNumber[9]:= ' ';
  149.   Buff.SNumber[10]:= '2';
  150.   Buff.Comment:= 'Record #2';
  151.   F.Insert;
  152.   Write('.');
  153.   {$IFNDEF WINDOWS}
  154.   Delay(50);
  155.   {$ENDIF}
  156.  
  157.   F.ClearBuffer;
  158.   Buff.Name   := 'CCCCCCCCCC';
  159.   Buff.Number := '3';
  160.   Buff.ZNumber[0] := ' ';
  161.   Buff.ZNumber[1] := ' ';
  162.   Buff.ZNumber[2] := ' ';
  163.   Buff.ZNumber[3] := ' ';
  164.   Buff.ZNumber[4] := ' ';
  165.   Buff.ZNumber[5] := ' ';
  166.   Buff.ZNumber[6] := ' ';
  167.   Buff.ZNumber[7] := ' ';
  168.   Buff.ZNumber[8] := ' ';
  169.   Buff.ZNumber[9] := ' ';
  170.   Buff.ZNumber[10] := ' ';
  171.   Buff.ZNumber[11] := ' ';
  172.   Buff.ZNumber[12] := ' ';
  173.   Buff.ZNumber[13] := ' ';
  174.   Buff.ZNumber[14] := '3';
  175.   Buff.ZNumber[15] := #0;
  176.   Buff.SNumber[1]:= '3';
  177.   Buff.Comment:= 'Record #3';
  178.   F.Insert;
  179.   Write('.');
  180.   {$IFNDEF WINDOWS}
  181.   Delay(50);
  182.   {$ENDIF}
  183.  
  184.   F.ClearBuffer;
  185.   Buff.Name   := 'DDDDDDDDDD';
  186.   Buff.ZNumber[0] := '4';
  187.   Buff.ZNumber[1] := #0;
  188.   Buff.SNumber[1]:= '4';
  189.   Buff.Number := '4';
  190.   Buff.Comment:= 'Record #4';
  191.   F.Insert;
  192.   Write('.');
  193.   {$IFNDEF WINDOWS}
  194.   Delay(50);
  195.   {$ENDIF}
  196.  
  197.   F.ClearBuffer;
  198.   Buff.Name   := 'EEEEEEEEEE';
  199.   Buff.Number := '5';
  200.   Buff.ZNumber[0] := '5';
  201.   Buff.ZNumber[1] := #0;
  202.   Buff.SNumber[1]:= '5';
  203.   Buff.Comment:= 'Record #5';
  204.   F.Insert;
  205.   Writeln('.');
  206.  
  207.   { let's see how big the file is }
  208.   Writeln('There are ', F.NumberOfRecords, ' records in the file.');
  209.   Writeln('Press a key...');
  210.   ReadKey;
  211.  
  212.   Writeln('Reading by key, should generate an error #4 and test error trapping');
  213.   Writeln('Press a key...');
  214.   ReadKey;
  215.   { remember keys start at zero }
  216.   F.SetKeyPath(1);
  217.   { build the key, we'll try out the error handler and won't set up a match }
  218.   Number  :=  '9';  { no need to right justify this }
  219.   F.MakeKey(@Number, nil, nil, nil, nil, nil);
  220.   { read it without locks }
  221.   F.Get(bGetEqual, bNoLock);
  222.  
  223.   { okay now we'll remove error key not found from from the trapped set }
  224.   { and handle it ourselves                                            }
  225.   Writeln('Reading by the second key again, this time without error trapping');
  226.   ErrHandler.RemoveErrors([bKeyNotFound]);
  227.   F.Get(bGetEqual, bNoLock);
  228.   Writeln('File status = ', F.bResult);
  229.   Writeln('Press a key...');
  230.   ReadKey;
  231.  
  232.   { put key not found back in }
  233.   ErrHandler.AddErrors([bKeyNotFound]);
  234.   { try a key that should be in the file }
  235.   Writeln('Reading by the second key yet again');
  236.   Number  :=  '3';
  237.   F.MakeKey(@Number, nil, nil, nil, nil, nil);
  238.   F.Get(bGetEqual, bNoLock);
  239.  
  240.   if (F.bResult = bOkay) then
  241.   begin
  242.     Writeln('I think I got one');
  243.   end
  244.  
  245.   else
  246.   begin
  247.     Writeln('Something is wrong?');
  248.     Halt;
  249.   end;
  250.  
  251.   { read by ZString key }
  252.   F.SetKeyPath(2);
  253.   ZNumber[0]  := '3';
  254.   ZNumber[1]  := #0;
  255.   F.MakeKey(@ZNumber, nil, nil, nil, nil, nil);
  256.   F.Get(bGetEqual, bNoLock);
  257.  
  258.   Writeln(Buff.Name);
  259.   Writeln(Buff.Number);
  260.   Writeln(' 123456789012345');
  261.   Write('<');
  262.  
  263.   for i := 0 to 14 do
  264.     Write(Buff.ZNumber[i]);
  265.  
  266.   Writeln('>');
  267.   Writeln(' 123456789012345');
  268.   Write('<');
  269.  
  270.   for i := 1 to 10 do
  271.     Write(Buff.SNumber[i]);
  272.  
  273.   Writeln('>');
  274.   Writeln(Buff.Comment);
  275.   Writeln('Current file positioning is ', Pos);
  276.   Writeln('Press a key...');
  277.   ReadKey;
  278.   F.SetKeyPath(1);
  279.  
  280.   { read and save the current file position }
  281.   Pos := F.GetPosition;
  282.   Writeln(Buff.Name);
  283.   Writeln(Buff.Number);
  284.   Writeln(' 123456789012345');
  285.   Write('<');
  286.  
  287.   for i := 0 to 14 do
  288.     Write(Buff.ZNumber[i]);
  289.  
  290.   Writeln('>');
  291.   Writeln(' 123456789012345');
  292.   Write('<');
  293.  
  294.   for i := 1 to 10 do
  295.     Write(Buff.SNumber[i]);
  296.  
  297.   Writeln('>');
  298.   Writeln(Buff.Comment);
  299.   Writeln('Current file positioning is ', Pos);
  300.   Writeln('Press a key...');
  301.   ReadKey;
  302.  
  303.   { Change it and write it back out }
  304.   Writeln('Changing that last record');
  305.   Buff.Comment := 'THIS RECORD HAS BEEN UPDATED!';
  306.   F.Update;
  307.   Writeln('Press a key...');
  308.   ReadKey;
  309.  
  310.   { read and display a record by position and see that it changed }
  311.   Writeln('Reading that last record by position');
  312.   Writeln('Here is a record in the file at position ', F.GetPosition);
  313.   F.GetDirect(bNoLock, Pos);
  314.   Writeln(Buff.Name);
  315.   Writeln(Buff.Number);
  316.   Writeln(Buff.Comment);
  317.   Writeln('Press a key...');
  318.   ReadKey;
  319.  
  320.   { read and display the next record  }
  321.   F.Get(bGetNext, bNoLock);
  322.   Writeln('Here is the next record in the file');
  323.   Writeln(Buff.Name);
  324.   Writeln(Buff.Comment);
  325.   Writeln('Press a key...');
  326.   ReadKey;
  327.  
  328.   { read and display the first record  }
  329.   Writeln('Here is the first record in the file');
  330.   F.Get(bGetFirst, bNoLock);
  331.   Writeln(Buff.Name);
  332.   Writeln(Buff.Number);
  333.   Writeln(Buff.Comment);
  334.   Writeln('Press a key...');
  335.   ReadKey;
  336.  
  337.  
  338.   { Show the btrieve version }
  339.   F.Version(Major, Minor, Flag);
  340.   Writeln('You are running BTRIEVE version ', Major, '.', Minor, ' ', Flag);
  341.   Writeln('Press a key...');
  342.   ReadKey;
  343.  
  344.  
  345.   F.Close;
  346. end.
  347.