| ASCL User's Guide: ASCL, ADA Standard Component Library; Version 0.1.0; Document Revision $Revision: 1.7 $ | ||
|---|---|---|
| Prev | Chapter 42. ASCL.Files.Binary_Pack | Next |
type Binary_File_Type is limited private;
data type representing binary files This type controls the access mode for a file the type representing a single binary digit a bit sequence of arbitrary length
procedure Create_Binary_File(
File : in out Binary_File_Type;
Mode : in Binary_File_Mode;
File_Name : String); Creates a new binary file with the given file name
P.1 - File is not already open
P.2 - Given file name has to be valid e.g. not empty
C.1 - File is created and available for operations as
specified in the Mode parameter.
P.1 violated - exception Status_Error is raised
Mode In_Mode should not be supplied as this is nonsensical, why would a new (empty) file be created for reading only?
procedure Open_Binary_File(
File : out Binary_File_Type;
Mode : in Binary_File_Mode;
File_Name : in String); Opens an already existing binary file with the given file name
P.1 - File is not already open
P.2 - the given file with name File_Name exists P.3 - Given file name has to be valid e.g. not empty
C.1 - File is opened and available for operations as
specified in the Mode parameter.
P.1 violated - exception Status_Error is raised P.2 violated - exception Name_Error is raised
If mode In_File is supplied, then the file will be read-only. If mode Out_File is supplied, the file that exists will be erased and a new file created for writing.
If mode Append_File is supplied, the file cursor will be set to the end for append writing.
procedure Close_Binary_File(
File : in out Binary_File_Type); Opens an already existing binary file with the given file name
P.1 - File is open
C.1 - File is opened with the specified mode for manipulation.
P.1 violated - exception Status_Error is raised
procedure Delete_Binary_File(
File : in out Binary_File_Type); Deletes the opened binary file
P.1 - File is open
P.2 - the external environment (Operating system et al) supports
the deletion of this file.
C.1 - File is deleted and the access closed.
P.1 violated - exception Status_Error is raised P.2 violated - exception Use_Error is raised
function Is_Open(
File : in Binary_File_Type) return Boolean; Checks to see if the given file handle is open.
None.
C.1 - a boolean value is returned, True if and only if the file is
open.
None.
procedure Read_Bit(
File : in out Binary_File_Type;
Bit : out Binary_Digit_Type); Reads a single bit from a file.
P.1 - The file is open.
P.2 - The file is of mode In_File.
P.3 - The file has not been depleted, i.e. there are still bits
to be read.
C.1 - Bit is read from the file, and the file cursor advanced
to the next bit.
P.1 violated - exception Status_Error is raised. P.2 violated - exception Mode_Error is raised. P.3 violated - exception End_Error is raised.
procedure Write_Bit(
File : in out Binary_File_Type;
Bit : in Binary_Digit_Type); Writes a single bit to a file.
P.1 - The file is open.
P.2 - The file is not of mode In_File.
P.3 - The external maximum capacity of the file has not been
exceeded.
C.1 - Bit is written to the end of the file.
P.1 violated - exception Status_Error is raised.
P.2 violated - exception Mode_Error is raised.
P.3 violated - exception Use_Error is raised.
bits should be written in blocks of 8 since the byte is the basic unit of most (if not all) file systems, so the bits are actually only written once a byte has been completed. If the file is closed in the middle of a byte, then the spare bits are set to 0 and written before the file is closed.
procedure Write_Bit_Sequence(
File : in out Binary_File_Type;
Bits : in Binary_Array_Type); Writes a bit sequence to a file.
P.1 - The file is open.
P.2 - The file is not of mode In_File.
P.3 - The external maximum capacity of the file has not been
exceeded.
C.1 - Bit is written to the end of the file.
P.1 violated - exception Status_Error is raised. P.2 violated - exception Mode_Error is raised.
P.3 violated - exception Use_Error is raised.
The same problem arises as with Write_Bit, bits should be written so that there are no spare bits in a byte, or that your program is aware of the fact that spare bits may exist and handles this appropriately.
function End_Of_File(
File : in Binary_File_Type) return Boolean; Checks to see if the file has been depleted, i.e. there are no bits left to be read.
P.1 - The file is open.
C.1 - a boolean value is returned, True if and only if the file is
open.
P.1 violated - exception Status_Error is raised.
Currently this does give any real meaningful result (or exception) if an attempt is made to call this with a file of mode Out_File or Append_File. This will be resolved shortly.
function Make_Integer(
Binary_Array : in Binary_Array_Type) return Integer; Creates an integer from an 8-bit or less binary sequence.
None.
C.1 - an Integer is created from the bit contents, with the
left-most bit (i.e. lowest index) being the most significant bit.
None.
If the binary sequence is larger than 8, then the bits after the 8th bit in the sequence are ignored.