istream-callback.h revision 3b4374ee8db3ee799c9e75b704ef695993b990ba
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen/* istream-callback can be used to implement an istream that returns data
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen by calling the specified callback. The callback needs to do:
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen a) Add data to buffer unless the buffer size is already too large
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen (the callback can decide by itself what is too large). Return TRUE
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen regardless of whether any data was added.
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen b) Return FALSE when it's finished adding data or when it reaches an error.
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen On error i_stream_callback_set_error() must be called before returning.
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen i_stream_add_destroy_callback() can be also added to do any cleanups that
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen the callback may need to do.
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainentypedef bool istream_callback_read_t(buffer_t *buf, void *context);
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Siraineni_stream_create_callback(istream_callback_read_t *callback, void *context);
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen#define i_stream_create_callback(callback, context) \
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen i_stream_create_callback(1 ? (istream_callback_read_t *)callback : \
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen CALLBACK_TYPECHECK(callback, bool (*)(buffer_t *buf, typeof(context))), \
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen/* Append data to the istream externally. Typically this is used to add a
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen header to the stream before the callbacks are called. */
37f96554a5734557cd454691d163e602d36384b4Timo Sirainenvoid i_stream_callback_append(struct istream *input,
6380f2bc729a03b328793e8ad6ba7587620fa184Timo Sirainenvoid i_stream_callback_append_str(struct istream *input, const char *str);
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen/* Returns the istream-callback's internal buffer. This buffer can be used to
20b136f04257b0ba338e49f31a999c0d4b243647Timo Sirainen append data to the stream. */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenbuffer_t *i_stream_callback_get_buffer(struct istream *input);
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainenvoid i_stream_callback_set_error(struct istream *input, int stream_errno,
009d6d90b33bc7f64fa8251ac392cc87a835b833Timo Sirainen const char *error);