istream-callback.h revision 3b4374ee8db3ee799c9e75b704ef695993b990ba
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef ISTREAM_CALLBACK_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define ISTREAM_CALLBACK_H
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen
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:
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen
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.
c37098f8ce6d512ba41f09564d04ed25720f0a77Timo Sirainen
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.
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
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 Sirainen*/
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainentypedef bool istream_callback_read_t(buffer_t *buf, void *context);
39ed514f9d401b3cb589595c6a2f532050254d77Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenstruct istream *
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))), \
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainen context)
62bf16bd8bb79e308e64110ae8d0b2a55a4c1490Timo Sirainen
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,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const void *data, size_t size);
6380f2bc729a03b328793e8ad6ba7587620fa184Timo Sirainenvoid i_stream_callback_append_str(struct istream *input, const char *str);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
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 Sirainen
57f4445a46726a17bfe78b0964dd301a6ccb40ecTimo Sirainenvoid i_stream_callback_set_error(struct istream *input, int stream_errno,
009d6d90b33bc7f64fa8251ac392cc87a835b833Timo Sirainen const char *error);
03f4c5f3502801f5b318f464cc75313a88558805Timo Sirainen
027c729b3107441f54a2602ccf2c67c6206998d5Timo Sirainen#endif
027c729b3107441f54a2602ccf2c67c6206998d5Timo Sirainen