2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen/* istream-callback can be used to implement an istream that returns data
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen by calling the specified callback. The callback needs to do:
1dd875d96ab5640f78250079961c10e99ed4aa79Timo Sirainen a) Add data to buffer unless the buffer size is already too large
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (the callback can decide by itself what is too large). Return TRUE
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainen regardless of whether any data was added.
89b548af722113acb5d63dfffb44423cb60f91e4Timo Sirainen b) Return FALSE when it's finished adding data or when it reaches an error.
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen On error i_stream_callback_set_error() must be called before returning.
ac26a4607cb12b156f6a42f1ead2881bedd43d94Timo Sirainen i_stream_add_destroy_callback() can be also added to do any cleanups that
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen the callback may need to do.
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainentypedef bool istream_callback_read_t(buffer_t *buf, void *context);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Siraineni_stream_create_callback(istream_callback_read_t *callback, void *context);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define i_stream_create_callback(callback, context) \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_stream_create_callback(1 ? (istream_callback_read_t *)callback : \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen CALLBACK_TYPECHECK(callback, bool (*)(buffer_t *buf, typeof(context))), \
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen/* Append data to the istream externally. Typically this is used to add a
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen header to the stream before the callbacks are called. */
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainenvoid i_stream_callback_append(struct istream *input,
0bd15afe9cadd09b01e68b493b30c9d7e92b4095Timo Sirainenvoid i_stream_callback_append_str(struct istream *input, const char *str);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Returns the istream-callback's internal buffer. This buffer can be used to
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen append data to the stream. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenbuffer_t *i_stream_callback_get_buffer(struct istream *input);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid i_stream_callback_set_error(struct istream *input, int stream_errno,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *error);