home *** CD-ROM | disk | FTP | other *** search
- Binary File Unit For Turbo Pascal 6.0
-
- Program Name : BinFile.Pas
- Written By Donald S. P. Golding, Jr.
- Date : March 13, 1993
- Version : 1.0
-
-
- This program may be freely distributed for non-commercial, non-business,
- and non-governmental uses, provided this notice is attached with it. This
- program is copyrighted by Donald Golding Jr. If you find any use for it
- please register it with me. Additionally, let me know of any useful programs
- that you develop with this program unit via my address.
-
- Home Address : 114 South 8th Avenue
- Mount Vernon, N.Y. 10550
- U.S.A.
-
- The compiled version of BINFILE.TPU was compiled in TURBO PASCAL 6.0. If you want
- a unit file for Turbo Pascal 5.5 or Turbo Pascal 1.5 for windows please contact me.
- These TPU's will be included at a later date.
-
- Introduction
- ------------
- This unit provides the user with an object called BinaryFileObj. This object
- allows the user to read, write, sort and do various operations on a data file.
- There are many other types in the unit that are used to aid the object. The
- following is an example program using the BinaryFileObj.
-
- Program Filetst ;
-
- Uses Crt, Dos, BinFile;
-
- {$F+}
- Function NameSort (Index1, Index2 : LongInt; Var Data1, Data2) : CompareType ;
-
- Begin
-
- If SearchRec(Data1).Name > SearchRec(Data2).Name Then
-
- NameSort := CompGreater
-
- Else If SearchRec(Data1).Name < SearchRec(Data2).Name Then
-
- NameSort := CompLess
-
- Else NameSort := CompEqual ;
-
- End ;
- {$F-}
-
- {$F+}
- Function Search1 (FileOffSet : LongInt; Var DataToFind, DataRead) : Boolean ;
-
- Begin
-
- Search1 := SearchRec(DataToFind).Name = SearchRec(DataRead).Name ;
-
- End ;
- {$F-}
-
- {$F+}
- Function Search2 (FileOffSet : LongInt; Var DataToFind, DataRead) : CompareType ;
-
- Begin
-
- If SearchRec(DataToFind).Name > SearchRec(DataRead).Name Then
-
- Search2 := CompGreater
-
- Else If SearchRec(DataToFind).Name < SearchRec(DataRead).Name Then
-
- Search2 := CompLess
-
- Else Search2 := CompEqual ;
-
- End ;
- {$F-}
-
- Var
-
- TestFile : BinaryFileObj ;
- SRec : SearchRec ;
- Count : LongInt ;
- SUC : Boolean ;
- OffS : LongInt ;
- TempChar : Char ;
-
- Begin
-
- TestFile.Init ('FileName.Dat', True, SizeOf(SearchRec)) ;
-
- Writeln ('Reading Files ....') ;
- FindFirst ('*.*',AnyFile,SRec) ;
-
- While (DosError = 0) And (TestFile.GetError = NoError) Do
-
- Begin
-
- TestFile.WriteData (NextWrite, SizeOf(SearchRec), SRec, Suc) ;
- FindNext (SRec) ;
-
- End ;
-
- Writeln ('Sorting File .....') ;
- TestFile.QuickSort (Ascending, SizeOf(SearchRec), NameSort, DefaultStartOffSet,
- DefaultEndOffSet, Suc) ;
- Writeln ('Sort SuccessFul : ',Suc) ;
-
- TempChar := ReadKey ;
- TestFile.ResetFile ;
-
- Count := 0 ;
- While (Not TestFile.GetEof) And (TestFile.GetError = NoError) Do
-
- Begin
-
- Inc (Count) ;
- TestFile.ReadData (NextRead, SizeOf(SearchRec), SRec, Suc) ;
- Writeln (Count, '. File Name : ',SRec.Name) ;
- If Count Mod 10 = 0 Then
-
- TempChar := ReadKey ;
-
- End ;
-
- TempChar := Readkey ;
-
- SRec.Name := '..' ;
- TestFile.LinearSearch (Search1, SizeOf(SearchRec), SRec,
- DefaultStartOffSet, Offs, Suc) ;
- Writeln (Suc, ' ', Offs) ;
- TempChar := ReadKey ;
-
- If Suc Then
-
- Begin
-
- TestFile.ReadData (OffS, SizeOf(SearchRec),SRec, Suc) ;
- Writeln (Suc, ' ',SRec.Name) ;
- TempChar := ReadKey;
-
- End ;
-
- SRec.Name := '..' ;
- TestFile.BinarySearch (Search2, Ascending, SizeOf(SRec), SRec,
- DefaultOffSetFactor, OffS, Suc) ;
- Writeln (Suc, ' ', Offs) ;
- TempChar := Readkey ;
-
- If Suc Then
-
- Begin
-
- TestFile.ReadData (OffS, SizeOf(SearchRec), SRec, Suc) ;
- Writeln (Suc, ' ',SRec.Name) ;
- TempChar := ReadKey ;
-
- End ;
-
- TestFile.Done (True);
-
- End.
-
- -----------------------------------------------------------------------------
-
-
- Constants
- ---------
-
- Constants available in BinFile unit.
-
- { Error constants }
- 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 ;
-
- -----------------------------------------------------------------------------
-
-
- Types
- -----
- Other types in BinFile unit.
-
- -----------------------------------------------------------------------------
- OrderType = (Ascending, Descending) ;
-
- Used for ordering sorts.
-
- -----------------------------------------------------------------------------
- CompareType = (CompEqual, CompGreater, CompLess) ;
-
- Used for comparison of data.
-
- -----------------------------------------------------------------------------
- CompareFuncType = Function (OffSet1, OffSet2 : LongInt ;
- Var Data1, Data2) : CompareType ;
-
- Declaration of the format of a user defined procedure for comparison.
-
- -----------------------------------------------------------------------------
- SearchFuncTypeL = Function (FileOffSet : LongInt;
- Var DataToFind, DataRead) : Boolean ;
-
- Declaration of the format of a user defined procedure for linear search.
-
- -----------------------------------------------------------------------------
- SearchFuncTypeB = Function (FileOffSet : LongInt;
- Var DataToFind, DataRead) : CompareType ;
-
- Declaration of the format of a user defined procedure for binary search.
-
- -----------------------------------------------------------------------------
- BinaryFileObjPtr = ^BinaryFileObj ;
-
- A pointer type to the binary file object.
-
- -----------------------------------------------------------------------------
- PerformType = Procedure (FileOffSet : LongInt; Var Data; DSize : Word;
- BinObjPtr : BinaryFileObjPtr) ;
-
- Declaration format for the perform all procedure.
- -----------------------------------------------------------------------------
-
-
- Methods
- -------
- The methods for BinaryFileObj object are as follows
-
- ----------------------------------------------------------------------------
- Constructor BinaryFileObj.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 BinareFileObj.Done (FileErase : Boolean) ;
-
- This object uninitialized all allocated memory and releases the file. If
- FILEERASE is true then the file is erased.
-
- ----------------------------------------------------------------------------
- Procedure BinaryFileObj.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 BinaryFileObj.WriteData (FileOffSet : LongInt; DataS : Word;
- Var Data; Var SuccessFul : Boolean) ;
-
- Same a ReadData except data is written.
-
- ----------------------------------------------------------------------------
- Procedure BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.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 BinaryFileObj.ResetFile ;
-
- Resets the file offsets to the beginning of the file.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetEof : Boolean ;
-
- Indicates whether we are at the end of a file.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetFileSize : LongInt ;
-
- Returns current file size.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetOffSet : LongInt ;
-
- Returns current file offset.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetFileName : String ;
-
- Returns file name.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetDataSize : Word ;
-
- Returns default data size.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetFileOpen : Boolean ;
-
- Returns if file is open.
-
- ----------------------------------------------------------------------------
- Function BinaryFileObj.GetError : Integer ;
-
- Returns current error code.
-
- ----------------------------------------------------------------------------
- Procedure BinaryFileObj.ClearError ;
-
- Sets the error code to NOERROR.
-