a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen#ifndef ISTREAM_CALLBACK_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define ISTREAM_CALLBACK_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
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:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
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.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
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.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ac26a4607cb12b156f6a42f1ead2881bedd43d94Timo Sirainen i_stream_add_destroy_callback() can be also added to do any cleanups that
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen the callback may need to do.
66ae183b6e895216037bd921367670f4b0665911Timo Sirainen*/
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainentypedef bool istream_callback_read_t(buffer_t *buf, void *context);
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct istream *
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))), \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen context)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
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 Sirainen const void *data, size_t size);
0bd15afe9cadd09b01e68b493b30c9d7e92b4095Timo Sirainenvoid i_stream_callback_append_str(struct istream *input, const char *str);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
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 Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid i_stream_callback_set_error(struct istream *input, int stream_errno,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen#endif
8fa41238067c854435884c459963fde6f8c6436bTimo Sirainen