home *** CD-ROM | disk | FTP | other *** search
- Unit BinFile ;
- {$O+ F+}
-
- Interface
-
- { Program Unit name : BinFile.TPU
- Unit source code : BinFile.PAS
- Interface File : BinFile.INT
- Documentation File : BinFile.DOC
- Version 1.0
- Written by Donald St. Patrick Golding Jr.
- Date of Creation : March 10, 1993
- Purpose Of Unit : This unit contains the BinaryFileObj Object.
- It allows flexible of a binary file.
-
- Note : File offsets start from 0 and range to an offset equal to the size
- of the file.
- }
-
- Uses Crt, Dos ;
-
- Const
-
- { Error Codes }
- NoError = 0 ;
- Err_FileNotFound = -1 ;
- Err_NoDiskSpace = -2 ;
- Err_InvalidOffSet = -3 ;
- Err_InvalidDataSize = -4 ;
- Err_DriveNotReady = -5 ;
- Err_ReadError = -6 ;
- Err_NotEnoughSpace = -7 ;
- Err_FileNotOpen = -8 ;
- Err_NotEnoughMem = -9 ;
- Err_DiskIO = -10 ;
- Err_InvalidFileSize = -11 ;
- Err_Unknown = -50 ;
-
- { Processing Constants }
- NextWrite = -1 ;
- NextRead = -2 ;
- DefaultStartOffSet = -3 ;
- DefaultEndOffSet = -4 ;
- DefaultOffSetFactor = -5 ;
-
- Type
-
- OrderType = (Ascending, Descending) ;
- CompareType = (CompEqual, CompGreater, CompLess) ;
- CompareFuncType = Function (OffSet1, OffSet2 : LongInt ;
- Var Data1, Data2) : CompareType ;
- SearchFuncTypeL = Function (FileOffSet : LongInt;
- Var DataToFind, DataRead) : Boolean ;
- SearchFuncTypeB = Function (FileOffSet : LongInt;
- Var DataToFind, DataRead) : CompareType ;
-
- BinaryFileObjPtr = ^BinaryFileObj ;
- PerformType = Procedure (FileOffSet : LongInt; Var Data; DSize : Word;
- BinObjPtr : BinaryFileObjPtr) ;
- BinaryFileObj = Object
-
- Constructor Init (FileName : String; NewFile : Boolean;
- DataS : Word) ;
- { Initializes the file object and must be called first.
- FILENAME is the name of the file to be accessed by the
- object. NEWFILE indicated whether or not the file should
- be considered a new file. If the file already exists then
- the existing file's data is erased. Otherwise the file is
- created. DATAS is the size of data to be stored in the
- file. This number is used as the default data size if it
- is greater than 0. If DATAS is 0 then the file's data will
- be retrieved dynamically. Any access to data in the file
- (reading or writing) this data size will have to be
- indicated.
- }
-
- Destructor Done (FileErase : Boolean) ;
- { This object uninitialized all allocated memory and
- releases the file. If FILEERASE is true then the file is
- erased.
- }
-
- Procedure ReadData (FileOffSet : LongInt; DataS : Word;
- Var Data; Var SuccessFul : Boolean) ;
- { This method allows you to read the data from the file.
- FILEOFFSET is the offset into the file to start the read.
- If FILEOFFSET is less than 0 then reading will start from
- the current file offset. DATAS is the size of the data to
- be read. If the data size that was indicated in the
- BinaryFileObj.Init method was greater than 0 then this
- value is ignored. If the size was not stated in the
- BinaryfileObj.Init method then this value is check. If
- DATAS is 0 then an error will be indicated. DATA is the
- variable that will recieve the data that is read from the
- data file. SUCCESSFUL is true is the read of the file
- was accomplished.
- }
-
- Procedure WriteData (FileOffSet : LongInt; DataS : Word;
- Var Data; Var SuccessFul : Boolean) ;
- { Same a ReadData except data is written. }
-
- Procedure Copy (SrcOffSet, DestOffSet : LongInt;
- DataS : Word ; Var SuccessFul : Boolean) ;
- { Copies data from one file offset to another. SRCOFFSET is
- the file offset for the source data to be copied.
- DESTOFFSET is the destination file offset. DATAS is the
- size of the data. If the data size that was indicated in
- the BinaryFileObj.Init method was greater than 0 then this
- value is ignored. If the size was not stated in the
- BinaryfileObj.Init method then this value is check. If
- DATAS is 0 then an error will be indicated. SUCCESSFUL is
- true if the copy was done correctly.
- }
-
- Procedure Swap (OffSet1, OffSet2 : LongInt;
- DataS : Word ; Var SuccessFul : Boolean) ;
- { Swaps data from one file offset to another. OFFSET1 is
- the first file offset. OFFSET2 is the second offset.
- DATAS is the size of the data. If the data size that was
- indicated in the BinaryFileObj.Init method was greater
- than 0 then this value is ignored. If the size was not
- stated in the BinaryfileObj.Init method then this value is
- check. If DATAS is 0 then an error will be indicated.
- SUCCESSFUL is true if the swap was done correctly.
- }
-
- Procedure BubbleSort (Order : OrderType; DataS : Word;
- CompareFunc : CompareFuncType ;
- StartOffS, EndOffS : LongInt ;
- Var SuccessFul : Boolean) ;
- { Uses the Bubble Sort to sort the file. ORDER is the order
- in which to sort the data file (Ascending or Descending).
- DATAS is the size of the data. If the data size that was
- indicated in the BinaryFileObj.Init method was greater
- than 0 then this value is ignored. If the size was not
- stated in the BinaryfileObj.Init method then this value is
- check. If DATAS is 0 then an error will be indicated.
- COMPAREFUNC is the user defined function that is used to
- get a result of a comparison between data. STARTOFFS is
- the starting offset in the file to start sorting. If
- STARTOFFS is negative then the sort starts from the
- beginning of the file. ENDOFFS is the ending offset to
- end the sort. If ENDOFFS is negative then the ending
- offset is the last record in the data file. SUCCESSFUL is
- true if the sort was done correctly.
- }
-
- Procedure QuickSort (Order : OrderType; DataS : Word;
- CompareFunc : CompareFuncType ;
- StartOffS, EndOffS : LongInt ;
- Var SuccessFul : Boolean) ;
- { Uses the Quick Sort to sort the file. (Faster than
- Bubble sort) ORDER is the order in which to sort the data
- file (Ascending or Descending). DATAS is the size of the
- data. If the data size that was indicated in the
- BinaryFileObj.Init method was greater than 0 then this
- value is ignored. If the size was not stated in the
- BinaryfileObj.Init method then this value is check. If
- DATAS is 0 then an error will be indicated. COMPAREFUNC is
- the user defined function that is used to get a result of
- a comparison between data. STARTOFFS is the starting
- offset in the file to start sorting. If STARTOFFS is
- negative then the sort starts from the beginning of the
- file. ENDOFFS is the ending offset to end the sort. If
- ENDOFFS is negative then the ending offset is the last
- record in the data file. SUCCESSFUL is true if the sort
- was done correctly.
- }
-
- Procedure LinearSearch (SearchFunc : SearchFuncTypeL;
- DataS : Word; Var DataToFind;
- StartOffSet : LongInt ;
- Var OffSet : LongInt ;
- Var SuccessFul : Boolean) ;
- { Searches the file for the data in DATATOFIND variable.
- SEARCHFUNC is the user defined function which returns
- true if the data was found. DATAS is the size of the
- data. If the data size that was indicated in the
- BinaryFileObj.Init method was greater than 0 then this
- value is ignored. If the size was not stated in the
- BinaryfileObj.Init method then this value is check. If
- DATAS is 0 then an error will be indicated. STARTOFFSET is
- the offset to start the search. If negative then the
- search starts at the beginning of the file. OFFSET will
- contain the file offset where the data was found. If the
- data is not found then OFFSET equals -1. SUCCESSFUL is
- true if the data was found.
- }
-
- Procedure BinarySearch (SearchFunc : SearchFuncTypeB;
- Order : OrderType ;
- DataS : Word; Var DataToFind;
- OffSetFactor : LongInt ;
- Var OffSet : LongInt ;
- Var SuccessFul : Boolean) ;
- { Searches the file for the data in DATATOFIND variable.
- SEARCHFUNC is the user defined function which returns
- true if the data was found. The only way that the binary
- search will work successfully is if the data file has
- already been sorted. ORDER is the order in which the file
- was sorted. DATAS is the size of the data. If the data
- size that was indicated in the BinaryFileObj.Init method
- was greater than 0 then this value is ignored. If the
- size was not stated in the BinaryfileObj.Init method then
- this value is check. If DATAS is 0 then an error will be
- indicated. OFFSETFACTOR is the offset to start the search.
- Any data below the OFFSETFACTOR will be ignored. If
- negative then the search starts at the beginning of the
- file. OFFSET will contain the file offset where the data
- was found. If the data is not found then OFFSET equals -1.
- SUCCESSFUL is true if the data was found.
- }
-
- Procedure PerformForAll (StartOffSet : LongInt; DataS : Word;
- UserProcedure : PerformType) ;
- { Calls the user defined procedure USERPROCEDURE and passed
- to that procedure the current data being accessed by the
- PERFORMFORALL method and the current offset. Refer to the
- declaration of the PERFORMTYPE variable type. STARTOFFS
- is the offset in the file to start reading at. If
- STARTOFFS is negative then this method starts at the
- beginning of the file. DATAS is the size of the data.
- If the data size that was indicated in the
- BinaryFileObj.Init method was greater than 0 then this
- value is ignored. If the size was not stated in the
- BinaryfileObj.Init method then this value is check.
- If DATAS is 0 then an error will be indicated.
- }
-
- Procedure ReduceFileSize (NewFileSize : LongInt;
- Var SuccessFul : Boolean) ;
- { Reduces the size of a file to size NEWFILESIZE. The value
- of NEWFILESIZE must be greater than or equal to 0.
- SUCCESSFUL is true if the method completed the operation
- without error.
- }
-
- Procedure ResetFile ;
- { Resets the file offsets to the beginning of the file. }
-
- Function GetEof : Boolean ;
- { Indicates whether we are at the end of a file. }
-
- Function GetFileSize : LongInt ;
- { Returns current file size }
-
- Function GetOffSet : LongInt ;
- { Returns current file offset }
-
- Function GetFileName : String ;
- { Returns file name }
-
- Function GetDataSize : Word ;
- { Returns default data size }
-
- Function GetFileOpen : Boolean ;
- { Returns if file is open. }
-
- Function GetError : Integer ;
- { Returns current error code. }
-
- Procedure ClearError ;
- { Sets the error code to NOERROR }
-
- End ;
-
- Function NoSort (OffSet1, OffSet2 : LongInt ;
- Var Data1, Data2) : CompareType ;
-
- Implementation
-