A new stream is created using Snew().
Before we can create a stream we must create a function block of type IOFUNCTIONS
that provide function pointers for the basic operations on the stream.
This type is defined as follows:
typedef struct io_functions
{ Sread_function read; /* fill the buffer */
Swrite_function write; /* empty the buffer */
Sseek_function seek; /* seek to position */
Sclose_function close; /* close stream */
Scontrol_function control; /* Info/control */
Sseek64_function seek64; /* seek to position (large files) */
} IOFUNCTIONS;
NULL if
repositioning is not possible on this type or they may return -1 and set
errno to EPIPE if the pointer cannot be
repositioned on this instance. The function returns the new file
position. See Sseek()
for details on how repositioning is implemented. See
section 12.9.4
for raising errors.SIO_GETPENDING, size_t*SIO_LASTERROR, char*SIO_SETENCODING, IOENC*SIO_FLUSHOUTPUT, NULLSIO_GETSIZE, int64_t*SIO_GETFILENO, int*SIO_GETWINSOCK, SOCKET*
Given an IOFUNCTIONS block we can create a new stream
from a
handle using Snew():
IOSTREAM* from a handle, flags and a block of
callback functions. The flags argument is a bitwise or of
SIO_* flags. Flags that control the creation are:
SIO_INPUTSIO_OUTPUTSIO_NBUFSIO_LBUFSIO_FBUFSIO_NBUF), line buffered (SIO_LBUF)
or fully buffered (SIO_FBUF)SIO_TEXTENC_OCTET.SIO_RECORDPOSposition property and related predicates.SIO_NOMUTEX
If the stream is associated with an OS file handle the system
initializes the SIO_ISATTY flag (on POSIX systems) and if
possible tells the OS not to inherit this stream to child processes.
The symbol Sfilefunctions is a IOFUNCTIONS
struct that contains the callbacks for accessing a regular file. After
opening an file using the POSIX open() API we can create a stream
to this file using Snew():
int fno = open(path, O_RDONLY);
IOSTREAM *s;
if ( fno >= 0 )
s = Snew((void*)fno,
SIO_INPUT|SIO_FBUF|SIO_RECORDPOS|SIO_TEXT,
&Sfilefunctions);
...
Snew() can
only fail if there is not enough memory. In that case the return value
is NULL and errno is set to ENOMEM.
r or w and may be followed
by b to create a binary stream. The default is to
create a text stream using the platform conventions and locale."wa". If the buffer is allocated or enlarged, this is
achieved using malloc() or realloc(). In this case the
returned buffer should be freed by the caller when done. Example:
{ char buf[1024]; // don't allocate for small stuff
char *s = buf;
IOSTREAM *fd;
size_t size = sizeof(buf);
fd = Sopenmem(&s, &size, "w");
...
Sclose(fd);
...
if ( s != buf ) // apparently moved
Sfree(s);
}
The mode is "r" or "w". The mode "rF"
calls
PL_free(buffer)
when closed.
Note: Its is not allowed to access streams created with this call from multiple threads. This is ok for all usage inside Prolog itself. This call is intended to use Sfprintf() and other output functions to create strings.
A stream can be made accessible from Prolog using PL_unify_stream():