Node:Streams and Cookies, Next:Hook Functions, Up:Custom Streams
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_io_functions_t | Data Type |
This is a structure type that holds the functions that define the
communications protocol between the stream and its cookie. It has
the following members:
|
FILE * fopencookie (void *cookie, const char *opentype, cookie_io_functions_t io-functions) | Function |
This function actually creates the stream for communicating with the
cookie using the functions in the io-functions argument.
The opentype argument is interpreted as for fopen ;
see Opening Streams. (But note that the "truncate on
open" option is ignored.) The new stream is fully buffered.
The |