Inside every custom stream is a special object called the cookie. This is an object supplied by you which records where to fetch or store the data read or written. It is up to you to define a data type to use for the cookie. The stream functions in the library never refer directly to its contents, and they don't even know what the type is; they record its address with type void *
.
To implement a custom stream, you must specify how to fetch or store the data in the specified place. You do this by defining hook functions to read, write, change ``file position'', and close the stream. All four of these functions will be passed the stream's cookie so they can tell where to fetch or store the data. The library functions don't know what's inside the cookie, but your functions will know.
When you create a custom stream, you must specify the cookie pointer, and also the four hook functions stored in a structure of type cookie_io_functions_t
.
These facilities are declared in `stdio.h'.
cookie_read_function_t *read
EOF
.
cookie_write_function_t *write
cookie_seek_function_t *seek
fseek
on this stream can only seek to locations within the buffer; any attempt to seek outside the buffer will return an ESPIPE
error.
cookie_close_function_t *close
fopen
; see Opening Streams. (But note that the ``truncate on open'' option is ignored.) The new stream is fully buffered.
The fopencookie
function returns the newly created stream, or a null pointer in case of an error.