home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR36 / BTV200.ZIP / BTV.INT < prev    next >
Text File  |  1993-12-18  |  21KB  |  551 lines

  1. {*
  2. *=========================================================================
  3. *  BTV.PAS  Version 2.00
  4. *
  5. *  BTRIEVE object oriented interface for Turbo Pascal 6.0, 7.0
  6. *
  7. *  Copyright (c) 1993 by Richard W. Hansen, all rights reserved.
  8. *
  9. *
  10. *
  11. *  Requires Turbo Pascal version 6.0, 7.0
  12. *
  13. *
  14. *  Registration and payment of a license fee is required for any use, whether
  15. *  in whole or part, of this source code.
  16. *=========================================================================
  17. *
  18. *}
  19.  
  20. {****************************************************************************}
  21. {*   REVISION HISTORY                                                       *}
  22. {*                                                                          *}
  23. {*  Date     Who  What                                                      *}
  24. {* ======================================================================== *}
  25. {* 02/01/92  RWH  Changed DataSize, BytesRead, BytesToWrite from Integer to *}
  26. {*                Word so variable length records can be up to 64K.         *}
  27. {* 02/04/92  RWH  Check that memory allocation size > 0 before issuing an   *}
  28. {*                out of memory error.                                      *}
  29. {*                Added ErrorHandler calls for out of memory errors.        *}
  30. {* 02/08/92  RWH  Added error setting routines to the file object, so calls *}
  31. {*                through the error handler object pointer are not needed.  *}
  32. {* 02/20/92  RWH  BUG FIX-In Clone the wrong file name being used caused    *}
  33. {*                lockup.                                                   *}
  34. {* 02/28/92  RWH  Added Recover, Save and Load methods.                     *}
  35. {* 03/14/92  RWH  BUG FIX-Open was not calculating the largest key correctly*}
  36. {* 04/25/92  RWH  Added the FillKeyBuffer method.                           *}
  37. {* 05/13/92  RWH  BUG FIX-Problem with KeyStart buffer not being setup when *}
  38. {*                key segments not defined before opening a file.           *}
  39. {*                Changed Error Handler and Error Display in fields in all  *}
  40. {*                objects, and the corresponding parameters in methods, to  *}
  41. {*                pointers. This allows nil objects.                        *}
  42. {* 12/30/92  RWH  BUG FIX-Off by one error in Open when loading key info.   *}
  43. {* 06/06/93  RWH  Added protected mode support for BP 7.0                   *}
  44. {*                Moved all consts into the file BtvConst.pas               *}
  45. {*                Added Windows 3.1 support.                                *}
  46. {*                Defined a DefErrorHandler. DefErrorHandler has the default*}
  47. {*                error messages that are stored in the data segment.       *}
  48. {*                Added ErrorON flag and ErrorsOnOff procedures to          *}
  49. {*                BtrieveFile and ErrorHandler.                             *}
  50. {* 09/07/93  RWH  BUG FIX-In CheckForBtrieve Stat was no initialized.       *}
  51. {*                Cleaned up the owner name (password) routines Open,       *}
  52. {*                SetOwner, ClearOwner in BtrieveFile.                      *}
  53. {*                Added bNoAccess constant for owner names.                 *}
  54. {* 11/03/93  RWH  Made all occurences of OpCode or Op integer.              *}
  55. {*                Added SetFillValue and GetFillValue procedures to         *}
  56. {*                BtrieveFile. Modified to ClearBuffer and ClearKey to use  *}
  57. {*                the new FillValue field in BtrieveFile.                   *}
  58. {*                Added AddKeyBuffer procedure to BtrieveFile for external  *}
  59. {*                programmer defined key buffers.                           *}
  60. {*                Renamed "Allocate" field in BtrieveFile to "AllocateData".*}
  61. {*                Added GetBtrieveVersion and UnloadBtrieve global          *}
  62. {*                procedures.                                               *}
  63. {*                Added a DiskErrorHandler object. DiskErrorHandler stores  *}
  64. {*                all messages strings in a BtrieveFile.                    *}
  65. {*                Changed BtrieveFile.MakeKey to a virtual function.        *}
  66. {*                Internal BTV.PAS errors increased to start at 5000.       *}
  67. {*                Renamed Recover to Copy.                                  *}
  68. {*                Added a new Recover that uses Step reads and writes to a  *}
  69. {*                DOS text file.                                            *}
  70. {*                Changed Save to use Get instead of Step reads, to allow   *}
  71. {*                for sorting.                                              *}
  72. {*                Added ResetStation for networks.                          *}
  73. {*                Added justification of ZString and String types.          *}
  74. {****************************************************************************}
  75.  
  76. Unit Btv;
  77. {$F-}
  78. {$V-}
  79. {$X+}
  80. {$A-}
  81.  
  82. {$IFOPT R+}
  83. {$DEFINE __RANGE_ON}
  84. {$ENDIF}
  85.  
  86. {$IFOPT Q+}
  87. {$DEFINE __OVERFLOW_ON}
  88. {$ENDIF}
  89.  
  90.  
  91. {.$DEFINE BCHECK}    { Define this to check for Btrieve during initialization }
  92. {$DEFINE BTRIEVE50} { Define this to make all opcodes new in V 5.0 available }
  93.  
  94.  
  95. INTERFACE
  96.  
  97.  
  98. USES
  99.   BtvConst,
  100.   {$IFDEF VER70}
  101.   WinDos,
  102.   {$ELSE}
  103.   Dos,
  104.   {$ENDIF}
  105.  
  106.   {$IFDEF MSDOS}
  107.   Crt,
  108.   BTRVDOS;
  109.   {$ENDIF}
  110.  
  111.   {$IFDEF DPMI}
  112.   Crt,
  113.   BTRVDPMI;
  114.   {$ENDIF}
  115.  
  116.   {$IFDEF WINDOWS}
  117.   WinProcs,
  118.   WinTypes,
  119.   BTRVWIN;
  120.   {$ENDIF}
  121.  
  122.  
  123. TYPE
  124.   {$IFDEF VER70}
  125.   PathStr  = String[fsPathName];
  126.   {$ENDIF}
  127.  
  128.   AllErrors     = bInvalidOp..bLastError;
  129.     {- a superset of all Btrieve errors allowing for customization }
  130.  
  131.   ErrorSet      = Set of AllErrors;
  132.     {- will hold Btrieve errors and possibly some custom error codes }
  133.  
  134.   ErrorAction = (erAbort, erDone, erRetry);
  135.     {- the possible return states from an error              }
  136.     {- these codes are returned by the error display routine }
  137.  
  138.  
  139.   PBytes        = ^Bytes;
  140.   Bytes         = Array[1..65534] of Byte;
  141.     {- define a byte array and pointer to make access easier }
  142.  
  143.  
  144.   PProgress = ^TProgress;
  145.   TProgress = Object
  146.     Constructor Init;
  147.     Procedure   Display(Count : LongInt);         Virtual;
  148.   end;
  149.     {- object to display progress for recover, save and load }
  150.  
  151.  
  152.   { Btrieve key specs record }
  153.   KeySpec       = record
  154.     KeyPos    : Word;           { position of key or segment in data      }
  155.     KeyLen    : Word;           { length of the key or segment            }
  156.     KeyFlags  : Word;           { key flags as defined by Btrieve         }
  157.     KeyCount  : LongInt;        { not used except for STAT                }
  158.     KeyType   : Byte;           { extended key type                       }
  159.     NullValue : Byte;           { null character if defined               }
  160.     Reserved  : Array[1..4] of Byte;
  161.   end;
  162.  
  163.   KeySpecArray   = Array[1..MaxSegments] of KeySpec;
  164.  
  165.  
  166.   { Our own key definition record }
  167.   KeyDef        = record
  168.     KeyPos    : Word;         { position of key or segment in data      }
  169.     KeyLen    : Word;         { length of the key or segment            }
  170.     KeyFlags  : Word;         { key flags as defined by Btrieve         }
  171.     KeyType   : Byte;         { extended key type                       }
  172.     NullValue : Byte;         { null character if defined               }
  173.     Justify   : Byte;         { lString justification type              }
  174.   end;
  175.  
  176.   KeyDefArray     = Array[1..MaxSegments] of KeyDef;
  177.  
  178.  
  179.   { Btrieve file specs record }
  180.   FileSpec      = record
  181.     RecordLen : Word;           { length of a record in the file          }
  182.     PageSize  : Word;           { physical page size for file             }
  183.     Indexes   : Word;           { number of keys                          }
  184.     Records   : LongInt;        { not used except for STAT                }
  185.     FileFlags : Word;           { file flags as defined by Btrieve        }
  186.     Reserved  : Array[1..2] of Byte;
  187.     FreePages : Word;           { pages to pre allocate                   }
  188.     KeyBuff   : KeySpecArray;   { array of key info (one for each segment)}
  189.     Extra     : Array[1..265] of Byte; { might be needed for alt. sequence}
  190.   end;
  191.  
  192.  
  193.  
  194.   { This is the object that will display errors to the user.              }
  195.   { This is an ABSTRACT object and should never be instantiated, you must }
  196.   { define a descendant object that does what you want in each program.   }
  197.   PErrorDisplay  = ^ErrorDisplay;
  198.   ErrorDisplay   = Object
  199.     Constructor Init;
  200.       {- init the error display }
  201.  
  202.     Function    Display(Error     : Integer;
  203.                         ErrorMsg  : String;
  204.                         OpCode    : Integer;
  205.                         OpCodeMsg : String;
  206.                         FileName  : PathStr
  207.                         ): ErrorAction;             Virtual;
  208.       {- display the error, returns True if program should abort }
  209.  
  210.     Destructor  Done;                               Virtual;
  211.       {- destroy the object }
  212.   end;
  213.  
  214.  
  215.  
  216.   { This is the error object used by the file to trap IO errors.  }
  217.   PErrorHandler   = ^ErrorHandler;
  218.   ErrorHandler    = Object
  219.     RetryCount    : Word;         { current number of retries on an error   }
  220.     MaxRetry      : Word;         { maximum number of retries on an error   }
  221.     RetryDelay    : Word;         { milliseconds between retries            }
  222.     TrappedErrors : ErrorSet;     { errors this object will handle          }
  223.     ErrDisplay    : PErrorDisplay;{ pointer to an error display object      }
  224.     ErrorsON      : Boolean;
  225.  
  226.     Constructor Init(DisplayObject : PErrorDisplay);
  227.       {- initialize the error object }
  228.  
  229.     Function    ErrorDispacther(ErrorCode : Integer;
  230.                                 OpCode    : Integer;
  231.                                 FileName  : PathStr
  232.                                ): ErrorAction;      Virtual;
  233.       {- send errors and messages to the user error display }
  234.  
  235.     Function    Error(Status   : Integer;
  236.                       OpCode   : Integer;
  237.                       FileName : PathStr
  238.                      ): Boolean;                    Virtual;
  239.       {- check for errors and control the number of retries after an error }
  240.  
  241.     Procedure   SetMaxRetry(Retry : Word);
  242.       {- set the maximum retries per error }
  243.  
  244.     Function    GetMaxRetry: Word;
  245.       {- return the maximum retries per error }
  246.  
  247.     Procedure   ClearRetry;
  248.       {- clear the current count of retries }
  249.  
  250.     Procedure   SetDelay(Seconds : Word);
  251.       {- set the delay in seconds between retries }
  252.  
  253.     Function    GetDelay: Word;
  254.       {- return the delay in seconds between retries }
  255.  
  256.     Procedure   AddErrors(ErrorCodes : ErrorSet);
  257.       {- add an error to the set of errors trapped }
  258.  
  259.     Procedure   RemoveErrors(ErrorCodes : ErrorSet);
  260.       {- remove an error from the set of errors trapped }
  261.  
  262.     Procedure   SetErrors(ErrorCodes : ErrorSet);
  263.       {- set the entire trapped error set }
  264.  
  265.     Procedure   GetErrors(var ErrorCodes : ErrorSet);
  266.       {- get the trapped error set }
  267.  
  268.     Procedure   ErrorsOnOff(State : Boolean);
  269.       {- toggle all error trapping ON/OFF }
  270.  
  271.     Function    ErrorMsg(ErrorCode : Integer): String; Virtual;
  272.       {- return an error message for a Btrieve error code }
  273.  
  274.     Function    OpMsg(OpCode : Integer): String;    Virtual;
  275.       {- return a message for a Btrieve operation code }
  276.  
  277.     Destructor  Done;                               Virtual;
  278.       {- destroy the object }
  279.   end;
  280.  
  281.  
  282.   { This error handler has descriptive error and opcode message }
  283.   { strings stored in memory.                                   }
  284.   PDefErrorHandler = ^DefErrorHandler;
  285.   DefErrorHandler = Object(ErrorHandler)
  286.     Function    ErrorMsg(ErrorCode : Integer): String; Virtual;
  287.     Function    OpMsg(OpCode : Integer): String;    Virtual;
  288.   end;
  289.  
  290.  
  291.   { This error handler has descriptive error and opcode message }
  292.   { strings stored in a Btrieve file.                           }
  293.   PDiskErrorHandler = ^DiskErrorHandler;
  294.   DiskErrorHandler = Object(ErrorHandler)
  295.     PosBlock  : Array[1..PosBlockSize] of Byte;
  296.     FileOpen  : Boolean;
  297.  
  298.     Constructor Init(DisplayObject : PErrorDisplay;
  299.                      ErrorPath : PathStr);
  300.     Destructor  Done;                               Virtual;
  301.     Function    ErrorMsg(ErrorCode : Integer): String; Virtual;
  302.     Function    OpMsg(OpCode : Integer): String;    Virtual;
  303.   end;
  304.  
  305.  
  306.   { record type for message file }
  307.   BtrieveMsgRec = record
  308.     TypeID  : Integer;
  309.     Code    : Integer;
  310.     Name    : Array[0..80] of Char;
  311.     MsgText : Array[0..900] of Char;
  312.   end;
  313.  
  314.  
  315.   { This is the Btrieve file file interface object  }
  316.   PBtrieveFile  = ^BtrieveFile;
  317.   BtrieveFile   = Object
  318.     Path        : PathStr;            { File name and path                }
  319.     AltPath     : PathStr;            { Alternate collating seq. file     }
  320.     Data        : Pointer;            { pointer to record data buffer     }
  321.     DataSize    : Word;               { length of record data buffer      }
  322.     AllocateData: Boolean;            { allocate data buffer memory       }
  323.     AllocateKey : Boolean;            { allocate key buffer memory        }
  324.     BytesRead   : Word;               { number of bytes on last file read }
  325.     BytesToWrite: Word;               { number of bytes to write to file  }
  326.     Key         : Pointer;            { pointer to the file key buffer    }
  327.     KeySize     : Byte;               { actual size of the key buffer     }
  328.     SegmentCnt  : Byte;               { total number of key segments      }
  329.     CurIndex    : Word;               { current key being used            }
  330.     IndexCnt    : Byte;               { number of defined keys            }
  331.     Status      : Integer;            { status of last Btrieve operation  }
  332.     FileOpen    : Boolean;            { is the file open                  }
  333.     ErrHandler  : PErrorHandler;      { pointer to the error handler      }
  334.     KeyList     : KeyDefArray;        { list of key definitions           }
  335.                                       { offset of 1st segment in each key }
  336.     KeyStart    : Array[0..MaxSegments - 1] of Byte;
  337.                                       { position block for Btrieve        }
  338.     PosBlock    : Array[1..PosBlockSize] of Byte;
  339.     VariableLen : Boolean;            { does file use var length records  }
  340.     SISegments  : Byte;
  341.     ReadKeyDefs : Boolean;
  342.     CurrentKeySize : Byte;
  343.     FillValue   : Byte;               { value used to clear buffers       }
  344.  
  345.     Constructor Init(FilePath    : PathStr;
  346.                      ErrorObject : PErrorHandler;
  347.                      DataBuf     : Pointer;
  348.                      DataBufSize : Word);
  349.       {- initialize a file object }
  350.  
  351.     Destructor  Done;                               Virtual;
  352.       {- destroy the object }
  353.  
  354.     Procedure   AbortTransaction;
  355.  
  356.     Procedure   AddAltSequence(AltSeqPath : PathStr);
  357.       {- add an alternate collating sequence file }
  358.  
  359.     Procedure   AddErrors(ErrorCodes : ErrorSet);
  360.       {- add an error to the set of errors trapped }
  361.  
  362.     Procedure   AddSupplKeySegment(Position  : Word;
  363.                                    Size      : Word;
  364.                                    Flags     : Word;
  365.                                    KeyType   : Byte;
  366.                                    NullValue : Byte;
  367.                                    Justify   : Byte);
  368.       {- define a key segment for a supplemental index }
  369.  
  370.     Procedure   AddKeyBuffer(KeyBuf     : Pointer;
  371.                              KeyBufSize : Byte);
  372.       {- setup a programmer defined external key buffer }
  373.  
  374.     Procedure   AddKeySegment(Position  : Word;
  375.                               Size      : Word;
  376.                               Flags     : Word;
  377.                               KeyType   : Byte;
  378.                               NullValue : Byte;
  379.                               Justify   : Byte);
  380.       {- define a key segment }
  381.  
  382.     Function    bResult: Integer;
  383.       {- return the last IO status }
  384.  
  385.     Procedure   ChangeBufferSize(Size : Word);
  386.       {- change the size of the output buffer }
  387.  
  388.     Procedure   ClearBuffer;
  389.       {- clear the data buffer }
  390.  
  391.     Procedure   ClearKey;
  392.       {- clear the key buffer }
  393.  
  394.     Procedure   ClearOwner;
  395.       {- set the file owner }
  396.  
  397.     Procedure   Clone(NewFilePath : PathStr;
  398.                       Mode        : Integer);
  399.       {- clone an empty copy of the file }
  400.  
  401.     Procedure   Close;
  402.       {- close the file }
  403.  
  404.     Function    Copy(OutFile    : PBtrieveFile;
  405.                      DisplayObj : PProgress): Integer;
  406.       {- copy all possible records to a new Btrieve file }
  407.  
  408.     Procedure   Create(Flags      : Word;
  409.                        RecordSize : Word;
  410.                        PageSize   : Word;
  411.                        Pages      : Word;
  412.                        Mode       : Integer);
  413.       {- create the file }
  414.  
  415.     Procedure   CreateIndex;
  416.       {- add a supplemental index to the file }
  417.  
  418.     Procedure   Delete;
  419.       {- delete the current record }
  420.  
  421.     Procedure   DropIndex(Index : Integer);
  422.       {- remove a supplemental index from the file }
  423.  
  424.     Procedure   EndTransaction;
  425.  
  426.     Function    Error(ErrStatus : Integer;
  427.                       OpCode    : Integer;
  428.                       FileName  : PathStr
  429.                      ): Boolean;
  430.       {- call the error handler to check for errors }
  431.  
  432.     Procedure   ErrorsOnOff(State : Boolean);
  433.       {- toggle all error trapping ON/OFF }
  434.  
  435.     Procedure   FillKeyBuffer(var Buff; Size : Byte);
  436.       {- fill the key buffer from the data in Buff }
  437.  
  438.     Procedure   Get(Op   : Integer;
  439.                     Lock : Word);
  440.       {- read a record using by a key }
  441.  
  442.     Procedure   GetDirect(Lock     : Word;
  443.                           Position : LongInt);
  444.       {- read a record by file position }
  445.  
  446.     Procedure   GetErrors(var ErrorCodes : ErrorSet);
  447.       {- get the trapped error set }
  448.  
  449.     Function    GetFillValue: Byte;
  450.       {- get the value used to clear data and key buffers }
  451.  
  452.     Function    GetPosition: LongInt;
  453.       {- return the position of the record }
  454.  
  455.     Procedure   Insert;
  456.       {- add a new record to the file }
  457.  
  458.     Function    IsOpen: Boolean;
  459.       {- return True if the file is open }
  460.  
  461.     Function    Load(InputFilePath : PathStr;
  462.                      DisplayObj    : PProgress): Integer;
  463.       {- read the contents of a DOS file and insert }
  464.  
  465.     Procedure   MakeKey(V1 : Pointer;
  466.                         V2 : Pointer;
  467.                         V3 : Pointer;
  468.                         V4 : Pointer;
  469.                         V5 : Pointer;
  470.                         V6 : Pointer);         Virtual;
  471.       {- copy the passed fields into the key buffer }
  472.  
  473.     Function    NumberOfRecords: LongInt;
  474.       {- return the number of records in the file }
  475.  
  476.     Procedure   Open(Mode : Integer;
  477.                      Owner: String);
  478.       {- open the file }
  479.  
  480.     Function    Recover(NewFilePath : PathStr;
  481.                         DisplayObj  : PProgress): Integer;
  482.       {- write the contents of the file to a DOS file }
  483.  
  484.     Procedure   RemoveErrors(ErrorCodes : ErrorSet);
  485.       {- remove an error from the set of errors trapped }
  486.  
  487.     Procedure   Reset;
  488.       {- reset Btrieve }
  489.  
  490.     Procedure   ResetStation(Connection : Word);
  491.       {- reset Btrieve }
  492.  
  493.     Function    Save(NewFilePath : PathStr;
  494.                      DisplayObj  : PProgress): Integer;
  495.       {- write the contents of the file to a DOS file }
  496.  
  497.     Procedure   SetErrors(ErrorCodes : ErrorSet);
  498.       {- set the entire trapped error set }
  499.  
  500.     Procedure   SetFillValue(Value : Byte);
  501.       {- set the value used to clear data and key buffers }
  502.  
  503.     Procedure   SetKeyPath(Number : Word);
  504.       {- change the current file key path }
  505.  
  506.     Procedure   SetOutputSize(Size : Word);
  507.       {- use for variable length records only, sets the size of the
  508.          record to be written to the file  }
  509.  
  510.     Procedure   SetOwner(Owner  : String;
  511.                          Mode   : Integer);
  512.       {- set the file owner }
  513.  
  514.     Procedure   StartTransaction(Lock : Word);
  515.  
  516.     Procedure   Stat(var FData : FileSpec);
  517.       {- get the file statistics }
  518.  
  519.     Procedure   Update;
  520.       {- update an existing record in the file }
  521.  
  522.     Procedure   Unload;
  523.       {- unload Btrieve }
  524.  
  525.     Procedure   UnlockAll(Lock : Word);
  526.       {- unlock all records in the file }
  527.  
  528.     Procedure   Version(var Ver    : Word;
  529.                         var Rev    : Word;
  530.                         var OSFlag : Char);
  531.       {- get Btrieve version }
  532.  
  533.     PRIVATE
  534.  
  535.     Procedure   FixKeyStrings;
  536.     Procedure   JustifyString(Buff    : PBytes;
  537.                               Size    : Byte;
  538.                               KeyType : Byte;
  539.                               Justify : Byte);
  540.   end;
  541.  
  542.  
  543. Procedure CheckForBtrieve;
  544.  
  545. Procedure GetBtrieveVersion(var Ver    : Word;
  546.                             var Rev    : Word;
  547.                             var OSFlag : Char);
  548.  
  549. Procedure UnloadBtrieve;
  550.  
  551.