Initialize and Register The Created Stream

The fields of QP_stream structure used internally by the Prolog system are initialized through QP_prepare_stream(). It should be called after other fields in QP_stream are properly set up. QP_prepare_stream() takes a pointer to QP_stream as its first parameter and the address of the input/output buffer for the stream as its second parameter.

QP_register_stream() is then called to register the user-defined stream so that the stream can be used in Prolog code. In our example, if the registration fails the bottom layer function is used to close the opened file and deallocate the memory space for the created stream and a null stream is returned.

             QP_prepare_stream(&stream->qpinfo, stream->buffer);
             if (QP_register_stream(&stream->qpinfo) == QP_ERROR)
             {   (void) stream->qpinfo.close(&stream->qpinfo);
                 *error_num = QP_errno;
                 return QP_NULL_STREAM;