Defining a Stream Structure

The first field of the user-defined stream structure must be of type QP_stream. Other fields in the user-defined stream structure can be anything that is required to operate on the user-defined stream. The Prolog input/output system passes a QP_stream pointer as the first argument to the bottom layer functions; casting this to the user-defined stream structure enables other fields in the user-defined stream to be accessed. The example below declares a binary stream structure as:

     typedef struct
         {
             QP_stream qpinfo;
             int fd;                 /* UNIX file descriptor */
             int last_rdsize;        /* size of last returned record */
             unsigned char buffer[Buffer_Size];      /* I/O buffer */
         } BinStream;
     
     #define CoerceBinStream(x) ((BinStream *)(x))
     

The field qpinfo stores information about the binary stream known to the Prolog input/output system. There is a buffer field in the structure since the I/O buffer is allocated by the user. The macro CoerceBinStream is used to convert a pointer to QP_stream into a pointer to BinStream. We use this macro to convert the pointer so that fields in the BinStream structure can be accessed.