Prolog Streams

From the Prolog level there is a unique number that identifies a stream. This identifier can be converted from/to a Prolog stream:

stream_code(+Stream,?StreamCode)
stream_code(?Stream,+StreamCode)

StreamCode is the C stream identifier (an integer) corresponding to the Prolog stream Stream. This predicate is only useful when streams are passed between Prolog and C. Note that StreamCode no longer has any relation to the file descriptor.

The StreamCode is a Prolog integer representing an SP_stream * pointer.

To read or write on a Prolog stream from C, special versions of the most common standard C I/O functions are used:

int SP_getc(void)

int SP_fgetc(SP_stream *s)

void SP_putc(int c)

void SP_fputc(int c, SP_stream *s)

The above functions deliver or accept wide character codes.

void SP_puts(char *string)

void SP_fputs(char *string, SP_stream *s)

int SP_printf(char *format, ...)

int SP_fprintf(SP_stream *s, char *format, ...)

int SP_fflush(SP_stream *s)

int SP_fclose(SP_stream *s)

The above functions expect and deliver encoded strings in their char * and char ** arguments. Specifically, in the SP_printf() and SP_fprintf() functions, first the formatting operation will be performed. The resulting string will be assumed to be in internal encoding, and will then be output using the SP_puts() or SP_fputs() function (see below). This means, e.g. that the %c printf conversion specification can only be used for ASCII characters, and the strings included using a %s specification should also be ASCII strings, or already transformed to the encoded form.

The SP_puts() and SP_fputs() functions first convert their encoded string argument into a sequence of wide character codes, and then output these on the required stream according to the external encoding; see WCX Foreign Interface.

There following predefined streams are accessible from C:

SP_stdin
Standard input. Refers to the same stream as user_input in Prolog. Which stream is referenced by user_input is controlled by the Prolog flag user_input
SP_stdout
Standard output. Refers to the same stream as user_output in Prolog. Which stream is referenced by user_output is controlled by the Prolog flag user_output.
SP_stderr
Standard error. Refers to the same stream as user_error in Prolog. Which stream is referenced by user_error is controlled by the flag user_error.
SP_curin
Current input. It is initially set equal to SP_stdin. It can be changed with the predicates see/1 and set_input/1.
SP_curout
Current output. It is initially set equal to SP_stdout. It can be changed with the predicates tell/1 and set_output/1.

Note that these variables are read only. They are set but never read by the stream handling.